Began implementing surveying
This commit is contained in:
parent
ec5d09586f
commit
93e6e02bca
4 changed files with 54 additions and 2 deletions
6
database/003_surveys.sql
Normal file
6
database/003_surveys.sql
Normal 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'));
|
|
@ -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
19
database/surveys.js
Normal 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));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue