1
0
Fork 0

[node] finished the great typescript rewrite

This commit is contained in:
Julien Dessaux 2024-03-27 15:20:14 +01:00
parent 8107afbd90
commit a1d6b03ec9
Signed by: adyxax
GPG key ID: F92E51B86E07177E
30 changed files with 1019 additions and 359 deletions
nodejs/database

View file

@ -1,56 +0,0 @@
import db from './db.js';
const addSystemStatement = db.prepare(`INSERT INTO systems(data) VALUES (json(?));`);
const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE data->>'symbol' = ?;`);
const getSystemUpdatedStatement = db.prepare(`SELECT updated FROM systems WHERE data->>'symbol' = ?;`);
const getSystemsCountStatement = db.prepare(`SELECT COUNT(data) as data FROM systems;`);
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 getSystem(symbol) {
const data = getSystemStatement.get(symbol);
if (data === undefined) {
return null;
}
return JSON.parse(data.data);
}
export function getSystemsCount() {
const data = getSystemsCountStatement.get();
if (data === undefined) {
return null;
}
return data.data;
}
export function getSystemUpdated(symbol) {
const updated = getSystemUpdatedStatement.get(symbol);
if (updated === undefined) {
return null;
}
return updated.updated;
}
export function setSystem(data) {
if (getSystem(data.symbol) === null) {
addSystem(data);
} else {
return setSystemStatement.run({
data: JSON.stringify(data),
date: new Date().toISOString(),
symbol: data.symbol,
}).changes;
}
}
export function setSystemWaypoints(symbol, waypoints) {
return setSystemWaypointsStatement.run({
date: new Date().toISOString(),
symbol: symbol,
waypoints: JSON.stringify(waypoints),
}).changes;
}