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
|
import { createClient } from '@libsql/client';
const client = createClient({ url: 'file:./data.db' });
const categories = {
Boissons: ['café', 'thé', "jus d'orange", 'lait', 'eau gazeuse'],
Crèmerie: ['fromage', 'yaourt', 'beurre', 'crème fraîche', 'oeufs'],
'Fruits & Légumes': [
'pommes',
'bananes',
'tomates',
'salade',
'carottes',
'oignons',
],
Epicerie: [
'pâtes',
'riz',
"huile d'olive",
'sel',
'poivre',
'farine',
'sucre',
],
'Viande & Poisson': ['poulet', 'saumon', 'boeuf haché', 'jambon'],
Surgelés: ['pizza', 'glace', 'petits pois', 'frites'],
};
for (const [catName, items] of Object.entries(categories)) {
await client.execute({
sql: 'INSERT OR IGNORE INTO categories (name) VALUES (?)',
args: [catName],
});
const row = await client.execute({
sql: 'SELECT id FROM categories WHERE name = ?',
args: [catName],
});
const catId = Number(row.rows[0].id);
for (const itemName of items) {
await client.execute({
sql: `INSERT OR IGNORE INTO groceries (name, checked, created_at, category_id)
VALUES (?, 0, datetime('now'), ?)`,
args: [itemName, catId],
});
}
}
const count = await client.execute('SELECT COUNT(*) as c FROM groceries');
console.log(
`Done. ${count.rows[0].c} items in ${Object.keys(categories).length} categories.`,
);
client.close();
|