-
Notifications
You must be signed in to change notification settings - Fork 10
/
database.js
56 lines (51 loc) · 1.44 KB
/
database.js
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
import * as SQLite from "expo-sqlite";
const db = SQLite.openDatabase("little_lemon");
export async function createTable() {
return new Promise((resolve, reject) => {
db.transaction(
tx => {
tx.executeSql(
"create table if not exists menuitems (id integer primary key not null, name text, price text, description text, image text, category text);"
);
},
reject,
resolve
);
});
}
export async function getMenuItems() {
return new Promise(resolve => {
db.transaction(tx => {
tx.executeSql("select * from menuitems", [], (_, { rows }) => {
resolve(rows._array);
});
});
});
}
export function saveMenuItems(menuItems) {
db.transaction(tx => {
tx.executeSql(
`insert into menuitems (id, name, price, description, image, category) values ${menuItems
.map(
item =>
`("${item.id}", "${item.name}", "${item.price}", "${item.description}", "${item.image}", "${item.category}")`
)
.join(", ")}`
);
});
}
export async function filterByQueryAndCategories(query, activeCategories) {
return new Promise((resolve, reject) => {
db.transaction(tx => {
tx.executeSql(
`select * from menuitems where name like ? and category in ('${activeCategories.join(
"','"
)}')`,
[`%${query}%`],
(_, { rows }) => {
resolve(rows._array);
}
);
}, reject);
});
}