diff options
author | Julien Dessaux | 2024-03-27 15:20:14 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-27 15:21:37 +0100 |
commit | a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9 (patch) | |
tree | 1566c60bf1155e62821d9561ba1cc998b04b8ea5 /nodejs/database | |
parent | [all] fixed erroneous contracts index (diff) | |
download | spacetraders-a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9.tar.gz spacetraders-a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9.tar.bz2 spacetraders-a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9.zip |
[node] finished the great typescript rewrite
Diffstat (limited to 'nodejs/database')
-rw-r--r-- | nodejs/database/agents.ts | 8 | ||||
-rw-r--r-- | nodejs/database/contracts.ts | 8 | ||||
-rw-r--r-- | nodejs/database/db.ts | 21 | ||||
-rw-r--r-- | nodejs/database/markets.js | 34 | ||||
-rw-r--r-- | nodejs/database/markets.ts | 26 | ||||
-rw-r--r-- | nodejs/database/ships.ts | 16 | ||||
-rw-r--r-- | nodejs/database/surveys.js | 20 | ||||
-rw-r--r-- | nodejs/database/surveys.ts | 20 | ||||
-rw-r--r-- | nodejs/database/systems.ts (renamed from nodejs/database/systems.js) | 47 | ||||
-rw-r--r-- | nodejs/database/tokens.ts | 6 |
10 files changed, 97 insertions, 109 deletions
diff --git a/nodejs/database/agents.ts b/nodejs/database/agents.ts index 41b956a..3fc4182 100644 --- a/nodejs/database/agents.ts +++ b/nodejs/database/agents.ts @@ -1,20 +1,20 @@ import { Agent } from '../model/agent.ts'; -import db from './db.js'; +import { DbData, db } from './db.ts'; 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) { +export function addAgent(agent: Agent): void { addAgentStatement.run(JSON.stringify(agent)); } export function getAgent(): Agent|null { - const data = getAgentStatement.get() as {data: string}|undefined; + const data = getAgentStatement.get() as DbData|undefined; if (!data) return null; return JSON.parse(data.data); } -export function setAgent(agent: Agent) { +export function setAgent(agent: Agent): void { setAgentStatement.run(JSON.stringify(agent)); } diff --git a/nodejs/database/contracts.ts b/nodejs/database/contracts.ts index 576f8dd..ff24524 100644 --- a/nodejs/database/contracts.ts +++ b/nodejs/database/contracts.ts @@ -1,5 +1,5 @@ import { Contract } from '../model/contract.ts'; -import db from './db.ts'; +import { DbData, 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' = ?;`); @@ -7,17 +7,17 @@ const getContractsStatement = db.prepare(`SELECT data FROM contracts WHERE data- 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; + const data = getContractStatement.get(id) as DbData|undefined; if (!data) return null; return JSON.parse(data.data); } export function getContracts(): Array<Contract> { - const data = getContractsStatement.all() as Array<{data: string}>; + const data = getContractsStatement.all() as Array<DbData>; return data.map(contractData => JSON.parse(contractData.data)); } -export function setContract(data: Contract) { +export function setContract(data: Contract): void { if (getContract(data.id) === null) { addContractStatement.run(JSON.stringify(data)); } else { diff --git a/nodejs/database/db.ts b/nodejs/database/db.ts index 247ee5a..115d6a3 100644 --- a/nodejs/database/db.ts +++ b/nodejs/database/db.ts @@ -1,22 +1,23 @@ import fs from 'fs'; +import path from 'path'; 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', -]; +let allMigrations: Array<string> = []; +fs.readdir('./database/', function(err, files) { + if (err) throw err; + allMigrations = files.filter(e => e.match(/\.sql$/)).map(e => path.join('./database', e)); +}); -const db = new Database( +export type DbData = {data: string}; + +export 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() { +function init(): void { db.transaction(function migrate() { let version; try { @@ -34,7 +35,7 @@ function init() { })(); } -export function reset() { +export function reset(): void { 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}>; diff --git a/nodejs/database/markets.js b/nodejs/database/markets.js deleted file mode 100644 index 522f57f..0000000 --- a/nodejs/database/markets.js +++ /dev/null @@ -1,34 +0,0 @@ -import db from './db.js'; -import * as utils from '../lib/utils.js'; - -const addMarketStatement = db.prepare(`INSERT INTO markets(system, data) VALUES (?, json(?));`); -const getMarketAtWaypointStatement = db.prepare(`SELECT data FROM markets WHERE data->>'symbol' = ?;`); -const getMarketsInSystemStatement = db.prepare(`SELECT data FROM markets WHERE system = ?;`); -const updateMarketStatement = db.prepare(`UPDATE markets SET data = json(:data) WHERE data->>'symbol' = :symbol;`); - -export function getMarketAtWaypoint(symbol) { - const data = getMarketAtWaypointStatement.get(symbol); - if (data === undefined) { - return null; - } - return JSON.parse(data.data); -} - -export function getMarketsInSystem(symbol) { - const data = getMarketsInSystemStatement.get(symbol); - if (data === undefined) { - return null; - } - return JSON.parse(data.data); -} - -export function setMarket(data) { - if (getMarketAtWaypoint(data.symbol) === null) { - const system = utils.systemFromWaypoint(data.symbol); - return addMarketStatement.run(system, JSON.stringify(data)).lastInsertRowid; - } - return updateMarketStatement.run({ - data: JSON.stringify(data), - symbol: data.symbol, - }).changes; -} diff --git a/nodejs/database/markets.ts b/nodejs/database/markets.ts new file mode 100644 index 0000000..7ae5ca2 --- /dev/null +++ b/nodejs/database/markets.ts @@ -0,0 +1,26 @@ +import { DbData, db } from './db.ts'; +import { Market } from '../model/market.ts'; +import { systemFromWaypoint } from '../lib/utils.ts'; + +const addMarketStatement = db.prepare(`INSERT INTO markets(system, data) VALUES (?, json(?));`); +const getMarketAtWaypointStatement = db.prepare(`SELECT data FROM markets WHERE data->>'symbol' = ?;`); +//const getMarketsInSystemStatement = db.prepare(`SELECT data FROM markets WHERE system = ?;`); +const updateMarketStatement = db.prepare(`UPDATE markets SET data = json(:data) WHERE data->>'symbol' = :symbol;`); + +export function getMarketAtWaypoint(symbol: string): Market|null { + const data = getMarketAtWaypointStatement.get(symbol) as DbData; + if (!data) return null; + return JSON.parse(data.data); +} + +export function setMarket(data: Market): void { + if (getMarketAtWaypoint(data.symbol) === null) { + const system = systemFromWaypoint(data.symbol); + addMarketStatement.run(system, JSON.stringify(data)); + } else { + updateMarketStatement.run({ + data: JSON.stringify(data), + symbol: data.symbol, + }); + } +} diff --git a/nodejs/database/ships.ts b/nodejs/database/ships.ts index 58c8abf..414a059 100644 --- a/nodejs/database/ships.ts +++ b/nodejs/database/ships.ts @@ -1,4 +1,4 @@ -import db from './db.ts'; +import { DbData, db } from './db.ts'; import { Cargo } from '../model/cargo.ts'; import { Fuel, Nav, Ship } from '../model/ship.ts'; @@ -11,18 +11,18 @@ const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set 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; + const data = getShipStatement.get(symbol) as DbData|undefined; if (!data) return null; return JSON.parse(data.data); } -export function getShipsAt(symbol: string) { - const data = getShipsAtStatement.all(symbol) as Array<{data: string}>; +export function getShipsAt(symbol: string): Array<Ship> { + const data = getShipsAtStatement.all(symbol) as Array<DbData>; return data.map(elt => JSON.parse(elt.data)); } -export function setShip(data: Ship) { +export function setShip(data: Ship): void { if (getShip(data.symbol) === null) { addShipStatement.run(JSON.stringify(data)); } else { @@ -33,21 +33,21 @@ export function setShip(data: Ship) { } } -export function setShipCargo(symbol: string, cargo: Cargo) { +export function setShipCargo(symbol: string, cargo: Cargo): void { setShipCargoStatement.run({ cargo: JSON.stringify(cargo), symbol: symbol, }); } -export function setShipFuel(symbol: string, fuel: Fuel) { +export function setShipFuel(symbol: string, fuel: Fuel): void { setShipFuelStatement.run({ fuel: JSON.stringify(fuel), symbol: symbol, }); } -export function setShipNav(symbol: string, nav: Nav) { +export function setShipNav(symbol: string, nav: Nav): void { setShipNavStatement.run({ nav: JSON.stringify(nav), symbol: symbol, diff --git a/nodejs/database/surveys.js b/nodejs/database/surveys.js deleted file mode 100644 index 3397374..0000000 --- a/nodejs/database/surveys.js +++ /dev/null @@ -1,20 +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(); - const data = getSurveysStatement.all(symbol); - return data.map(elt => JSON.parse(elt.data)); -} - -export function set(survey) { - deleteExpired(); - return setSurveysStatement.run(JSON.stringify(survey)); -} diff --git a/nodejs/database/surveys.ts b/nodejs/database/surveys.ts new file mode 100644 index 0000000..6492805 --- /dev/null +++ b/nodejs/database/surveys.ts @@ -0,0 +1,20 @@ +import { DbData, db } from './db.ts'; + +//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(); +// const data = getSurveysStatement.all(symbol); +// return data.map(elt => JSON.parse(elt.data)); +//} +// +//export function set(survey) { +// deleteExpired(); +// return setSurveysStatement.run(JSON.stringify(survey)); +//} diff --git a/nodejs/database/systems.js b/nodejs/database/systems.ts index f989218..d6d6cf1 100644 --- a/nodejs/database/systems.js +++ b/nodejs/database/systems.ts @@ -1,4 +1,5 @@ -import db from './db.js'; +import { DbData, db } from './db.ts'; +import { System, Waypoint } from '../model/system.ts'; const addSystemStatement = db.prepare(`INSERT INTO systems(data) VALUES (json(?));`); const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE data->>'symbol' = ?;`); @@ -7,50 +8,44 @@ const getSystemsCountStatement = db.prepare(`SELECT COUNT(data) as data FROM sys const setSystemStatement = db.prepare(`UPDATE systems SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`); 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 addSystem(data) { - return addSystemStatement.run(JSON.stringify(data)).lastInsertRowid; +export function addSystem(data: System): void { + addSystemStatement.run(JSON.stringify(data)).lastInsertRowid; } -export function getSystem(symbol) { - const data = getSystemStatement.get(symbol); - if (data === undefined) { - return null; - } +export function getSystem(symbol: string): System|null { + const data = getSystemStatement.get(symbol) as DbData|undefined; + if (!data) return null; return JSON.parse(data.data); } -export function getSystemsCount() { - const data = getSystemsCountStatement.get(); - if (data === undefined) { - return null; - } - return data.data; +export function getSystemsCount(): number { + const data = getSystemsCountStatement.get() as number|undefined; + if (!data) return 0; + return data; } -export function getSystemUpdated(symbol) { - const updated = getSystemUpdatedStatement.get(symbol); - if (updated === undefined) { - return null; - } - return updated.updated; +export function getSystemUpdated(symbol: string): Date|null { + const data = getSystemUpdatedStatement.get(symbol) as {updated: Date}|undefined; + if (!data) return null; + return data.updated; } -export function setSystem(data) { +export function setSystem(data: System): void { if (getSystem(data.symbol) === null) { addSystem(data); } else { - return setSystemStatement.run({ + setSystemStatement.run({ data: JSON.stringify(data), date: new Date().toISOString(), symbol: data.symbol, - }).changes; + }); } } -export function setSystemWaypoints(symbol, waypoints) { - return setSystemWaypointsStatement.run({ +export function setSystemWaypoints(symbol: string, waypoints: Array<Waypoint>): void { + setSystemWaypointsStatement.run({ date: new Date().toISOString(), symbol: symbol, waypoints: JSON.stringify(waypoints), - }).changes; + }); } diff --git a/nodejs/database/tokens.ts b/nodejs/database/tokens.ts index 4495a65..4d67ded 100644 --- a/nodejs/database/tokens.ts +++ b/nodejs/database/tokens.ts @@ -1,14 +1,14 @@ -import db from './db.ts'; +import { DbData, 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) { +export function addToken(token: string): void { addTokenStatement.run(token); } export function getToken(): string|null { - const data = getTokenStatement.get() as {data: string}|undefined; + const data = getTokenStatement.get() as DbData|undefined; if (data === undefined) return null; return data.data; } |