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/systems.ts | |
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 '')
-rw-r--r-- | nodejs/database/systems.ts (renamed from nodejs/database/systems.js) | 47 |
1 files changed, 21 insertions, 26 deletions
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; + }); } |