-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathdatabase.js
More file actions
119 lines (108 loc) · 2.67 KB
/
database.js
File metadata and controls
119 lines (108 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as SQLite from 'expo-sqlite';
// openDatabase has been removed from the stable version of expo-sqlite
//import * as SQLite from 'expo-sqlite/legacy';
import { Place } from '../models/place';
const database = SQLite.openDatabase('places.db');
export function init() {
const promise = new Promise((resolve, reject) => {
database.transaction((tx) => {
tx.executeSql(
`CREATE TABLE IF NOT EXISTS places (
id INTEGER PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
imageUri TEXT NOT NULL,
address TEXT NOT NULL,
lat REAL NOT NULL,
lng REAL NOT NULL
)`,
[],
() => {
resolve();
},
(_, error) => {
reject(error);
}
);
});
});
return promise;
}
export function insertPlace(place) {
const promise = new Promise((resolve, reject) => {
database.transaction((tx) => {
tx.executeSql(
`INSERT INTO places (title, imageUri, address, lat, lng) VALUES (?, ?, ?, ?, ?)`,
[
place.title,
place.imageUri,
place.address,
place.location.lat,
place.location.lng,
],
(_, result) => {
resolve(result);
},
(_, error) => {
reject(error);
}
);
});
});
return promise;
}
export function fetchPlaces() {
const promise = new Promise((resolve, reject) => {
database.transaction((tx) => {
tx.executeSql(
'SELECT * FROM places',
[],
(_, result) => {
const places = [];
for (const dp of result.rows._array) {
places.push(
new Place(
dp.title,
dp.imageUri,
{
address: dp.address,
lat: dp.lat,
lng: dp.lng,
},
dp.id
)
);
}
resolve(places);
},
(_, error) => {
reject(error);
}
);
});
});
return promise;
}
export function fetchPlaceDetails(id) {
const promise = new Promise((resolve, reject) => {
database.transaction((tx) => {
tx.executeSql(
'SELECT * FROM places WHERE id = ?',
[id],
(_, result) => {
const dbPlace = result.rows._array[0];
const place = new Place(
dbPlace.title,
dbPlace.imageUri,
{ lat: dbPlace.lat, lng: dbPlace.lng, address: dbPlace.address },
dbPlace.id
);
resolve(place);
},
(_, error) => {
reject(error);
}
);
});
});
return promise;
}