summaryrefslogtreecommitdiff
path: root/nodejs/database/systems.js
blob: dd70d330e285267c277afc969be90a4d383a089b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 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 setSystemWaypoints(symbol, waypoints) {
	return setSystemWaypointsStatement.run({
		date: new Date().toISOString(),
		symbol: symbol,
		waypoints: JSON.stringify(waypoints),
	}).changes;
}