From de0251bc22d554e2ace4d2d3d061dc1054656bcd Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 13 May 2024 23:45:45 +0200 Subject: [node] Big Contracts lib refactoring --- nodejs/database/contracts.ts | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 nodejs/database/contracts.ts (limited to 'nodejs/database') diff --git a/nodejs/database/contracts.ts b/nodejs/database/contracts.ts deleted file mode 100644 index 9adb4c8..0000000 --- a/nodejs/database/contracts.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Contract } from '../lib/types.ts'; -import { DbData, db } from './db.ts'; - -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: string): Contract { - const data = getContractStatement.get(id) as DbData|undefined; - if (!data) throw `invalid id ${id} in getContract database call`; - return JSON.parse(data.data); -} - -export function getContracts(): Array { - const data = getContractsStatement.all() as Array; - return data.map(contractData => JSON.parse(contractData.data)); -} - -export function setContract(data: Contract): void { - const changes = updateContractStatement.run({ - data: JSON.stringify(data), - id: data.id, - }).changes; - if (changes === 0) addContractStatement.run(JSON.stringify(data)); -} -- cgit v1.2.3