diff options
Diffstat (limited to 'database')
-rw-r--r-- | database/000_init.sql | 8 | ||||
-rw-r--r-- | database/001_systems.sql | 6 | ||||
-rw-r--r-- | database/002_ships.sql | 6 | ||||
-rw-r--r-- | database/003_surveys.sql | 6 | ||||
-rw-r--r-- | database/config.js | 23 | ||||
-rw-r--r-- | database/db.js | 33 | ||||
-rw-r--r-- | database/ships.js | 82 | ||||
-rw-r--r-- | database/surveys.js | 19 | ||||
-rw-r--r-- | database/systems.js | 71 |
9 files changed, 0 insertions, 254 deletions
diff --git a/database/000_init.sql b/database/000_init.sql deleted file mode 100644 index ecda95a..0000000 --- a/database/000_init.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE schema_version ( - version INTEGER NOT NULL -); -CREATE TABLE config ( - id INTEGER PRIMARY KEY, - key TEXT NOT NULL UNIQUE, - value TEXT NOT NULL -); diff --git a/database/001_systems.sql b/database/001_systems.sql deleted file mode 100644 index 9ad76b1..0000000 --- a/database/001_systems.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE systems ( - id INTEGER PRIMARY KEY, - data JSON NOT NULL, - updated DATE DEFAULT NULL -); -CREATE UNIQUE INDEX systems_data_symbol on systems (json_extract(data, '$.symbol')); diff --git a/database/002_ships.sql b/database/002_ships.sql deleted file mode 100644 index 629f739..0000000 --- a/database/002_ships.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE ships ( - id INTEGER PRIMARY KEY, - data JSON NOT NULL, - updated DATE DEFAULT NULL -); -CREATE UNIQUE INDEX ships_data_symbol on ships (json_extract(data, '$.symbol')); diff --git a/database/003_surveys.sql b/database/003_surveys.sql deleted file mode 100644 index 0548796..0000000 --- a/database/003_surveys.sql +++ /dev/null @@ -1,6 +0,0 @@ -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/config.js b/database/config.js deleted file mode 100644 index 7a50f68..0000000 --- a/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/database/db.js b/database/db.js deleted file mode 100644 index 4855233..0000000 --- a/database/db.js +++ /dev/null @@ -1,33 +0,0 @@ -import fs from 'fs'; -import Database from 'better-sqlite3'; - -const allMigrations = [ - 'database/000_init.sql', - 'database/001_systems.sql', - 'database/002_ships.sql', - 'database/003_surveys.sql', -]; - -const db = new Database( - process.env.NODE_ENV === 'test' ? 'test.db' : 'spacetraders.db', - process.env.NODE_ENV === 'development' ? { verbose: console.log } : null -); -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});`); -})(); - -export default db; diff --git a/database/ships.js b/database/ships.js deleted file mode 100644 index 0220be6..0000000 --- a/database/ships.js +++ /dev/null @@ -1,82 +0,0 @@ -import db from './db.js'; - -const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`); -const setShipStatement = db.prepare(`INSERT INTO ships(data, updated) VALUES (json(?), ?);`); -const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`); -const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.fuel', json(:fuel)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`); -const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.nav', json(:nav)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`); -const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`); - -export function getShip(symbol) { - try { - const data = getShipStatement.get(symbol); - if (data === undefined) { - return null; - } - return JSON.parse(data.data); - } catch (err) { - console.log(err); - return null; - } -} - -export function setShip(data) { - if (getShip(data.symbol) === null) { - try { - return setShipStatement.run(JSON.stringify(data), new Date().toISOString()).lastInsertRowid; - } catch (err) { - console.log(err); - return null; - } - } else { - try { - return updateShipStatement.run({ - data: JSON.stringify(data), - date: new Date().toISOString(), - symbol: data.symbol, - }).changes; - } catch (err) { - console.log(err); - return null; - } - } -} - -export function setShipCargo(symbol, cargo) { - try { - setShipCargoStatement.run({ - cargo: JSON.stringify(cargo), - date: new Date().toISOString(), - symbol: symbol, - }).changes; - } catch (err) { - console.log(err); - return null; - } -} - -export function setShipFuel(symbol, fuel) { - try { - setShipFuelStatement.run({ - date: new Date().toISOString(), - fuel: JSON.stringify(fuel), - symbol: symbol, - }).changes; - } catch (err) { - console.log(err); - return null; - } -} - -export function setShipNav(symbol, nav) { - try { - setShipNavStatement.run({ - date: new Date().toISOString(), - nav: JSON.stringify(nav), - symbol: symbol, - }).changes; - } catch (err) { - console.log(err); - return null; - } -} diff --git a/database/surveys.js b/database/surveys.js deleted file mode 100644 index 60c85b2..0000000 --- a/database/surveys.js +++ /dev/null @@ -1,19 +0,0 @@ -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)); -} diff --git a/database/systems.js b/database/systems.js deleted file mode 100644 index 0d637df..0000000 --- a/database/systems.js +++ /dev/null @@ -1,71 +0,0 @@ -import db from './db.js'; - -const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE data->>'symbol' = ?;`); -const getSystemUpdatedStatement = db.prepare(`SELECT updated FROM systems WHERE data->>'symbol' = ?;`); -const initStatement = db.prepare(`INSERT INTO config(key, value) VALUES ('systems_initialized', TRUE);`); -const isInitStatement = db.prepare(`SELECT value FROM config WHERE key = 'systems_initialized'`); -const setSystemStatement = db.prepare(`INSERT INTO systems(data) VALUES (json(?));`); -const setSystemWaypointsStatement = db.prepare(`UPDATE systems SET data = (SELECT json_set(data, '$.waypoints', json(:waypoints)) FROM systems WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`); - -export function init() { - try { - return initStatement.run().lastInsertRowid; - } catch (err) { - return null; - } -} - -export function isInit() { - try { - return isInitStatement.get().value === '1'; - } catch (err) { - return false; - } -} - -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 getSystemUpdated(symbol) { - try { - const updated = getSystemUpdatedStatement.get(symbol); - if (updated === undefined) { - return null; - } - return updated.updated; - } catch (err) { - console.log(err); - return null; - } -} - -export function setSystem(data) { - try { - return setSystemStatement.run(JSON.stringify(data)).lastInsertRowid; - } catch (err) { - return null; - } -} - -export function setSystemWaypoints(symbol, waypoints) { - try { - return setSystemWaypointsStatement.run({ - date: new Date().toISOString(), - symbol: symbol, - waypoints: JSON.stringify(waypoints), - }).changes; - } catch (err) { - console.log(err); - return null; - } -} |