1
0
Fork 0

Improved ships lib with ships db caching

This commit is contained in:
Julien Dessaux 2023-06-02 00:00:09 +02:00
parent 68c457964a
commit 7008c91c6f
Signed by: adyxax
GPG key ID: F92E51B86E07177E
10 changed files with 170 additions and 32 deletions

82
database/ships.js Normal file
View file

@ -0,0 +1,82 @@
import db from './db.js';
const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`);
const setShipStatement = db.prepare(`INSERT INTO ships(data, updated) VALUES (json(?), ?);`);
const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date 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), updated = :date 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), updated = :date WHERE data->>'symbol' = :symbol;`);
const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`);
export function getShip(symbol) {
try {
const data = getShipStatement.get(symbol);
if (data === undefined) {
return null;
}
return JSON.parse(data.data);
} catch (err) {
console.log(err);
return null;
}
}
export function setShip(data) {
if (getShip(data.symbol) === null) {
try {
return setShipStatement.run(JSON.stringify(data), new Date().toISOString()).lastInsertRowid;
} catch (err) {
console.log(err);
return null;
}
} else {
try {
return updateShipStatement.run({
data: JSON.stringify(data),
date: new Date().toISOString(),
symbol: data.symbol,
}).changes;
} catch (err) {
console.log(err);
return null;
}
}
}
export function setShipCargo(symbol, cargo) {
try {
setShipCargoStatement.run({
cargo: JSON.stringify(cargo),
date: new Date().toISOString(),
symbol: symbol,
}).changes;
} catch (err) {
console.log(err);
return null;
}
}
export function setShipFuel(symbol, fuel) {
try {
setShipFuelStatement.run({
date: new Date().toISOString(),
fuel: JSON.stringify(fuel),
symbol: symbol,
}).changes;
} catch (err) {
console.log(err);
return null;
}
}
export function setShipNav(symbol, nav) {
try {
setShipNavStatement.run({
date: new Date().toISOString(),
nav: JSON.stringify(nav),
symbol: symbol,
}).changes;
} catch (err) {
console.log(err);
return null;
}
}