diff options
author | Julien Dessaux | 2024-05-13 23:45:45 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-05-13 23:45:45 +0200 |
commit | de0251bc22d554e2ace4d2d3d061dc1054656bcd (patch) | |
tree | 7e0f00f2ded551a9904a772c5cf6f37a2c06bb61 /nodejs/database | |
parent | [node] improved sql migrations code (diff) | |
download | spacetraders-de0251bc22d554e2ace4d2d3d061dc1054656bcd.tar.gz spacetraders-de0251bc22d554e2ace4d2d3d061dc1054656bcd.tar.bz2 spacetraders-de0251bc22d554e2ace4d2d3d061dc1054656bcd.zip |
[node] Big Contracts lib refactoring
Diffstat (limited to 'nodejs/database')
-rw-r--r-- | nodejs/database/contracts.ts | 26 |
1 files changed, 0 insertions, 26 deletions
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<Contract> { - const data = getContractsStatement.all() as Array<DbData>; - 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)); -} |