summaryrefslogtreecommitdiff
path: root/nodejs/database/surveys.js
blob: 3397374dccd1d75e4dd050601e8b3a7475d8ac01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
	const data = getSurveysStatement.all(symbol);
	return data.map(elt => JSON.parse(elt.data));
}

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