tables.ts
This file
Path
packages/project/src/project-store/tables.ts
187 lines · 5.1 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–187why, one step back1import { ProjectEvent } from "@repo/protocol/domain/event";2import { Array, DateTime, Effect, Option, Schema } from "effect";34import type { ProjectWrites } from "../project-store.ts";5import {6 AnswerRow,7 AskRow,8 AttemptRow,9 BudgetDayRow,10 ClaimRow,11 DecisionRow,12 EnvValueRow,13 LandingRow,14 LayerReadRow,15 LayerRow,16 LedgerRow,17 OutboxRow,18 ProjectRow,19 ProposalRow,20 PushEventRow,21 RatchetRow,22} from "./rows.ts";2324export type Column = string | number;2526export interface Table<A> {27 readonly columns: (row: A) => Readonly<Record<string, Column>>;28 readonly decode: (body: string) => Effect.Effect<A>;29 readonly encode: (row: A) => Effect.Effect<string>;30 readonly key: readonly string[];31 readonly name: string;32}3334// A body that won't encode or decode is a broken invariant, not an outcome a35// caller can handle: it dies, and a commit that dies writes nothing.36const tableOf = <A, I>(37 name: string,38 schema: Schema.Codec<A, I>,39 key: readonly string[],40 columns: (row: A) => Readonly<Record<string, Column>>41): Table<A> => {42 const body = Schema.fromJsonString(schema);43 const encode = Schema.encodeEffect(body);44 const decode = Schema.decodeEffect(body);4546 return {47 columns,48 decode: (text) => Effect.orDie(decode(text)),49 encode: (row) => Effect.orDie(encode(row)),50 key,51 name,52 };53};5455export const Tables = {56 answers: tableOf("answers", AnswerRow, ["request_id"], (row) => ({57 request_id: row.requestId,58 })),59 asks: tableOf("asks", AskRow, ["id"], (row) => ({ id: row.id })),60 attempts: tableOf(61 "attempts",62 AttemptRow,63 ["ask", "claimant", "area", "week"],64 (row) => ({65 area: row.area,66 ask: row.ask,67 claimant: row.claimant,68 week: row.week,69 })70 ),71 budgetDays: tableOf(72 "budget_days",73 BudgetDayRow,74 ["decider", "day"],75 (row) => ({ day: row.day, decider: row.decider })76 ),77 claims: tableOf("claims", ClaimRow, ["ask", "claimant"], (row) => ({78 ask: row.ask,79 claimant: row.claimant,80 })),81 decisions: tableOf("decisions", DecisionRow, ["id"], (row) => ({82 id: row.id,83 })),84 envValues: tableOf("env_values", EnvValueRow, ["key"], (row) => ({85 key: row.key,86 })),87 landings: tableOf("landings", LandingRow, ["sha"], (row) => ({88 landed_at: DateTime.toEpochMillis(row.landedAt),89 sha: row.sha,90 })),91 layers: tableOf("layers", LayerRow, ["name"], (row) => ({ name: row.name })),92 ledger: tableOf("ledger", LedgerRow, ["seq"], (row) => ({93 at: DateTime.toEpochMillis(row.at),94 seq: row.seq,95 })),96 outbox: tableOf("outbox", OutboxRow, ["id"], (row) => ({ id: row.id })),97 proposals: tableOf("proposals", ProposalRow, ["id"], (row) => ({98 id: row.id,99 })),100 pushEvents: tableOf("push_events", PushEventRow, ["event_id"], (row) => ({101 event_id: row.eventId,102 })),103 ratchets: tableOf(104 "ratchets",105 RatchetRow,106 ["ratchet", "territory"],107 (row) => ({ ratchet: row.ratchet, territory: row.territory })108 ),109} as const;110111const single = { id: 1 } as const;112113export const ProjectTable = tableOf(114 "project",115 ProjectRow,116 ["id"],117 () => single118);119120export const QueueTable = tableOf(121 "landing_queue",122 Schema.Json,123 ["id"],124 () => single125);126127export const EventTable = tableOf("events", ProjectEvent, ["seq"], (event) => ({128 seq: event.seq,129}));130131export const LayerReadTable = tableOf(132 "layer_reads",133 LayerReadRow,134 ["seq"],135 (read) => ({ at: DateTime.toEpochMillis(read.at), seq: read.seq })136);137138export type Upsert<E, R> = <A>(139 table: Table<A>,140 rows: readonly A[]141) => Effect.Effect<void, E, R>;142143export interface LastNumbers {144 readonly read: number;145 readonly seq: number;146}147148export const writeAll = <E, R>(149 writes: ProjectWrites,150 last: LastNumbers,151 upsert: Upsert<E, R>152): Effect.Effect<readonly number[], E, R> =>153 Effect.gen(function* () {154 const events = Array.map(writes.events ?? [], (event, index) => ({155 ...event,156 seq: last.seq + index + 1,157 }));158159 const reads = Array.map(writes.layerReads ?? [], (read, index) => ({160 ...read,161 seq: last.read + index + 1,162 }));163164 yield* upsert(165 ProjectTable,166 Option.toArray(Option.fromUndefinedOr(writes.project))167 );168 yield* upsert(Tables.asks, writes.asks ?? []);169 yield* upsert(Tables.claims, writes.claims ?? []);170 yield* upsert(Tables.answers, writes.answers ?? []);171 yield* upsert(QueueTable, writes.queue === undefined ? [] : [writes.queue]);172 yield* upsert(Tables.landings, writes.landings ?? []);173 yield* upsert(Tables.proposals, writes.proposals ?? []);174 yield* upsert(Tables.decisions, writes.decisions ?? []);175 yield* upsert(Tables.budgetDays, writes.budgetDays ?? []);176 yield* upsert(Tables.ratchets, writes.ratchets ?? []);177 yield* upsert(Tables.pushEvents, writes.pushEvents ?? []);178 yield* upsert(EventTable, events);179 yield* upsert(Tables.outbox, writes.outbox ?? []);180 yield* upsert(Tables.layers, writes.layers ?? []);181 yield* upsert(Tables.attempts, writes.attempts ?? []);182 yield* upsert(Tables.ledger, writes.ledger ?? []);183 yield* upsert(Tables.envValues, writes.envValues ?? []);184 yield* upsert(LayerReadTable, reads);185186 return Array.map(events, (event) => event.seq);187 });