summaryrefslogtreecommitdiff
path: root/nodejs/database/surveys.js
blob: 60c85b2895befdb963426af71b8467079d3b1239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import db from './db.js';

const deleteExpiredSurveysStatement = db.prepare(`DELETE FROM surveys WHERE data->>'expiration' < ?;`);
const getSurveysStatement = db.prepare(`SELECT data FROM surveys WHERE data->>'symbol' = ?;`);
const setSurveysStatement = db.prepare(`INSERT INTO surveys(data) VALUES (json(?));`);

export function deleteExpired() {
	return deleteExpiredSurveysStatement.run(new Date().toISOString()).changes;
}

export function get(symbol) {
	deleteExpired();
	return getSurveysStatement.all(symbol);
}

export function set(survey) {
	deleteExpired();
	return setSurveysStatement.run(JSON.stringify(survey));
}