summaryrefslogtreecommitdiff
path: root/nodejs/database/systems.ts
blob: 646222e3009e65e199af77e5e1daae6c77155b68 (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
44
45
46
47
48
49
50
51
import { DbData, db } from './db.ts';
import { System, Waypoint } from '../lib/types.ts';

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: System): void {
	addSystemStatement.run(JSON.stringify(data)).lastInsertRowid;
}

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(): number {
	const data = getSystemsCountStatement.get() as number|undefined;
	if (!data) return 0;
	return data;
}

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: System): void {
	if (getSystem(data.symbol) === null) {
		addSystem(data);
	} else {
		setSystemStatement.run({
			data: JSON.stringify(data),
			date: new Date().toISOString(),
			symbol: data.symbol,
		});
	}
}

export function setSystemWaypoints(symbol: string, waypoints: Array<Waypoint>): void {
	setSystemWaypointsStatement.run({
		date: new Date().toISOString(),
		symbol: symbol,
		waypoints: JSON.stringify(waypoints),
	});
}