From 2ae1d4e8fd7b8d295b12d73d55f25e709bbce355 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 8 Sep 2023 22:34:22 +0200 Subject: [javascript] add database function to fetch all contracts --- nodejs/database/contracts.js | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'nodejs/database') diff --git a/nodejs/database/contracts.js b/nodejs/database/contracts.js index ac583de..d7f9aab 100644 --- a/nodejs/database/contracts.js +++ b/nodejs/database/contracts.js @@ -2,6 +2,7 @@ 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) { @@ -12,6 +13,11 @@ export function getContract(id) { 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; -- cgit v1.2.3