Versie module 1 van 26-7
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
require("dotenv").config();
|
||||
|
||||
const db = require("../config/database");
|
||||
|
||||
|
||||
console.log("Database migratie gestart...");
|
||||
|
||||
|
||||
db.exec(`
|
||||
|
||||
CREATE TABLE IF NOT EXISTS migrations (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
|
||||
executed_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
name TEXT NOT NULL,
|
||||
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
|
||||
password TEXT NOT NULL,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS households (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
name TEXT NOT NULL,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS household_members (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
household_id INTEGER NOT NULL,
|
||||
|
||||
user_id INTEGER NOT NULL,
|
||||
|
||||
role TEXT NOT NULL DEFAULT 'member',
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
|
||||
FOREIGN KEY (household_id)
|
||||
REFERENCES households(id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
|
||||
FOREIGN KEY (user_id)
|
||||
REFERENCES users(id)
|
||||
ON DELETE CASCADE
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS categories (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
name TEXT NOT NULL,
|
||||
|
||||
icon TEXT,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
name TEXT NOT NULL,
|
||||
|
||||
category_id INTEGER,
|
||||
|
||||
barcode TEXT,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
|
||||
FOREIGN KEY(category_id)
|
||||
REFERENCES categories(id)
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS shopping_lists (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
household_id INTEGER NOT NULL,
|
||||
|
||||
name TEXT NOT NULL,
|
||||
|
||||
archived INTEGER DEFAULT 0,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
|
||||
FOREIGN KEY(household_id)
|
||||
REFERENCES households(id)
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS shopping_list_items (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
list_id INTEGER NOT NULL,
|
||||
|
||||
product_id INTEGER NOT NULL,
|
||||
|
||||
amount REAL DEFAULT 1,
|
||||
|
||||
checked INTEGER DEFAULT 0,
|
||||
|
||||
sort_order INTEGER DEFAULT 0,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
|
||||
FOREIGN KEY(list_id)
|
||||
REFERENCES shopping_lists(id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
|
||||
FOREIGN KEY(product_id)
|
||||
REFERENCES products(id)
|
||||
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS stores (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS product_favorites (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
user_id INTEGER NOT NULL,
|
||||
|
||||
product_id INTEGER NOT NULL,
|
||||
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
|
||||
UNIQUE(user_id, product_id),
|
||||
|
||||
|
||||
FOREIGN KEY(user_id)
|
||||
REFERENCES users(id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
|
||||
FOREIGN KEY(product_id)
|
||||
REFERENCES products(id)
|
||||
ON DELETE CASCADE
|
||||
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS product_stores (
|
||||
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
product_id INTEGER NOT NULL,
|
||||
|
||||
store_id INTEGER NOT NULL,
|
||||
|
||||
|
||||
aisle TEXT,
|
||||
|
||||
|
||||
FOREIGN KEY(product_id)
|
||||
REFERENCES products(id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
|
||||
FOREIGN KEY(store_id)
|
||||
REFERENCES stores(id)
|
||||
ON DELETE CASCADE
|
||||
|
||||
);
|
||||
|
||||
`);
|
||||
|
||||
|
||||
console.log("Database migratie voltooid.");
|
||||
@@ -0,0 +1,107 @@
|
||||
require("dotenv").config();
|
||||
|
||||
const db = require("../config/database");
|
||||
|
||||
|
||||
const categories = [
|
||||
|
||||
["Groente", "🥬"],
|
||||
|
||||
["Fruit", "🍎"],
|
||||
|
||||
["Zuivel", "🥛"],
|
||||
|
||||
["Brood", "🍞"],
|
||||
|
||||
["Dranken", "🥤"],
|
||||
|
||||
["Huishouden", "🧻"]
|
||||
|
||||
];
|
||||
|
||||
const products = [
|
||||
|
||||
[
|
||||
"Melk",
|
||||
3
|
||||
],
|
||||
|
||||
[
|
||||
"Bananen",
|
||||
2
|
||||
],
|
||||
|
||||
[
|
||||
"Brood",
|
||||
4
|
||||
],
|
||||
|
||||
[
|
||||
"Koffie",
|
||||
5
|
||||
],
|
||||
|
||||
[
|
||||
"WC papier",
|
||||
6
|
||||
]
|
||||
|
||||
];
|
||||
|
||||
|
||||
const insertProduct = db.prepare(`
|
||||
|
||||
INSERT OR IGNORE INTO products
|
||||
|
||||
(
|
||||
name,
|
||||
category_id
|
||||
)
|
||||
|
||||
VALUES (?,?)
|
||||
|
||||
`);
|
||||
|
||||
|
||||
|
||||
|
||||
const seedProducts = db.transaction(() => {
|
||||
|
||||
|
||||
for(const product of products){
|
||||
|
||||
insertProduct.run(product);
|
||||
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
seedProducts();
|
||||
|
||||
const insert = db.prepare(`
|
||||
|
||||
INSERT OR IGNORE INTO categories
|
||||
(name, icon)
|
||||
|
||||
VALUES (?,?)
|
||||
|
||||
`);
|
||||
|
||||
|
||||
const seed = db.transaction(() => {
|
||||
|
||||
for (const category of categories) {
|
||||
|
||||
insert.run(category);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
seed();
|
||||
|
||||
|
||||
console.log("Seed voltooid.");
|
||||
Reference in New Issue
Block a user