-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseederScript.js
79 lines (65 loc) · 1.58 KB
/
seederScript.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
const { Client } = require('pg');
// Config
const {
host,
user,
database,
password,
port,
} = require('./src/config/dbConfig');
// DB queries
const query = require('./src/dbQueries/createTable');
// Initialize Client instance
const client = new Client({
host,
user,
database,
password,
port,
});
// Connect to Db
async function connectToDB() {
try {
// Start connection
await client.connect();
console.log('Connected to DB successfully!');
} catch (error) {
console.log(`Error connecting to db >> ${error}`);
// End connection
await client.end();
console.log('Connection ended!');
}
}
// create rooms table
async function createRoomsTable() {
try {
// Call function to connect to DB
connectToDB();
await client.query(query.createRoomsTableQuery);
console.log('Rooms table created successfully!');
} catch (error) {
console.log(`Error im creating rooms table ${error}`);
// End connection
await client.end();
console.log('Connection ended!');
}
}
createRoomsTable();
// create students table
async function createStudentsTable() {
try {
// Continue with queries since the connection is already open,
await client.query(query.createStudentsTableQuery);
console.log('Student table created successfully!');
} catch (error) {
console.log(`Error in creating students table ${error}`);
// End connection
await client.end();
console.log('Connection ended!');
} finally {
// End connection
await client.end();
console.log('Connection ended!');
}
}
createStudentsTable();