-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
118 lines (107 loc) · 2.23 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
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 createIncidentTable = () => {
const queryText = `CREATE TABLE IF NOT EXISTS
incidents(
id SERIAL PRIMARY KEY,
createdOn TIMESTAMP,
createdBy INTEGER NOT NULL,
title VARCHAR(128) NOT NULL,
type VARCHAR(128) NOT NULL,
location VARCHAR(128) NOT NULL,
status VARCHAR(128) NOT NULL,
images TEXT,
videos TEXT,
comments TEXT NOT NULL
)`;
pool
.query(queryText)
.then(res => {
console.log(res);
pool.end();
})
.catch(err => {
console.log(err);
pool.end();
});
};
/**
* Create User Table
*/
const createUserTable = () => {
const queryText = `CREATE TABLE IF NOT EXISTS
users(
id SERIAL PRIMARY KEY,
firstname VARCHAR(128) NOT NULL,
lastname VARCHAR(128) NOT NULL,
email VARCHAR(128) UNIQUE NOT NULL,
phonenumber VARCHAR(128) NOT NULL,
username VARCHAR(128) UNIQUE NOT NULL,
password VARCHAR(128) NOT NULL,
registered TIMESTAMP,
isadmin BOOL DEFAULT 'f'
)`;
pool
.query(queryText)
.then(res => {
console.log(res);
pool.end();
})
.catch(err => {
console.log(err);
pool.end();
});
};
/**
* Drop User Table
*/
const dropUserTable = () => {
const queryText = 'DROP TABLE IF EXISTS users returning *';
pool
.query(queryText)
.then(res => {
console.log(res);
pool.end();
})
.catch(err => {
console.log(err);
pool.end();
});
};
/**
* Drop Tables
*/
const dropIncidentTable = () => {
const queryText = 'DROP TABLE IF EXISTS incidents';
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 = {
createIncidentTable,
createUserTable,
dropIncidentTable,
dropUserTable
};
require('make-runnable');