diff options
author | Julien Dessaux | 2023-05-24 23:03:19 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-05-24 23:03:19 +0200 |
commit | 9963ab79b7c7392d5f87c98cd55953ffa004efd9 (patch) | |
tree | efcd42cbe0f2614c863a05c77d797c7a4645b003 /database | |
parent | Implemented a basic extraction loop (diff) | |
download | spacetraders-9963ab79b7c7392d5f87c98cd55953ffa004efd9.tar.gz spacetraders-9963ab79b7c7392d5f87c98cd55953ffa004efd9.tar.bz2 spacetraders-9963ab79b7c7392d5f87c98cd55953ffa004efd9.zip |
Rewrote the api rate limiter with promises instead of callbacks
Diffstat (limited to '')
-rw-r--r-- | database/001_systems.sql | 6 | ||||
-rw-r--r-- | database/db.js | 1 | ||||
-rw-r--r-- | database/systems.js | 26 |
3 files changed, 33 insertions, 0 deletions
diff --git a/database/001_systems.sql b/database/001_systems.sql new file mode 100644 index 0000000..8448955 --- /dev/null +++ b/database/001_systems.sql @@ -0,0 +1,6 @@ +CREATE TABLE systems ( + id INTEGER PRIMARY KEY, + symbol TEXT NOT NULL UNIQUE, + data TEXT NOT NULL, + updated DATE DEFAULT (datetime('now')) +); diff --git a/database/db.js b/database/db.js index db71080..6deddcc 100644 --- a/database/db.js +++ b/database/db.js @@ -3,6 +3,7 @@ import Database from 'better-sqlite3'; const allMigrations = [ 'database/000_init.sql', + 'database/001_systems.sql', ]; const db = new Database( diff --git a/database/systems.js b/database/systems.js new file mode 100644 index 0000000..46b4663 --- /dev/null +++ b/database/systems.js @@ -0,0 +1,26 @@ +import db from './db.js'; + +const getSystemStatement = db.prepare(`SELECT data from systems where symbol = ?;`); +const setSystemStatement = db.prepare(`INSERT INTO systems(symbol, data) VALUES (?, ?);`); + +export function getSystem(symbol) { + try { + const data = getSystemStatement.get(symbol); + if (data === undefined) { + return null; + } + return JSON.parse(data.data); + } catch (err) { + console.log(err); + return null; + } +} + +export function setSystem(symbol, data) { + try { + return setSystemStatement.run(symbol, JSON.stringify(data)).lastInsertRowid; + } catch (err) { + console.log(err); + return null; + } +} |