@lunora/sql-store 1.0.0-alpha.9 → 1.0.0-alpha.91
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/LICENSE.md +6 -0
- package/README.md +2 -1
- package/dist/dialect.d.mts +136 -38
- package/dist/dialect.d.ts +136 -38
- package/dist/dialect.mjs +0 -1
- package/dist/index.d.mts +264 -121
- package/dist/index.d.ts +264 -121
- package/dist/index.mjs +1 -2
- package/dist/packem_shared/SEARCH_STATE_TABLE-DA3LgCT0.mjs +1 -0
- package/dist/packem_shared/backfillSqlSearchIndexes-CWhgwSCR.mjs +1 -0
- package/dist/packem_shared/createSqlCtxDb-Bn3b-5Wa.mjs +7 -0
- package/dist/packem_shared/ctx-db-search-DlItpO6A.mjs +1 -0
- package/dist/packem_shared/decodeBigint-B4wY6fcF.mjs +1 -0
- package/dist/packem_shared/decodeGlobalRow-BKIjk_Zk.mjs +1 -0
- package/package.json +3 -2
- package/dist/packem_shared/createSqlCtxDb-DYqDyPq8.mjs +0 -1754
- package/dist/packem_shared/decodeBigint-Dedu92k4.mjs +0 -69
package/dist/index.d.mts
CHANGED
|
@@ -1,139 +1,202 @@
|
|
|
1
|
-
import { ServerDefaultContextLike, DatabaseWriterLike,
|
|
2
|
-
import {
|
|
1
|
+
import { TableDefinitionLike, SchemaLike, ServerDefaultContextLike, DatabaseWriterLike, CrossShardReadArgs, QueryPage, SchedulerLike, CdcChange, ValidatorLike } from '@lunora/shard-engine';
|
|
2
|
+
import { SqlRunResult, SqlDialect } from "./dialect.mjs";
|
|
3
3
|
export type { SqlExec } from "./dialect.mjs";
|
|
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 → decimal string), 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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
35
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
127
|
+
* Scheduler exposed to global-table trigger handlers as `ctx.scheduler`.
|
|
128
|
+
* Absent it, `ctx.scheduler` is a stub that throws on use — pass one when
|
|
129
|
+
* triggers on `.global()` tables need to enqueue follow-up work.
|
|
130
|
+
*/
|
|
73
131
|
scheduler?: SchedulerLike;
|
|
74
132
|
schema: SchemaLike;
|
|
75
133
|
}
|
|
76
134
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
|
|
88
|
-
|
|
135
|
+
* Auto-provision every `.global()` table from the schema: `CREATE TABLE IF NOT
|
|
136
|
+
* EXISTS` with the physical `id`/`_creationTime` columns plus a typed column per
|
|
137
|
+
* declared field, then its secondary and `.unique()` indexes. This is the D1
|
|
138
|
+
* twin of `@lunora/do`'s `runShardMigrations` (which self-creates shard-local
|
|
139
|
+
* tables) — it makes the schema the single source of truth for global tables
|
|
140
|
+
* too, so a fresh database serves them without a hand-applied migration. The
|
|
141
|
+
* column set and dialect match exactly what this module reads and writes
|
|
142
|
+
* (`columnRef`, `serializeColumnValue`, `decodeGlobalRow`).
|
|
143
|
+
*
|
|
144
|
+
* Idempotent (`CREATE TABLE/INDEX IF NOT EXISTS`); additive only — it never
|
|
145
|
+
* drops or retypes an existing column, so destructive schema changes still need
|
|
146
|
+
* an explicit migration.
|
|
147
|
+
*/
|
|
89
148
|
declare const runSqlGlobalTableMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
|
|
90
149
|
/**
|
|
91
|
-
* Materialize the `__agg_
|
|
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
|
-
*/
|
|
150
|
+
* Materialize the `__agg_<index>` companion tables for every declared
|
|
151
|
+
* `aggregateIndex` on a global table. Global tables in Lunora ship their own
|
|
152
|
+
* DDL — counter tables are opt-in so production hosts can decide where they
|
|
153
|
+
* live. Tests and dev hosts can call this once after their schema migration to
|
|
154
|
+
* unlock O(1) counts.
|
|
155
|
+
*
|
|
156
|
+
* Idempotent (`CREATE TABLE IF NOT EXISTS`).
|
|
157
|
+
*/
|
|
99
158
|
declare const runSqlAggregateMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
|
|
100
159
|
/**
|
|
101
|
-
* Materialize the `__rank_
|
|
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
|
-
*/
|
|
160
|
+
* Materialize the `__rank_<index>` companion tables for every declared
|
|
161
|
+
* `rankIndex` on a global table. Mirrors `runSqlAggregateMigrations` — same
|
|
162
|
+
* opt-in pattern so production hosts decide whether to spend the DDL.
|
|
163
|
+
*
|
|
164
|
+
* Idempotent (`CREATE TABLE IF NOT EXISTS` + `createIndexIfNotExists`).
|
|
165
|
+
*/
|
|
107
166
|
declare const runSqlRankMigrations: (exec: SqlCtxExec, schema: SchemaLike, dialect: SqlDialect) => Promise<void>;
|
|
108
|
-
/**
|
|
109
|
-
* Materialize the `__fts_<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
167
|
/** Create the `__cdc_log` table. Idempotent; only run when CDC is enabled. */
|
|
132
168
|
declare const runSqlCdcMigration: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<void>;
|
|
133
169
|
/**
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
|
|
170
|
+
* Delete `.global()` changelog entries older than `retentionMs`, at most
|
|
171
|
+
* {@link GLOBAL_CDC_SWEEP_MAX_ROWS} per pass, and only if this writer wins the lease.
|
|
172
|
+
*
|
|
173
|
+
* **Time, not rows.** The shard-local twin bounds its log by row count because
|
|
174
|
+
* one shard owns it and a row count is a memory bound on that one object. This
|
|
175
|
+
* log is shared, so a row count means nothing to any individual consumer — but
|
|
176
|
+
* "older than N" is directly what every consumer needs to reason about, and it
|
|
177
|
+
* is the unit an operator can compare against their own connector's lag.
|
|
178
|
+
*
|
|
179
|
+
* **Which consumers this can strand, and what protects each:**
|
|
180
|
+
*
|
|
181
|
+
* - `.global()` shape pollers hold in-memory cursors this store cannot see. They
|
|
182
|
+
* are protected EXACTLY rather than approximately: {@link readSqlCdcChangedTables}
|
|
183
|
+
* reports the retained floor, and a poller below it treats the tick as "no
|
|
184
|
+
* visibility" and re-reads every shape. That is the same self-healing path a
|
|
185
|
+
* changelog error already takes, so a trimmed poller is slow for one tick, not
|
|
186
|
+
* wrong. No cursor registry, no assumption about how far behind a shard can be.
|
|
187
|
+
* - Streaming-export / warehouse consumers hold opaque cursors issued outside
|
|
188
|
+
* this deployment. Nothing here can see them, so nothing here guesses:
|
|
189
|
+
* {@link readSqlCdcChanges} refuses a page below the floor rather than serving
|
|
190
|
+
* the surviving tail, and retention stays OFF unless an operator states a
|
|
191
|
+
* window they know covers their connector.
|
|
192
|
+
*/
|
|
193
|
+
declare const sweepSqlCdcRetention: (exec: SqlCtxExec, dialect: SqlDialect, retentionMs: number, now: number) => Promise<void>;
|
|
194
|
+
/** Oldest `seq` still retained in the `.global()` changelog, or `undefined` when it is empty. */
|
|
195
|
+
declare const readSqlCdcFloor: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<number | undefined>;
|
|
196
|
+
/**
|
|
197
|
+
* Read changelog entries newer than `sinceSeq` in commit order, up to `limit`
|
|
198
|
+
* (clamped to [1, 10000]); plus the cursor to resume from.
|
|
199
|
+
*/
|
|
137
200
|
declare const readSqlCdcChanges: (exec: SqlCtxExec, options: {
|
|
138
201
|
limit?: number;
|
|
139
202
|
sinceSeq?: number;
|
|
@@ -141,9 +204,44 @@ declare const readSqlCdcChanges: (exec: SqlCtxExec, options: {
|
|
|
141
204
|
changes: CdcChange[];
|
|
142
205
|
cursor: number;
|
|
143
206
|
}>;
|
|
144
|
-
/**
|
|
145
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Which tables the changelog recorded a write to after `sinceSeq`, plus the
|
|
209
|
+
* cursor to resume from. Metadata only — it reads no `doc`, so its cost is a
|
|
210
|
+
* grouped scan over an index range rather than the size of the documents in it.
|
|
211
|
+
*
|
|
212
|
+
* The `.global()` shape poll asks this once per tick for the whole shard, and a
|
|
213
|
+
* shape whose table is absent from the answer skips its membership read
|
|
214
|
+
* entirely.
|
|
215
|
+
*
|
|
216
|
+
* **One statement, and the cursor comes out of the same rows as the tables.**
|
|
217
|
+
* Reading the head separately — in either order — opens a window where a write
|
|
218
|
+
* commits between the two round trips and ends up absent from `tables` while
|
|
219
|
+
* sitting at or below the adopted `cursor`, which loses it for good. Deriving
|
|
220
|
+
* the cursor as the max `seq` actually returned closes that window by
|
|
221
|
+
* construction: the caller can never advance past a row this scan did not see.
|
|
222
|
+
*
|
|
223
|
+
* **What it does NOT close**, and the poll's resync interval is what covers it:
|
|
224
|
+
* Postgres and MySQL allocate `seq` from a sequence BEFORE commit, so a
|
|
225
|
+
* transaction holding a lower `seq` can commit after one holding a higher one.
|
|
226
|
+
* No single read can see the uncommitted row, so a change can land below a
|
|
227
|
+
* cursor already adopted. That change is invisible to the changelog probe until
|
|
228
|
+
* the next unconditional pass — bounded by `GLOBAL_SHAPE_RESYNC_MS`, which is
|
|
229
|
+
* the same bound already accepted for an out-of-band writer. D1 has no such
|
|
230
|
+
* window (single writer, and `withSession` gives both reads one snapshot).
|
|
231
|
+
*/
|
|
232
|
+
declare const readSqlCdcChangedTables: (exec: SqlCtxExec, sinceSeq: number, dialect: SqlDialect, options?: {
|
|
233
|
+
cursorOnly?: boolean;
|
|
234
|
+
retained?: boolean;
|
|
235
|
+
}) => Promise<{
|
|
236
|
+
cursor: number;
|
|
237
|
+
floor?: number;
|
|
238
|
+
tables: string[];
|
|
239
|
+
}>;
|
|
146
240
|
declare const createSqlCtxDb: (options: SqlCtxDbOptions) => DatabaseWriterLike;
|
|
241
|
+
/** Reserved table holding one backfill-progress row per search companion. */
|
|
242
|
+
declare const SEARCH_STATE_TABLE = "__lunora_search_state";
|
|
243
|
+
/** Create the progress table. Idempotent; runs alongside the companion DDL. */
|
|
244
|
+
declare const migrateSearchState: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<void>;
|
|
147
245
|
/** Map a JS value onto its SQLite storage form — SQLite has no boolean, so true/false → 1/0. */
|
|
148
246
|
declare const sqliteEncode: (value: unknown) => unknown;
|
|
149
247
|
/** Parse `raw` as JSON, returning `raw` unchanged when it is not valid JSON. */
|
|
@@ -151,24 +249,69 @@ declare const tryJsonParse: (raw: string) => unknown;
|
|
|
151
249
|
/** Decode a `bigint` column: a decimal string back into a `BigInt`, else verbatim. */
|
|
152
250
|
declare const decodeBigint: (raw: unknown) => unknown;
|
|
153
251
|
/**
|
|
154
|
-
* Resolve the *effective* storage kind of a column validator.
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
|
|
252
|
+
* Resolve the *effective* storage kind of a column validator: `v.optional(inner)`
|
|
253
|
+
* unwrapped to `inner`'s kind, since encoding keys off the runtime value's JS
|
|
254
|
+
* type and the validator's own `kind` of `"optional"` hides that.
|
|
255
|
+
*
|
|
256
|
+
* A thin alias over `shared/effective-kind`, which is where the rule lives so
|
|
257
|
+
* the DO row store applies the identical one — it reads the same validators and
|
|
258
|
+
* has the same failure mode, and two copies drifted the last time.
|
|
259
|
+
*/
|
|
161
260
|
declare const effectiveColumnKind: (validator: ValidatorLike) => string | undefined;
|
|
162
261
|
/**
|
|
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
|
-
* - `
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
|
|
262
|
+
* Inverse of {@link sqliteEncode}: map a SQLite storage value back onto its JS
|
|
263
|
+
* form, driven by the field's effective validator `kind`:
|
|
264
|
+
*
|
|
265
|
+
* - `boolean`: 1/0 → true/false (SQLite has no boolean type).
|
|
266
|
+
* - `bigint`: decimal string → `BigInt`.
|
|
267
|
+
* - `bytes`: normalizes any driver return shape to a genuine `ArrayBuffer` — a
|
|
268
|
+
* view (`Uint8Array`/`Buffer`/…) is sliced to its own byte window, a plain
|
|
269
|
+
* `ArrayBuffer` passes through. Required because `v.bytes()` validates
|
|
270
|
+
* `value instanceof ArrayBuffer` and different backends return different BLOB
|
|
271
|
+
* shapes (workerd D1 `ArrayBuffer`, node:sqlite `Uint8Array`, pg/mysql2 `Buffer`).
|
|
272
|
+
* - `object`/`array`/`record`: JSON string → parsed value.
|
|
273
|
+
* - `union`/`any`/`from`: parsed back only when the stored string is a JSON
|
|
274
|
+
* non-scalar (a scalar member round-trips through SQLite's native column type).
|
|
275
|
+
* `from` belongs to THIS group, not to `object`/`array`/`record`: an external
|
|
276
|
+
* Standard Schema can describe a string just as easily as an object, and
|
|
277
|
+
* {@link sqliteEncode} keys off the runtime JS type — so a `v.from(z.string())`
|
|
278
|
+
* column holding `"123"` is stored verbatim, and unconditional parsing would
|
|
279
|
+
* read it back as the NUMBER 123.
|
|
280
|
+
* CAVEAT: a union/any/from member is stored verbatim by {@link sqliteEncode}, so
|
|
281
|
+
* a legitimate *string* value that itself looks like JSON (`'{"a":1}'`, `'[1,2]'`)
|
|
282
|
+
* is ambiguous on read and decodes back to the parsed object/array, not the
|
|
283
|
+
* original string. This is inherent to sharing one TEXT column between a string
|
|
284
|
+
* and an object member; disambiguating would require a breaking storage-format
|
|
285
|
+
* change (tagging encoded non-scalars), so it is documented rather than fixed.
|
|
286
|
+
* - everything else (string/number/date/timestamp/id/literal): verbatim.
|
|
287
|
+
*/
|
|
173
288
|
declare const sqliteDecode: (raw: unknown, kind: string | undefined) => unknown;
|
|
174
|
-
export {
|
|
289
|
+
export { SEARCH_STATE_TABLE,
|
|
290
|
+
/**
|
|
291
|
+
* `@lunora/sql-store` — the internal, dialect-parameterized SQL store core
|
|
292
|
+
* shared by Lunora's `.global()` table backends.
|
|
293
|
+
*
|
|
294
|
+
* One ORM implementation (`createSqlCtxDb`) drives any SQL engine through a
|
|
295
|
+
* `SqlDialect`: SQLite via `@lunora/d1`, and Postgres/MySQL (PlanetScale, Neon,
|
|
296
|
+
* any Hyperdrive-reachable database) via `@lunora/hyperdrive`. Reactivity is
|
|
297
|
+
* unaffected — the writer is injected as `globalDb` into `createShardCtxDb`,
|
|
298
|
+
* whose `broadcast` hook drives live queries regardless of engine.
|
|
299
|
+
*
|
|
300
|
+
* This package is internal: consumers depend on `@lunora/d1` or
|
|
301
|
+
* `@lunora/hyperdrive`, which assemble the concrete dialect and wrap the core.
|
|
302
|
+
*/
|
|
303
|
+
type SqlCtxDbOptions,
|
|
304
|
+
/**
|
|
305
|
+
* `@lunora/sql-store` — the internal, dialect-parameterized SQL store core
|
|
306
|
+
* shared by Lunora's `.global()` table backends.
|
|
307
|
+
*
|
|
308
|
+
* One ORM implementation (`createSqlCtxDb`) drives any SQL engine through a
|
|
309
|
+
* `SqlDialect`: SQLite via `@lunora/d1`, and Postgres/MySQL (PlanetScale, Neon,
|
|
310
|
+
* any Hyperdrive-reachable database) via `@lunora/hyperdrive`. Reactivity is
|
|
311
|
+
* unaffected — the writer is injected as `globalDb` into `createShardCtxDb`,
|
|
312
|
+
* whose `broadcast` hook drives live queries regardless of engine.
|
|
313
|
+
*
|
|
314
|
+
* This package is internal: consumers depend on `@lunora/d1` or
|
|
315
|
+
* `@lunora/hyperdrive`, which assemble the concrete dialect and wrap the core.
|
|
316
|
+
*/
|
|
317
|
+
type SqlCtxExec, type SqlDialect, type SqlRunResult, backfillSqlSearchIndexes, createSqlCtxDb, decodeBigint, decodeGlobalRow, effectiveColumnKind, migrateSearchState, readSqlCdcChangedTables, readSqlCdcChanges, readSqlCdcFloor, runSqlAggregateMigrations, runSqlCdcMigration, runSqlGlobalTableMigrations, runSqlRankMigrations, runSqlSearchMigrations, sqliteDecode, sqliteEncode, sweepSqlCdcRetention, tryJsonParse };
|