From d668eac4a63a9aa98c3efff395faa23cfcea1c1b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 21 Mar 2024 17:08:37 +0100 Subject: [node] begin the great typescript rewrite --- nodejs/database/agents.js | 21 ---------------- nodejs/database/agents.ts | 20 +++++++++++++++ nodejs/database/contracts.js | 30 ----------------------- nodejs/database/contracts.ts | 29 ++++++++++++++++++++++ nodejs/database/db.js | 51 -------------------------------------- nodejs/database/db.ts | 52 +++++++++++++++++++++++++++++++++++++++ nodejs/database/ships.js | 58 -------------------------------------------- nodejs/database/ships.ts | 55 +++++++++++++++++++++++++++++++++++++++++ nodejs/database/tokens.js | 16 ------------ nodejs/database/tokens.ts | 14 +++++++++++ 10 files changed, 170 insertions(+), 176 deletions(-) delete mode 100644 nodejs/database/agents.js create mode 100644 nodejs/database/agents.ts delete mode 100644 nodejs/database/contracts.js create mode 100644 nodejs/database/contracts.ts delete mode 100644 nodejs/database/db.js create mode 100644 nodejs/database/db.ts delete mode 100644 nodejs/database/ships.js create mode 100644 nodejs/database/ships.ts delete mode 100644 nodejs/database/tokens.js create mode 100644 nodejs/database/tokens.ts (limited to 'nodejs/database') diff --git a/nodejs/database/agents.js b/nodejs/database/agents.js deleted file mode 100644 index 8b7203b..0000000 --- a/nodejs/database/agents.js +++ /dev/null @@ -1,21 +0,0 @@ -import db from './db.js'; - -const addAgentStatement = db.prepare(`INSERT INTO agents(data) VALUES (json(?));`); -const getAgentStatement = db.prepare(`SELECT data FROM agents;`); -const setAgentStatement = db.prepare(`UPDATE agents SET data = json(?);`); - -export function addAgent(agent) { - return addAgentStatement.run(JSON.stringify(agent)).lastInsertRowid; -} - -export function getAgent() { - const data = getAgentStatement.get(); - if (data === undefined) { - return null; - } - return JSON.parse(data.data); -} - -export function setAgent(agent) { - return setAgentStatement.run(JSON.stringify(agent)).changes; -} diff --git a/nodejs/database/agents.ts b/nodejs/database/agents.ts new file mode 100644 index 0000000..41b956a --- /dev/null +++ b/nodejs/database/agents.ts @@ -0,0 +1,20 @@ +import { Agent } from '../model/agent.ts'; +import db from './db.js'; + +const addAgentStatement = db.prepare(`INSERT INTO agents(data) VALUES (json(?));`); +const getAgentStatement = db.prepare(`SELECT data FROM agents;`); +const setAgentStatement = db.prepare(`UPDATE agents SET data = json(?);`); + +export function addAgent(agent: Agent) { + addAgentStatement.run(JSON.stringify(agent)); +} + +export function getAgent(): Agent|null { + const data = getAgentStatement.get() as {data: string}|undefined; + if (!data) return null; + return JSON.parse(data.data); +} + +export function setAgent(agent: Agent) { + setAgentStatement.run(JSON.stringify(agent)); +} diff --git a/nodejs/database/contracts.js b/nodejs/database/contracts.js deleted file mode 100644 index d7f9aab..0000000 --- a/nodejs/database/contracts.js +++ /dev/null @@ -1,30 +0,0 @@ -import db from './db.js'; - -const addContractStatement = db.prepare(`INSERT INTO contracts(data) VALUES (json(?));`); -const getContractStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'id' = ?;`); -const getContractsStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'fulfilled' = false;`); -const updateContractStatement = db.prepare(`UPDATE contracts SET data = json(:data) WHERE data->>'id' = :id;`); - -export function getContract(id) { - const data = getContractStatement.get(id); - if (data === undefined) { - return null; - } - return JSON.parse(data.data); -} - -export function getContracts() { - const data = getContractsStatement.all(); - return data.map(contractData => JSON.parse(contractData.data)); -} - -export function setContract(data) { - if (getContract(data.id) === null) { - return addContractStatement.run(JSON.stringify(data)).lastInsertRowid; - } else { - return updateContractStatement.run({ - data: JSON.stringify(data), - id: data.id, - }).changes; - } -} diff --git a/nodejs/database/contracts.ts b/nodejs/database/contracts.ts new file mode 100644 index 0000000..576f8dd --- /dev/null +++ b/nodejs/database/contracts.ts @@ -0,0 +1,29 @@ +import { Contract } from '../model/contract.ts'; +import db from './db.ts'; + +const addContractStatement = db.prepare(`INSERT INTO contracts(data) VALUES (json(?));`); +const getContractStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'id' = ?;`); +const getContractsStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'fulfilled' = false;`); +const updateContractStatement = db.prepare(`UPDATE contracts SET data = json(:data) WHERE data->>'id' = :id;`); + +export function getContract(id: string): Contract|null { + const data = getContractStatement.get(id) as {data: string}|undefined; + if (!data) return null; + return JSON.parse(data.data); +} + +export function getContracts(): Array { + const data = getContractsStatement.all() as Array<{data: string}>; + return data.map(contractData => JSON.parse(contractData.data)); +} + +export function setContract(data: Contract) { + if (getContract(data.id) === null) { + addContractStatement.run(JSON.stringify(data)); + } else { + updateContractStatement.run({ + data: JSON.stringify(data), + id: data.id, + }); + } +} diff --git a/nodejs/database/db.js b/nodejs/database/db.js deleted file mode 100644 index 4438fec..0000000 --- a/nodejs/database/db.js +++ /dev/null @@ -1,51 +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', - 'database/004_markets.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'); - -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/db.ts b/nodejs/database/db.ts new file mode 100644 index 0000000..247ee5a --- /dev/null +++ b/nodejs/database/db.ts @@ -0,0 +1,52 @@ +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', + 'database/004_markets.sql', +]; + +const db = new Database( + process.env.NODE_ENV === 'test' ? 'test.db' : 'spacetraders.db', + process.env.NODE_ENV === 'development' ? { verbose: console.log } : undefined +); +db.pragma('foreign_keys = ON'); +db.pragma('journal_mode = WAL'); + +function init() { + db.transaction(function migrate() { + let version; + try { + const res = db.prepare('SELECT version FROM schema_version').get() as {version: number}; + version = res.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() as Array<{name: string}>; + const tables = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table';`).all() as Array<{name: string}>; + const triggers = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'trigger';`).all() as Array<{name: string}>; + const views = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'view';`).all() as Array<{name: string}>; + 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/ships.js b/nodejs/database/ships.js deleted file mode 100644 index f9eb668..0000000 --- a/nodejs/database/ships.js +++ /dev/null @@ -1,58 +0,0 @@ -import db from './db.js'; - -const addShipStatement = db.prepare(`INSERT INTO ships(data) VALUES (json(?));`); -const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`); -const getShipsAtStatement = db.prepare(`SELECT data FROM ships WHERE data->>'$.nav.systemSymbol' = ?;`); -const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol) 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) 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) WHERE data->>'symbol' = :symbol;`); -const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data) WHERE data->>'symbol' = :symbol;`); - -export function getShip(symbol) { - const data = getShipStatement.get(symbol); - if (data === undefined) { - return null; - } - return JSON.parse(data.data); -} - -export function getShipsAt(symbol) { - const data = getShipsAtStatement.all(symbol); - if (data === undefined) { - return null; - } - return data.map(elt => JSON.parse(elt.data)); -} - - -export function setShip(data) { - if (getShip(data.symbol) === null) { - return addShipStatement.run(JSON.stringify(data)).lastInsertRowid; - } else { - return updateShipStatement.run({ - data: JSON.stringify(data), - symbol: data.symbol, - }).changes; - } -} - -export function setShipCargo(symbol, cargo) { - return setShipCargoStatement.run({ - cargo: JSON.stringify(cargo), - symbol: symbol, - }).changes; -} - -export function setShipFuel(symbol, fuel) { - return setShipFuelStatement.run({ - fuel: JSON.stringify(fuel), - symbol: symbol, - }).changes; -} - -export function setShipNav(symbol, nav) { - return setShipNavStatement.run({ - nav: JSON.stringify(nav), - symbol: symbol, - }).changes; -} diff --git a/nodejs/database/ships.ts b/nodejs/database/ships.ts new file mode 100644 index 0000000..58c8abf --- /dev/null +++ b/nodejs/database/ships.ts @@ -0,0 +1,55 @@ +import db from './db.ts'; +import { Cargo } from '../model/cargo.ts'; +import { Fuel, Nav, Ship } from '../model/ship.ts'; + +const addShipStatement = db.prepare(`INSERT INTO ships(data) VALUES (json(?));`); +const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`); +const getShipsAtStatement = db.prepare(`SELECT data FROM ships WHERE data->>'$.nav.systemSymbol' = ?;`); +const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol) 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) 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) WHERE data->>'symbol' = :symbol;`); +const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data) WHERE data->>'symbol' = :symbol;`); + +export function getShip(symbol: string): Ship|null { + const data = getShipStatement.get(symbol) as {data: string}|undefined; + if (!data) return null; + return JSON.parse(data.data); +} + +export function getShipsAt(symbol: string) { + const data = getShipsAtStatement.all(symbol) as Array<{data: string}>; + return data.map(elt => JSON.parse(elt.data)); +} + + +export function setShip(data: Ship) { + if (getShip(data.symbol) === null) { + addShipStatement.run(JSON.stringify(data)); + } else { + updateShipStatement.run({ + data: JSON.stringify(data), + symbol: data.symbol, + }); + } +} + +export function setShipCargo(symbol: string, cargo: Cargo) { + setShipCargoStatement.run({ + cargo: JSON.stringify(cargo), + symbol: symbol, + }); +} + +export function setShipFuel(symbol: string, fuel: Fuel) { + setShipFuelStatement.run({ + fuel: JSON.stringify(fuel), + symbol: symbol, + }); +} + +export function setShipNav(symbol: string, nav: Nav) { + setShipNavStatement.run({ + nav: JSON.stringify(nav), + symbol: symbol, + }); +} diff --git a/nodejs/database/tokens.js b/nodejs/database/tokens.js deleted file mode 100644 index 2a781af..0000000 --- a/nodejs/database/tokens.js +++ /dev/null @@ -1,16 +0,0 @@ -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/database/tokens.ts b/nodejs/database/tokens.ts new file mode 100644 index 0000000..4495a65 --- /dev/null +++ b/nodejs/database/tokens.ts @@ -0,0 +1,14 @@ +import db from './db.ts'; + +const addTokenStatement = db.prepare(`INSERT INTO tokens(data) VALUES (?);`); +const getTokenStatement = db.prepare(`SELECT data FROM tokens;`); + +export function addToken(token: string) { + addTokenStatement.run(token); +} + +export function getToken(): string|null { + const data = getTokenStatement.get() as {data: string}|undefined; + if (data === undefined) return null; + return data.data; +} -- cgit v1.2.3