forked from sphoebs/examware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
46 lines (31 loc) · 1.32 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
/**
* Created by fabio on 02/12/2017.
*/
const pg = require('pg')
const pgClientData = require('./secrets')
const pgClient = new pg.Client(pgClientData);
pgClient.connect()
//const res = await pgClient.query('CREATE TABLE IF NOT EXISTS ASSIGNMENTS (EXAM_ID integer, STUDENT_ID integer, JSON_DATA JSON)')
//pgClient.query('ALTER TABLE ASSIGNMENTS ADD TIMESTAMP timestamp')
pgClient.query('CREATE TABLE IF NOT EXISTS ASSIGNMENTS (EXAM_ID integer, STUDENT_ID integer, TIMESTAMP timestamp, JSON_DATA JSON)')
.then(r=> {console.log(r)})
.catch(e=> console.log(e))
exports.addAssignment= function addAssignment (examId, studentID, data) {
const text = 'insert into assignments (EXAM_ID, STUDENT_ID, TIMESTAMP, JSON_DATA) values ($1, $2, CURRENT_TIMESTAMP, $3) returning *'
const values = [examId, studentID, data]
pgClient.query(text, values)
.then(res => {
for (const i of res.rows) console.log(i)
})
.catch(e => console.error(e.stack))
}
exports.getAllAssignments= function getAllAssignments (examId) {
const text = 'select * from assignments where EXAM_ID=$1'
const values = [examId]
pgClient.query(text, values)
.then(res => {
for (const i of res.rows) console.log(i)
})
.catch(e => console.error(e.stack))
}
//module.exports = db