summaryrefslogtreecommitdiff
path: root/nodejs/database/contracts.js
blob: d7f9aab41ebce3b67942bb815d88cf90ee855ae6 (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
import db from './db.js';

const addContractStatement = db.prepare(`INSERT INTO contracts(data) VALUES (json(?));`);
const getContractStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'id' = ?;`);
const getContractsStatement = db.prepare(`SELECT data FROM contracts WHERE data->>'fulfilled' = false;`);
const updateContractStatement = db.prepare(`UPDATE contracts SET data = json(:data) WHERE data->>'id' = :id;`);

export function getContract(id) {
	const data = getContractStatement.get(id);
	if (data === undefined) {
		return null;
	}
	return JSON.parse(data.data);
}

export function getContracts() {
	const data = getContractsStatement.all();
	return data.map(contractData => JSON.parse(contractData.data));
}

export function setContract(data) {
	if (getContract(data.id) === null) {
		return addContractStatement.run(JSON.stringify(data)).lastInsertRowid;
	} else {
		return updateContractStatement.run({
			data: JSON.stringify(data),
			id: data.id,
		}).changes;
	}
}