-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvasLib.js
50 lines (43 loc) · 1.38 KB
/
canvasLib.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
require('dotenv').config();
const pg = require('pg-promise')(),
db = pg(process.env.DATABASE_URL);
let getElementHistory = () => {
return db.query(`SELECT * FROM el_history ORDER BY projectid, el_count;`)
.then((elements) => {
el_history = [];
elements.forEach((element) => {
element.d = JSON.parse(element.d);
el_history.push(element);
})
return el_history;
})
}
let insertDB = (objD) => {
return db.query(`
INSERT INTO el_history VALUES (
1, ${objD.el_count}, '${objD.type}', '${JSON.stringify(objD.d)}', '${objD.color}', '${objD.size}');`);
}
let updateElDB = (objD) => {
return db.query(`
UPDATE el_history SET
d = '${JSON.stringify(objD.d)}'
WHERE projectid = ${objD.projectid} AND
el_count = ${objD.el_count};`);
}
let deleteLastElDB = (projectId) => {
return db.query(`
SELECT max(el_count) FROM el_history
WHERE projectid = ${projectId};`)
.then( result => result[0].max)
.then( (lastCount) => {
db.query(`DELETE FROM el_history
WHERE projectid = ${projectId}
AND el_count = ${lastCount};`)
})
}
module.exports = {
getElementHistory,
insertDB,
updateElDB,
deleteLastElDB,
}