summaryrefslogtreecommitdiff
path: root/nodejs/database/shipyards.ts
blob: 4c243f9870d9fd224a391b6407e405be29743625 (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
import { DbData, db } from './db.ts';
import { Shipyard } from '../lib/types';

const addStatement = db.prepare(`INSERT INTO shipyards(data, updated) VALUES (json(:data), :date);`);
const getStatement = db.prepare(`SELECT data FROM shipyards WHERE data->>'symbol' = ?;`);
const updateStatement = db.prepare(`UPDATE shipyards SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`);

export function get(symbol: string): Shipyard|null {
	const data = getStatement.get(symbol) as DbData|undefined;
	if (!data) return null;
	return JSON.parse(data.data);
}

export function set(data: Shipyard): void {
    if (get(data.symbol) === null) {
		addStatement.run({
			data: JSON.stringify(data),
			date: new Date().toISOString(),
		});
    } else {
		updateStatement.run({
			data: JSON.stringify(data),
			date: new Date().toISOString(),
			symbol: data.symbol,
		});
	}
}