-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
80 lines (71 loc) · 1.7 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
const { Pool } = require('pg');
const dotenv = require('dotenv');
dotenv.config();
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
pool.on('connect', () => {
console.log('connected to the db');
});
/**
* Create Tables
*/
const createTables = () => {
const queryText =
`CREATE TABLE IF NOT EXISTS
users(
id UUID PRIMARY KEY,
firstname VARCHAR(128) NOT NULL,
lastname VARCHAR(128) NOT NULL,
othernames VARCHAR(128) NOT NULL,
email VARCHAR(128) NOT NULL,
isAdmin VARCHAR(128) NOT NULL,
registered TIMESTAMP,
modifiedDate TIMESTAMP
),
parcels(
id UUID PRIMARY KEY,
placedBy VARCHAR(128) NOT NULL,
weight VARCHAR(128) NOT NULL,
weightmetric VARCHAR(128) NOT NULL,
status VARCHAR(128) NOT NULL,
from VARCHAR(128) NOT NULL,
to VARCHAR(128) NOT NULL,
currentLocation VARCHAR(128) NOT NULL,
sentOn TIMESTAMP,
deliveredOn TIMESTAMP
)`;
pool.query(queryText)
.then((res) => {
console.log(res);
pool.end();
})
.catch((err) => {
console.log(err);
pool.end();
});
};
/**
* Drop Tables
*/
const dropTables = () => {
const queryText = 'DROP TABLE IF EXISTS users';
pool.query(queryText)
.then((res) => {
console.log(res);
pool.end();
})
.catch((err) => {
console.log(err);
pool.end();
});
};
pool.on('remove', () => {
console.log('client removed');
process.exit(0);
});
module.exports = {
createTables,
dropTables
};
require('make-runnable');