diff options
author | Julien Dessaux | 2023-09-02 01:06:11 +0200 |
---|---|---|
committer | Julien Dessaux | 2023-09-02 01:06:11 +0200 |
commit | 9a71163dda2e0857e2fe3d32be18a54e35bc837c (patch) | |
tree | 49fe3370eae2ef86d8673f98a8dfb593c9525219 | |
parent | [javascript] implemented paginated requests (diff) | |
download | spacetraders-9a71163dda2e0857e2fe3d32be18a54e35bc837c.tar.gz spacetraders-9a71163dda2e0857e2fe3d32be18a54e35bc837c.tar.bz2 spacetraders-9a71163dda2e0857e2fe3d32be18a54e35bc837c.zip |
[javascript] began reworking token handling to match what is done with the haskell client
-rw-r--r-- | nodejs/database/000_init.sql | 5 | ||||
-rw-r--r-- | nodejs/database/config.js | 23 | ||||
-rw-r--r-- | nodejs/database/db.js | 45 | ||||
-rw-r--r-- | nodejs/database/tokens.js | 16 | ||||
-rw-r--r-- | nodejs/lib/api.js | 2 |
5 files changed, 50 insertions, 41 deletions
diff --git a/nodejs/database/000_init.sql b/nodejs/database/000_init.sql index ecda95a..c06d2d3 100644 --- a/nodejs/database/000_init.sql +++ b/nodejs/database/000_init.sql @@ -1,8 +1,7 @@ CREATE TABLE schema_version ( version INTEGER NOT NULL ); -CREATE TABLE config ( +CREATE TABLE tokens ( id INTEGER PRIMARY KEY, - key TEXT NOT NULL UNIQUE, - value TEXT NOT NULL + data TEXT NOT NULL ); diff --git a/nodejs/database/config.js b/nodejs/database/config.js deleted file mode 100644 index 7a50f68..0000000 --- a/nodejs/database/config.js +++ /dev/null @@ -1,23 +0,0 @@ -import db from './db.js'; - -const getTokenStatement = db.prepare(`SELECT value->>'token' as token from config where key = 'register_data';`); -const registerAgentStatement = db.prepare(`INSERT INTO config(key, value) VALUES ('register_data', json(?));`); - -export function getToken() { - try { - return getTokenStatement.get().token; - } catch (err) { - console.log(err); - return null; - } -} - -export function registerAgent(data) { - try { - registerAgentStatement.run(JSON.stringify(data)); - return true; - } catch (err) { - console.log(err); - return false; - } -} diff --git a/nodejs/database/db.js b/nodejs/database/db.js index 4855233..78fa0dd 100644 --- a/nodejs/database/db.js +++ b/nodejs/database/db.js @@ -15,19 +15,36 @@ const db = new Database( db.pragma('foreign_keys = ON'); db.pragma('journal_mode = WAL'); -db.transaction(function migrate() { - let version; - try { - version = db.prepare('SELECT version FROM schema_version').get().version; - } catch { - version = 0; - } - if (version === allMigrations.length) return; - while (version < allMigrations.length) { - db.exec(fs.readFileSync(allMigrations[version], 'utf8')); - version++; - } - db.exec(`DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (${version});`); -})(); +function init() { + db.transaction(function migrate() { + let version; + try { + version = db.prepare('SELECT version FROM schema_version').get().version; + } catch { + version = 0; + } + if (version === allMigrations.length) return; + while (version < allMigrations.length) { + db.exec(fs.readFileSync(allMigrations[version], 'utf8')); + version++; + } + db.exec(`DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (${version});`); + })(); +} + +export function reset() { + const indices = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'index';`).all(); + const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table';`).all(); + const triggers = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'trigger';`).all(); + const views = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'view';`).all(); + indices.forEach(elt => db.exec(`DROP INDEX ${elt.name};`)); + tables.forEach(elt => db.exec(`DROP TABLE ${elt.name};`)); + triggers.forEach(elt => db.exec(`DROP TRIGGER ${elt.name};`)); + views.forEach(elt => db.exec(`DROP VIEW ${elt.name};`)); + db.exec(`VACUUM;`); + init(); +} + +init(); export default db; diff --git a/nodejs/database/tokens.js b/nodejs/database/tokens.js new file mode 100644 index 0000000..2a781af --- /dev/null +++ b/nodejs/database/tokens.js @@ -0,0 +1,16 @@ +import db from './db.js'; + +const addTokenStatement = db.prepare(`INSERT INTO tokens(data) VALUES (?);`); +const getTokenStatement = db.prepare(`SELECT data FROM tokens;`); + +export function addToken(token) { + return addTokenStatement.run(token).lastInsertRowid; +} + +export function getToken() { + const data = getTokenStatement.get(); + if (data === undefined) { + return null; + } + return data.data; +} diff --git a/nodejs/lib/api.js b/nodejs/lib/api.js index 3e2bbd0..f9b981a 100644 --- a/nodejs/lib/api.js +++ b/nodejs/lib/api.js @@ -1,7 +1,7 @@ import * as fs from 'fs'; import * as events from 'events'; -import { getToken } from '../database/config.js'; +import { getToken } from '../database/tokens.js'; import { PriorityQueue } from './priority_queue.js'; // queue processor module variables |