-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
40 lines (33 loc) · 848 Bytes
/
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
const _ = require('lodash')
const level = require('level')
const { appEmitter } = require('./emitters.js')
const { SMPC_DB_PATH } = require('./config/constants')
const logger = require('./config/winston')
const db = level(SMPC_DB_PATH, { valueEncoding: 'json' })
appEmitter.on('update-computation', msg => {
try {
updateJob(msg)
} catch (e) {
logger.error('DB: Update computation failed', e)
}
})
const normalizeDBRecord = rec => {
const forbiddenAttributes = ['link', 'totalClients']
return _.omit(rec, forbiddenAttributes)
}
const addJobToDB = async job => {
await db.put(job.id, { ...job })
}
const updateJob = async job => {
await db.put(job.id, { ...job })
}
const getJob = async id => {
let out = await db.get(id)
return normalizeDBRecord(out)
}
module.exports = {
db,
addJobToDB,
updateJob,
getJob
}