-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanageDB.js
111 lines (97 loc) · 2.43 KB
/
manageDB.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
require("dotenv/config");
const { Pool } = require("pg");
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
pool.on("connect", () => {
console.log("...connected to database...");
});
const create = async (table, statement) => {
try {
await pool.query(`CREATE TABLE IF NOT EXISTS ${table}(${statement})`);
await pool.end();
console.log(`....${table} table successfully created....`);
} catch (error) {
console.log(error);
}
};
const drop = async table => {
try {
await pool.query(`DROP TABLE IF EXISTS ${table}`);
console.log(`....${table} table successfully dropped....`);
await pool.end();
} catch (error) {
console.log(error);
}
};
/** Create users table
* * @returns {Promise}
*/
const createUsersTable = () => {
create(
"users",
"id SERIAL PRIMARY KEY, email VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(100) NOT NULL, dateCreated TIMESTAMP, dateModified TIMESTAMP"
);
};
/**Drop users table
** @returns {Promise}
*/
const dropUsersTable = () => {
drop("users");
};
/** Create owners table
* * @returns {Promise}
*/
const createOwnersTable = () => {
create(
"owners",
"id SERIAL PRIMARY KEY,firstName VARCHAR(50) NOT NULL,lastName VARCHAR(50) NOT NULL, email VARCHAR(50) UNIQUE NOT NULL,createdBy VARCHAR(50) REFERENCES users(email),dateCreated TIMESTAMP,dateModified TIMESTAMP"
);
};
/**
* * @returns {Promise}
*/
const dropOwnersTable = () => {
drop("owners");
};
/** Create owners table
* * @returns {Promise}
*/
const createCategoriesTable = () => {
create(
"categories",
"id SERIAL PRIMARY KEY,categoryName VARCHAR(50) NOT NULL,createdBy INTEGER REFERENCES users(id),dateCreated TIMESTAMP, dateModified TIMESTAMP"
);
};
/**
* * @returns {Promise}
*/
const dropCategoriesTable = () => {
drop("categories");
};
/** Create items table
* * @returns {Promise}
*/
const createItemsTable = () => {
create(
"items",
"id SERIAL PRIMARY KEY,categoryId INTEGER REFERENCES categories(id),ownerId INTEGER REFERENCES owners(id),itemName VARCHAR(50) NOT NULL,createdBy INTEGER REFERENCES users(id),dateCreated TIMESTAMP,dateModified TIMESTAMP"
);
};
/**
* * @returns {Promise}
*/
const dropItemsTable = () => {
drop("items");
};
module.exports = {
createUsersTable,
createOwnersTable,
createItemsTable,
createCategoriesTable,
dropUsersTable,
dropOwnersTable,
dropCategoriesTable,
dropItemsTable
};
require("make-runnable");