The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { type LedgerRow, LedgerStore } from "@bank/domain/Ledger/LedgerStore"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import { LedgerDatabase } from "./LedgerDatabase.ts"; |
| 6 | |
| 7 | /** The ledger in D1. */ |
| 8 | export const D1LedgerStore = Layer.effect( |
| 9 | LedgerStore, |
| 10 | Effect.gen(function* () { |
| 11 | const db = yield* Cloudflare.D1.QueryDatabase(yield* LedgerDatabase); |
| 12 | return { |
| 13 | record: (rows) => |
| 14 | rows.length === 0 |
| 15 | ? Effect.void |
| 16 | : db |
| 17 | .batch( |
| 18 | rows.map((row) => |
| 19 | db |
| 20 | .prepare( |
| 21 | "INSERT INTO ledger (id, account_id, kind, amount, balance_after, at) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO NOTHING", |
| 22 | ) |
| 23 | .bind(row.id, row.accountId, row.kind, row.amount, row.balanceAfter, row.at), |
| 24 | ), |
| 25 | ) |
| 26 | .pipe(Effect.asVoid), |
| 27 | forAccount: (accountId) => |
| 28 | db |
| 29 | .prepare( |
| 30 | "SELECT id, account_id AS accountId, kind, amount, balance_after AS balanceAfter, at FROM ledger WHERE account_id = ? ORDER BY at, id", |
| 31 | ) |
| 32 | .bind(accountId) |
| 33 | .all<LedgerRow>() |
| 34 | .pipe(Effect.map((result) => result.results)), |
| 35 | }; |
| 36 | }), |
| 37 | ); |