From 93e6e02bcafaac0a57b7c1d98dca5ba52f32684b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 15 Jun 2023 01:02:03 +0200 Subject: Began implementing surveying --- database/003_surveys.sql | 6 ++++++ database/db.js | 1 + database/surveys.js | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 database/003_surveys.sql create mode 100644 database/surveys.js (limited to 'database') diff --git a/database/003_surveys.sql b/database/003_surveys.sql new file mode 100644 index 0000000..0548796 --- /dev/null +++ b/database/003_surveys.sql @@ -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')); diff --git a/database/db.js b/database/db.js index a6d5174..0fdbcfe 100644 --- a/database/db.js +++ b/database/db.js @@ -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( diff --git a/database/surveys.js b/database/surveys.js new file mode 100644 index 0000000..60c85b2 --- /dev/null +++ b/database/surveys.js @@ -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)); +} -- cgit v1.2.3