-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
107 lines (93 loc) · 2.64 KB
/
db.js
File metadata and controls
107 lines (93 loc) · 2.64 KB
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
const pool = require('./connect.js');
const deleteTables = () => {
const users = 'DROP TABLE users IF EXISTS';
const meetups = 'DROP TABLE meetups IF EXISTS';
const questions = 'DROP TABLE questions IF EXISTS';
const rsvp = 'DROP TABLE rsvp IF EXISTS';
const dropQueries = `${users}; ${questions}; ${rsvp}; ${meetups};`;
pool.query(dropQueries)
.then((res) => {
console.log(res);
pool.end();
})
.catch((err) => {
console.log(err);
pool.end();
});
pool.on('remove', () => {
console.log('client removed');
process.exit(0);
});
};
const createTables = () => {
const users = `CREATE TABLE IF NOT EXISTS
users(
id SERIAL PRIMARY KEY,
firstname VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
othername VARCHAR(50) NOT NULL,
email VARCHAR(100) NULL,
phone VARCHAR(15) NOT NULL,
username VARCHAR(50) NOT NULL,
password TEXT NOT NULL,
registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
isadmin BOOLEAN NOT NULL DEFAULT false
)`;
const meetups = `CREATE TABLE IF NOT EXISTS
meetups(
id SERIAL PRIMARY KEY,
createdon TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
location VARCHAR(100) NOT NULL,
images TEXT NULL,
topic VARCHAR(50) NOT NULL,
happeningon VARCHAR(255),
tags TEXT NULL
)`;
const questions = `CREATE TABLE IF NOT EXISTS
questions(
id SERIAL PRIMARY KEY,
createdOn TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
createdBy INT NOT NULL REFERENCES users(id),
meetup INT NOT NULL REFERENCES meetups(id),
title VARCHAR(100) NOT NULL,
body TEXT NULL,
upvotes INT NOT NULL DEFAULT 0,
downvotes INT NOT NULL DEFAULT 0
)`;
const rsvp = `CREATE TABLE IF NOT EXISTS
rsvp(
id SERIAL PRIMARY KEY,
meetup INT NOT NULL REFERENCES meetups(id),
topic VARCHAR(255) NOT NULL,
createdby INT NOT NULL REFERENCES users(id),
response VARCHAR(15) NOT NULL
)`;
const vote = `CREATE TABLE IF NOT EXISTS
votes(
id SERIAL PRIMARY KEY,
question INT NOT NULL REFERENCES questions(id),
votedby INT NOT NULL REFERENCES users(id),
upvotes BOOLEAN,
downvotes BOOLEAN
)`;
const createQueries = `${users}; ${meetups}; ${questions}; ${rsvp}; ${vote}`;
pool.query(createQueries)
.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 = {
deleteTables,
createTables,
pool
};
require('make-runnable');