summaryrefslogtreecommitdiff
path: root/nodejs/database/systems.js
blob: 0d637df8ad5846aac584caff46308f5e2efecf6f (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import db from './db.js';

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 getSystem(symbol) {
	try {
		const data = getSystemStatement.get(symbol);
		if (data === undefined) {
			return null;
		}
		return JSON.parse(data.data);
	} catch (err) {
		console.log(err);
		return null;
	}
}

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) {
		return null;
	}
}

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;
	}
}