-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
67 lines (58 loc) · 1.18 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
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 User Table
*/
const createUserTable = () => {
const queryText = `CREATE TABLE IF NOT EXISTS
users(
id UUID PRIMARY KEY,
email VARCHAR(128) UNIQUE NOT NULL,
user_name VARCHAR(128) NOT NULL,
password VARCHAR(224) 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 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 = {
createUserTable,
dropUserTable
};
require("make-runnable");