diff options
author | Julien Dessaux | 2023-06-15 01:02:03 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-06-15 01:02:03 +0200 |
commit | 93e6e02bcafaac0a57b7c1d98dca5ba52f32684b (patch) | |
tree | 965134bd28dab9acf6c313a78ea1099e60126406 /database | |
parent | Fixed api errors and some async issues (diff) | |
download | spacetraders-93e6e02bcafaac0a57b7c1d98dca5ba52f32684b.tar.gz spacetraders-93e6e02bcafaac0a57b7c1d98dca5ba52f32684b.tar.bz2 spacetraders-93e6e02bcafaac0a57b7c1d98dca5ba52f32684b.zip |
Began implementing surveying
Diffstat (limited to 'database')
-rw-r--r-- | database/003_surveys.sql | 6 | ||||
-rw-r--r-- | database/db.js | 1 | ||||
-rw-r--r-- | database/surveys.js | 19 |
3 files changed, 26 insertions, 0 deletions
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)); +} |