1
0
Fork 0

Began implementing surveying

This commit is contained in:
Julien Dessaux 2023-06-15 01:02:03 +02:00
parent ec5d09586f
commit 93e6e02bca
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 54 additions and 2 deletions

6
database/003_surveys.sql Normal file
View file

@ -0,0 +1,6 @@
CREATE TABLE surveys (
id INTEGER PRIMARY KEY,
data JSON NOT NULL
);
CREATE INDEX surveys_data_symbol on surveys (json_extract(data, '$.symbol'));
CREATE INDEX surveys_data_expiration on surveys (json_extract(data, '$.expiration'));

View file

@ -5,6 +5,7 @@ const allMigrations = [
'database/000_init.sql',
'database/001_systems.sql',
'database/002_ships.sql',
'database/003_surveys.sql',
];
const db = new Database(

19
database/surveys.js Normal file
View file

@ -0,0 +1,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));
}