-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb-helpers.js
More file actions
105 lines (95 loc) · 3.05 KB
/
db-helpers.js
File metadata and controls
105 lines (95 loc) · 3.05 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const __ = require('lodash');
const throwError = (res, status) => {
return (err) => {
return res.status(status || 500).json({ error: err.message });
};
};
const getAll = (file, table) => {
return (req, res) => {
return file.download()
.then(file.getDb)
.then((db) => db.query(`SELECT * FROM ${table}`))
.then((items) => res.json(items))
.catch(throwError(res));
};
};
const getOneBy = (file, table, column) => {
return (req, res) => {
return file.download()
.then(file.getDb)
.then((db) => db.query(`SELECT * FROM ${table} WHERE ${column} = :value`, { value: req.params[column] }))
.then((items) => {
return res
.status(items && items[0] ? 200 : 404)
.json(items[0]);
})
.catch(throwError(res));
};
};
const deleteOneBy = (file, table, column) => {
return (req, res) => {
return file.download()
.then(file.getDb)
.then((db) => db.query(`DELETE FROM ${table} WHERE ${column} = :value`, { value: req.params[column] }))
.then(file.upload)
.then(file.getDb)
.then((db) => db.query(`SELECT changes() as changes`))
.then((items) => {
return res
.status(items && items[0] && parseInt(items[0]['changes']) >= 1 ? 200 : 404)
.json(items[0]);
})
.then(file.getDb)
.then((db) => db.close())
.catch(throwError(res));
};
};
const updateOneBy = (file, table, column) => {
return (req, res) => {
const keys = __.map(__.keys(req.body), (key) => `${key}=:${key}`);
const query = `UPDATE ${table} SET ${__.join(keys, ', ')} WHERE ${column} = :__value`;
return file.download()
.then(file.getDb)
.then((db) => db.query(query, __.assign({}, req.body, { __value: req.params[column] })))
.then(file.upload)
.then(file.getDb)
.then((db) => db.query(`SELECT changes() as changes`))
.then((items) => {
return res
.status(items && items[0] && parseInt(items[0]['changes']) >= 1 ? 200 : 404)
.json(items[0]);
})
.then(file.getDb)
.then((db) => db.close())
.catch(throwError(res));
};
};
const insert = (file, table) => {
return (req, res) => {
const keys = __.keys(req.body);
const query = `INSERT INTO ${table} (${__.join(keys, ', ')}) VALUES (:${__.join(keys, ', :')})`;
return file.download()
.then(file.getDb)
.then((db) => db.query(query, req.body))
.catch(throwError(res, 400))
.then(file.upload)
.then(file.getDb)
.then((db) => new Promise((resolve) => {
db.lastRowID(table, (rowId) => resolve(rowId));
}))
.then((rowId) => res.json({ rowId }))
.then(file.getDb)
.then((db) => db.close())
.catch(throwError(res));
};
};
const customQuery = (file, query) => {
return (req, res) => {
return file.download()
.then(file.getDb)
.then((db) => db.query(query))
.then((items) => res.json(items))
.catch(throwError(res));
};
};
module.exports = { getAll, getOneBy, insert, deleteOneBy, updateOneBy, customQuery };