-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
63 lines (56 loc) · 1.77 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
'use strict';
const pg = require('pg');
const pool = new pg.Pool({
user: 'marcus',
database: 'example',
password: 'marcus',
port: 5432,
host: '127.0.0.1',
});
module.exports = (table) => ({
query: async(sql, args) => await pool.query(sql, args),
create: async ({ ...data }) => {
const names = Object.keys(data);
const values = Object.values(data);
const numbers = new Array(names.length);
for (let i = 0; i < names.length; i++) numbers[i] = '$' + (i + 1);
const fields = names.join(',');
const indices = numbers.join(',');
const sql = `INSERT INTO ${table} (${fields}) VALUES (${indices})`;
return await pool.query(sql, values);
},
read: async (id, fields = ['*']) => {
const names = fields.join(',');
if (!id) return await pool.query(`SELECT ${names} FROM ${table}`);
const sql = `SELECT ${names} FROM ${table} WHERE id = $1`;
return await pool.query(sql, [id]);
},
update: async ({ ...data }) => {
const { id } = data;
delete data.id;
const keys = Object.keys(data);
const values = Object.values(data);
const { length } = keys;
const args = new Array(length);
for (let i = 0; i < length; i++) {
const arg = keys[i] + '=$' + (i + 1);
args[i] = arg;
}
const delta = args.join(',');
const sql = `UPDATE ${table} SET ${delta} WHERE id=$${length + 1}`;
values.push(id);
return await pool.query(sql, values);
},
delete: async ({ ...selectors }) => {
const args = [];
const values = [];
let index = 1;
for (const key in selectors) {
values.push(selectors[key]);
const arg = key + '=$' + index++;
args.push(arg);
}
const sql = `DELETE FROM ${table} WHERE ${args.join(' AND ')}`;
return await pool.query(sql, values);
},
});