-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.sql
95 lines (83 loc) · 2.37 KB
/
database.sql
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- users
CREATE TABLE users(
id BIGSERIAL PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password TEXT NOT NULL
);
-- users sessions
CREATE TABLE user_sessions (
sid VARCHAR NOT NULL COLLATE "default",
sess JSON NOT NULL,
expire timestamp(6) NOT NULL
)
WITH (OIDS=FALSE);
ALTER TABLE user_sessions ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;
CREATE INDEX "IDX_session_expire" ON "user_sessions" ("expire");
-- products
CREATE TABLE products(
id BIGSERIAL PRIMARY KEY,
name VARCHAR(70) NOT NULL,
description TEXT NOT NULL,
price NUMERIC(8, 2) NOT NULL,
stock INTEGER NOT NULL,
category VARCHAR(20) NOT NULL,
currency VARCHAR(3) NOT NULL
);
-- images
CREATE TABLE images(
id BIGSERIAL PRIMARY KEY,
image_url TEXT NOT NULL,
filename TEXT NOT NULL,
product_id BIGINT REFERENCES products(id) ON DELETE CASCADE
);
-- reviews
CREATE TABLE reviews(
id BIGSERIAL PRIMARY KEY,
product_id BIGINT REFERENCES products(id) ON DELETE CASCADE,
user_id BIGINT REFERENCES users(id) ON DELETE CASCADE,
rating SMALLINT DEFAULT 1,
review TEXT NOT NULL,
created_at DATE DEFAULT CURRENT_TIMESTAMP
);
-- orders
CREATE TABLE orders(
id BIGSERIAL PRIMARY KEY,
user_id BIGINT REFERENCES users(id) ON DELETE CASCADE,
created_at DATE DEFAULT CURRENT_TIMESTAMP,
price NUMERIC(20, 4) NOT NULL,
currency VARCHAR(3) NOT NULL,
payment_status VARCHAR(10),
shipping_date DATE,
delivery_status VARCHAR(20)
);
-- order items
CREATE TABLE order_items(
id BIGSERIAL PRIMARY KEY,
order_id BIGINT REFERENCES orders(id) ON DELETE CASCADE,
product_id BIGINT REFERENCES products(id) ON DELETE CASCADE,
quantity INTEGER NOT NULL,
subtotal NUMERIC(20, 4) NOT NULL,
currency VARCHAR(3) NOT NULL
);
-- shipping address
CREATE TABLE shipping_address(
id BIGSERIAL PRIMARY KEY,
order_id BIGINT REFERENCES orders(id) ON DELETE CASCADE,
address VARCHAR(100) NOT NULL,
city VARCHAR(30) NOT NULL,
state VARCHAR(30) NOT NULL,
country VARCHAR(30) NOT NULL,
postal_code VARCHAR(6) NOT NULL,
phone VARCHAR(10) NOT NULL
);
-- roles
CREATE TABLE roles(
id BIGSERIAL PRIMARY KEY,
name VARCHAR(20) NOT NULL
);
-- user roles
CREATE TABLE user_roles(
user_id BIGINT REFERENCES users(id) ON DELETE CASCADE,
roles_id BIGINT REFERENCES roles(id) ON DELETE CASCADE
);