forked from olawalejarvis/reflection_app_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
112 lines (100 loc) · 2.1 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
// db.js
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 Reflection Table
*/
const createReflectionTable = () => {
const queryText =
`CREATE TABLE IF NOT EXISTS
reflections(
id UUID PRIMARY KEY,
success TEXT NOT NULL,
low_point TEXT NOT NULL,
take_away TEXT NOT NULL,
owner_id UUID NOT NULL,
created_date TIMESTAMP,
modified_date TIMESTAMP,
FOREIGN KEY (owner_id) REFERENCES users (id) ON DELETE CASCADE
)`;
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 UUID PRIMARY KEY,
email VARCHAR(128) UNIQUE NOT NULL,
password VARCHAR(128) NOT NULL,
created_date TIMESTAMP,
modified_date TIMESTAMP
)`;
pool.query(queryText)
.then((res) => {
console.log(res);
pool.end();
})
.catch((err) => {
console.log(err);
pool.end();
});
}
/**
* Drop Reflection Table
*/
const dropReflectionTable = () => {
const queryText = 'DROP TABLE IF EXISTS reflections returning *';
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();
});
}
pool.on('remove', () => {
console.log('client removed');
process.exit(0);
});
module.exports = {
createReflectionTable,
createUserTable,
dropReflectionTable,
dropUserTable
};
require('make-runnable');