-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
70 lines (59 loc) · 1.45 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
70
const { Client } = require("pg");
class DB {
constructor(user, host, database, password, port) {
const credentials = {
user,
host,
database,
password,
port
};
console.log(credentials);
this.client = new Client(credentials);
}
async init() {
await this.client.connect();
}
async getUser(userid) {
const query = `SELECT * FROM users WHERE userid = $1`;
const values = [userid];
try {
const {rows} = await this.client.query(query, values);
return rows.length === 0 ? null : rows[0];
} catch (err) {
console.log(err);
return null;
}
}
async createUser(userid, password, email) {
const query = `
INSERT INTO users (userid, password, email)
VALUES ($1, $2, $3)
`;
const values = [userid, password, email];
try {
await this.client.query(query, values);
return {userid, password, email};
} catch (err) {
console.log(err);
return null;
}
}
async createBenchmark(userid, timestamp, op, l, r, elapsed, attempts) {
const query = `
INSERT INTO benchmarks (userid, timestamp, op, l, r, elapsed, attempts)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (userid, timestamp) DO NOTHING
`;
const values = [userid, new Date(timestamp), op, l, r, elapsed, attempts];
console.log(values);
try {
await this.client.query(query, values);
return {userid, timestamp, op, l, r, elapsed, attempts};
} catch (err) {
console.log(err);
return null;
}
}
}
module.exports = DB;