From 9a71163dda2e0857e2fe3d32be18a54e35bc837c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 2 Sep 2023 01:06:11 +0200 Subject: [javascript] began reworking token handling to match what is done with the haskell client --- nodejs/database/000_init.sql | 5 ++--- nodejs/database/config.js | 23 ---------------------- nodejs/database/db.js | 45 ++++++++++++++++++++++++++++++-------------- nodejs/database/tokens.js | 16 ++++++++++++++++ 4 files changed, 49 insertions(+), 40 deletions(-) delete mode 100644 nodejs/database/config.js create mode 100644 nodejs/database/tokens.js (limited to 'nodejs/database') 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; +} -- cgit v1.2.3