-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
69 lines (63 loc) · 1.43 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
68
69
var { Client } = require("pg");
require("dotenv").config();
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
});
var connect = function () {
client
.connect()
.then(() => console.log("connected"))
.catch((e) => console.error("connection error", e.stack));
};
var newEntry = function ({ folderName, dlLink }, callback) {
var text =
"INSERT INTO downloadables (folder_name, download_link, active) VALUES ($1,$2,$3) RETURNING id_number";
var values = [folderName, dlLink, true];
client.query(text, values, (err, res) => {
if (err) {
console.log(err.stack);
} else {
callback(res.rows[0].id_number);
}
});
};
var checkUser = function (userId, callback) {
client.query(
"SELECT active FROM downloadables WHERE folder_name = $1",
[userId],
(err, res) => {
if (err) {
console.log(err.stack);
} else {
if (res.rows[0]) {
callback(true);
} else {
callback(false);
}
}
}
);
};
var getFolderName = function (idNumber, callback) {
client.query(
"SELECT folder_name FROM downloadables WHERE id_number = $1",
[idNumber],
(err, res) => {
if (err) {
console.log(err.stack);
} else {
callback(res.rows[0].folder_name);
}
}
);
};
module.exports = {
client,
connect,
newEntry,
checkUser,
getFolderName,
};