-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-setup.js
43 lines (37 loc) · 985 Bytes
/
db-setup.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
// Sets up the database with the required tables and data
import pg from "pg";
import "dotenv/config";
const client = new pg.Client({
connectionString: process.env.POSTGRES_CONNECTION_STRING,
});
// Connect to Postgres
client.connect();
if (process.env.DANGEROUS_DELETE_TABLE) {
// Delete the keys table
client.query(`
DROP TABLE IF EXISTS keys;
`, (err, res) => {
if (err) {
console.error(err);
} else {
console.log("Table deleted successfully.");
}
});
}
// Create the keys table
client.query(`
CREATE TABLE IF NOT EXISTS keys (
name TEXT PRIMARY KEY NOT NULL,
roname TEXT,
value TEXT,
last_accessed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
`, (err, res) => {
if (err) {
console.error(err);
} else {
console.log("Data created successfully, you can now run the server.");
}
// Now close the connection
client.end();
});