summaryrefslogtreecommitdiff
path: root/database
diff options
context:
space:
mode:
Diffstat (limited to 'database')
-rw-r--r--database/003_surveys.sql6
-rw-r--r--database/db.js1
-rw-r--r--database/surveys.js19
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));
+}