Chore: refactor to use new schema changing queries

This commit is contained in:
csrapr 2021-03-01 15:55:29 +00:00 committed by Josh Harvey
parent 304f792484
commit e6059be8d2
44 changed files with 185 additions and 171 deletions

View file

@ -22,7 +22,7 @@ function getLastSeen (deviceId) {
const sql = `select id, timestamp, serial from logs
where device_id=$1
order by timestamp desc, serial desc limit 1`
return db.oneOrNone(sql, [deviceId])
return db.$oneOrNone(sql, [deviceId])
.then(log => log ? {timestamp: log.timestamp, serial: log.serial, id: log.id} : null)
}
@ -56,14 +56,14 @@ function update (deviceId, logLines) {
}, logLines)
const sql = pgp.helpers.insert(logs, cs) + 'on conflict do nothing'
return db.none(sql)
return db.$none(sql)
}
function clearOldLogs () {
const sql = `delete from logs
where timestamp < now() - interval '3 days'`
return db.none(sql)
return db.$none(sql)
}
function getUnlimitedMachineLogs (deviceId, until = new Date().toISOString()) {
@ -75,7 +75,7 @@ function getUnlimitedMachineLogs (deviceId, until = new Date().toISOString()) {
and timestamp > (timestamp $2 - interval '2 days')
order by timestamp desc, serial desc`
return Promise.all([db.any(sql, [ deviceId, until ]), getMachineName(deviceId)])
return Promise.all([db.$any(sql, [ deviceId, until ]), getMachineName(deviceId)])
.then(([logs, machineName]) => ({
logs: _.map(_.mapKeys(_.camelCase), logs),
currentMachine: {deviceId, name: machineName}
@ -90,7 +90,7 @@ function getMachineLogs (deviceId, until = new Date().toISOString(), limit = nul
limit $3
offset $4`
return Promise.all([db.any(sql, [ deviceId, until, limit, offset ]), getMachineName(deviceId)])
return Promise.all([db.$any(sql, [ deviceId, until, limit, offset ]), getMachineName(deviceId)])
.then(([logs, machineName]) => ({
logs: _.map(_.mapKeys(_.camelCase), logs),
currentMachine: {deviceId, name: machineName}
@ -106,7 +106,7 @@ function simpleGetMachineLogs (deviceId, from = new Date(0).toISOString(), until
limit $4
offset $5`
return db.any(sql, [ deviceId, from, until, limit, offset ])
return db.$any(sql, [ deviceId, from, until, limit, offset ])
.then(_.map(_.mapKeys(_.camelCase)))
}