@palbase/backend 10.2.0 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/chunk-7LAXRLPG.js +418 -0
  2. package/dist/chunk-7LAXRLPG.js.map +1 -0
  3. package/dist/{chunk-4WOQWFUP.js → chunk-LUV36KQU.js} +26 -11
  4. package/dist/{chunk-4WOQWFUP.js.map → chunk-LUV36KQU.js.map} +1 -1
  5. package/dist/{chunk-RGQUB66H.js → chunk-XATG7BRC.js} +22 -2
  6. package/dist/chunk-XATG7BRC.js.map +1 -0
  7. package/dist/db/index.cjs +438 -10
  8. package/dist/db/index.cjs.map +1 -1
  9. package/dist/db/index.d.cts +2 -2
  10. package/dist/db/index.d.ts +2 -2
  11. package/dist/db/index.js +13 -1
  12. package/dist/{endpoint-Cn3ICGTf.d.cts → endpoint-Ck4hER_7.d.cts} +397 -11
  13. package/dist/{endpoint-Cn3ICGTf.d.ts → endpoint-Ck4hER_7.d.ts} +397 -11
  14. package/dist/{index-Bt7UHkAM.d.cts → index-BJAf1uPC.d.cts} +64 -34
  15. package/dist/{index-V0ealeY-.d.ts → index-l7DhBDtn.d.ts} +64 -34
  16. package/dist/index.cjs +776 -108
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +203 -9
  19. package/dist/index.d.ts +203 -9
  20. package/dist/index.js +325 -94
  21. package/dist/index.js.map +1 -1
  22. package/dist/purchases/keys.cjs +19 -0
  23. package/dist/purchases/keys.cjs.map +1 -0
  24. package/dist/purchases/keys.d.cts +42 -0
  25. package/dist/purchases/keys.d.ts +42 -0
  26. package/dist/purchases/keys.js +1 -0
  27. package/dist/purchases/keys.js.map +1 -0
  28. package/dist/test/index.cjs +559 -13
  29. package/dist/test/index.cjs.map +1 -1
  30. package/dist/test/index.d.cts +1 -1
  31. package/dist/test/index.d.ts +1 -1
  32. package/dist/test/index.js +166 -13
  33. package/dist/test/index.js.map +1 -1
  34. package/docs/README.md +7 -6
  35. package/docs/database.md +106 -16
  36. package/docs/getting-started.md +14 -12
  37. package/docs/llms-full.txt +140 -45
  38. package/docs/migrations.md +8 -8
  39. package/docs/schema.md +6 -4
  40. package/package.json +11 -1
  41. package/dist/chunk-RGQUB66H.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/test/mock-db.ts","../../src/test/context.ts"],"sourcesContent":["import type { DBClient, TxClient } from \"../endpoint.js\";\n\n/** Tracked records for assertions. */\ninterface TrackedRecords {\n inserted: Map<string, Record<string, unknown>[]>;\n updated: Map<string, Record<string, unknown>[]>;\n deleted: Map<string, string[]>;\n}\n\n/** Mock DB client with tracking and seed data support. */\nexport interface MockDBClient extends DBClient {\n /** Get records inserted into a table. */\n inserted(table: string): Record<string, unknown>[];\n /** Get records updated in a table. */\n updated(table: string): Record<string, unknown>[];\n /** Get IDs deleted from a table. */\n deleted(table: string): string[];\n /** Pre-seed data into a table for findById/findMany. */\n seed(table: string, data: Record<string, unknown>[]): void;\n}\n\n/** Create a mock DB client with in-memory tracking. */\nexport function createMockDB(): MockDBClient {\n const store = new Map<string, Record<string, unknown>[]>();\n const tracked: TrackedRecords = {\n inserted: new Map(),\n updated: new Map(),\n deleted: new Map(),\n };\n\n // Build the op surface first (TxClient = the five DB ops + query). The\n // transaction below reuses this same `ops` object as the tx-scoped client,\n // so writes inside a transaction land in the same in-memory store and\n // tracking maps — exactly the semantics a user endpoint test expects.\n const ops: TxClient = {\n async query(_sql: string, _params?: unknown[]) {\n return [];\n },\n\n async insert(table: string, data: Record<string, unknown>) {\n const record = { id: crypto.randomUUID(), ...data };\n if (!store.has(table)) store.set(table, []);\n store.get(table)!.push(record);\n if (!tracked.inserted.has(table)) tracked.inserted.set(table, []);\n tracked.inserted.get(table)!.push(record);\n return record;\n },\n\n async update(table: string, id: string, data: Record<string, unknown>) {\n const rows = store.get(table) ?? [];\n const idx = rows.findIndex((r) => r[\"id\"] === id);\n const updated = idx >= 0\n ? { ...rows[idx], ...data }\n : { id, ...data };\n if (idx >= 0) {\n rows[idx] = updated;\n }\n if (!tracked.updated.has(table)) tracked.updated.set(table, []);\n tracked.updated.get(table)!.push(updated);\n return updated;\n },\n\n async delete(table: string, id: string) {\n const rows = store.get(table) ?? [];\n const idx = rows.findIndex((r) => r[\"id\"] === id);\n if (idx >= 0) rows.splice(idx, 1);\n if (!tracked.deleted.has(table)) tracked.deleted.set(table, []);\n tracked.deleted.get(table)!.push(id);\n },\n\n async findById(table: string, id: string) {\n const rows = store.get(table) ?? [];\n return rows.find((r) => r[\"id\"] === id) ?? null;\n },\n\n async findMany(table: string, query?: Record<string, unknown>) {\n const rows = store.get(table) ?? [];\n if (!query) return rows;\n return rows.filter((row) =>\n Object.entries(query).every(([key, val]) => row[key] === val),\n );\n },\n };\n\n const client: MockDBClient = {\n ...ops,\n\n transaction<T>(fn: (tx: TxClient) => Promise<T>): Promise<T> {\n return fn(ops);\n },\n\n // In tests there is no real DB role; `asService()` returns the same\n // in-memory client so RLS-bypass code paths still hit the same store and\n // tracking maps. The omitted `asService` matches the contract (no\n // double-bypass), so callers can't recurse.\n asService(): Omit<DBClient, \"asService\"> {\n return client;\n },\n\n inserted(table: string) {\n return tracked.inserted.get(table) ?? [];\n },\n\n updated(table: string) {\n return tracked.updated.get(table) ?? [];\n },\n\n deleted(table: string) {\n return tracked.deleted.get(table) ?? [];\n },\n\n seed(table: string, data: Record<string, unknown>[]) {\n store.set(table, [...data]);\n },\n };\n\n return client;\n}\n","import type {\n CacheClient,\n ClientInfo,\n Logger,\n PBRequest,\n PalbaseModuleClients,\n QueueClient,\n} from \"../endpoint.js\";\nimport type {\n PalbaseAuthClient,\n PalbaseStorageClient,\n PalbaseRealtimeClient,\n PalbaseFunctionsClient,\n PalbaseFlagsClient,\n PalbaseNotificationsClient,\n PalbaseAnalyticsClient,\n PalbaseLinksClient,\n} from \"../clients.js\";\nimport type { User } from \"../types.js\";\nimport { __setRuntime } from \"../runtime.js\";\nimport { createMockDB, type MockDBClient } from \"./mock-db.js\";\n\n/** Options for creating a test context. */\nexport interface TestContextOptions<TInput = unknown> {\n user?: User | null;\n input?: TInput;\n params?: Record<string, string>;\n query?: Record<string, string>;\n headers?: Record<string, string>;\n env?: Record<string, string>;\n db?: { seed?: Record<string, Record<string, unknown>[]> };\n}\n\n/** Log entry captured by the mock logger. */\nexport interface LogEntry {\n level: \"info\" | \"warn\" | \"error\" | \"debug\";\n message: string;\n args: unknown[];\n}\n\n/** Test context for exercising endpoint handlers.\n *\n * Handlers now receive a {@link PBRequest} (no services attached) and reach\n * services via the PascalCase singletons (`Database`, `Log`, …). So\n * `createTestContext` does two things:\n * 1. returns a `PBRequest` (the object you pass to `handler(...)`), and\n * 2. installs mock services into the runtime via `__setRuntime`, so the\n * singletons resolve to the same mocks while the handler runs.\n *\n * For assertions and for building sibling (worker/job/hook/webhook) contexts,\n * the mock service handles are also attached here (`db`, `log`, `cache`,\n * `queue`, `env`, plus the module clients and captured `logs`). The user\n * defaults to nullable in tests (`PBRequest<TInput, false>`) so test code can\n * pass any auth shape without a cast. */\nexport interface TestContext<TInput = unknown>\n extends PBRequest<TInput, false>,\n PalbaseModuleClients {\n db: MockDBClient;\n env: Record<string, string>;\n log: Logger;\n cache: CacheClient;\n queue: QueueClient;\n /** Captured log entries. */\n logs: LogEntry[];\n}\n\n/** Create a mock logger that captures entries. */\nfunction createMockLogger(logs: LogEntry[]): Logger {\n return {\n info(message: string, ...args: unknown[]) {\n logs.push({ level: \"info\", message, args });\n },\n warn(message: string, ...args: unknown[]) {\n logs.push({ level: \"warn\", message, args });\n },\n error(message: string, ...args: unknown[]) {\n logs.push({ level: \"error\", message, args });\n },\n debug(message: string, ...args: unknown[]) {\n logs.push({ level: \"debug\", message, args });\n },\n };\n}\n\n/** Create a mock in-memory cache.\n *\n * Mirrors the runtime's JSON-typed semantics (values are arbitrary JSON, not\n * just strings). getOrSet is single-process here, so it does not need the\n * distributed lock the real runtime uses — it is just get-miss → fn → set.\n * The cross-replica stampede protection is covered by the worker.js tests.\n */\nfunction createMockCache(): CacheClient {\n const store = new Map<string, unknown>();\n\n const get = async <T = unknown>(key: string): Promise<T | null> => {\n return store.has(key) ? (store.get(key) as T) : null;\n };\n const set = async (key: string, value: unknown, _ttl?: number): Promise<void> => {\n store.set(key, value);\n };\n\n return {\n get,\n set,\n async del(key: string) {\n store.delete(key);\n },\n async incr(key: string) {\n const raw = store.get(key);\n const current = typeof raw === \"number\" ? raw : parseInt(String(raw ?? \"0\"), 10);\n const next = current + 1;\n store.set(key, next);\n return next;\n },\n async getOrSet<T>(key: string, ttl: number, fn: () => Promise<T> | T): Promise<T> {\n const hit = await get<T>(key);\n if (hit !== null) {\n return hit;\n }\n const value = await fn();\n await set(key, value, ttl);\n return value;\n },\n };\n}\n\n/** Create mock Palbase module clients (Documents, Storage, …).\n *\n * Every slot throws with a descriptive error so tests that access a module\n * client surface without configuring it fail loudly rather than silently\n * returning undefined. Override individual clients on the returned context for\n * tests that need them.\n */\nfunction createMockModuleClients(): PalbaseModuleClients {\n const notImpl = (label: string): never => {\n throw new Error(\n `${label} not configured in test mock — override the matching client on the returned context`,\n );\n };\n\n const docs: PalbaseModuleClients[\"docs\"] = {\n collection: () => notImpl(\"docs.collection\"),\n doc: () => notImpl(\"docs.doc\"),\n };\n\n const auth: PalbaseAuthClient = {\n verifyUserToken: () => notImpl(\"auth.verifyUserToken\"),\n getSession: () => notImpl(\"auth.getSession\"),\n mfa: {\n enroll: () => notImpl(\"auth.mfa.enroll\"),\n verifyEnrollment: () => notImpl(\"auth.mfa.verifyEnrollment\"),\n challenge: () => notImpl(\"auth.mfa.challenge\"),\n recovery: () => notImpl(\"auth.mfa.recovery\"),\n listFactors: () => notImpl(\"auth.mfa.listFactors\"),\n removeFactor: () => notImpl(\"auth.mfa.removeFactor\"),\n regenerateRecoveryCodes: () => notImpl(\"auth.mfa.regenerateRecoveryCodes\"),\n emailEnroll: () => notImpl(\"auth.mfa.emailEnroll\"),\n emailChallenge: () => notImpl(\"auth.mfa.emailChallenge\"),\n emailVerify: () => notImpl(\"auth.mfa.emailVerify\"),\n },\n device: {\n generateChallenge: () => notImpl(\"auth.device.generateChallenge\"),\n attestAndroid: () => notImpl(\"auth.device.attestAndroid\"),\n attestiOS: () => notImpl(\"auth.device.attestiOS\"),\n bind: () => notImpl(\"auth.device.bind\"),\n list: () => notImpl(\"auth.device.list\"),\n delete: () => notImpl(\"auth.device.delete\"),\n verifyRequestSignature: () => notImpl(\"auth.device.verifyRequestSignature\"),\n getToken: () => notImpl(\"auth.device.getToken\"),\n get isActive(): never {\n return notImpl(\"auth.device.isActive\");\n },\n setCachedToken: () => notImpl(\"auth.device.setCachedToken\"),\n dispose: () => notImpl(\"auth.device.dispose\"),\n },\n };\n\n const storage: PalbaseStorageClient = {\n bucket: () => notImpl(\"storage.bucket\"),\n };\n\n const realtime: PalbaseRealtimeClient = {\n broadcast: async () => ({ data: undefined, error: null }),\n };\n\n const functions: PalbaseFunctionsClient = {\n invoke: () => notImpl(\"functions.invoke\"),\n };\n\n const flags: PalbaseFlagsClient = {\n isEnabled: () => notImpl(\"flags.isEnabled\"),\n getVariant: () => notImpl(\"flags.getVariant\"),\n getAll: () => notImpl(\"flags.getAll\"),\n setOverride: () => notImpl(\"flags.setOverride\"),\n asService: () => ({\n setOverrideForUser: () => notImpl(\"flags.asService.setOverrideForUser\"),\n setOverridesForUser: () => notImpl(\"flags.asService.setOverridesForUser\"),\n clearOverrideForUser: () => notImpl(\"flags.asService.clearOverrideForUser\"),\n clearAllOverridesForUser: () => notImpl(\"flags.asService.clearAllOverridesForUser\"),\n batchSetOverrides: () => notImpl(\"flags.asService.batchSetOverrides\"),\n }),\n };\n\n const notifications: PalbaseNotificationsClient = {\n push: { send: () => notImpl(\"notifications.push.send\") },\n email: { send: () => notImpl(\"notifications.email.send\") },\n sms: { send: () => notImpl(\"notifications.sms.send\") },\n inbox: {\n send: () => notImpl(\"notifications.inbox.send\"),\n list: () => notImpl(\"notifications.inbox.list\"),\n unreadCount: () => notImpl(\"notifications.inbox.unreadCount\"),\n markRead: () => notImpl(\"notifications.inbox.markRead\"),\n markAllRead: () => notImpl(\"notifications.inbox.markAllRead\"),\n archive: () => notImpl(\"notifications.inbox.archive\"),\n },\n preferences: {\n get: () => notImpl(\"notifications.preferences.get\"),\n update: () => notImpl(\"notifications.preferences.update\"),\n },\n templates: {\n email: {\n list: () => notImpl(\"notifications.templates.email.list\"),\n get: () => notImpl(\"notifications.templates.email.get\"),\n create: () => notImpl(\"notifications.templates.email.create\"),\n update: () => notImpl(\"notifications.templates.email.update\"),\n delete: () => notImpl(\"notifications.templates.email.delete\"),\n },\n sms: {\n list: () => notImpl(\"notifications.templates.sms.list\"),\n get: () => notImpl(\"notifications.templates.sms.get\"),\n create: () => notImpl(\"notifications.templates.sms.create\"),\n update: () => notImpl(\"notifications.templates.sms.update\"),\n delete: () => notImpl(\"notifications.templates.sms.delete\"),\n },\n },\n registerDevice: () => notImpl(\"notifications.registerDevice\"),\n unregisterDevice: () => notImpl(\"notifications.unregisterDevice\"),\n };\n\n const analytics: PalbaseAnalyticsClient = {\n capture: () => notImpl(\"analytics.capture\"),\n identify: () => notImpl(\"analytics.identify\"),\n screen: () => notImpl(\"analytics.screen\"),\n query: {\n count: () => notImpl(\"analytics.query.count\"),\n events: () => notImpl(\"analytics.query.events\"),\n properties: () => notImpl(\"analytics.query.properties\"),\n users: () => notImpl(\"analytics.query.users\"),\n funnel: () => notImpl(\"analytics.query.funnel\"),\n retention: () => notImpl(\"analytics.query.retention\"),\n cohort: () => notImpl(\"analytics.query.cohort\"),\n },\n management: {\n overview: () => notImpl(\"analytics.management.overview\"),\n eventNames: () => notImpl(\"analytics.management.eventNames\"),\n userDetail: () => notImpl(\"analytics.management.userDetail\"),\n deleteUser: () => notImpl(\"analytics.management.deleteUser\"),\n },\n };\n\n const links: PalbaseLinksClient = {\n create: () => notImpl(\"links.create\"),\n list: () => notImpl(\"links.list\"),\n get: () => notImpl(\"links.get\"),\n update: () => notImpl(\"links.update\"),\n delete: () => notImpl(\"links.delete\"),\n analytics: () => notImpl(\"links.analytics\"),\n qrCode: () => notImpl(\"links.qrCode\"),\n match: () => notImpl(\"links.match\"),\n };\n\n return {\n auth,\n storage,\n docs,\n realtime,\n functions,\n flags,\n notifications,\n analytics,\n links,\n };\n}\n\n/** Null-by-default calling-client metadata for tests. */\nconst NULL_CLIENT_INFO: ClientInfo = {\n sdkVersion: null,\n appVersion: null,\n platform: null,\n osVersion: null,\n};\n\n/** Create a fully mocked endpoint test context.\n *\n * Returns a `PBRequest` (pass it to `handler(...)`) with the mock service\n * handles attached for assertions, and installs those mocks into the runtime\n * via `__setRuntime` so the `Database`/`Log`/… singletons resolve to them\n * while the handler runs.\n */\nexport function createTestContext<TInput = unknown>(\n options: TestContextOptions<TInput> = {},\n): TestContext<TInput> {\n const logs: LogEntry[] = [];\n const db = createMockDB();\n\n // Seed data if provided\n if (options.db?.seed) {\n for (const [table, data] of Object.entries(options.db.seed)) {\n db.seed(table, data);\n }\n }\n\n const mockQueue: QueueClient = {\n push: async (_worker: string, _payload: unknown) => ({ jobId: \"\" }),\n };\n const log = createMockLogger(logs);\n const cache = createMockCache();\n const moduleClients = createMockModuleClients();\n\n // Install the mocks so the PascalCase singletons (Database, Log, …) resolve\n // to them while the handler under test runs.\n __setRuntime({\n Database: db,\n Documents: moduleClients.docs,\n Storage: moduleClients.storage,\n Cache: cache,\n Queue: mockQueue,\n Log: log,\n Notifications: moduleClients.notifications,\n Flags: moduleClients.flags,\n Realtime: moduleClients.realtime,\n });\n\n const ctx: TestContext<TInput> = {\n input: (options.input ?? {}) as TInput,\n params: options.params ?? {},\n query: options.query ?? {},\n headers: options.headers ?? {},\n user: options.user ?? null,\n client: NULL_CLIENT_INFO,\n method: \"POST\",\n file: null,\n db,\n env: options.env ?? {},\n log,\n cache,\n queue: mockQueue,\n ...moduleClients,\n // Empty errors map in tests by default. Tests that exercise an endpoint's\n // declared errors construct their own throwers; this stub satisfies the\n // PBRequest shape without forcing every test to declare `errors:`.\n errors: {},\n requestId: \"req_test_000000000000\",\n traceId: \"0\".repeat(32),\n spanId: \"0\".repeat(16),\n logs,\n };\n\n return ctx;\n}\n"],"mappings":";;;;;AAsBO,SAAS,eAA6B;AAC3C,QAAM,QAAQ,oBAAI,IAAuC;AACzD,QAAM,UAA0B;AAAA,IAC9B,UAAU,oBAAI,IAAI;AAAA,IAClB,SAAS,oBAAI,IAAI;AAAA,IACjB,SAAS,oBAAI,IAAI;AAAA,EACnB;AAMA,QAAM,MAAgB;AAAA,IACpB,MAAM,MAAM,MAAc,SAAqB;AAC7C,aAAO,CAAC;AAAA,IACV;AAAA,IAEA,MAAM,OAAO,OAAe,MAA+B;AACzD,YAAM,SAAS,EAAE,IAAI,OAAO,WAAW,GAAG,GAAG,KAAK;AAClD,UAAI,CAAC,MAAM,IAAI,KAAK,EAAG,OAAM,IAAI,OAAO,CAAC,CAAC;AAC1C,YAAM,IAAI,KAAK,EAAG,KAAK,MAAM;AAC7B,UAAI,CAAC,QAAQ,SAAS,IAAI,KAAK,EAAG,SAAQ,SAAS,IAAI,OAAO,CAAC,CAAC;AAChE,cAAQ,SAAS,IAAI,KAAK,EAAG,KAAK,MAAM;AACxC,aAAO;AAAA,IACT;AAAA,IAEA,MAAM,OAAO,OAAe,IAAY,MAA+B;AACrE,YAAM,OAAO,MAAM,IAAI,KAAK,KAAK,CAAC;AAClC,YAAM,MAAM,KAAK,UAAU,CAAC,MAAM,EAAE,IAAI,MAAM,EAAE;AAChD,YAAM,UAAU,OAAO,IACnB,EAAE,GAAG,KAAK,GAAG,GAAG,GAAG,KAAK,IACxB,EAAE,IAAI,GAAG,KAAK;AAClB,UAAI,OAAO,GAAG;AACZ,aAAK,GAAG,IAAI;AAAA,MACd;AACA,UAAI,CAAC,QAAQ,QAAQ,IAAI,KAAK,EAAG,SAAQ,QAAQ,IAAI,OAAO,CAAC,CAAC;AAC9D,cAAQ,QAAQ,IAAI,KAAK,E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wB;AAI9C,eAAa;AAAA,IACX,UAAU;AAAA,IACV,WAAW,cAAc;AAAA,IACzB,SAAS,cAAc;AAAA,IACvB,OAAO;AAAA,IACP,OAAO;AAAA,IACP,KAAK;AAAA,IACL,eAAe,cAAc;AAAA,IAC7B,OAAO,cAAc;AAAA,IACrB,UAAU,cAAc;AAAA,EAC1B,CAAC;AAED,QAAM,MAA2B;AAAA,IAC/B,OAAQ,QAAQ,SAAS,CAAC;AAAA,IAC1B,QAAQ,QAAQ,UAAU,CAAC;AAAA,IAC3B,OAAO,QAAQ,SAAS,CAAC;AAAA,IACzB,SAAS,QAAQ,WAAW,CAAC;AAAA,IAC7B,MAAM,QAAQ,QAAQ;AAAA,IACtB,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,MAAM;AAAA,IACN;AAAA,IACA,KAAK,QAAQ,OAAO,CAAC;AAAA,IACrB;AAAA,IACA;AAAA,IACA,OAAO;AAAA,IACP,GAAG;AAAA;AAAA;AAAA;AAAA,IAIH,QAAQ,CAAC;AAAA,IACT,WAAW;AAAA,IACX,SAAS,IAAI,OAAO,EAAE;AAAA,IACtB,QAAQ,IAAI,OAAO,EAAE;AAAA,IACrB;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../src/test/mock-db.ts","../../src/test/context.ts"],"sourcesContent":["import type { DBClient, DBOps } from \"../endpoint.js\";\nimport type {\n TxPlanBody,\n TxPlanOpResult,\n TxPlanRejection,\n TxPlanResponse,\n TxWireGuard,\n TxWireOp,\n TxWireValue,\n} from \"../db/tx-plan.js\";\n\n/** Tracked records for assertions. */\ninterface TrackedRecords {\n inserted: Map<string, Record<string, unknown>[]>;\n updated: Map<string, Record<string, unknown>[]>;\n deleted: Map<string, string[]>;\n}\n\n/** Mock DB client with tracking and seed data support. */\nexport interface MockDBClient extends DBClient {\n /** Get records inserted into a table. */\n inserted(table: string): Record<string, unknown>[];\n /** Get records updated in a table. */\n updated(table: string): Record<string, unknown>[];\n /** Get IDs deleted from a table. */\n deleted(table: string): string[];\n /** Pre-seed data into a table for findById/findMany. */\n seed(table: string, data: Record<string, unknown>[]): void;\n}\n\n/** Create a mock DB client with in-memory tracking. */\nexport function createMockDB(): MockDBClient {\n const store = new Map<string, Record<string, unknown>[]>();\n const tracked: TrackedRecords = {\n inserted: new Map(),\n updated: new Map(),\n deleted: new Map(),\n };\n\n function rowsOf(table: string): Record<string, unknown>[] {\n let rows = store.get(table);\n if (!rows) {\n rows = [];\n store.set(table, rows);\n }\n return rows;\n }\n\n function track(\n map: Map<string, Record<string, unknown>[]>,\n table: string,\n row: Record<string, unknown>,\n ): void {\n const list = map.get(table);\n if (list) list.push(row);\n else map.set(table, [row]);\n }\n\n // Build the op surface first (the six string-keyed ops). `txPlan` below\n // interprets a whole plan against the SAME in-memory store and tracking maps,\n // so a transaction's writes are visible to later assertions exactly as a\n // direct write would be.\n const ops: DBOps = {\n async query(_sql: string, _params?: unknown[]) {\n return [];\n },\n\n async insert(table: string, data: Record<string, unknown>) {\n const record = { id: crypto.randomUUID(), ...data };\n rowsOf(table).push(record);\n track(tracked.inserted, table, record);\n return record;\n },\n\n async update(table: string, id: string, data: Record<string, unknown>) {\n const rows = store.get(table) ?? [];\n const idx = rows.findIndex((r) => r[\"id\"] === id);\n const updated = idx >= 0\n ? { ...rows[idx], ...data }\n : { id, ...data };\n if (idx >= 0) {\n rows[idx] = updated;\n }\n track(tracked.updated, table, updated);\n return updated;\n },\n\n async delete(table: string, id: string) {\n const rows = store.get(table) ?? [];\n const idx = rows.findIndex((r) => r[\"id\"] === id);\n if (idx >= 0) rows.splice(idx, 1);\n const list = tracked.deleted.get(table);\n if (list) list.push(id);\n else tracked.deleted.set(table, [id]);\n },\n\n async findById(table: string, id: string) {\n const rows = store.get(table) ?? [];\n return rows.find((r) => r[\"id\"] === id) ?? null;\n },\n\n async findMany(table: string, query?: Record<string, unknown>) {\n const rows = store.get(table) ?? [];\n if (!query) return rows;\n return rows.filter((row) =>\n Object.entries(query).every(([key, val]) => row[key] === val),\n );\n },\n };\n\n /**\n * Interpret a whole plan, atomically.\n *\n * The rollback is the point. A test that asserts \"the second write failed, so\n * the first one is not there\" must be able to FAIL — a mock that applied ops\n * and left them applied would pass that test while the real broker rolled the\n * transaction back, or the other way round. So the store and the tracking maps\n * are snapshotted, and any failure restores both before rejecting.\n *\n * The rejection carries the same envelope fields the runtime copies off the\n * broker's response (`error_code`, `slot`), because the SDK maps `slot` back\n * to the caller's own Error — a mock that rejected with a bare Error would\n * make every guard in every tenant test look like a generic failure.\n */\n async function txPlan(plan: TxPlanBody): Promise<TxPlanResponse> {\n const snapshot = new Map<string, Record<string, unknown>[]>();\n for (const [table, rows] of store) snapshot.set(table, [...rows]);\n const trackedSnapshot: TrackedRecords = {\n inserted: cloneTracked(tracked.inserted),\n updated: cloneTracked(tracked.updated),\n deleted: new Map([...tracked.deleted].map(([k, v]) => [k, [...v]])),\n };\n\n const results: TxPlanOpResult[] = [];\n try {\n for (const op of plan.ops) {\n const result = applyOp(op, results);\n results.push(result);\n const failure = guardFailure(op.guard, result.rows.length);\n if (failure) throw failure;\n }\n } catch (err) {\n store.clear();\n for (const [table, rows] of snapshot) store.set(table, rows);\n tracked.inserted = trackedSnapshot.inserted;\n tracked.updated = trackedSnapshot.updated;\n tracked.deleted = trackedSnapshot.deleted;\n throw err;\n }\n return { results };\n }\n\n function applyOp(op: TxWireOp, results: TxPlanOpResult[]): TxPlanOpResult {\n switch (op.op) {\n case \"insert\": {\n const record = { id: crypto.randomUUID(), ...resolveMap(op.values ?? {}, results, null) };\n rowsOf(op.table).push(record);\n track(tracked.inserted, op.table, record);\n return { rows: [record], rows_affected: 1 };\n }\n case \"insertMany\": {\n const written = (op.rows ?? []).map((row) => {\n const record = { id: crypto.randomUUID(), ...resolveMap(row, results, null) };\n rowsOf(op.table).push(record);\n track(tracked.inserted, op.table, record);\n return record;\n });\n return { rows: written, rows_affected: written.length };\n }\n case \"update\": {\n const rows = rowsOf(op.table);\n const where = resolveMap(op.where ?? {}, results, null);\n const written: Record<string, unknown>[] = [];\n for (let i = 0; i < rows.length; i++) {\n const row = rows[i];\n if (!row || !matches(row, where)) continue;\n const next = { ...row, ...resolveMap(op.set ?? {}, results, row) };\n rows[i] = next;\n track(tracked.updated, op.table, next);\n written.push(next);\n }\n return { rows: written, rows_affected: written.length };\n }\n case \"delete\": {\n const rows = rowsOf(op.table);\n const where = resolveMap(op.where ?? {}, results, null);\n const removed = rows.filter((row) => matches(row, where));\n for (const row of removed) {\n rows.splice(rows.indexOf(row), 1);\n const id = row[\"id\"];\n const list = tracked.deleted.get(op.table);\n const key = typeof id === \"string\" ? id : String(id);\n if (list) list.push(key);\n else tracked.deleted.set(op.table, [key]);\n }\n return { rows: removed, rows_affected: removed.length };\n }\n case \"select\": {\n const where = resolveMap(op.where ?? {}, results, null);\n let found = rowsOf(op.table).filter((row) => matches(row, where));\n if (op.limit !== undefined) found = found.slice(0, op.limit);\n return { rows: found, rows_affected: found.length };\n }\n }\n }\n\n const client: MockDBClient = {\n ...ops,\n\n txPlan,\n\n // In tests there is no real DB role; `asService()` returns the same\n // in-memory client so RLS-bypass code paths still hit the same store and\n // tracking maps. The omitted `asService` matches the contract (no\n // double-bypass), so callers can't recurse.\n asService(): Omit<DBClient, \"asService\"> {\n return client;\n },\n\n inserted(table: string) {\n return tracked.inserted.get(table) ?? [];\n },\n\n updated(table: string) {\n return tracked.updated.get(table) ?? [];\n },\n\n deleted(table: string) {\n return tracked.deleted.get(table) ?? [];\n },\n\n seed(table: string, data: Record<string, unknown>[]) {\n store.set(table, [...data]);\n },\n };\n\n return client;\n}\n\nfunction cloneTracked(\n map: Map<string, Record<string, unknown>[]>,\n): Map<string, Record<string, unknown>[]> {\n return new Map([...map].map(([k, v]) => [k, [...v]]));\n}\n\n/** Resolve one plan value: a `$ref` into an earlier result, a `$expr`, or a\n * literal. `current` is the row being updated, which is what `inc`/`dec` read. */\nfunction resolveValue(\n value: TxWireValue,\n results: TxPlanOpResult[],\n current: Record<string, unknown> | null,\n column: string,\n): unknown {\n if (typeof value !== \"object\" || value === null) return value;\n const tagged = value as { $ref?: { op: number; field: string }; $expr?: Record<string, unknown> };\n\n if (tagged.$ref) {\n const row = results[tagged.$ref.op]?.rows[0];\n if (!row) {\n throw txRejection(409, \"tx_ref_unresolved\", {\n message: `operation ${tagged.$ref.op} produced no row to reference`,\n });\n }\n return row[tagged.$ref.field];\n }\n\n if (tagged.$expr) {\n const fn = tagged.$expr[\"fn\"];\n if (fn === \"now\") return new Date().toISOString();\n const by = Number(tagged.$expr[\"by\"]);\n const base = Number(current?.[column] ?? 0);\n return fn === \"dec\" ? base - by : base + by;\n }\n\n return value;\n}\n\nfunction resolveMap(\n map: Record<string, TxWireValue>,\n results: TxPlanOpResult[],\n current: Record<string, unknown> | null,\n): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(map)) {\n out[key] = resolveValue(value, results, current, key);\n }\n return out;\n}\n\n/** Equality filter, with `null` meaning IS NULL — the broker's rule, so a\n * `{ accepted_at: null }` guard behaves the same in a test as in production. */\nfunction matches(row: Record<string, unknown>, where: Record<string, unknown>): boolean {\n return Object.entries(where).every(([key, value]) =>\n value === null ? row[key] === null || row[key] === undefined : row[key] === value,\n );\n}\n\nfunction guardFailure(guard: TxWireGuard | undefined, count: number): unknown {\n if (!guard) return null;\n const ok =\n guard.kind === \"one\"\n ? count === 1\n : guard.kind === \"none\"\n ? count === 0\n : guard.kind === \"atLeast\"\n ? count >= guard.n\n : count <= guard.n;\n if (ok) return null;\n return txRejection(409, \"tx_guard_failed\", {\n slot: guard.slot,\n message: `expected ${guard.kind} ${guard.n} row(s), got ${count}`,\n });\n}\n\n/** Build a rejection shaped like the one the runtime throws for a broker error:\n * an Error carrying the envelope's `status`/`error_code`/`slot`. */\nfunction txRejection(\n status: number,\n code: string,\n extra: { slot?: number; message: string },\n): Error & TxPlanRejection {\n const err = new Error(extra.message) as Error & TxPlanRejection;\n err.status = status;\n err.error_code = code;\n if (extra.slot !== undefined) err.slot = extra.slot;\n return err;\n}\n","import type {\n CacheClient,\n ClientInfo,\n Logger,\n PBRequest,\n PalbaseModuleClients,\n QueueClient,\n} from \"../endpoint.js\";\nimport type {\n PalbaseAuthClient,\n PalbaseStorageClient,\n PalbaseRealtimeClient,\n PalbaseFunctionsClient,\n PalbaseFlagsClient,\n PalbaseNotificationsClient,\n PalbaseAnalyticsClient,\n PalbaseLinksClient,\n} from \"../clients.js\";\nimport type { User } from \"../types.js\";\nimport { __setRuntime } from \"../runtime.js\";\nimport type { PurchasesService } from \"../purchases/service.js\";\nimport { createMockDB, type MockDBClient } from \"./mock-db.js\";\n\n/** Options for creating a test context. */\nexport interface TestContextOptions<TInput = unknown> {\n user?: User | null;\n input?: TInput;\n params?: Record<string, string>;\n query?: Record<string, string>;\n headers?: Record<string, string>;\n env?: Record<string, string>;\n db?: { seed?: Record<string, Record<string, unknown>[]> };\n}\n\n/** Log entry captured by the mock logger. */\nexport interface LogEntry {\n level: \"info\" | \"warn\" | \"error\" | \"debug\";\n message: string;\n args: unknown[];\n}\n\n/** Test context for exercising endpoint handlers.\n *\n * Handlers now receive a {@link PBRequest} (no services attached) and reach\n * services via the PascalCase singletons (`Database`, `Log`, …). So\n * `createTestContext` does two things:\n * 1. returns a `PBRequest` (the object you pass to `handler(...)`), and\n * 2. installs mock services into the runtime via `__setRuntime`, so the\n * singletons resolve to the same mocks while the handler runs.\n *\n * For assertions and for building sibling (worker/job/hook/webhook) contexts,\n * the mock service handles are also attached here (`db`, `log`, `cache`,\n * `queue`, `env`, plus the module clients and captured `logs`). The user\n * defaults to nullable in tests (`PBRequest<TInput, false>`) so test code can\n * pass any auth shape without a cast. */\nexport interface TestContext<TInput = unknown>\n extends PBRequest<TInput, false>,\n PalbaseModuleClients {\n db: MockDBClient;\n env: Record<string, string>;\n log: Logger;\n cache: CacheClient;\n queue: QueueClient;\n /** Captured log entries. */\n logs: LogEntry[];\n}\n\n/** Create a mock logger that captures entries. */\nfunction createMockLogger(logs: LogEntry[]): Logger {\n return {\n info(message: string, ...args: unknown[]) {\n logs.push({ level: \"info\", message, args });\n },\n warn(message: string, ...args: unknown[]) {\n logs.push({ level: \"warn\", message, args });\n },\n error(message: string, ...args: unknown[]) {\n logs.push({ level: \"error\", message, args });\n },\n debug(message: string, ...args: unknown[]) {\n logs.push({ level: \"debug\", message, args });\n },\n };\n}\n\n/** Create a mock in-memory cache.\n *\n * Mirrors the runtime's JSON-typed semantics (values are arbitrary JSON, not\n * just strings). getOrSet is single-process here, so it does not need the\n * distributed lock the real runtime uses — it is just get-miss → fn → set.\n * The cross-replica stampede protection is covered by the worker.js tests.\n */\nfunction createMockCache(): CacheClient {\n const store = new Map<string, unknown>();\n\n const get = async <T = unknown>(key: string): Promise<T | null> => {\n return store.has(key) ? (store.get(key) as T) : null;\n };\n const set = async (key: string, value: unknown, _ttl?: number): Promise<void> => {\n store.set(key, value);\n };\n\n return {\n get,\n set,\n async del(key: string) {\n store.delete(key);\n },\n async incr(key: string) {\n const raw = store.get(key);\n const current = typeof raw === \"number\" ? raw : parseInt(String(raw ?? \"0\"), 10);\n const next = current + 1;\n store.set(key, next);\n return next;\n },\n async getOrSet<T>(key: string, ttl: number, fn: () => Promise<T> | T): Promise<T> {\n const hit = await get<T>(key);\n if (hit !== null) {\n return hit;\n }\n const value = await fn();\n await set(key, value, ttl);\n return value;\n },\n };\n}\n\n/** Create mock Palbase module clients (Documents, Storage, …).\n *\n * Every slot throws with a descriptive error so tests that access a module\n * client surface without configuring it fail loudly rather than silently\n * returning undefined. Override individual clients on the returned context for\n * tests that need them.\n */\nfunction createMockModuleClients(): PalbaseModuleClients {\n const notImpl = (label: string): never => {\n throw new Error(\n `${label} not configured in test mock — override the matching client on the returned context`,\n );\n };\n\n const docs: PalbaseModuleClients[\"docs\"] = {\n collection: () => notImpl(\"docs.collection\"),\n doc: () => notImpl(\"docs.doc\"),\n };\n\n const auth: PalbaseAuthClient = {\n verifyUserToken: () => notImpl(\"auth.verifyUserToken\"),\n getSession: () => notImpl(\"auth.getSession\"),\n mfa: {\n enroll: () => notImpl(\"auth.mfa.enroll\"),\n verifyEnrollment: () => notImpl(\"auth.mfa.verifyEnrollment\"),\n challenge: () => notImpl(\"auth.mfa.challenge\"),\n recovery: () => notImpl(\"auth.mfa.recovery\"),\n listFactors: () => notImpl(\"auth.mfa.listFactors\"),\n removeFactor: () => notImpl(\"auth.mfa.removeFactor\"),\n regenerateRecoveryCodes: () => notImpl(\"auth.mfa.regenerateRecoveryCodes\"),\n emailEnroll: () => notImpl(\"auth.mfa.emailEnroll\"),\n emailChallenge: () => notImpl(\"auth.mfa.emailChallenge\"),\n emailVerify: () => notImpl(\"auth.mfa.emailVerify\"),\n },\n device: {\n generateChallenge: () => notImpl(\"auth.device.generateChallenge\"),\n attestAndroid: () => notImpl(\"auth.device.attestAndroid\"),\n attestiOS: () => notImpl(\"auth.device.attestiOS\"),\n bind: () => notImpl(\"auth.device.bind\"),\n list: () => notImpl(\"auth.device.list\"),\n delete: () => notImpl(\"auth.device.delete\"),\n verifyRequestSignature: () => notImpl(\"auth.device.verifyRequestSignature\"),\n getToken: () => notImpl(\"auth.device.getToken\"),\n get isActive(): never {\n return notImpl(\"auth.device.isActive\");\n },\n setCachedToken: () => notImpl(\"auth.device.setCachedToken\"),\n dispose: () => notImpl(\"auth.device.dispose\"),\n },\n };\n\n const storage: PalbaseStorageClient = {\n bucket: () => notImpl(\"storage.bucket\"),\n };\n\n const realtime: PalbaseRealtimeClient = {\n broadcast: async () => ({ data: undefined, error: null }),\n };\n\n const functions: PalbaseFunctionsClient = {\n invoke: () => notImpl(\"functions.invoke\"),\n };\n\n const flags: PalbaseFlagsClient = {\n isEnabled: () => notImpl(\"flags.isEnabled\"),\n getVariant: () => notImpl(\"flags.getVariant\"),\n getAll: () => notImpl(\"flags.getAll\"),\n setOverride: () => notImpl(\"flags.setOverride\"),\n asService: () => ({\n setOverrideForUser: () => notImpl(\"flags.asService.setOverrideForUser\"),\n setOverridesForUser: () => notImpl(\"flags.asService.setOverridesForUser\"),\n clearOverrideForUser: () => notImpl(\"flags.asService.clearOverrideForUser\"),\n clearAllOverridesForUser: () => notImpl(\"flags.asService.clearAllOverridesForUser\"),\n batchSetOverrides: () => notImpl(\"flags.asService.batchSetOverrides\"),\n }),\n };\n\n const notifications: PalbaseNotificationsClient = {\n push: { send: () => notImpl(\"notifications.push.send\") },\n email: { send: () => notImpl(\"notifications.email.send\") },\n sms: { send: () => notImpl(\"notifications.sms.send\") },\n verifications: {\n start: () => notImpl(\"notifications.verifications.start\"),\n check: () => notImpl(\"notifications.verifications.check\"),\n },\n inbox: {\n send: () => notImpl(\"notifications.inbox.send\"),\n list: () => notImpl(\"notifications.inbox.list\"),\n unreadCount: () => notImpl(\"notifications.inbox.unreadCount\"),\n markRead: () => notImpl(\"notifications.inbox.markRead\"),\n markAllRead: () => notImpl(\"notifications.inbox.markAllRead\"),\n archive: () => notImpl(\"notifications.inbox.archive\"),\n },\n preferences: {\n get: () => notImpl(\"notifications.preferences.get\"),\n update: () => notImpl(\"notifications.preferences.update\"),\n },\n templates: {\n email: {\n list: () => notImpl(\"notifications.templates.email.list\"),\n get: () => notImpl(\"notifications.templates.email.get\"),\n create: () => notImpl(\"notifications.templates.email.create\"),\n update: () => notImpl(\"notifications.templates.email.update\"),\n delete: () => notImpl(\"notifications.templates.email.delete\"),\n },\n sms: {\n list: () => notImpl(\"notifications.templates.sms.list\"),\n get: () => notImpl(\"notifications.templates.sms.get\"),\n create: () => notImpl(\"notifications.templates.sms.create\"),\n update: () => notImpl(\"notifications.templates.sms.update\"),\n delete: () => notImpl(\"notifications.templates.sms.delete\"),\n },\n },\n registerDevice: () => notImpl(\"notifications.registerDevice\"),\n unregisterDevice: () => notImpl(\"notifications.unregisterDevice\"),\n };\n\n const analytics: PalbaseAnalyticsClient = {\n capture: () => notImpl(\"analytics.capture\"),\n identify: () => notImpl(\"analytics.identify\"),\n screen: () => notImpl(\"analytics.screen\"),\n query: {\n count: () => notImpl(\"analytics.query.count\"),\n events: () => notImpl(\"analytics.query.events\"),\n properties: () => notImpl(\"analytics.query.properties\"),\n users: () => notImpl(\"analytics.query.users\"),\n funnel: () => notImpl(\"analytics.query.funnel\"),\n retention: () => notImpl(\"analytics.query.retention\"),\n cohort: () => notImpl(\"analytics.query.cohort\"),\n },\n management: {\n overview: () => notImpl(\"analytics.management.overview\"),\n eventNames: () => notImpl(\"analytics.management.eventNames\"),\n userDetail: () => notImpl(\"analytics.management.userDetail\"),\n deleteUser: () => notImpl(\"analytics.management.deleteUser\"),\n },\n };\n\n const links: PalbaseLinksClient = {\n create: () => notImpl(\"links.create\"),\n list: () => notImpl(\"links.list\"),\n get: () => notImpl(\"links.get\"),\n update: () => notImpl(\"links.update\"),\n delete: () => notImpl(\"links.delete\"),\n analytics: () => notImpl(\"links.analytics\"),\n qrCode: () => notImpl(\"links.qrCode\"),\n match: () => notImpl(\"links.match\"),\n };\n\n return {\n auth,\n storage,\n docs,\n realtime,\n functions,\n flags,\n notifications,\n analytics,\n links,\n };\n}\n\n/** A permissive purchases double: the subject resolves, the entitlement is\n * present, and a spend just runs its handler. That keeps a unit test of a\n * decorated handler about the handler's own logic instead of about billing.\n *\n * ponytail: deliberately has no \"deny\" mode. The 403/429 paths are about the\n * server's real accounting (reserve/commit/cancel, quota windows), and a double\n * that answered them would be asserting its own script — those belong in an\n * integration test against a real palstore, which is how they are covered.\n */\nfunction createMockPurchases(): PurchasesService {\n return {\n resolveSubject: async ({ userRef }) => ({ subjectId: `psj_test_${userRef}` }),\n require: async () => undefined,\n withSpend: async (_subjectId, _key, _opts, handler) => handler(),\n };\n}\n\n/** Null-by-default calling-client metadata for tests. */\nconst NULL_CLIENT_INFO: ClientInfo = {\n sdkVersion: null,\n appVersion: null,\n platform: null,\n osVersion: null,\n};\n\n/** Create a fully mocked endpoint test context.\n *\n * Returns a `PBRequest` (pass it to `handler(...)`) with the mock service\n * handles attached for assertions, and installs those mocks into the runtime\n * via `__setRuntime` so the `Database`/`Log`/… singletons resolve to them\n * while the handler runs.\n */\nexport function createTestContext<TInput = unknown>(\n options: TestContextOptions<TInput> = {},\n): TestContext<TInput> {\n const logs: LogEntry[] = [];\n const db = createMockDB();\n\n // Seed data if provided\n if (options.db?.seed) {\n for (const [table, data] of Object.entries(options.db.seed)) {\n db.seed(table, data);\n }\n }\n\n const mockQueue: QueueClient = {\n push: async (_worker: string, _payload: unknown) => ({ jobId: \"\" }),\n };\n const log = createMockLogger(logs);\n const cache = createMockCache();\n const moduleClients = createMockModuleClients();\n\n // Install the mocks so the PascalCase singletons (Database, Log, …) resolve\n // to them while the handler under test runs.\n __setRuntime({\n Database: db,\n Documents: moduleClients.docs,\n Storage: moduleClients.storage,\n Cache: cache,\n Queue: mockQueue,\n Log: log,\n Notifications: moduleClients.notifications,\n Flags: moduleClients.flags,\n Realtime: moduleClients.realtime,\n Purchases: createMockPurchases(),\n });\n\n const ctx: TestContext<TInput> = {\n input: (options.input ?? {}) as TInput,\n params: options.params ?? {},\n query: options.query ?? {},\n headers: options.headers ?? {},\n user: options.user ?? null,\n client: NULL_CLIENT_INFO,\n method: \"POST\",\n file: null,\n db,\n env: options.env ?? {},\n log,\n cache,\n queue: mockQueue,\n ...moduleClients,\n // Empty errors map in tests by default. Tests that exercise an endpoint's\n // declared errors construct their own throwers; this stub satisfies the\n // PBRequest shape without forcing every test to declare `errors:`.\n errors: {},\n requestId: \"req_test_000000000000\",\n traceId: \"0\".repeat(32),\n spanId: \"0\".repeat(16),\n logs,\n };\n\n return ctx;\n}\n"],"mappings":";;;;;;AA+BO,SAAS,eAA6B;AAC3C,QAAM,QAAQ,oBAAI,IAAuC;AACzD,QAAM,UAA0B;AAAA,IAC9B,UAAU,oBAAI,IAAI;AAAA,IAClB,SAAS,oBAAI,IAAI;AAAA,IACjB,SAAS,oBAAI,IAAI;AAAA,EACnB;AAEA,WAAS,OAAO,OAA0C;AACxD,QAAI,OAAO,MAAM,IAAI,KAAK;A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AA2B;AAAA,IAC/B,OAAQ,QAAQ,SAAS,CAAC;AAAA,IAC1B,QAAQ,QAAQ,UAAU,CAAC;AAAA,IAC3B,OAAO,QAAQ,SAAS,CAAC;AAAA,IACzB,SAAS,QAAQ,WAAW,CAAC;AAAA,IAC7B,MAAM,QAAQ,QAAQ;AAAA,IACtB,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,MAAM;AAAA,IACN;AAAA,IACA,KAAK,QAAQ,OAAO,CAAC;AAAA,IACrB;AAAA,IACA;AAAA,IACA,OAAO;AAAA,IACP,GAAG;AAAA;AAAA;AAAA;AAAA,IAIH,QAAQ,CAAC;AAAA,IACT,WAAW;AAAA,IACX,SAAS,IAAI,OAAO,EAAE;AAAA,IACtB,QAAQ,IAAI,OAAO,EAAE;AAAA,IACrB;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
package/docs/README.md CHANGED
@@ -184,12 +184,13 @@ generated client surface) changes; the verb/path do not affect it.
184
184
 
185
185
  ### CLI workflow
186
186
 
187
- - `palbase serve` — run `controllers/` locally with hot reload (proxies
188
- `Database`/services to the selected deployed Environment; runs `gen-types` on
189
- startup).
190
- - `palbase gen-types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
191
- `Database.tables.*` is typed (no import, no generic). Standalone version of what
192
- `serve` runs.
187
+ - `palbase build` — validate the tree locally exactly the way a deploy would
188
+ (stage, bundle, extract controller metadata). Exits non-zero on a decorator,
189
+ return-type or version-skew error, so a push that would deploy zero endpoints
190
+ fails on your machine instead.
191
+ - `palbase db types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
192
+ `Database.tables.*` is typed (no import, no generic). Run it after editing the
193
+ schema.
193
194
  - `palbase push` — deploy the current backend to the selected Environment. For a
194
195
  GitHub-backed project it runs `git push`; the pushed source Git branch deploys
195
196
  to the Environment configured for that branch.
package/docs/database.md CHANGED
@@ -41,7 +41,7 @@ available:
41
41
  | `Database.findById(table, id)` | the row or `null` |
42
42
  | `Database.findMany(table, query?)` | matching rows (array) |
43
43
  | `Database.query(sql, params?)` | rows from a read-only SQL query (runs in a READ ONLY transaction) |
44
- | `Database.transaction(fn)` | runs `fn(tx)` in a transaction |
44
+ | `Database.transaction(fn)` | runs a whole transaction plan in one request |
45
45
 
46
46
  `findMany`'s `query` is an equality filter: keys are ANDed together. For
47
47
  anything richer (ranges, ordering, joins) use `Database.query`.
@@ -58,19 +58,108 @@ const rows = await Database.query(
58
58
 
59
59
  ## Transactions
60
60
 
61
- `transaction(fn)` gives you a `tx` with the same DB ops (no nested
62
- transaction). Returning commits; throwing rolls back.
61
+ A transaction is a **plan**, not a conversation. The callback DESCRIBES the
62
+ operations; the whole description travels in one request, and the broker runs it
63
+ inside a single transaction — committing when it finishes, rolling back on any
64
+ failure. That is unchanged. What changed is that no round trip happens in the
65
+ middle, so nothing holds a database connection open while your code thinks.
63
66
 
64
67
  ```ts
65
- await Database.transaction(async (tx) => {
66
- const order = await tx.tables.orders.insert({ amount: 1000, status: "pending" });
67
- await tx.tables.order_items.insert({ order_id: order.id, sku: "ABC" });
68
- // throw here → both inserts roll back
68
+ import { Database, NotFound } from "@palbase/backend";
69
+
70
+ const { orderId } = await Database.transaction((tx) => {
71
+ const order = tx.tables.orders
72
+ .insert({ amount: 1000, status: "pending" })
73
+ .expectOne(new NotFound("order could not be created"));
74
+
75
+ tx.tables.order_items.insertMany(
76
+ cart.map((line) => ({ order_id: order.id, sku: line.sku })),
77
+ );
78
+
79
+ return { orderId: order.id };
69
80
  });
70
81
  ```
71
82
 
72
- The `tx` carries the same typed `tx.tables.<name>` API as `Database.tables`
73
- (no nested transaction). See [schema.md](./schema.md) for the full surface.
83
+ Four things follow from "it is a plan", and the compiler enforces all four.
84
+
85
+ **The callback is synchronous.** Nothing has run when it returns, so there is
86
+ nothing to await. `async` on the callback and `await` inside it are compile
87
+ errors.
88
+
89
+ **An operation returns a result handle, not a row.** `insert()` gives you a
90
+ `TxRows`; to read a column you first say what you expect:
91
+
92
+ | Expectation | Holds when | Gives you |
93
+ |---|---|---|
94
+ | `.expectOne(err)` | exactly 1 row | the row — the only way to read columns |
95
+ | `.expectNone(err)` | 0 rows | nothing |
96
+ | `.expectAtLeast(n, err)` | ≥ n rows | nothing |
97
+ | `.expectAtMost(n, err)` | ≤ n rows | nothing |
98
+
99
+ If an expectation does not hold, the whole transaction rolls back and **your**
100
+ `err` is thrown — the error object stays on this side, the server only reports
101
+ which expectation failed.
102
+
103
+ **A column you read is a `Ref`, not a value.** It is a promise of what the
104
+ server will produce. Write it into a later operation, or return it and read the
105
+ real value after `transaction()` resolves. You cannot print it, concatenate it,
106
+ do arithmetic on it, or `JSON.stringify` it — all of those throw.
107
+
108
+ > ⚠️ **`if (ref)` is always true.** JavaScript does not let a Ref refuse a
109
+ > truthiness test, so branching on one silently takes the wrong path and commits
110
+ > the wrong data. Never branch on a value the transaction has not produced yet:
111
+ > read it before the transaction, or express the condition as a filter plus an
112
+ > expectation.
113
+
114
+ **Reads you do not write move outside.** A lookup whose result the transaction
115
+ does not write is not part of the transaction:
116
+
117
+ ```ts
118
+ // Before the transaction: an ordinary value you can branch on.
119
+ const overrides = await Database.tables.category_overrides.findMany({ household_id });
120
+
121
+ const stmt = await Database.transaction((tx) => { /* … */ });
122
+ ```
123
+
124
+ ### Writing conditions as filters
125
+
126
+ `if (row.accepted_at) throw new Conflict(…)` needs a real value, so it becomes a
127
+ filter plus an expectation — which is also stronger, because the check and the
128
+ write are now the same statement and nothing can slip between them:
129
+
130
+ ```ts
131
+ tx.tables.invites
132
+ .updateWhere({ token, accepted_at: null }, { accepted_at: now() })
133
+ .expectOne(new Conflict("invite already used", "invite_used"));
134
+ ```
135
+
136
+ ### The table surface inside a transaction
137
+
138
+ | Operation | Notes |
139
+ |---|---|
140
+ | `insert(values)` | one row |
141
+ | `insertMany(rows)` | one statement; every row must set the same columns. An empty list writes nothing |
142
+ | `updateWhere(where, set)` | **filter first**. A filterless update is refused |
143
+ | `deleteWhere(where)` | a filterless delete is refused |
144
+ | `select(where?, { limit, lock })` | `lock: "update"` takes a real `FOR UPDATE` row lock |
145
+
146
+ A `where` is equality-only and ANDed; `null` means `IS NULL`.
147
+
148
+ Three expressions may appear in the values you write:
149
+
150
+ | Expression | Where | Meaning |
151
+ |---|---|---|
152
+ | `now()` | anywhere | the server's clock |
153
+ | `inc(n)` | `updateWhere`'s `set` | `column = column + n`, atomically |
154
+ | `dec(n)` | `updateWhere`'s `set` | `column = column - n`, atomically |
155
+
156
+ `inc`/`dec` read the column's current value, which an inserted row does not
157
+ have — using them in an `insert` is a compile error.
158
+
159
+ ### Limits
160
+
161
+ A plan may carry at most 1000 operations, 5000 rows in one `insertMany`, and
162
+ 8 MiB of JSON. Exceeding any of them is reported before the request is sent.
74
163
 
75
164
  ## Bypassing RLS — `Database.asService()`
76
165
 
@@ -95,9 +184,10 @@ const mine = await Database.tables.todos.findMany({});
95
184
  const all = await Database.asService().tables.todos.findMany({});
96
185
  const rows = await Database.asService().query("SELECT count(*) FROM todos");
97
186
 
98
- // A service-role transaction (the role is fixed for the whole tx):
99
- await Database.asService().transaction(async (tx) => {
100
- await tx.tables.todos.update(id, { done: true });
187
+ // A service-role transaction (the role is fixed for the whole plan):
188
+ await Database.asService().transaction((tx) => {
189
+ tx.tables.todos.updateWhere({ id }, { done: true });
190
+ return null;
101
191
  });
102
192
  ```
103
193
 
@@ -107,7 +197,7 @@ Guidelines:
107
197
  only where you genuinely need cross-user access. It is intentionally easy to
108
198
  grep for in review.
109
199
  - **No double-bypass / no nesting.** The sibling does not re-expose
110
- `asService()`, and `tx` never exposes it — a transaction's role is fixed when
111
- it begins. Use `Database.transaction(...)` for an authenticated tx and
112
- `Database.asService().transaction(...)` for a service-role tx; you cannot mix
113
- enforced and bypassed ops inside one interactive transaction.
200
+ `asService()`, and `tx` never exposes it — a plan's role is fixed for the whole
201
+ transaction. Use `Database.transaction(...)` for an authenticated one and
202
+ `Database.asService().transaction(...)` for a service-role one; you cannot mix
203
+ enforced and bypassed operations inside a single plan.
@@ -12,7 +12,7 @@ Your project depends on the SDK and uses the Palbase CLI for the dev loop:
12
12
  "name": "my-backend",
13
13
  "private": true,
14
14
  "scripts": {
15
- "dev": "palbase serve",
15
+ "build": "palbase build",
16
16
  "typecheck": "tsc --noEmit"
17
17
  },
18
18
  "dependencies": { "@palbase/backend": "latest" },
@@ -37,17 +37,19 @@ The controllers use **decorators**, so the `tsconfig.json` must set
37
37
  }
38
38
  ```
39
39
 
40
- ## Local dev loop
41
-
42
- - `palbase serve` — run your backend locally with hot reload. It runs your
43
- `controllers/` locally but proxies `Database` and the service singletons to
44
- the selected deployed Environment, so that Environment must already have a
45
- deployment (`serve` tells you to push first if it does not). On startup it
46
- also runs `gen-types`. See
47
- [migrations.md](./migrations.md) for the schema/migration side of this.
48
- - `palbase gen-types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
49
- `Database.tables.*` is typed (no import, no generic). Run it standalone after
50
- editing the schema, or rely on `palbase serve` running it for you.
40
+ ## Dev loop
41
+
42
+ Your backend runs on Palbase, not on your laptop — there is no local runtime to
43
+ start. The loop is: edit, validate, push to a dev Environment.
44
+
45
+ - `palbase build` — validate the tree the way the deploy will. It stages and
46
+ bundles your `controllers/` exactly as the deploy does and runs the deploy's
47
+ own metadata extractor over the result, so a bad decorator, an illegal return
48
+ type or an SDK major skew fails here rather than shipping a deploy that
49
+ serves zero endpoints. It is wired into a `pre-push` git hook for you.
50
+ - `palbase db types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
51
+ `Database.tables.*` is typed (no import, no generic). Run it after editing the
52
+ schema. See [migrations.md](./migrations.md) for the schema/migration side.
51
53
  - `palbase push` deploys the current backend to the selected Environment. For a
52
54
  GitHub-backed project it runs `git push`; Studio maps the pushed source Git
53
55
  branch to its configured Environment.
@@ -192,12 +192,13 @@ generated client surface) changes; the verb/path do not affect it.
192
192
 
193
193
  ### CLI workflow
194
194
 
195
- - `palbase serve` — run `controllers/` locally with hot reload (proxies
196
- `Database`/services to the selected deployed Environment; runs `gen-types` on
197
- startup).
198
- - `palbase gen-types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
199
- `Database.tables.*` is typed (no import, no generic). Standalone version of what
200
- `serve` runs.
195
+ - `palbase build` — validate the tree locally exactly the way a deploy would
196
+ (stage, bundle, extract controller metadata). Exits non-zero on a decorator,
197
+ return-type or version-skew error, so a push that would deploy zero endpoints
198
+ fails on your machine instead.
199
+ - `palbase db types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
200
+ `Database.tables.*` is typed (no import, no generic). Run it after editing the
201
+ schema.
201
202
  - `palbase push` — deploy the current backend to the selected Environment. For a
202
203
  GitHub-backed project it runs `git push`; the pushed source Git branch deploys
203
204
  to the Environment configured for that branch.
@@ -298,7 +299,7 @@ Your project depends on the SDK and uses the Palbase CLI for the dev loop:
298
299
  "name": "my-backend",
299
300
  "private": true,
300
301
  "scripts": {
301
- "dev": "palbase serve",
302
+ "build": "palbase build",
302
303
  "typecheck": "tsc --noEmit"
303
304
  },
304
305
  "dependencies": { "@palbase/backend": "latest" },
@@ -323,17 +324,19 @@ The controllers use **decorators**, so the `tsconfig.json` must set
323
324
  }
324
325
  ```
325
326
 
326
- ## Local dev loop
327
-
328
- - `palbase serve` — run your backend locally with hot reload. It runs your
329
- `controllers/` locally but proxies `Database` and the service singletons to
330
- the selected deployed Environment, so that Environment must already have a
331
- deployment (`serve` tells you to push first if it does not). On startup it
332
- also runs `gen-types`. See
333
- [migrations.md](./migrations.md) for the schema/migration side of this.
334
- - `palbase gen-types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
335
- `Database.tables.*` is typed (no import, no generic). Run it standalone after
336
- editing the schema, or rely on `palbase serve` running it for you.
327
+ ## Dev loop
328
+
329
+ Your backend runs on Palbase, not on your laptop — there is no local runtime to
330
+ start. The loop is: edit, validate, push to a dev Environment.
331
+
332
+ - `palbase build` — validate the tree the way the deploy will. It stages and
333
+ bundles your `controllers/` exactly as the deploy does and runs the deploy's
334
+ own metadata extractor over the result, so a bad decorator, an illegal return
335
+ type or an SDK major skew fails here rather than shipping a deploy that
336
+ serves zero endpoints. It is wired into a `pre-push` git hook for you.
337
+ - `palbase db types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
338
+ `Database.tables.*` is typed (no import, no generic). Run it after editing the
339
+ schema. See [migrations.md](./migrations.md) for the schema/migration side.
337
340
  - `palbase push` deploys the current backend to the selected Environment. For a
338
341
  GitHub-backed project it runs `git push`; Studio maps the pushed source Git
339
342
  branch to its configured Environment.
@@ -684,7 +687,7 @@ available:
684
687
  | `Database.findById(table, id)` | the row or `null` |
685
688
  | `Database.findMany(table, query?)` | matching rows (array) |
686
689
  | `Database.query(sql, params?)` | rows from a read-only SQL query (runs in a READ ONLY transaction) |
687
- | `Database.transaction(fn)` | runs `fn(tx)` in a transaction |
690
+ | `Database.transaction(fn)` | runs a whole transaction plan in one request |
688
691
 
689
692
  `findMany`'s `query` is an equality filter: keys are ANDed together. For
690
693
  anything richer (ranges, ordering, joins) use `Database.query`.
@@ -701,19 +704,108 @@ const rows = await Database.query(
701
704
 
702
705
  ## Transactions
703
706
 
704
- `transaction(fn)` gives you a `tx` with the same DB ops (no nested
705
- transaction). Returning commits; throwing rolls back.
707
+ A transaction is a **plan**, not a conversation. The callback DESCRIBES the
708
+ operations; the whole description travels in one request, and the broker runs it
709
+ inside a single transaction — committing when it finishes, rolling back on any
710
+ failure. That is unchanged. What changed is that no round trip happens in the
711
+ middle, so nothing holds a database connection open while your code thinks.
706
712
 
707
713
  ```ts
708
- await Database.transaction(async (tx) => {
709
- const order = await tx.tables.orders.insert({ amount: 1000, status: "pending" });
710
- await tx.tables.order_items.insert({ order_id: order.id, sku: "ABC" });
711
- // throw here → both inserts roll back
714
+ import { Database, NotFound } from "@palbase/backend";
715
+
716
+ const { orderId } = await Database.transaction((tx) => {
717
+ const order = tx.tables.orders
718
+ .insert({ amount: 1000, status: "pending" })
719
+ .expectOne(new NotFound("order could not be created"));
720
+
721
+ tx.tables.order_items.insertMany(
722
+ cart.map((line) => ({ order_id: order.id, sku: line.sku })),
723
+ );
724
+
725
+ return { orderId: order.id };
712
726
  });
713
727
  ```
714
728
 
715
- The `tx` carries the same typed `tx.tables.<name>` API as `Database.tables`
716
- (no nested transaction). See [schema.md](./schema.md) for the full surface.
729
+ Four things follow from "it is a plan", and the compiler enforces all four.
730
+
731
+ **The callback is synchronous.** Nothing has run when it returns, so there is
732
+ nothing to await. `async` on the callback and `await` inside it are compile
733
+ errors.
734
+
735
+ **An operation returns a result handle, not a row.** `insert()` gives you a
736
+ `TxRows`; to read a column you first say what you expect:
737
+
738
+ | Expectation | Holds when | Gives you |
739
+ |---|---|---|
740
+ | `.expectOne(err)` | exactly 1 row | the row — the only way to read columns |
741
+ | `.expectNone(err)` | 0 rows | nothing |
742
+ | `.expectAtLeast(n, err)` | ≥ n rows | nothing |
743
+ | `.expectAtMost(n, err)` | ≤ n rows | nothing |
744
+
745
+ If an expectation does not hold, the whole transaction rolls back and **your**
746
+ `err` is thrown — the error object stays on this side, the server only reports
747
+ which expectation failed.
748
+
749
+ **A column you read is a `Ref`, not a value.** It is a promise of what the
750
+ server will produce. Write it into a later operation, or return it and read the
751
+ real value after `transaction()` resolves. You cannot print it, concatenate it,
752
+ do arithmetic on it, or `JSON.stringify` it — all of those throw.
753
+
754
+ > ⚠️ **`if (ref)` is always true.** JavaScript does not let a Ref refuse a
755
+ > truthiness test, so branching on one silently takes the wrong path and commits
756
+ > the wrong data. Never branch on a value the transaction has not produced yet:
757
+ > read it before the transaction, or express the condition as a filter plus an
758
+ > expectation.
759
+
760
+ **Reads you do not write move outside.** A lookup whose result the transaction
761
+ does not write is not part of the transaction:
762
+
763
+ ```ts
764
+ // Before the transaction: an ordinary value you can branch on.
765
+ const overrides = await Database.tables.category_overrides.findMany({ household_id });
766
+
767
+ const stmt = await Database.transaction((tx) => { /* … */ });
768
+ ```
769
+
770
+ ### Writing conditions as filters
771
+
772
+ `if (row.accepted_at) throw new Conflict(…)` needs a real value, so it becomes a
773
+ filter plus an expectation — which is also stronger, because the check and the
774
+ write are now the same statement and nothing can slip between them:
775
+
776
+ ```ts
777
+ tx.tables.invites
778
+ .updateWhere({ token, accepted_at: null }, { accepted_at: now() })
779
+ .expectOne(new Conflict("invite already used", "invite_used"));
780
+ ```
781
+
782
+ ### The table surface inside a transaction
783
+
784
+ | Operation | Notes |
785
+ |---|---|
786
+ | `insert(values)` | one row |
787
+ | `insertMany(rows)` | one statement; every row must set the same columns. An empty list writes nothing |
788
+ | `updateWhere(where, set)` | **filter first**. A filterless update is refused |
789
+ | `deleteWhere(where)` | a filterless delete is refused |
790
+ | `select(where?, { limit, lock })` | `lock: "update"` takes a real `FOR UPDATE` row lock |
791
+
792
+ A `where` is equality-only and ANDed; `null` means `IS NULL`.
793
+
794
+ Three expressions may appear in the values you write:
795
+
796
+ | Expression | Where | Meaning |
797
+ |---|---|---|
798
+ | `now()` | anywhere | the server's clock |
799
+ | `inc(n)` | `updateWhere`'s `set` | `column = column + n`, atomically |
800
+ | `dec(n)` | `updateWhere`'s `set` | `column = column - n`, atomically |
801
+
802
+ `inc`/`dec` read the column's current value, which an inserted row does not
803
+ have — using them in an `insert` is a compile error.
804
+
805
+ ### Limits
806
+
807
+ A plan may carry at most 1000 operations, 5000 rows in one `insertMany`, and
808
+ 8 MiB of JSON. Exceeding any of them is reported before the request is sent.
717
809
 
718
810
  ## Bypassing RLS — `Database.asService()`
719
811
 
@@ -738,9 +830,10 @@ const mine = await Database.tables.todos.findMany({});
738
830
  const all = await Database.asService().tables.todos.findMany({});
739
831
  const rows = await Database.asService().query("SELECT count(*) FROM todos");
740
832
 
741
- // A service-role transaction (the role is fixed for the whole tx):
742
- await Database.asService().transaction(async (tx) => {
743
- await tx.tables.todos.update(id, { done: true });
833
+ // A service-role transaction (the role is fixed for the whole plan):
834
+ await Database.asService().transaction((tx) => {
835
+ tx.tables.todos.updateWhere({ id }, { done: true });
836
+ return null;
744
837
  });
745
838
  ```
746
839
 
@@ -750,10 +843,10 @@ Guidelines:
750
843
  only where you genuinely need cross-user access. It is intentionally easy to
751
844
  grep for in review.
752
845
  - **No double-bypass / no nesting.** The sibling does not re-expose
753
- `asService()`, and `tx` never exposes it — a transaction's role is fixed when
754
- it begins. Use `Database.transaction(...)` for an authenticated tx and
755
- `Database.asService().transaction(...)` for a service-role tx; you cannot mix
756
- enforced and bypassed ops inside one interactive transaction.
846
+ `asService()`, and `tx` never exposes it — a plan's role is fixed for the whole
847
+ transaction. Use `Database.transaction(...)` for an authenticated one and
848
+ `Database.asService().transaction(...)` for a service-role one; you cannot mix
849
+ enforced and bypassed operations inside a single plan.
757
850
 
758
851
 
759
852
 
@@ -852,10 +945,12 @@ export default class RoomsController {
852
945
  ```
853
946
 
854
947
  `Database.tables.<name>` exposes `insert`, `update(id, data)`, `delete(id)`,
855
- `findById(id)`, `findMany(query?)`, and `Database.transaction(fn)` yields a `tx`
856
- with the same typed tables. The raw string-keyed ops
857
- (`Database.insert("rooms", …)`, `Database.query(…)`) are still available for
858
- dynamic table names and read-only SQL.
948
+ `findById(id)`, `findMany(query?)`. `Database.transaction(fn)` yields a `tx`
949
+ whose `tx.tables.<name>` is typed from the same schema, but carries plan
950
+ operations (`insert`/`insertMany`/`updateWhere`/`deleteWhere`/`select`) rather
951
+ than awaited calls — see [database.md](./database.md#transactions). The raw
952
+ string-keyed ops (`Database.insert("rooms", …)`, `Database.query(…)`) are still
953
+ available for dynamic table names and read-only SQL.
859
954
 
860
955
  If you want a row type explicitly, import it from the generated env module:
861
956
 
@@ -1038,14 +1133,14 @@ You can't push a schema change without its migration:
1038
1133
  database. Any unresolved drift **fails the deploy** and keeps the previous
1039
1134
  version live — a broken schema never goes out silently.
1040
1135
 
1041
- ## `palbase serve` uses the deployed database
1136
+ ## Your schema change is not live until you deploy it
1042
1137
 
1043
- `palbase serve` runs your controllers locally but proxies `Database` and `ctx.*`
1044
- to the selected deployed Environment — it does not spin up a local Postgres. So
1045
- a schema change in `db/schema.ts` does not exist in that database until you
1046
- generate the migration and deploy it. Run `palbase gen-types` (or just
1047
- `palbase serve`, which regenerates on change) after editing the schema to refresh
1048
- `palbase-env.d.ts` so `Database.tables.<name>` is fully typed in your services.
1138
+ There is no local database. `Database` always talks to the selected deployed
1139
+ Environment, so a change in `db/schema.ts` does not exist in that database until
1140
+ you generate the migration and deploy it — the types can be ahead of the tables.
1141
+ Run `palbase db types` after editing the schema to refresh `palbase-env.d.ts` so
1142
+ `Database.tables.<name>` is fully typed in your services, and deploy to a dev
1143
+ Environment to exercise it against real data.
1049
1144
 
1050
1145
  ## Hand-written migrations
1051
1146