From 9963ab79b7c7392d5f87c98cd55953ffa004efd9 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 24 May 2023 23:03:19 +0200 Subject: Rewrote the api rate limiter with promises instead of callbacks --- database/001_systems.sql | 6 ++++++ database/db.js | 1 + database/systems.js | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 database/001_systems.sql create mode 100644 database/systems.js (limited to 'database') 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; + } +} -- cgit v1.2.3