1
0
Fork 0

[javascript] refactored ships and systems database interface

This commit is contained in:
Julien Dessaux 2023-09-03 23:01:18 +02:00
parent e03bb90611
commit f89af1145d
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 40 additions and 112 deletions

View file

@ -1,71 +1,34 @@
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 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 addSystem(data) {
return addSystemStatement.run(JSON.stringify(data)).lastInsertRowid;
}
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);
const data = getSystemStatement.get(symbol);
if (data === undefined) {
return null;
}
return JSON.parse(data.data);
}
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) {
const updated = getSystemUpdatedStatement.get(symbol);
if (updated === undefined) {
return null;
}
return updated.updated;
}
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;
}
return setSystemWaypointsStatement.run({
date: new Date().toISOString(),
symbol: symbol,
waypoints: JSON.stringify(waypoints),
}).changes;
}