-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
167 lines (149 loc) · 5.53 KB
/
db.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const { Pool } = require('pg');
const connection = require('./server/services/config');
require("@babel/polyfill");
const pool = new Pool({ connectionString: connection });
pool.on('connect', () => {
console.log('info', `Connected to ${connection} database`);
});
// const createTriggerFn = () => {
// const query = 'CREATE OR REPLACE FUNCTION trigger_set_timestamp() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at= NOW(); RETURN NEW; END;';
// pool.query(query).then((res) => {
// console.log('info', res);
// pool.end();
// })
// .catch((err) => {
// console.log('error', err);
// pool.end();
// });
// };
const createUsersTable = () => {
const query = `CREATE TABLE IF NOT EXISTS users ( id BIGINT PRIMARY KEY, email VARCHAR(30) NOT NULL UNIQUE, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, password VARCHAR(140) NOT NULL, address VARCHAR(400) NOT NULL, is_admin BOOLEAN NOT NULL DEFAULT FALSE, status VARCHAR(10) NOT NULL DEFAULT 'active', created_on TIMESTAMPTZ NOT NULL DEFAULT NOW())`;
pool.query(query).then((res) => {
console.log('info', res);
pool.end();
})
.catch((err) => {
console.log('error', err);
pool.end();
});
};
const dropUsersTable = () => {
const query = 'DROP TABLE IF EXISTS users';
pool.query(query).then((res) => {
console.log('info', res);
pool.end();
}).catch((err) => {
console.log('error', err);
pool.end();
});
};
const createCarsTable = () => {
const query = `CREATE TABLE IF NOT EXISTS cars (id BIGINT PRIMARY KEY, owner BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, created_on TIMESTAMPTZ NOT NULL DEFAULT NOW(), state VARCHAR(8) NOT NULL, status VARCHAR(15) NOT NULL DEFAULT 'available', price NUMERIC(10, 2) NOT NULL CHECK(price > 0), manufacturer VARCHAR(30) NOT NULL, model VARCHAR(30) NOT NULL, body_type VARCHAR(30) NOT NULL, description TEXT, image_url VARCHAR(150), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ) `;
pool.query(query).then((res) => {
console.log('info', res);
pool.end();
}).catch((err) => {
console.log('error', err);
pool.end();
});
};
const dropCarsTable = async() => {
const query = 'DROP TABLE IF EXISTS cars';
try {
const res = await pool.query(query);
console.log('info', res);
pool.end();
} catch (error) {
console.log('error', error);
pool.end();
}
};
const createOrdersTable = () => {
const query = `CREATE TABLE IF NOT EXISTS orders (id BIGINT PRIMARY KEY, buyer_id BIGINT REFERENCES users(id) ON DELETE CASCADE, car_id BIGINT NOT NULL REFERENCES cars(id) ON DELETE CASCADE, seller_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, price NUMERIC NOT NULL CHECK(price > 0), status VARCHAR(20) NOT NULL DEFAULT '
pending ', date TIMESTAMPTZ NOT NULL DEFAULT NOW(), price_offered NUMERIC NOT NULL CHECK(price_offered > 0), new_price_offered NUMERIC, updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW())`;
pool.query(query).then((res) => {
console.log('info', res);
pool.end();
}).catch((err) => {
console.log('error', err);
pool.end();
});
};
const dropOrdersTable = async() => {
const query = 'DROP TABLE IF EXISTS orders';
try {
const res = await pool.query(query);
console.log('info', res);
pool.end();
} catch (err) {
console.log('error', err);
pool.end();
}
};
const createFlagsTable = () => {
const query = `CREATE TABLE IF NOT EXISTS flags (id BIGINT PRIMARY KEY, car_id BIGINT REFERENCES cars(id) ON DELETE RESTRICT, created_on TIMESTAMPTZ NOT NULL DEFAULT NOW(), reason VARCHAR(20) NOT NULL, description TEXT, reportedBy BIGINT NOT NULL REFERENCES users(id), status VARCHAR(20) NOT NULL DEFAULT 'pending', severity VARCHAR(20) NOT NULL DEFAULT 'minor') `;
pool.query(query).then((res) => {
console.log('info', res);
pool.end();
}).catch((err) => {
console.log('error', err);
pool.end();
});
};
const dropFlagsTable = async() => {
const query = 'DROP TABLE IF EXISTS flags';
try {
const res = await pool.query(query);
console.log('info', res);
pool.end();
} catch (err) {
console.log('error', err);
pool.end();
}
};
// const createTriggerOnTable = (table) => {
// const query = `CREATE TRIGGER set_timestamp BEFORE UPDATE ON ${table} FOR EACH ROW EXECUTE PROCEDURE trigger_set_timestamp()`;
// pool.query(query).then((res) => {
// console.log('info', res);
// })
// .catch((err) => {
// console.log('error', err);
// });
// };
const createAllTables = () => {
// createTriggerFn();
createUsersTable();
createUsersTable();
createCarsTable();
createCarsTable();
// createTriggerOnTable('cars');
createOrdersTable();
createOrdersTable();
// createTriggerOnTable('orders');
createFlagsTable();
createFlagsTable();
};
const dropAllTables = () => {
dropUsersTable();
dropCarsTable();
dropOrdersTable();
dropFlagsTable();
};
pool.on('remove', () => {
console.log('info', 'client removed');
process.exit(0);
});
module.exports = {
// createTriggerFn,
createUsersTable,
createCarsTable,
createOrdersTable,
createFlagsTable,
dropUsersTable,
dropCarsTable,
dropOrdersTable,
dropFlagsTable,
createAllTables,
dropAllTables,
};
require('make-runnable');