-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupDB.js
More file actions
78 lines (64 loc) · 2.64 KB
/
setupDB.js
File metadata and controls
78 lines (64 loc) · 2.64 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
67
68
69
70
71
72
73
74
75
76
77
78
const db = require("./db")
const usersTableStatement =
`CREATE TABLE IF NOT EXISTS "users" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"email" varchar UNIQUE,
"user_name" varchar UNIQUE NOT NULL,
"password" varchar NOT NULL
);`
const productsTableStatement =
`CREATE TABLE IF NOT EXISTS "products" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar NOT NULL,
"price" decimal NOT NULL
);`
const ordersTableStatement =
`CREATE TABLE IF NOT EXISTS "orders" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"status" varchar NOT NULL,
"user_id" integer NOT NULL,
"total" decimal NOT NULL,
"created" timestamp NOT NULL,
"modified" timestamp,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE
);`
const cartsTableStatement =
`CREATE TABLE IF NOT EXISTS "carts" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"user_id" integer NOT NULL,
"created" timestamp,
FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE
);`
const cartsProductsTableStatement =
`CREATE TABLE IF NOT EXISTS "carts_products" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"cart_id" integer NOT NULL,
"product_id" integer NOT NULL,
"qty" integer NOT NULL,
FOREIGN KEY ("cart_id") REFERENCES "carts" ("id") ON DELETE CASCADE,
FOREIGN KEY ("product_id") REFERENCES "products" ("id")
);`
const ordersProductsTableStatement =
`CREATE TABLE IF NOT EXISTS "orders_products" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"product_id" integer NOT NULL,
"order_id" integer NOT NULL,
"qty" integer NOT NULL,
FOREIGN KEY ("product_id") REFERENCES "products" ("id"),
FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE CASCADE
);`
const createDB = async () => {
await db.query(usersTableStatement);
await db.query(productsTableStatement);
await db.query(ordersTableStatement);
await db.query(cartsTableStatement);
await db.query(cartsProductsTableStatement);
await db.query(ordersProductsTableStatement);
}
createDB();
// `ALTER TABLE "orders" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE;`
// `ALTER TABLE "carts" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE;`
// `ALTER TABLE "carts_products" ADD FOREIGN KEY ("cart_id") REFERENCES "carts" ("id") ON DELETE CASCADE;`
// `ALTER TABLE "carts_products" ADD FOREIGN KEY ("product_id") REFERENCES "products" ("id");`
// `ALTER TABLE "orders_products" ADD FOREIGN KEY ("product_id") REFERENCES "products" ("id");`
// `ALTER TABLE "orders_products" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE CASCADE;`