@lunora/sql-store 1.0.0-alpha.14 → 1.0.0-alpha.140

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