-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
66 lines (61 loc) · 2.44 KB
/
Copy pathdatabase.js
File metadata and controls
66 lines (61 loc) · 2.44 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
// Import SQLite library to work with a local database in the application.
import * as SQLite from "expo-sqlite";
// Open a database named "little_lemon". If it doesn't exist, it will be created automatically.
const db = SQLite.openDatabase("little_lemon");
// Create a table named "menuitems" if it doesn't already exist.
// This table will store menu information, including id, name, price, description, image, and category.
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, // Handle transaction failure
resolve // Handle transaction success
);
});
}
// Fetch all menu items from the "menuitems" table.
// Returns a promise that resolves with the rows from the database.
export async function getMenuItems() {
return new Promise(resolve => {
db.transaction(tx => {
tx.executeSql("select * from menuitems", [], (_, { rows }) => {
resolve(rows._array); // Extract the rows as an array.
});
});
});
}
// Save a list of menu items into the "menuitems" table.
// Each menu item is inserted using a bulk SQL query.
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(", ")}` // Join all individual insert values into a single query.
);
});
}
// Filter menu items based on a search query and active categories.
// The query matches item names, and only items in the specified categories are returned.
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}%`], // Bind the search query as a parameter to prevent SQL injection.
(_, { rows }) => {
resolve(rows._array); // Resolve with the filtered array of rows.
}
);
}, reject); // Handle any transaction errors.
});
}