@lunora/sql-store 1.0.0-alpha.13 → 1.0.0-alpha.130

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.
package/dist/index.d.ts CHANGED
@@ -1,139 +1,204 @@
1
- import { ServerDefaultContextLike, DatabaseWriterLike, SchedulerLike, SchemaLike, TableDefinitionLike, ValidatorLike } from '@lunora/do';
2
- import { SqlDialect, SqlRunResult } from "./dialect.js";
1
+ import { TableDefinitionLike, SchemaLike, ServerDefaultContextLike, DatabaseWriterLike, CrossShardReadArgs, QueryPage, SchedulerLike, CdcChange, ValidatorLike } from '@lunora/shard-engine';
2
+ import { SqlRunResult, SqlDialect } from "./dialect.js";
3
3
  export type { SqlExec } from "./dialect.js";
4
4
  import 'drizzle-orm';
5
5
  /**
6
- * Async SQL surface the D1 ORM needs: `all` for reads, `run` for writes.
7
- * Satisfied by a `D1Session`/`D1Client` in production and a `node:sqlite`
8
- * adapter in tests, so the query logic runs against a real SQLite engine.
9
- */
6
+ * Async SQL surface the D1 ORM needs: `all` for reads, `run` for writes.
7
+ * Satisfied by a `D1Session`/`D1Client` in production and a `node:sqlite`
8
+ * adapter in tests, so the query logic runs against a real SQLite engine.
9
+ */
10
10
  interface SqlCtxExec {
11
11
  all: (sql: string, parameters: ReadonlyArray<unknown>) => Promise<Record<string, unknown>[]>;
12
+ batch?: (statements: ReadonlyArray<{
13
+ params: ReadonlyArray<unknown>;
14
+ sql: string;
15
+ }>) => Promise<void>;
12
16
  run: (sql: string, parameters: ReadonlyArray<unknown>) => Promise<SqlRunResult | void>;
13
17
  }
18
+ /**
19
+ * Decode a SELECTed row back into a document: `id` → `_id`, `_creationTime`
20
+ * preserved, and every column run through the shared {@link sqliteDecode} so the
21
+ * stored form is reversed back into its JS shape. Exported so the data-browser
22
+ * (`introspect.ts`) and admin export/import paths share the exact same decode.
23
+ *
24
+ * The decode is engine-agnostic: every backend stores SQLite-shaped values
25
+ * (boolean → 1/0, JSON → text, bigint → an order-preserving text key), and `sqliteDecode` is
26
+ * robust to a driver returning either the stored string OR a natively-parsed
27
+ * value (e.g. mysql2 returns JSON columns pre-parsed) — so the same decoder is
28
+ * correct on SQLite, Postgres and MySQL.
29
+ */
30
+ declare const decodeGlobalRow: (definition: TableDefinitionLike, row: Record<string, unknown>) => Record<string, unknown>;
31
+ /**
32
+ * Provision the search companions, then index one bounded page of the rows that
33
+ * predate each index — unless it is declared `staged: true`, which leaves the
34
+ * whole backfill to {@link backfillSqlSearchIndexes}.
35
+ *
36
+ * Idempotent (`CREATE … IF NOT EXISTS` throughout, and the backfill resumes
37
+ * from recorded progress).
38
+ */
39
+ declare const runSqlSearchMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
40
+ /**
41
+ * Run every declared search index — including the `staged: true` ones the
42
+ * migration pass skips — through to completion. The entry point a host calls
43
+ * out-of-band after deploying a search index over a table too large to index a
44
+ * page at a time.
45
+ *
46
+ * Idempotent and resumable: an index recorded as complete is skipped, and an
47
+ * interrupted run picks up from its cursor.
48
+ */
49
+ declare const backfillSqlSearchIndexes: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
14
50
  interface SqlCtxDbOptions {
15
51
  /**
16
- * Resolved request auth handed to `.serverDefault(fn)` column factories so
17
- * server-trusted columns (owner/tenant ids) stamp from the verified caller,
18
- * never the client. The generated worker passes the per-request identity;
19
- * absent it, server-trusted columns stamp the anonymous slice (`userId: null`).
20
- */
52
+ * Resolved request auth handed to `.serverDefault(fn)` column factories so
53
+ * server-trusted columns (owner/tenant ids) stamp from the verified caller,
54
+ * never the client. The generated worker passes the per-request identity;
55
+ * absent it, server-trusted columns stamp the anonymous slice (`userId: null`).
56
+ */
21
57
  auth?: ServerDefaultContextLike["auth"];
22
58
  /**
23
- * Opt into change-data-capture: when `true`, every committed write appends a
24
- * post-image to the `__cdc_log` table (created lazily alongside the other
25
- * companion tables). Backs CDC streaming export for `.global()` tables — the
26
- * log is for export/CDC consumers, NOT point-in-time recovery: D1's PITR is
27
- * the platform's own Time Travel (`wrangler d1 time-travel restore`), an
28
- * atomic restore, not a changelog replay. Leave undefined for zero-cost
29
- * legacy behaviour.
30
- */
59
+ * Opt into change-data-capture: when `true`, every committed write appends a
60
+ * post-image to the `__cdc_log` table (created lazily alongside the other
61
+ * companion tables). Backs CDC streaming export for `.global()` tables — the
62
+ * log is for export/CDC consumers, NOT point-in-time recovery: D1's PITR is
63
+ * the platform's own Time Travel (`wrangler d1 time-travel restore`), an
64
+ * atomic restore, not a changelog replay. Leave undefined for zero-cost
65
+ * legacy behaviour.
66
+ */
31
67
  cdc?: boolean;
68
+ /**
69
+ * How long `.global()` changelog entries are kept, in milliseconds. Absent
70
+ * (the default) means forever — the log grows for the life of the database.
71
+ *
72
+ * Opt-in for the same reason the shard-local windows are, and the reason is
73
+ * not caution: this log's streaming-export consumers hold opaque cursors
74
+ * issued outside this deployment, and nothing here can see where they sit.
75
+ * A default window would be a guess whose failure mode is silent data loss
76
+ * in someone's warehouse. A deployment that wants the log bounded states a
77
+ * window it knows covers its consumers; `.global()` shape pollers are then
78
+ * protected exactly, by the floor this read path reports rather than by the
79
+ * window. See `sweepSqlCdcRetention`.
80
+ *
81
+ * Time rather than rows because the log is SHARED: every shard in every
82
+ * region writes it, so a row count is not a bound any single consumer can
83
+ * reason about, while "older than N" is exactly what they compare their own
84
+ * lag against.
85
+ */
86
+ cdcRetentionMs?: number;
32
87
  clock?: () => number;
33
88
  /**
34
- * Cross-shard counter for **reverse cross-backend relations** — the `_count`
35
- * mirror of the `crossShardReader` option below.
36
- */
89
+ * Cross-shard counter for **reverse cross-backend relations** — the `_count`
90
+ * mirror of the `crossShardReader` option below.
91
+ */
37
92
  crossShardCounter?: DatabaseWriterLike["count"];
38
93
  /**
39
- * Optional cross-shard reader for **reverse cross-backend relations**: a
40
- * `.global()` (D1) parent loading a shard-local (`.shardBy()`/root) child.
41
- * Such a child's rows are partitioned across every shard DO, so the local D1
42
- * writer can't resolve it. When provided, the relation loader routes the
43
- * child's read through this (the host wires it to the Query Coordinator's
44
- * RLS-correct `fanOut`, with identity forwarded so each shard applies its own
45
- * RLS). Absent it, loading such a relation throws a clear "not supported"
46
- * error (legacy behaviour). The forward direction (shard-local parent →
47
- * global child) and same-backend relations never touch this.
48
- */
49
- crossShardReader?: DatabaseWriterLike["findMany"];
94
+ * Optional cross-shard reader for **reverse cross-backend relations**: a
95
+ * `.global()` (D1) parent loading a shard-local (`.shardBy()`/root) child.
96
+ * Such a child's rows are partitioned across every shard DO, so the local D1
97
+ * writer can't resolve it. When provided, the relation loader routes the
98
+ * child's read through this (the host wires it to the Query Coordinator's
99
+ * RLS-correct `fanOut`, with identity forwarded so each shard applies its own
100
+ * RLS). Absent it, loading such a relation throws a clear "not supported"
101
+ * error (legacy behaviour). The forward direction (shard-local parent →
102
+ * global child) and same-backend relations never touch this.
103
+ *
104
+ * Takes {@link CrossShardReadArgs}, not `QueryArgs`: the hop is JSON, so the
105
+ * RLS filters are handed over as data (see that type's docblock).
106
+ */
107
+ crossShardReader?: (table: string, args: CrossShardReadArgs) => Promise<QueryPage>;
50
108
  /**
51
- * The SQL dialect that shapes every statement (identifier quoting, value
52
- * encode/decode, column types, upserts, RETURNING vs affected-rows).
53
- * `@lunora/d1` passes its `sqliteDialect`; the PlanetScale/Hyperdrive backend
54
- * passes its Postgres/MySQL dialect. Required — the core is engine-blind.
55
- */
109
+ * The SQL dialect that shapes every statement (identifier quoting, value
110
+ * encode/decode, column types, upserts, RETURNING vs affected-rows).
111
+ * `@lunora/d1` passes its `sqliteDialect`; the PlanetScale/Hyperdrive backend
112
+ * passes its Postgres/MySQL dialect. Required — the core is engine-blind.
113
+ */
56
114
  dialect: SqlDialect;
57
115
  exec: SqlCtxExec;
58
116
  idGenerator?: () => string;
59
117
  /**
60
- * Ceiling on the number of child join keys a relation-crossing `where`
61
- * predicate may materialize before the semijoin pre-resolver fails closed
62
- * (`relation predicate … exceeding the N-key limit`). D1 has no EXISTS
63
- * push-down, so an overflow here can only fail closed — never truncate the
64
- * `IN (...)` and silently mis-match. Defaults to the pre-resolver's shared
65
- * key cap when omitted.
66
- */
118
+ * Ceiling on the number of child join keys a relation-crossing `where`
119
+ * predicate may materialize before the semijoin pre-resolver fails closed
120
+ * (`relation predicate … exceeding the N-key limit`). D1 has no EXISTS
121
+ * push-down, so an overflow here can only fail closed — never truncate the
122
+ * `IN (...)` and silently mis-match. Defaults to the pre-resolver's shared
123
+ * key cap when omitted.
124
+ */
67
125
  maxRelationKeys?: number;
68
126
  /**
69
- * Scheduler exposed to global-table trigger handlers as `ctx.scheduler`.
70
- * Absent it, `ctx.scheduler` is a stub that throws on use — pass one when
71
- * triggers on `.global()` tables need to enqueue follow-up work.
72
- */
127
+ * Object identifying the database this store provisions, so the one-shot
128
+ * `CREATE TABLE/INDEX IF NOT EXISTS` sweep is shared by every ctx-db built
129
+ * against it instead of repeating per instance.
130
+ *
131
+ * Hosts build a ctx-db **per request** (the writer captures the caller's
132
+ * identity and D1 bookmark), so without this the sweep — one round trip per
133
+ * global table plus one per index — runs again on the first `.global()`
134
+ * access of every request. On a 50-table schema that is ~1s in `lunora dev`
135
+ * and a real latency floor in production.
136
+ *
137
+ * WHAT TO PASS: something that outlives the request AND names one database
138
+ * (and one configuration). The D1 binding off `env` and the declaration
139
+ * object a host builds once both qualify; the result of a per-request
140
+ * factory does not — it keys the memo on a fresh object every time and
141
+ * shares nothing (which is silent, and looks exactly like not passing it).
142
+ *
143
+ * INVARIANT, unenforced: one scope ⇒ one `schema`, `dialect` and `cdc`. The
144
+ * memoised run closes over the FIRST writer's, so a later writer sharing a
145
+ * scope with `cdc: true` behind one with `cdc: false` never gets
146
+ * `__cdc_log`. Hosts pass app-level constants for all three, which is why
147
+ * this is a note rather than a composite key.
148
+ *
149
+ * Two behaviours change with a shared memo, both benign and both worth
150
+ * knowing: the DDL runs in the FIRST writer's D1 session, so a later
151
+ * session's first read is no longer implicitly pinned behind a write of its
152
+ * own (on a replicated binding that can mean one transient stale read on a
153
+ * freshly-provisioned database); and a database reset out from under a live
154
+ * isolate is no longer healed by the next request, since the entry lives as
155
+ * long as the scope does.
156
+ *
157
+ * Omit it and the memo stays per ctx-db — slower, never wrong, and what the
158
+ * tests want (they pair a fresh database with a reused schema object).
159
+ */
160
+ provisionScope?: object;
161
+ /**
162
+ * Scheduler exposed to global-table trigger handlers as `ctx.scheduler`.
163
+ * Absent it, `ctx.scheduler` is a stub that throws on use — pass one when
164
+ * triggers on `.global()` tables need to enqueue follow-up work.
165
+ */
73
166
  scheduler?: SchedulerLike;
74
167
  schema: SchemaLike;
75
168
  }
76
- /**
77
- * Decode a SELECTed row back into a document: `id` → `_id`, `_creationTime`
78
- * preserved, and every column run through the shared {@link sqliteDecode} so the
79
- * stored form is reversed back into its JS shape. Exported so the data-browser
80
- * (`introspect.ts`) and admin export/import paths share the exact same decode.
81
- *
82
- * The decode is engine-agnostic: every backend stores SQLite-shaped values
83
- * (boolean → 1/0, JSON → text, bigint → decimal string), and `sqliteDecode` is
84
- * robust to a driver returning either the stored string OR a natively-parsed
85
- * value (e.g. mysql2 returns JSON columns pre-parsed) — so the same decoder is
86
- * correct on SQLite, Postgres and MySQL.
87
- */
88
- declare const decodeGlobalRow: (definition: TableDefinitionLike, row: Record<string, unknown>) => Record<string, unknown>;
89
- declare const runSqlGlobalTableMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
90
- /**
91
- * Materialize the `__agg_&lt;index>` companion tables for every declared
92
- * `aggregateIndex` on a global table. Global tables in Lunora ship their own
93
- * DDL — counter tables are opt-in so production hosts can decide where they
94
- * live. Tests and dev hosts can call this once after their schema migration to
95
- * unlock O(1) counts.
96
- *
97
- * Idempotent (`CREATE TABLE IF NOT EXISTS`).
98
- */
99
- declare const runSqlAggregateMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
100
- /**
101
- * Materialize the `__rank_&lt;index>` companion tables for every declared
102
- * `rankIndex` on a global table. Mirrors `runSqlAggregateMigrations` — same
103
- * opt-in pattern so production hosts decide whether to spend the DDL.
104
- *
105
- * Idempotent (`CREATE TABLE IF NOT EXISTS` + `createIndexIfNotExists`).
106
- */
107
- declare const runSqlRankMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
108
- /**
109
- * Materialize the `__fts_&lt;index>` FTS5 shadow tables for every declared
110
- * `.searchIndex()` on a global table. Mirrors `runSqlAggregateMigrations` — same
111
- * opt-in pattern so production hosts decide whether to spend the DDL. Only runs
112
- * on engines that ship FTS5 (D1 does; the `node:sqlite` test runner doesn't,
113
- * where `.search()` transparently falls back to a scan). `__text__` holds the
114
- * indexed field; `__id__` (UNINDEXED) joins back to the row.
115
- *
116
- * Idempotent (`CREATE VIRTUAL TABLE IF NOT EXISTS`).
117
- */
118
- declare const runSqlSearchMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
119
- /** One change-data-capture entry: a committed mutation, in monotonic `seq` order. Mirrors the DO twin. */
120
- interface CdcChange {
121
- /** Post-image document for insert/update; absent for delete (the `id` identifies the removed row). */
122
- doc?: Record<string, unknown>;
123
- id: string;
124
- op: "delete" | "insert" | "update";
125
- /** Monotonic per-database cursor — strictly increasing, never reused. */
126
- seq: number;
127
- table: string;
128
- /** Wall-clock millis when the change committed (the ctx-db `clock`). */
129
- ts: number;
130
- }
131
169
  /** Create the `__cdc_log` table. Idempotent; only run when CDC is enabled. */
132
170
  declare const runSqlCdcMigration: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<void>;
133
171
  /**
134
- * Read changelog entries newer than `sinceSeq` in commit order, up to `limit`
135
- * (clamped to [1, 10000]); plus the cursor to resume from.
136
- */
172
+ * Delete `.global()` changelog entries older than `retentionMs`, at most
173
+ * {@link GLOBAL_CDC_SWEEP_MAX_ROWS} per pass, and only if this writer wins the lease.
174
+ *
175
+ * **Time, not rows.** The shard-local twin bounds its log by row count because
176
+ * one shard owns it and a row count is a memory bound on that one object. This
177
+ * log is shared, so a row count means nothing to any individual consumer — but
178
+ * "older than N" is directly what every consumer needs to reason about, and it
179
+ * is the unit an operator can compare against their own connector's lag.
180
+ *
181
+ * **Which consumers this can strand, and what protects each:**
182
+ *
183
+ * - `.global()` shape pollers hold in-memory cursors this store cannot see. They
184
+ * are protected EXACTLY rather than approximately: {@link readSqlCdcChangedTables}
185
+ * reports the retained floor, and a poller below it treats the tick as "no
186
+ * visibility" and re-reads every shape. That is the same self-healing path a
187
+ * changelog error already takes, so a trimmed poller is slow for one tick, not
188
+ * wrong. No cursor registry, no assumption about how far behind a shard can be.
189
+ * - Streaming-export / warehouse consumers hold opaque cursors issued outside
190
+ * this deployment. Nothing here can see them, so nothing here guesses:
191
+ * {@link readSqlCdcChanges} refuses a page below the floor rather than serving
192
+ * the surviving tail, and retention stays OFF unless an operator states a
193
+ * window they know covers their connector.
194
+ */
195
+ declare const sweepSqlCdcRetention: (exec: SqlCtxExec, dialect: SqlDialect, retentionMs: number, now: number) => Promise<void>;
196
+ /** Oldest `seq` still retained in the `.global()` changelog, or `undefined` when it is empty. */
197
+ declare const readSqlCdcFloor: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<number | undefined>;
198
+ /**
199
+ * Read changelog entries newer than `sinceSeq` in commit order, up to `limit`
200
+ * (clamped to [1, 10000]); plus the cursor to resume from.
201
+ */
137
202
  declare const readSqlCdcChanges: (exec: SqlCtxExec, options: {
138
203
  limit?: number;
139
204
  sinceSeq?: number;
@@ -141,34 +206,185 @@ declare const readSqlCdcChanges: (exec: SqlCtxExec, options: {
141
206
  changes: CdcChange[];
142
207
  cursor: number;
143
208
  }>;
144
- /** Drop changelog entries at or below a checkpointed `throughSeq` (retention). */
145
- declare const trimSqlCdcChanges: (exec: SqlCtxExec, throughSeq: number, dialect: SqlDialect) => Promise<void>;
209
+ /**
210
+ * Which tables the changelog recorded a write to after `sinceSeq`, plus the
211
+ * cursor to resume from. Metadata only — it reads no `doc`, so its cost is a
212
+ * grouped scan over an index range rather than the size of the documents in it.
213
+ *
214
+ * The `.global()` shape poll asks this once per tick for the whole shard, and a
215
+ * shape whose table is absent from the answer skips its membership read
216
+ * entirely.
217
+ *
218
+ * **One statement, and the cursor comes out of the same rows as the tables.**
219
+ * Reading the head separately — in either order — opens a window where a write
220
+ * commits between the two round trips and ends up absent from `tables` while
221
+ * sitting at or below the adopted `cursor`, which loses it for good. Deriving
222
+ * the cursor as the max `seq` actually returned closes that window by
223
+ * construction: the caller can never advance past a row this scan did not see.
224
+ *
225
+ * **What it does NOT close**, and the poll's resync interval is what covers it:
226
+ * Postgres and MySQL allocate `seq` from a sequence BEFORE commit, so a
227
+ * transaction holding a lower `seq` can commit after one holding a higher one.
228
+ * No single read can see the uncommitted row, so a change can land below a
229
+ * cursor already adopted. That change is invisible to the changelog probe until
230
+ * the next unconditional pass — bounded by `GLOBAL_SHAPE_RESYNC_MS`, which is
231
+ * the same bound already accepted for an out-of-band writer. D1 has no such
232
+ * window (single writer, and `withSession` gives both reads one snapshot).
233
+ */
234
+ declare const readSqlCdcChangedTables: (exec: SqlCtxExec, sinceSeq: number, dialect: SqlDialect, options?: {
235
+ cursorOnly?: boolean;
236
+ retained?: boolean;
237
+ }) => Promise<{
238
+ cursor: number;
239
+ floor?: number;
240
+ tables: string[];
241
+ }>;
146
242
  declare const createSqlCtxDb: (options: SqlCtxDbOptions) => DatabaseWriterLike;
243
+ /**
244
+ * Auto-provision every `.global()` table from the schema: `CREATE TABLE IF NOT
245
+ * EXISTS` with the physical `id`/`_creationTime` columns plus a typed column per
246
+ * declared field, then its secondary and `.unique()` indexes. This is the D1
247
+ * twin of `@lunora/do`'s `runShardMigrations` (which self-creates shard-local
248
+ * tables) — it makes the schema the single source of truth for global tables
249
+ * too, so a fresh database serves them without a hand-applied migration. The
250
+ * column set and dialect match exactly what this module reads and writes
251
+ * (`columnRef`, `serializeColumnValue`, `decodeGlobalRow`).
252
+ *
253
+ * Idempotent (`CREATE TABLE/INDEX IF NOT EXISTS`); additive only — it never
254
+ * drops or retypes an existing column, so destructive schema changes still need
255
+ * an explicit migration.
256
+ */
257
+ declare const runSqlGlobalTableMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
258
+ /**
259
+ * Materialize the `__agg_<index>` companion tables for every declared
260
+ * `aggregateIndex` on a global table. Global tables in Lunora ship their own
261
+ * DDL — counter tables are opt-in so production hosts can decide where they
262
+ * live. Tests and dev hosts can call this once after their schema migration to
263
+ * unlock O(1) counts.
264
+ *
265
+ * Idempotent (`CREATE TABLE IF NOT EXISTS`).
266
+ */
267
+ declare const runSqlAggregateMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
268
+ /**
269
+ * Materialize the `__rank_<index>` companion tables for every declared
270
+ * `rankIndex` on a global table. Mirrors `runSqlAggregateMigrations` — same
271
+ * opt-in pattern so production hosts decide whether to spend the DDL.
272
+ *
273
+ * Idempotent (`CREATE TABLE IF NOT EXISTS` + `createIndexIfNotExists`).
274
+ */
275
+ declare const runSqlRankMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
276
+ /** Reserved table holding one backfill-progress row per search companion. */
277
+ declare const SEARCH_STATE_TABLE = "__lunora_search_state";
278
+ /** Create the progress table. Idempotent; runs alongside the companion DDL. */
279
+ declare const migrateSearchState: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<void>;
147
280
  /** Map a JS value onto its SQLite storage form — SQLite has no boolean, so true/false → 1/0. */
148
- declare const sqliteEncode: (value: unknown) => unknown;
281
+ declare const sqliteEncode: (value: unknown, kind?: string) => unknown;
149
282
  /** Parse `raw` as JSON, returning `raw` unchanged when it is not valid JSON. */
150
283
  declare const tryJsonParse: (raw: string) => unknown;
151
- /** Decode a `bigint` column: a decimal string back into a `BigInt`, else verbatim. */
284
+ /**
285
+ * Decode a `bigint` column: the order-preserving key {@link bigintSqlKey} writes,
286
+ * or — for a row stored by a build that wrote plain decimal text — the decimal
287
+ * string, else verbatim.
288
+ */
152
289
  declare const decodeBigint: (raw: unknown) => unknown;
153
290
  /**
154
- * Resolve the *effective* storage kind of a column validator. Encoding keys off
155
- * the runtime value's JS type, so a `v.optional(inner)` column stores its
156
- * present value exactly as `inner` would. The validator's own `kind` is
157
- * `"optional"`, which hides that — unwrap to the inner validator's kind so the
158
- * decode reverses the real storage form. The inner validator is stashed on
159
- * `_meta.inner` by `@lunora/values`' `createValidator`.
160
- */
291
+ * Resolve the *effective* storage kind of a column validator: `v.optional(inner)`
292
+ * unwrapped to `inner`'s kind, since encoding keys off the runtime value's JS
293
+ * type and the validator's own `kind` of `"optional"` hides that.
294
+ *
295
+ * A thin alias over `shared/effective-kind`, which is where the rule lives so
296
+ * the DO row store applies the identical one — it reads the same validators and
297
+ * has the same failure mode, and two copies drifted the last time.
298
+ */
161
299
  declare const effectiveColumnKind: (validator: ValidatorLike) => string | undefined;
162
300
  /**
163
- * Inverse of {@link sqliteEncode}: map a SQLite storage value back onto its JS
164
- * form, driven by the field's effective validator `kind`:
165
- *
166
- * - `boolean`: 1/0 → true/false (SQLite has no boolean type).
167
- * - `bigint`: decimal string → `BigInt`.
168
- * - `object`/`array`/`record`: JSON string → parsed value.
169
- * - `union`/`any`: parsed back only when the stored string is a JSON non-scalar
170
- * (a scalar union member round-trips through SQLite's native column type).
171
- * - everything else (string/number/date/timestamp/id/literal): verbatim.
172
- */
301
+ * Inverse of {@link sqliteEncode}: map a SQLite storage value back onto its JS
302
+ * form, driven by the field's effective validator `kind`:
303
+ *
304
+ * - `boolean`: 1/0 → true/false (SQLite has no boolean type).
305
+ * - `bigint`: decimal string → `BigInt`.
306
+ * - `bytes`: normalizes any driver return shape to a genuine `ArrayBuffer` — a
307
+ * view (`Uint8Array`/`Buffer`/…) is sliced to its own byte window, a plain
308
+ * `ArrayBuffer` passes through. Required because `v.bytes()` validates
309
+ * `value instanceof ArrayBuffer` and different backends return different BLOB
310
+ * shapes (workerd D1 `ArrayBuffer`, node:sqlite `Uint8Array`, pg/mysql2 `Buffer`).
311
+ * - `object`/`array`/`record`/`geoPoint`: JSON string → parsed value. `geoPoint`
312
+ * belongs here because {@link sqliteEncode} keys off the runtime JS type and
313
+ * stores the `{ lat, lng }` object as JSON in a TEXT column; without the case
314
+ * it fell through to `default:` and every client read back the raw JSON text.
315
+ * - `union`/`any`/`from`: parsed back when the stored string is a JSON
316
+ * non-scalar, or when it carries the {@link WIRE_PREFIX} marker.
317
+ * A `number` or `boolean` does NOT round-trip through the column's native
318
+ * type — all three kinds store as TEXT on every engine, which coerces a bound
319
+ * `42` to the text `42.0` — so {@link sqliteEncode} writes those two in the
320
+ * marked form and this branch reverses them by the marker alone. A number's
321
+ * marked payload is `float64SqlKey`, so its text order is numeric order; the
322
+ * marked JSON an earlier build wrote is still read back here, which is what
323
+ * makes a table correct between the format change and the rewrite pass.
324
+ * `from` belongs to THIS group, not to `object`/`array`/`record`: an external
325
+ * Standard Schema can describe a string just as easily as an object, and
326
+ * {@link sqliteEncode} keys off the runtime JS type — so a `v.from(z.string())`
327
+ * column holding `"123"` is stored verbatim, and unconditional parsing would
328
+ * read it back as the NUMBER 123.
329
+ * A `bigint` member decodes too, by SHAPE: {@link sqliteEncode} keys off the
330
+ * runtime type, so a bigint here is stored as the same order-preserving key a
331
+ * declared `v.bigint()` column gets, and returning that verbatim handed the
332
+ * caller 40 characters of padding instead of the value it wrote. See the
333
+ * ambiguity this buys, and why the wire marker cannot carry it instead, below.
334
+ * CAVEAT: a union/any/from member is stored verbatim by {@link sqliteEncode}, so
335
+ * a legitimate *string* value that itself looks like JSON (`'{"a":1}'`, `'[1,2]'`)
336
+ * is ambiguous on read and decodes back to the parsed object/array, not the
337
+ * original string. The bigint test admits the same class of ambiguity, narrower
338
+ * but real: `decodeBigintSqlKey` accepts EXACTLY 40 characters — `"0"` or `"1"`,
339
+ * then 39 digits — and a stored *string* of that shape (a zero-padded account
340
+ * number, a numeric external id) is byte-identical on disk to a key and reads
341
+ * back as a `bigint`. Pinned by a test in `ctx-db.test.ts` so the trade is
342
+ * visible rather than rediscovered.
343
+ *
344
+ * Preferred anyway, because the alternative is not "no ambiguity" but
345
+ * guaranteed corruption: WITHOUT the test, every bigint any code writes to an
346
+ * untyped column comes back as padding, on every read. The narrow false
347
+ * positive costs a specific 40-character digit string its type; the absent test
348
+ * cost every such column its value.
349
+ *
350
+ * The unambiguous {@link WIRE_PREFIX} marker cannot carry this instead. It is
351
+ * not reached: {@link sqliteEncode} returns at its `bigint` branch first, and it
352
+ * takes no `kind`, so it cannot encode an untyped column differently from a
353
+ * declared one. It is also `serializeColumnValue`, which builds every WHERE
354
+ * binding — a prefixed bigint would never match a row stored as a key, and the
355
+ * padded key is order-preserving on purpose (indexes, range predicates, MIN/MAX
356
+ * all read it). Two storage forms for one runtime type breaks comparison
357
+ * between them. Disambiguating properly means tagging every encoded non-scalar,
358
+ * a breaking storage-format change, so it is documented rather than fixed.
359
+ * - everything else (string/number/date/timestamp/id/literal): verbatim.
360
+ */
173
361
  declare const sqliteDecode: (raw: unknown, kind: string | undefined) => unknown;
174
- export { type SqlCtxDbOptions, type SqlCtxExec, type SqlDialect, type SqlRunResult, createSqlCtxDb, decodeBigint, decodeGlobalRow, effectiveColumnKind, readSqlCdcChanges, runSqlAggregateMigrations, runSqlCdcMigration, runSqlGlobalTableMigrations, runSqlRankMigrations, runSqlSearchMigrations, sqliteDecode, sqliteEncode, trimSqlCdcChanges, tryJsonParse };
362
+ export { SEARCH_STATE_TABLE,
363
+ /**
364
+ * `@lunora/sql-store` — the internal, dialect-parameterized SQL store core
365
+ * shared by Lunora's `.global()` table backends.
366
+ *
367
+ * One ORM implementation (`createSqlCtxDb`) drives any SQL engine through a
368
+ * `SqlDialect`: SQLite via `@lunora/d1`, and Postgres/MySQL (PlanetScale, Neon,
369
+ * any Hyperdrive-reachable database) via `@lunora/hyperdrive`. Reactivity is
370
+ * unaffected — the writer is injected as `globalDb` into `createShardCtxDb`,
371
+ * whose `broadcast` hook drives live queries regardless of engine.
372
+ *
373
+ * This package is internal: consumers depend on `@lunora/d1` or
374
+ * `@lunora/hyperdrive`, which assemble the concrete dialect and wrap the core.
375
+ */
376
+ type SqlCtxDbOptions,
377
+ /**
378
+ * `@lunora/sql-store` — the internal, dialect-parameterized SQL store core
379
+ * shared by Lunora's `.global()` table backends.
380
+ *
381
+ * One ORM implementation (`createSqlCtxDb`) drives any SQL engine through a
382
+ * `SqlDialect`: SQLite via `@lunora/d1`, and Postgres/MySQL (PlanetScale, Neon,
383
+ * any Hyperdrive-reachable database) via `@lunora/hyperdrive`. Reactivity is
384
+ * unaffected — the writer is injected as `globalDb` into `createShardCtxDb`,
385
+ * whose `broadcast` hook drives live queries regardless of engine.
386
+ *
387
+ * This package is internal: consumers depend on `@lunora/d1` or
388
+ * `@lunora/hyperdrive`, which assemble the concrete dialect and wrap the core.
389
+ */
390
+ type SqlCtxExec, type SqlDialect, type SqlRunResult, backfillSqlSearchIndexes, createSqlCtxDb, decodeBigint, decodeGlobalRow, effectiveColumnKind, migrateSearchState, readSqlCdcChangedTables, readSqlCdcChanges, readSqlCdcFloor, runSqlAggregateMigrations, runSqlCdcMigration, runSqlGlobalTableMigrations, runSqlRankMigrations, runSqlSearchMigrations, sqliteDecode, sqliteEncode, sweepSqlCdcRetention, tryJsonParse };
package/dist/index.mjs CHANGED
@@ -1,2 +1 @@
1
- export { createSqlCtxDb, decodeGlobalRow, readSqlCdcChanges, runSqlAggregateMigrations, runSqlCdcMigration, runSqlGlobalTableMigrations, runSqlRankMigrations, runSqlSearchMigrations, trimSqlCdcChanges } from './packem_shared/createSqlCtxDb-BlGq23Wp.mjs';
2
- export { decodeBigint, effectiveColumnKind, sqliteDecode, sqliteEncode, tryJsonParse } from './packem_shared/decodeBigint-Dedu92k4.mjs';
1
+ import{createSqlCtxDb as a,readSqlCdcChangedTables as o,readSqlCdcChanges as t,readSqlCdcFloor as l,runSqlCdcMigration as s,sweepSqlCdcRetention as n}from"./packem_shared/createSqlCtxDb-BBn2L287.mjs";import{runSqlAggregateMigrations as d,runSqlGlobalTableMigrations as S,runSqlRankMigrations as c}from"./packem_shared/runSqlAggregateMigrations-D_MIiVIp.mjs";import{d as g,r as f}from"./packem_shared/ctx-db-search-CkWBiP2e.mjs";import{S as m,m as x}from"./packem_shared/ctx-db-search-state-Xa6UmA3s.mjs";import{f as p,e as u,b as h,s as M,t as T}from"./packem_shared/value-codec-DC_dHxUY.mjs";import{decodeGlobalRow as E}from"./packem_shared/decodeGlobalRow-D0xGl2NU.mjs";export{m as SEARCH_STATE_TABLE,g as backfillSqlSearchIndexes,a as createSqlCtxDb,p as decodeBigint,E as decodeGlobalRow,u as effectiveColumnKind,x as migrateSearchState,o as readSqlCdcChangedTables,t as readSqlCdcChanges,l as readSqlCdcFloor,d as runSqlAggregateMigrations,s as runSqlCdcMigration,S as runSqlGlobalTableMigrations,c as runSqlRankMigrations,f as runSqlSearchMigrations,h as sqliteDecode,M as sqliteEncode,n as sweepSqlCdcRetention,T as tryJsonParse};
@@ -0,0 +1 @@
1
+ import{S as t,a as S,m as c,r as l,b as i,w as s}from"./ctx-db-search-state-Xa6UmA3s.mjs";import"./decodeGlobalRow-D0xGl2NU.mjs";export{t as SEARCH_STATE_TABLE,S as clearSearchBackfillState,c as migrateSearchState,l as readSearchBackfillState,i as readSearchIndexCoverage,s as writeSearchBackfillState};
@@ -0,0 +1 @@
1
+ import"./ctx-db-search-state-Xa6UmA3s.mjs";import{d as S,c as s,a as i,r as l}from"./ctx-db-search-CkWBiP2e.mjs";import"./decodeGlobalRow-D0xGl2NU.mjs";export{S as backfillSqlSearchIndexes,s as createSearchSync,i as runSqlSearch,l as runSqlSearchMigrations};