Multi tenant DB, registratie per huishouden V1.2

This commit is contained in:
2026-08-09 19:50:53 +02:00
parent 1f9fd73026
commit c37b8956d4
15 changed files with 148 additions and 96 deletions
+30 -20
View File
@@ -4,7 +4,7 @@ const db = require("../config/database");
class ProductRepository {
getAll() {
getAll(householdId) {
return db.prepare(`
@@ -24,16 +24,18 @@ class ProductRepository {
ON categories.id = products.category_id
WHERE products.household_id = ?
ORDER BY products.name
`).all();
`).all(householdId);
}
findById(id) {
findById(id, householdId) {
return db.prepare(`
@@ -41,9 +43,9 @@ class ProductRepository {
FROM products
WHERE id = ?
WHERE id = ? AND household_id = ?
`).get(id);
`).get(id, householdId);
}
@@ -59,10 +61,11 @@ class ProductRepository {
(
name,
category_id,
barcode
barcode,
household_id
)
VALUES (?, ?, ?)
VALUES (?, ?, ?, ?)
`).run(
@@ -70,7 +73,8 @@ class ProductRepository {
data.category_id || null,
data.barcode || null
data.barcode || null,
data.household_id
);
@@ -81,9 +85,9 @@ class ProductRepository {
createMany(products) {
const insert = db.prepare(`
INSERT INTO products (name, category_id, barcode)
INSERT INTO products (name, category_id, barcode, household_id)
VALUES (?, ?, ?)
VALUES (?, ?, ?, ?)
`);
@@ -92,7 +96,8 @@ class ProductRepository {
insert.run(
product.name,
product.category_id || null,
product.barcode || null
product.barcode || null,
product.household_id
);
}
});
@@ -102,7 +107,7 @@ class ProductRepository {
}
update(id, data) {
update(id, householdId, data) {
return db.prepare(`
@@ -110,7 +115,7 @@ class ProductRepository {
SET name = ?, category_id = ?
WHERE id = ?
WHERE id = ? AND household_id = ?
`).run(
@@ -118,14 +123,15 @@ class ProductRepository {
data.category_id || null,
id
id,
householdId
);
}
hasListItems(id) {
hasListItems(id, householdId) {
return db.prepare(`
@@ -133,24 +139,28 @@ class ProductRepository {
FROM shopping_list_items
WHERE product_id = ?
WHERE product_id = ? AND EXISTS (
SELECT 1 FROM shopping_lists
WHERE shopping_lists.id = shopping_list_items.list_id
AND shopping_lists.household_id = ?
)
LIMIT 1
`).get(id) !== undefined;
`).get(id, householdId) !== undefined;
}
delete(id) {
delete(id, householdId) {
return db.prepare(`
DELETE FROM products
WHERE id = ?
WHERE id = ? AND household_id = ?
`).run(id);
`).run(id, householdId);
}