@lunora/sql-store 1.0.0-alpha.107 → 1.0.0-alpha.109
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/dialect.d.mts +12 -1
- package/dist/dialect.d.ts +12 -1
- package/dist/index.d.mts +69 -6
- package/dist/index.d.ts +69 -6
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/{SEARCH_STATE_TABLE-DB6-HNEc.mjs → SEARCH_STATE_TABLE-DpqGt5X6.mjs} +1 -1
- package/dist/packem_shared/backfillSqlSearchIndexes-NAtRFn30.mjs +1 -0
- package/dist/packem_shared/createSqlCtxDb-BIFOgsTf.mjs +7 -0
- package/dist/packem_shared/{ctx-db-search-BT7H8AM6.mjs → ctx-db-search-3QrN4xRN.mjs} +1 -1
- package/dist/packem_shared/{ctx-db-search-state-DRx8DyOa.mjs → ctx-db-search-state-C0hUwzYJ.mjs} +1 -1
- package/dist/packem_shared/{decodeBigint-CAz4a7qU.mjs → decodeBigint-D9Ak-rky.mjs} +1 -1
- package/dist/packem_shared/decodeGlobalRow-B6zmyAi5.mjs +1 -0
- package/dist/packem_shared/runSqlAggregateMigrations-DA8EVLuX.mjs +1 -0
- package/dist/packem_shared/value-codec-nCDJEXH_.mjs +1 -0
- package/package.json +3 -3
- package/dist/packem_shared/OCC_VERSION_COLUMN-ByC64Jgx.mjs +0 -1
- package/dist/packem_shared/backfillSqlSearchIndexes-CpZUw3h9.mjs +0 -1
- package/dist/packem_shared/createSqlCtxDb-3s6Cc5vc.mjs +0 -7
- package/dist/packem_shared/decodeGlobalRow-C76NMu9P.mjs +0 -1
- package/dist/packem_shared/runSqlAggregateMigrations-BAp7JaB-.mjs +0 -1
- package/dist/packem_shared/value-codec-DMP5JYuK.mjs +0 -1
package/dist/dialect.d.mts
CHANGED
|
@@ -63,8 +63,19 @@ interface SqlDialect {
|
|
|
63
63
|
* decimal string), and `LONGTEXT` for everything else — strings unbounded
|
|
64
64
|
* so they never truncate, composites because their wire-marked form is not
|
|
65
65
|
* valid JSON and a `JSON` column would reject it on insert.
|
|
66
|
+
*
|
|
67
|
+
* `unique` says the column carries a `.unique()` constraint, so the type has
|
|
68
|
+
* to be one the engine can index in FULL. It exists for MySQL: InnoDB cannot
|
|
69
|
+
* index a `LONGTEXT` without a key prefix, and a prefixed UNIQUE index
|
|
70
|
+
* enforces uniqueness of the PREFIX — two distinct 200-character emails
|
|
71
|
+
* sharing their first 191 characters collided as a duplicate. A bounded
|
|
72
|
+
* `VARCHAR` indexes whole, so the constraint means what it says; a value
|
|
73
|
+
* past the bound is a loud write error rather than a wrong conflict. SQLite
|
|
74
|
+
* and Postgres index text of any length and ignore the flag.
|
|
66
75
|
*/
|
|
67
|
-
columnType: (kind: string | undefined
|
|
76
|
+
columnType: (kind: string | undefined, options?: {
|
|
77
|
+
unique?: boolean;
|
|
78
|
+
}) => string;
|
|
68
79
|
/**
|
|
69
80
|
* Engine SQL types for the **internal companion tables** (aggregate / rank /
|
|
70
81
|
* CDC), which are built from raw SQL types, not validator kinds. SQLite uses
|
package/dist/dialect.d.ts
CHANGED
|
@@ -63,8 +63,19 @@ interface SqlDialect {
|
|
|
63
63
|
* decimal string), and `LONGTEXT` for everything else — strings unbounded
|
|
64
64
|
* so they never truncate, composites because their wire-marked form is not
|
|
65
65
|
* valid JSON and a `JSON` column would reject it on insert.
|
|
66
|
+
*
|
|
67
|
+
* `unique` says the column carries a `.unique()` constraint, so the type has
|
|
68
|
+
* to be one the engine can index in FULL. It exists for MySQL: InnoDB cannot
|
|
69
|
+
* index a `LONGTEXT` without a key prefix, and a prefixed UNIQUE index
|
|
70
|
+
* enforces uniqueness of the PREFIX — two distinct 200-character emails
|
|
71
|
+
* sharing their first 191 characters collided as a duplicate. A bounded
|
|
72
|
+
* `VARCHAR` indexes whole, so the constraint means what it says; a value
|
|
73
|
+
* past the bound is a loud write error rather than a wrong conflict. SQLite
|
|
74
|
+
* and Postgres index text of any length and ignore the flag.
|
|
66
75
|
*/
|
|
67
|
-
columnType: (kind: string | undefined
|
|
76
|
+
columnType: (kind: string | undefined, options?: {
|
|
77
|
+
unique?: boolean;
|
|
78
|
+
}) => string;
|
|
68
79
|
/**
|
|
69
80
|
* Engine SQL types for the **internal companion tables** (aggregate / rank /
|
|
70
81
|
* CDC), which are built from raw SQL types, not validator kinds. SQLite uses
|
package/dist/index.d.mts
CHANGED
|
@@ -123,6 +123,41 @@ interface SqlCtxDbOptions {
|
|
|
123
123
|
* key cap when omitted.
|
|
124
124
|
*/
|
|
125
125
|
maxRelationKeys?: number;
|
|
126
|
+
/**
|
|
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;
|
|
126
161
|
/**
|
|
127
162
|
* Scheduler exposed to global-table trigger handlers as `ctx.scheduler`.
|
|
128
163
|
* Absent it, `ctx.scheduler` is a stub that throws on use — pass one when
|
|
@@ -243,7 +278,7 @@ declare const SEARCH_STATE_TABLE = "__lunora_search_state";
|
|
|
243
278
|
/** Create the progress table. Idempotent; runs alongside the companion DDL. */
|
|
244
279
|
declare const migrateSearchState: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<void>;
|
|
245
280
|
/** Map a JS value onto its SQLite storage form — SQLite has no boolean, so true/false → 1/0. */
|
|
246
|
-
declare const sqliteEncode: (value: unknown) => unknown;
|
|
281
|
+
declare const sqliteEncode: (value: unknown, kind?: string) => unknown;
|
|
247
282
|
/** Parse `raw` as JSON, returning `raw` unchanged when it is not valid JSON. */
|
|
248
283
|
declare const tryJsonParse: (raw: string) => unknown;
|
|
249
284
|
/**
|
|
@@ -277,19 +312,47 @@ declare const effectiveColumnKind: (validator: ValidatorLike) => string | undefi
|
|
|
277
312
|
* belongs here because {@link sqliteEncode} keys off the runtime JS type and
|
|
278
313
|
* stores the `{ lat, lng }` object as JSON in a TEXT column; without the case
|
|
279
314
|
* it fell through to `default:` and every client read back the raw JSON text.
|
|
280
|
-
* - `union`/`any`/`from`: parsed back
|
|
281
|
-
* non-scalar
|
|
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.
|
|
282
321
|
* `from` belongs to THIS group, not to `object`/`array`/`record`: an external
|
|
283
322
|
* Standard Schema can describe a string just as easily as an object, and
|
|
284
323
|
* {@link sqliteEncode} keys off the runtime JS type — so a `v.from(z.string())`
|
|
285
324
|
* column holding `"123"` is stored verbatim, and unconditional parsing would
|
|
286
325
|
* read it back as the NUMBER 123.
|
|
326
|
+
* A `bigint` member decodes too, by SHAPE: {@link sqliteEncode} keys off the
|
|
327
|
+
* runtime type, so a bigint here is stored as the same order-preserving key a
|
|
328
|
+
* declared `v.bigint()` column gets, and returning that verbatim handed the
|
|
329
|
+
* caller 40 characters of padding instead of the value it wrote. See the
|
|
330
|
+
* ambiguity this buys, and why the wire marker cannot carry it instead, below.
|
|
287
331
|
* CAVEAT: a union/any/from member is stored verbatim by {@link sqliteEncode}, so
|
|
288
332
|
* a legitimate *string* value that itself looks like JSON (`'{"a":1}'`, `'[1,2]'`)
|
|
289
333
|
* is ambiguous on read and decodes back to the parsed object/array, not the
|
|
290
|
-
* original string.
|
|
291
|
-
*
|
|
292
|
-
*
|
|
334
|
+
* original string. The bigint test admits the same class of ambiguity, narrower
|
|
335
|
+
* but real: `decodeBigintSqlKey` accepts EXACTLY 40 characters — `"0"` or `"1"`,
|
|
336
|
+
* then 39 digits — and a stored *string* of that shape (a zero-padded account
|
|
337
|
+
* number, a numeric external id) is byte-identical on disk to a key and reads
|
|
338
|
+
* back as a `bigint`. Pinned by a test in `ctx-db.test.ts` so the trade is
|
|
339
|
+
* visible rather than rediscovered.
|
|
340
|
+
*
|
|
341
|
+
* Preferred anyway, because the alternative is not "no ambiguity" but
|
|
342
|
+
* guaranteed corruption: WITHOUT the test, every bigint any code writes to an
|
|
343
|
+
* untyped column comes back as padding, on every read. The narrow false
|
|
344
|
+
* positive costs a specific 40-character digit string its type; the absent test
|
|
345
|
+
* cost every such column its value.
|
|
346
|
+
*
|
|
347
|
+
* The unambiguous {@link WIRE_PREFIX} marker cannot carry this instead. It is
|
|
348
|
+
* not reached: {@link sqliteEncode} returns at its `bigint` branch first, and it
|
|
349
|
+
* takes no `kind`, so it cannot encode an untyped column differently from a
|
|
350
|
+
* declared one. It is also `serializeColumnValue`, which builds every WHERE
|
|
351
|
+
* binding — a prefixed bigint would never match a row stored as a key, and the
|
|
352
|
+
* padded key is order-preserving on purpose (indexes, range predicates, MIN/MAX
|
|
353
|
+
* all read it). Two storage forms for one runtime type breaks comparison
|
|
354
|
+
* between them. Disambiguating properly means tagging every encoded non-scalar,
|
|
355
|
+
* a breaking storage-format change, so it is documented rather than fixed.
|
|
293
356
|
* - everything else (string/number/date/timestamp/id/literal): verbatim.
|
|
294
357
|
*/
|
|
295
358
|
declare const sqliteDecode: (raw: unknown, kind: string | undefined) => unknown;
|
package/dist/index.d.ts
CHANGED
|
@@ -123,6 +123,41 @@ interface SqlCtxDbOptions {
|
|
|
123
123
|
* key cap when omitted.
|
|
124
124
|
*/
|
|
125
125
|
maxRelationKeys?: number;
|
|
126
|
+
/**
|
|
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;
|
|
126
161
|
/**
|
|
127
162
|
* Scheduler exposed to global-table trigger handlers as `ctx.scheduler`.
|
|
128
163
|
* Absent it, `ctx.scheduler` is a stub that throws on use — pass one when
|
|
@@ -243,7 +278,7 @@ declare const SEARCH_STATE_TABLE = "__lunora_search_state";
|
|
|
243
278
|
/** Create the progress table. Idempotent; runs alongside the companion DDL. */
|
|
244
279
|
declare const migrateSearchState: (exec: SqlCtxExec, dialect: SqlDialect) => Promise<void>;
|
|
245
280
|
/** Map a JS value onto its SQLite storage form — SQLite has no boolean, so true/false → 1/0. */
|
|
246
|
-
declare const sqliteEncode: (value: unknown) => unknown;
|
|
281
|
+
declare const sqliteEncode: (value: unknown, kind?: string) => unknown;
|
|
247
282
|
/** Parse `raw` as JSON, returning `raw` unchanged when it is not valid JSON. */
|
|
248
283
|
declare const tryJsonParse: (raw: string) => unknown;
|
|
249
284
|
/**
|
|
@@ -277,19 +312,47 @@ declare const effectiveColumnKind: (validator: ValidatorLike) => string | undefi
|
|
|
277
312
|
* belongs here because {@link sqliteEncode} keys off the runtime JS type and
|
|
278
313
|
* stores the `{ lat, lng }` object as JSON in a TEXT column; without the case
|
|
279
314
|
* it fell through to `default:` and every client read back the raw JSON text.
|
|
280
|
-
* - `union`/`any`/`from`: parsed back
|
|
281
|
-
* non-scalar
|
|
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.
|
|
282
321
|
* `from` belongs to THIS group, not to `object`/`array`/`record`: an external
|
|
283
322
|
* Standard Schema can describe a string just as easily as an object, and
|
|
284
323
|
* {@link sqliteEncode} keys off the runtime JS type — so a `v.from(z.string())`
|
|
285
324
|
* column holding `"123"` is stored verbatim, and unconditional parsing would
|
|
286
325
|
* read it back as the NUMBER 123.
|
|
326
|
+
* A `bigint` member decodes too, by SHAPE: {@link sqliteEncode} keys off the
|
|
327
|
+
* runtime type, so a bigint here is stored as the same order-preserving key a
|
|
328
|
+
* declared `v.bigint()` column gets, and returning that verbatim handed the
|
|
329
|
+
* caller 40 characters of padding instead of the value it wrote. See the
|
|
330
|
+
* ambiguity this buys, and why the wire marker cannot carry it instead, below.
|
|
287
331
|
* CAVEAT: a union/any/from member is stored verbatim by {@link sqliteEncode}, so
|
|
288
332
|
* a legitimate *string* value that itself looks like JSON (`'{"a":1}'`, `'[1,2]'`)
|
|
289
333
|
* is ambiguous on read and decodes back to the parsed object/array, not the
|
|
290
|
-
* original string.
|
|
291
|
-
*
|
|
292
|
-
*
|
|
334
|
+
* original string. The bigint test admits the same class of ambiguity, narrower
|
|
335
|
+
* but real: `decodeBigintSqlKey` accepts EXACTLY 40 characters — `"0"` or `"1"`,
|
|
336
|
+
* then 39 digits — and a stored *string* of that shape (a zero-padded account
|
|
337
|
+
* number, a numeric external id) is byte-identical on disk to a key and reads
|
|
338
|
+
* back as a `bigint`. Pinned by a test in `ctx-db.test.ts` so the trade is
|
|
339
|
+
* visible rather than rediscovered.
|
|
340
|
+
*
|
|
341
|
+
* Preferred anyway, because the alternative is not "no ambiguity" but
|
|
342
|
+
* guaranteed corruption: WITHOUT the test, every bigint any code writes to an
|
|
343
|
+
* untyped column comes back as padding, on every read. The narrow false
|
|
344
|
+
* positive costs a specific 40-character digit string its type; the absent test
|
|
345
|
+
* cost every such column its value.
|
|
346
|
+
*
|
|
347
|
+
* The unambiguous {@link WIRE_PREFIX} marker cannot carry this instead. It is
|
|
348
|
+
* not reached: {@link sqliteEncode} returns at its `bigint` branch first, and it
|
|
349
|
+
* takes no `kind`, so it cannot encode an untyped column differently from a
|
|
350
|
+
* declared one. It is also `serializeColumnValue`, which builds every WHERE
|
|
351
|
+
* binding — a prefixed bigint would never match a row stored as a key, and the
|
|
352
|
+
* padded key is order-preserving on purpose (indexes, range predicates, MIN/MAX
|
|
353
|
+
* all read it). Two storage forms for one runtime type breaks comparison
|
|
354
|
+
* between them. Disambiguating properly means tagging every encoded non-scalar,
|
|
355
|
+
* a breaking storage-format change, so it is documented rather than fixed.
|
|
293
356
|
* - everything else (string/number/date/timestamp/id/literal): verbatim.
|
|
294
357
|
*/
|
|
295
358
|
declare const sqliteDecode: (raw: unknown, kind: string | undefined) => unknown;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createSqlCtxDb as a,readSqlCdcChangedTables as o,readSqlCdcChanges as t,readSqlCdcFloor as l,runSqlCdcMigration as s,sweepSqlCdcRetention as n}from"./packem_shared/createSqlCtxDb-
|
|
1
|
+
import{createSqlCtxDb as a,readSqlCdcChangedTables as o,readSqlCdcChanges as t,readSqlCdcFloor as l,runSqlCdcMigration as s,sweepSqlCdcRetention as n}from"./packem_shared/createSqlCtxDb-BIFOgsTf.mjs";import{runSqlAggregateMigrations as d,runSqlGlobalTableMigrations as S,runSqlRankMigrations as c}from"./packem_shared/runSqlAggregateMigrations-DA8EVLuX.mjs";import{d as g,r as C}from"./packem_shared/ctx-db-search-3QrN4xRN.mjs";import{S as m,m as x}from"./packem_shared/ctx-db-search-state-C0hUwzYJ.mjs";import{c as b,e as u,a as h,s as M,t as T}from"./packem_shared/value-codec-nCDJEXH_.mjs";import{decodeGlobalRow as E}from"./packem_shared/decodeGlobalRow-B6zmyAi5.mjs";export{m as SEARCH_STATE_TABLE,g as backfillSqlSearchIndexes,a as createSqlCtxDb,b 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,C as runSqlSearchMigrations,h as sqliteDecode,M as sqliteEncode,n as sweepSqlCdcRetention,T as tryJsonParse};
|
package/dist/packem_shared/{SEARCH_STATE_TABLE-DB6-HNEc.mjs → SEARCH_STATE_TABLE-DpqGt5X6.mjs}
RENAMED
|
@@ -1 +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-
|
|
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-C0hUwzYJ.mjs";import"./decodeGlobalRow-B6zmyAi5.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-C0hUwzYJ.mjs";import{d as S,c as s,a as i,r as l}from"./ctx-db-search-3QrN4xRN.mjs";import"./decodeGlobalRow-B6zmyAi5.mjs";export{S as backfillSqlSearchIndexes,s as createSearchSync,i as runSqlSearch,l as runSqlSearchMigrations};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import{LunoraError as T}from"@lunora/errors";import{m as qt,D as Ot}from"./ctx-db-search-state-C0hUwzYJ.mjs";import{c as Wt,r as Ft,A as Ut,C as xt,R as Pt,a as Ht,T as Kt,x as jt,b as Gt}from"./ctx-db-search-3QrN4xRN.mjs";import{d as fi}from"./ctx-db-search-3QrN4xRN.mjs";import{literalInList as Vt,throwingScheduler as Yt,CDC_LOG_TABLE as G,cursorBelowRetainedFloor as Xt,cdcTrimmedError as zt,assertNoExplicitUndefined as Ze,hasTrigger as Qt,runRowValidators as Ne,assertFlatPredicate as et,mergeWhere as q,rankTableName as de,sortColumnName as $e,resolveRankPartition as tt,encodePartitionKey as Ie,resolveRankSeekTuple as Jt,RANK_TIEBREAK as _e,CountRlsUnsupportedError as nt,normalizeIdStructurally as Zt,assertValidClientId as en,aggregateSqlFunction as le,softDeleteScope as ue,compileWhereSql as fe,normalizeOrderKeys as tn,buildSeekWhere as nn,decodeCursor as on,resolveRelationPredicates as it,resolveWith as ot,relationHooks as rt,applySelect as st,encodeCursor as rn,NotFoundError as sn,applyOnDelete as an,ConflictError as Ce,normalizeCountArgument as cn,selectIndexForCount as dn,encodeAggregateKey as Y,aggregateTableName as Q,selectIndexForAggregate as ln,readAggregateValue as De,runTriggers as un,matchesRankStaticWhere as at,rankPivotConditionSql as yt,CURSOR_PREFIX as fn,selectIndexForGroupBy as wn,tiebreakDirectionFor as $n,fanOutScalarCounts as _n,matchesStaticWhere as Be,foldAggregateTally as ct,coerceAggregateNumber as qe,CDC_LOG_TABLE_SEQ_INDEX as hn,NotUniqueError as En}from"@lunora/shard-engine";import{sql as e}from"drizzle-orm";import{d as pt,e as Rt,n as yn,b as pn}from"./value-codec-nCDJEXH_.mjs";import{runSqlGlobalTableMigrations as Rn,runSqlAggregateMigrations as Sn,runSqlRankMigrations as gn}from"./runSqlAggregateMigrations-DA8EVLuX.mjs";import{queryAll as I,queryRun as k,createIndexIfNotExists as Tn,decodeRow as Z,serializeDocumentColumn as be,serializeColumnValue as we,columnRefSql as O,decodeRows as dt,tableColumns as St,OCC_VERSION_COLUMN as lt,forEachRowPaged as ut,BACKFILL_BATCH_SIZE as Oe,queryBatch as ft}from"./decodeGlobalRow-B6zmyAi5.mjs";import{decodeGlobalRow as $i}from"./decodeGlobalRow-B6zmyAi5.mjs";const vn=(a,c)=>{if(a.size<c)return;const _=a.keys().next().value;_!==void 0&&a.delete(_)},wt=50,mn=(a,c={})=>{const _=c.maxBatch??wt,r=Number.isInteger(_)&&_>0?_:wt,p=new Map;let $=!1;const u=async(g,v)=>{const m=[...v.keys()];for(let M=0;M<m.length;M+=r){const N=m.slice(M,M+r);try{const W=await a(g,N);for(const V of N){const P=W.get(V);for(const Ae of v.get(V)??[])Ae.resolve(P)}}catch(W){for(const V of N)for(const P of v.get(V)??[])P.reject(W)}}},b=()=>{$=!1;const g=[...p.entries()];p.clear();for(const[v,m]of g)u(v,m).catch(()=>{})};return{async load(g,v){return new Promise((m,M)=>{let N=p.get(g);N||(N=new Map,p.set(g,N));const W=N.get(v);W?W.push({reject:M,resolve:m}):N.set(v,[{reject:M,resolve:m}]),$||($=!0,queueMicrotask(b))})}}},In=new Set(["_id","id"]),xe=(a,c,_)=>a==="postgres"?e`${c} IS NOT DISTINCT FROM ${_}`:a==="mysql"?e`${c} <=> ${_}`:e`${c} IS ${_}`,gt=(a,c)=>a.name!=="postgres"||c.nullable!==!0?"":c.direction==="desc"?" NULLS LAST":" NULLS FIRST",bn=(a,c)=>{const _=a.map(r=>e`${O(r.field)} ${e.raw(`${r.direction==="desc"?"DESC":"ASC"}${gt(c,r)}`)}`);if(!a.some(r=>In.has(r.field))){const r=$n(a);_.push(e`${O("id")} ${e.raw(r==="desc"?"DESC":"ASC")}`)}return e.join(_,e`, `)},$t=50,Cn=(a,c,_)=>{const r={...c};for(const[p,$]of St(a)){if($.serverDefault){r[p]=$.serverDefault({auth:_});continue}r[p]===void 0&&($.defaultFn?r[p]=$.defaultFn():"defaultValue"in $&&(r[p]=$.defaultValue))}return r},_t=(a,c,_,r)=>{for(const[p,$]of St(a)){if($.serverDefault){p in c&&(_[p]=$.serverDefault({auth:r}));continue}$.onUpdateFn&&!(p in c)&&(_[p]=$.onUpdateFn())}},An=128,Ln=()=>{const a=new Map;return{delete:c=>{a.delete(c)},get:c=>{const _=a.get(c);if(_!==void 0)return a.delete(c),a.set(c,_),_},set:(c,_)=>{a.has(c)?a.delete(c):vn(a,An),a.set(c,_)}}},kn=async(a,c,_,r,p)=>{const $=p.get(r);if($!==void 0)return $;const u=Object.entries(_.tables).filter(([,g])=>g.shardMode===void 0||g.shardMode.kind==="global").map(([g])=>g),b=await Promise.all(u.map(async g=>({found:(await I(a,c,e`SELECT 1 FROM ${e.identifier(g)} WHERE ${e.identifier("id")} = ${r} LIMIT 1`)).length>0,tableName:g})));for(const g of b)if(g.found)return p.set(r,g.tableName),g.tableName},ee=a=>a==null?null:Number(a),Mn=(a,c)=>c.map(_=>{const r={};for(const p of a)r[p]=_[p]??null;return{key:r,value:ee(_.value)}}),Nn=(a,c,_)=>[...a.partitionBy??[],...a.sortBy.map(p=>p.field),...a.where?Object.keys(a.where):[]].every(p=>c[p]===_[p]),Tt=a=>a.length===1?a[0]:e`(${e.join(a,e` AND `)})`,J=a=>e.join(a.map(c=>e`${e.identifier(c)}`),e`, `),te=a=>e.join(a.map(c=>e`${c}`),e`, `),Dn=(a,c,_,r,p)=>{const $=[];for(let u=0;u<_.length+1;u+=1){const b=_[u],g=c.sortBy[u],v=u<_.length&&b!==void 0&&g!==void 0,m=g?.direction==="desc"?"desc":"asc",M=v?yt(b,r[b],m,!1):e`${e.identifier(_e)} < ${p}`;if(M===void 0)continue;const N=_.slice(0,u).map(W=>xe(a,e`${e.identifier(W)}`,r[W]));N.push(M),$.push(Tt(N))}return $.length>0?e.join($,e` OR `):e`1 = 0`},Bn=(a,c,_)=>{if(_.length!==c.length)return;const r=[];for(const[p,$]of c.entries()){const u=yt($.column,_[p],$.direction,!0);if(u===void 0)continue;const b=[];for(let g=0;g<p;g+=1){const v=c[g];v!==void 0&&b.push(xe(a,e`${e.identifier(v.column)}`,_[g]))}b.push(u),r.push(Tt(b))}return r.length===0?e`(1 = 0)`:e`(${e.join(r,e` OR `)})`},qn=(a,c)=>{if(a.sortBy.length===0)throw new T("INTERNAL",`rankIndex "${a.name}" requires at least one "sortBy" column for stable pagination`);const _=[{column:"__partition__",direction:"asc",nullable:!1}];for(const[r,p]of a.sortBy.entries())_.push({column:c[r]??$e(r),direction:p.direction,nullable:!0});return _.push({column:_e,direction:"asc",nullable:!1}),_},On=async(a,c,_,r,p)=>{const u=[];for(let m=0;m<p.length;m+=50)u.push(p.slice(m,m+50));const b=await Promise.all(u.map(async m=>I(a,c,e`SELECT * FROM ${e.identifier(r)} WHERE ${e.identifier("id")} IN (${te(m)})`))),g=new Map;for(const m of b)for(const M of m)g.set(M.id,M);const v=[];for(const m of p){const M=Z(_,g.get(m));M&&v.push(M)}return v},Wn=a=>{const c=JSON.stringify(a),_=new TextEncoder().encode(c);let r="";for(const p of _)r+=String.fromCodePoint(p);return fn+btoa(r)},Fe=(a,c,_)=>{const r=a.shape[c];if(r!==void 0&&Rt(r)==="bigint")throw new T("BAD_REQUEST",`${_}: "${c}" is stored as an order-preserving key, which SQL cannot reduce or group — declare an aggregateIndex covering this (by, field, op) so the maintained companion answers it instead (its running total is a REAL, so it stays exact only while the total is inside 2^53)`)},Fn=a=>{if(a===void 0)return!1;const c=Rt(a);return c==="any"||c==="bigint"||c==="from"||c==="union"},Un=(a,c,_,r)=>{for(const p of _)Fe(a,p,`groupBy(${c}, { by: [..."${p}"] })`);r.field!==void 0&&Fe(a,r.field,`groupBy(${c}, { agg: { op: "${r.op}", field: "${r.field}" } })`)},Ue="__cdc_sweep",vt="global",xn=6e4,Pn=1e4,Hn=3e4;let ht=0;const Kn=async(a,c)=>{const{autoincrementPrimaryKey:_,key:r,real:p,text:$}=c.companionTypes;await k(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(G)} (${e.identifier("seq")} ${e.raw(_)}, ${e.identifier("ts")} ${e.raw(p)} NOT NULL, ${e.identifier("table")} ${e.raw(r)} NOT NULL, ${e.identifier("id")} ${e.raw(r)} NOT NULL, ${e.identifier("op")} ${e.raw(r)} NOT NULL, ${e.identifier("doc")} ${e.raw($)})`);const u=c.indexKeyPrefix?.("string");await Tn(a,c,{columns:e`${u===void 0?e`${e.identifier("table")}`:e`${e.identifier("table")}(${e.raw(String(u))})`}, ${e.identifier("seq")}`,name:hn,table:G,unique:!1}),await k(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(Ue)} (${e.identifier("id")} ${e.raw(r)} NOT NULL PRIMARY KEY, ${e.identifier("lease_until")} ${e.raw(p)} NOT NULL)`),await k(a,c,e`INSERT INTO ${e.identifier(Ue)} (${e.identifier("id")}, ${e.identifier("lease_until")}) VALUES (${vt}, ${0}) ${c.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${e.identifier("id")} = ${e.identifier("id")}`:e`ON CONFLICT(${e.identifier("id")}) DO NOTHING`}`)},jn=async(a,c,_)=>{const r=e`UPDATE ${e.identifier(Ue)} SET ${e.identifier("lease_until")} = ${_+xn} WHERE ${e.identifier("id")} = ${vt} AND ${e.identifier("lease_until")} <= ${_}`;if(c.supportsReturning)return(await I(a,c,e`${r} RETURNING ${e.identifier("id")}`)).length>0;const p=await k(a,c,r);return(c.affectedRows?c.affectedRows(p??{rowsAffected:0}):0)>0},Gn=async(a,c,_,r)=>{if(!await jn(a,c,r))return;const p=r-_;await k(a,c,e`
|
|
2
|
+
DELETE FROM ${e.identifier(G)} WHERE ${e.identifier("seq")} IN (
|
|
3
|
+
SELECT ${e.identifier("seq")} FROM (
|
|
4
|
+
SELECT ${e.identifier("seq")} FROM ${e.identifier(G)} WHERE ${e.identifier("ts")} <= ${p} ORDER BY ${e.identifier("seq")} ASC LIMIT ${e.raw(String(Pn))}
|
|
5
|
+
) AS ${e.identifier("expired")}
|
|
6
|
+
)
|
|
7
|
+
`)},mt=async(a,c)=>{const _=await I(a,c,e`SELECT MIN(${e.identifier("seq")}) AS ${e.identifier("seq")} FROM ${e.identifier(G)}`),r=Number(_[0]?.seq??Number.NaN);return Number.isFinite(r)?r:void 0},Vn=a=>JSON.stringify(yn(a)?pn(a):a),Yn=async(a,c,_,r,p,$,u)=>{await k(a,u,e`INSERT INTO ${e.identifier(G)} (${e.identifier("ts")}, ${e.identifier("table")}, ${e.identifier("id")}, ${e.identifier("op")}, ${e.identifier("doc")}) VALUES (${c}, ${_}, ${r}, ${p}, ${$===void 0?null:Vn($)})`)},ci=async(a,c,_)=>{const r=c.sinceSeq??0,p=Math.max(1,Math.min(c.limit??1e3,1e4)),$=await mt(a,_);if($!==void 0&&Xt($,r))throw zt($,r,"global");const b=(await I(a,_,e`SELECT seq, ts, ${e.identifier("table")}, id, op, doc FROM ${e.identifier(G)} WHERE seq > ${r} ORDER BY seq ASC LIMIT ${e.raw(String(p))}`)).map(g=>{const{doc:v}=g,m={id:String(g.id),op:String(g.op),seq:Number(g.seq),table:String(g.table),ts:Number(g.ts)};return typeof v=="string"?{...m,doc:pt(JSON.parse(v))}:m});return{changes:b,cursor:b.at(-1)?.seq??r}},Xn=async(a,c,_,r={})=>{if(r.cursorOnly){const g=await I(a,_,e`SELECT MAX(seq) AS seq FROM ${e.identifier(G)}`),v=Number(g[0]?.seq??c);return{cursor:Number.isFinite(v)?v:c,tables:[]}}const p=await I(a,_,e`SELECT ${e.identifier("table")} AS ${e.identifier("table")}, MAX(seq) AS seq FROM ${e.identifier(G)} WHERE seq > ${c} GROUP BY ${e.identifier("table")}`);let $=c;const u=[];for(const g of p){const v=Number(g.seq);Number.isFinite(v)&&v>$&&($=v),u.push(String(g.table))}const b=r.retained===!0?await mt(a,_):void 0;return{cursor:$,tables:u,...b===void 0?{}:{floor:b}}},zn=/string or blob too big/iu,Qn=/row is too big/iu,Jn=(a,c)=>{const{code:_,errno:r}=c,p=c instanceof Error?c.message:"";switch(a.name){case"mysql":return r===1118||_==="ER_TOO_BIG_ROWSIZE"?"InnoDB's per-row ceiling — roughly 8 KB, half a 16 KB page, for the part of the row stored inline":void 0;case"postgres":return Qn.test(p)?"the 8 KB heap page a tuple must fit once its wide columns have been TOASTed out":void 0;default:return zn.test(p)?"the storage engine's per-row ceiling (2 MB on D1)":void 0}},Zn=(a,c,_)=>{const r=Jn(a,c);if(r!==void 0)throw new T("PAYLOAD_TOO_LARGE",`document is too large to store in "${_}": a single row cannot exceed ${r}. The limit is on the STORED bytes, which are UTF-8. Keep the payload in R2 (ctx.storage) and store a reference on the row.`)},Et=(a,c,_)=>{throw a.isUniqueViolation(c)?new Ce(`unique constraint violation on "${_}"`,"unique"):(Zn(a,c,_),c)},We=new WeakMap,di=a=>{const{crossShardCounter:c,crossShardReader:_,exec:r,maxRelationKeys:p,schema:$}=a,{dialect:u}=a,b={fieldRef:O,serialize:we,...u.name==="mysql"?{containsExpr:(t,n)=>e`LOCATE(${n}, ${t}) > 0`}:{},...u.name==="postgres"?{containsExpr:(t,n)=>e`strpos(${t}, ${n}) > 0`}:{},...u.name==="sqlite"?{}:{inList:Vt}},g=(t,n)=>xe(u.name,t,n),v=t=>{const n=J(t.columns),i=te(t.values),o=l=>u.name==="mysql"?e`VALUES(${e.identifier(l)})`:e`excluded.${e.identifier(l)}`,s=l=>u.name==="postgres"?e`${e.identifier(t.table)}.${e.identifier(l)}`:e`${e.identifier(l)}`,f=e.join(Object.entries(t.set(o,s)).map(([l,E])=>e`${e.identifier(l)} = ${E}`),e`, `),h=u.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${f}`:e`ON CONFLICT(${e.identifier(t.conflictKey)}) DO UPDATE SET ${f}`;return e`INSERT INTO ${e.identifier(t.table)} (${n}) VALUES (${i}) ${h}`},m=a.clock??(()=>Date.now()),M=a.idGenerator??(()=>crypto.randomUUID()),N=a.cdc??!1,W=typeof a.cdcRetentionMs=="number"&&Number.isFinite(a.cdcRetentionMs)&&a.cdcRetentionMs>0?a.cdcRetentionMs:void 0,V=a.auth??{identity:null,userId:null},P=async(t,n,i,o)=>{if(!N||(await Yn(r,m(),t,n,i,o,u),W===void 0))return;const s=m();if(!(s-ht<Hn)){ht=s;try{await Gn(r,u,W,s)}catch{}}},Ae=a.scheduler??Yt,Le=Ln();let he=0;const ke=a.provisionScope??{},U=async()=>{const t=We.get(ke);if(t)return t;const n=(async()=>{await Rn(r,$,u),await Sn(r,$,u),await gn(r,$,u),await qt(r,u),await Ft(r,$,u),N&&await Kn(r,u)})().catch(i=>{throw We.delete(ke),i});return We.set(ke,n),n},ne=async(t,n)=>{await U();const i=await kn(r,u,$,t,Le);if(!(n!==void 0&&i!==n))return i},ie=new Map,Pe=async(t,n)=>{const i=Q(t,n);return(await I(r,u,u.tableExists(i))).length>0},Ee=async(t,n)=>{const i=`${t}::${n.name}`,o=ie.get(i);if(o!==void 0)return o;if(!await Pe(t,n.name))return ie.set(i,!1),!1;const f=$.tables[t];if(!f)return ie.set(i,!1),!1;const h=n.by??[],l=new Map;await ut(r,u,f,t,w=>{if(n.where&&!Be(w,n.where))return;const y=Y(h,w);ct(l,y,n,w)});const E=Q(t,n.name);await k(r,u,e`DELETE FROM ${e.identifier(E)}`);const d=[];for(const[w,y]of l)d.push(e`INSERT INTO ${e.identifier(E)} (${e.identifier("__key__")}, ${e.identifier("__value__")}, ${e.identifier("__count__")}) VALUES (${w}, ${y.value}, ${y.count})`);return await ft(r,u,d),ie.set(i,!0),!0},It=async(t,n,i,o)=>{const s=new Map;let f,h=!0;for(;h;){const l=f===void 0?o:[...o,e`${e.identifier("id")} > ${f}`],E=l.length>0?e` WHERE ${e.join(l,e` AND `)}`:e``,d=await I(r,u,e`SELECT ${e.identifier("id")}, ${O(i.field??"")} FROM ${e.identifier(n)}${E} ORDER BY ${e.identifier("id")} ASC LIMIT ${e.raw(String(Oe))}`);for(const w of dt(t,d))ct(s,"",i,w);f=d.at(-1)?.id,h=d.length===Oe}return s.get("")?.value??null},bt=async(t,n,i)=>{const o=n.field??"",s=[];for(const d of n.by??[]){const w=we(i[d]??null);s.push(w===null?e`${O(d)} IS NULL`:e`${O(d)} = ${w}`)}for(const[d,w]of Object.entries(n.where??{})){const y=w!==null&&typeof w=="object"&&!Array.isArray(w)?w.eq:w,R=we(y);s.push(R===null?e`${O(d)} IS NULL`:e`${O(d)} = ${R}`)}const f=s.length>0?e` WHERE ${e.join(s,e` AND `)}`:e``,h=$.tables[t];if(h&&Fn(h.shape[o]))return It(h,t,n,s);const l=le(n.op),E=await I(r,u,e`SELECT ${e.raw(l)}(${O(o)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}${f}`);return ee(E[0]?.value)},Ct=async(t,n)=>{await k(r,u,e`DELETE FROM ${e.identifier(t)} WHERE ${e.identifier("__key__")} = ${n} AND ${e.identifier("__count__")} <= 0`)},At=async(t,n,i,o)=>{const s=Q(t,n.name),{op:f}=n,h=n.field??"",l=i&&(!n.where||Be(i,n.where))?i:void 0,E=o&&(!n.where||Be(o,n.where))?o:void 0;if(!(!l&&!E)){if(f==="count"||f==="sum"||f==="avg"){const d=new Set;for(const[w,y]of[[l,-1],[E,1]]){if(!w)continue;const R=f==="count"?1:qe(w[h]);if(R===void 0)continue;const S=Y(n.by??[],w);d.add(S),await k(r,u,v({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(C,A)=>({__count__:e`${A("__count__")} + ${C("__count__")}`,__value__:f==="count"?e`${A("__value__")} + ${C("__value__")}`:e`COALESCE(${A("__value__")}, 0) + ${C("__value__")}`}),table:s,values:[S,y*R,y]}))}for(const w of d)await Ct(s,w);return}if(l){const d=Y(n.by??[],l),w=qe(l[h]),R=(await I(r,u,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${d}`))[0],S=ee(R?.value),C=(R?.count??0)-1;if(C<=0)await k(r,u,e`DELETE FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${d}`);else if(R&&w!==void 0&&S!==null&&w===S){const A=await bt(t,n,l);await k(r,u,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__value__")} = ${A}, ${e.identifier("__count__")} = ${C} WHERE ${e.identifier("__key__")} = ${d}`)}else await k(r,u,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__count__")} = ${e.identifier("__count__")} - 1 WHERE ${e.identifier("__key__")} = ${d}`)}if(E){const d=Y(n.by??[],E),w=qe(E[h]);if(w===void 0)await k(r,u,v({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(y,R)=>({__count__:e`${R("__count__")} + 1`}),table:s,values:[d,null,1]}));else{const y=f==="min"?"MIN":"MAX";await k(r,u,v({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(R,S)=>({__count__:e`${S("__count__")} + 1`,__value__:e`${e.raw(y)}(COALESCE(${S("__value__")}, ${R("__value__")}), ${R("__value__")})`}),table:s,values:[d,w,1]}))}}}},ye=async t=>{const n=$.tables[t]?.aggregateIndexes;if(!(!n||n.length===0))for(const i of n)await Ee(t,i)},oe=async(t,n,i)=>{const o=$.tables[t]?.aggregateIndexes;if(!(!o||o.length===0))for(const s of o){const f=`${t}::${s.name}`;(ie.get(f)??await Pe(t,s.name))&&await At(t,s,n,i)}},re=new Map,He=async(t,n)=>{const i=de(t,n);return(await I(r,u,u.tableExists(i))).length>0},Me=async(t,n)=>{const i=`${t}::rank::${n.name}`,o=re.get(i);if(o!==void 0)return o;if(!await He(t,n.name))return re.set(i,!1),!1;const f=$.tables[t];if(!f)return re.set(i,!1),!1;const h=de(t,n.name);await k(r,u,e`DELETE FROM ${e.identifier(h)}`);const l=n.sortBy.map((y,R)=>$e(R)),E=J(["__id__","__partition__",...l]);let d=[];const w=async()=>{if(d.length===0)return;const y=d;d=[],await ft(r,u,y)};return await ut(r,u,f,t,async y=>{if(n.where&&!at(y,n.where))return;const R=Ie(n.partitionBy??[],y),S=n.sortBy.map(C=>we(y[C.field]??null));d.push(e`INSERT INTO ${e.identifier(h)} (${E}) VALUES (${te([y._id,R,...S])})`),d.length>=Oe&&await w()}),await w(),re.set(i,!0),!0},pe=async t=>{const n=$.tables[t]?.rankIndexes;if(!(!n||n.length===0))for(const i of n)await Me(t,i)},X=async(t,n,i,o)=>{const s=$.tables[t],f=s?.rankIndexes;if(!f||f.length===0)return;const h=s?.softDeleteMode?.field,l=h===void 0||o===void 0?void 0:o[h],E=l!=null;for(const d of f){if(i!==void 0&&o!==void 0&&Nn(d,i,o))continue;const w=`${t}::rank::${d.name}`;if(!(re.get(w)??await He(t,d.name)))continue;const S=de(t,d.name);if(i&&await k(r,u,e`DELETE FROM ${e.identifier(S)} WHERE ${e.identifier("__id__")} = ${n}`),o&&!E){if(d.where&&!at(o,d.where))continue;const C=d.sortBy.map((B,j)=>$e(j)),A=J(["__id__","__partition__",...C]),F=Ie(d.partitionBy??[],o),L=d.sortBy.map(B=>we(o[B.field]??null));await k(r,u,e`INSERT INTO ${e.identifier(S)} (${A}) VALUES (${te([n,F,...L])})`)}}},se=Wt({dialect:u,exec:r,schema:$}),Ke=new Set;for(const[t,n]of Object.entries($.tables))for(const i of Object.values(n.triggerMap??{}))Ke.add(`${t} ${i.timing} ${i.op}`);const H=(t,n,i)=>Ke.has(`${t} ${n} ${i}`);let je;const K=async(t,n,i)=>{if(he+=1,he>$t)throw he-=1,new Ce(`trigger recursion exceeded ${String($t)} levels on "${i.table}" — check for a self-triggering write`,"trigger");try{await un({ctx:je,event:i,op:n,schema:$,tableName:i.table,timing:t})}finally{he-=1}},Lt=async(t,n)=>{try{await k(r,u,n)}catch(i){Et(u,i,t)}},Re=async(t,n)=>(await I(r,u,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} = ${n}`))[0],Se=async(t,n,i,o)=>{if(o===void 0)return;const s=e`${e.identifier(lt)}`,f=e.join([g(e`${e.identifier("id")}`,o.id),g(s,o[lt]??null)],e` AND `),h=n==="UPDATE"?e`UPDATE ${e.identifier(t)} SET ${i}, ${s} = COALESCE(${s}, 0) + 1 WHERE ${f}`:e`DELETE FROM ${e.identifier(t)} WHERE ${f}`,l=()=>{throw new Ce(`optimistic concurrency conflict on "${t}" — the row changed during this mutation; refetch and retry`,"occ")};try{if(u.supportsReturning)(await I(r,u,e`${h} RETURNING ${e.identifier("id")}`)).length===0&&l();else{const E=await k(r,u,h);(u.affectedRows?u.affectedRows(E??{rowsAffected:0}):0)===0&&l()}}catch(E){Et(u,E,t)}},kt=(t,n,i,o)=>{const s=Object.keys(t.shape);return{columns:["id","_creationTime",...s],values:[n,i,...s.map(f=>be(t,f,o[f]??null))]}},Mt=async(t,n,i,o)=>{const s=wn(n,i.op,i.field,o.by,o.where);if(!s||!await Ee(t,s.index))return;const h=Q(t,s.index.name),l=Object.keys(s.partial);if(l.length===(s.index.by??[]).length&&l.length>0){const d=Y(s.index.by??[],s.partial),w=await I(r,u,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(h)} WHERE ${e.identifier("__key__")} = ${d}`);if(w.length===0)return[];const y=w[0];return[{key:{...s.partial},value:De(i.op,{count:y.count,value:ee(y.value)})}]}return l.length>0?void 0:(await I(r,u,e`SELECT ${e.identifier("__key__")} AS ${e.identifier("key")}, ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(h)}`)).map(d=>{const w=d;return{key:pt(JSON.parse(w.key)),value:De(i.op,{count:w.count,value:ee(w.value)})}})},Ge=t=>{const n=$.tables[t]?.shardMode?.kind;return n==="shardBy"||n==="root"},Ve=t=>{throw new T("INTERNAL",`cross-backend relation: a global table cannot load the shard-local relation '${t}' (it spans every shard) — wire a cross-shard reader to support it`)},Nt=(t,n)=>{if(n?.relationMask!==void 0&&n.with!==void 0&&Object.keys(n.with).length>0)throw new T("MASK_UNSUPPORTED",`masking cannot follow a nested \`with\` across the cross-shard relation hop to "${t}" — the nested rows are hydrated on the serving shard, where the mask policy does not exist. Read the nested relation at its own (masked) call site instead.`);const i={};if(n?.relationBaseWhere)for(const o of Object.keys($.tables)){const s=n.relationBaseWhere(o);s!==void 0&&(i[o]=s)}return{orderBy:n?.orderBy,relationPolicies:i,where:q(n?.baseWhere,n?.where),with:n?.with}},Ye=(t,n)=>Ge(t)?_?_(t,Nt(t,n)):Ve(t):x.findMany(t,n),ge=(t,n,i)=>it(t,{fetcher:Ye,maxRelationKeys:p,relationBaseWhere:i,schema:$,tableName:n}),Dt=mn(async(t,n)=>{const i=await I(r,u,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} IN (${te(n)})`),o=new Map;for(const s of i){const{id:f}=s;typeof f=="string"&&o.set(f,s)}return o}),x={async cdcChangedTables(t,n){if(N)try{return await U(),await Xn(r,t,u,{...n,retained:W!==void 0})}catch{return}},async aggregate(t,n){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);if(await U(),le(n.op),n.op==="count")return x.count(t,{baseWhere:n.baseWhere,relationBaseWhere:n.relationBaseWhere,restrictsCounts:n.restrictsCounts,where:n.where});if(!n.field)throw new T("INTERNAL",`aggregate(${t}, { op: "${n.op}" }): "field" is required for non-count reducers`);const o=ue(i.softDeleteMode,void 0),s=q(q(n.baseWhere,n.where),o),f=await ge(s,t,n.relationBaseWhere),h=f!==s;if(i.aggregateIndexes&&!n.baseWhere&&!h&&!o){const R=ln(i.aggregateIndexes,n.op,n.field,n.where);if(R&&await Ee(t,R.index)){const C=Y(R.index.by??[],R.key),A=Q(t,R.index.name),L=(await I(r,u,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(A)} WHERE ${e.identifier("__key__")} = ${C}`))[0];return De(n.op,L===void 0?void 0:{count:L.count,value:ee(L.value)})}}Fe(i,n.field,`aggregate(${t}, { op: "${n.op}", field: "${n.field}" })`);const l=fe(f,b),E=e.raw(le(n.op)),d=e`SELECT ${E}(${O(n.field)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}`,y=(await I(r,u,l?e`${d} WHERE ${l}`:d))[0]?.value;return y==null?null:Number(y)},async count(t,n){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=cn(n);if(o.restrictsCounts)throw new nt(t);const s=ue(i.softDeleteMode,void 0),f=q(q(o.baseWhere,o.where),s),h=await ge(f,t,o.relationBaseWhere),l=h!==f;if(i.aggregateIndexes&&!o.baseWhere&&!l&&!s){const y=dn(i.aggregateIndexes,o.where);if(y&&await Ee(t,y.index)){const S=Y(y.index.by??[],y.key),C=Q(t,y.index.name),A=await I(r,u,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")} FROM ${e.identifier(C)} WHERE ${e.identifier("__key__")} = ${S}`);return Number(A[0]?.value??0)}}const E=fe(h,b),d=e`SELECT COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(t)}`,w=await I(r,u,E?e`${d} WHERE ${E}`:d);return Number(w[0]?.count??0)},async delete(t,n,i){const o=await ne(t,n);if(!o)return;const s=$.tables[o];if(!s)return;const f=await Re(o,t),h=Z(s,f),l=i?.hard===!0,E=!l&&s.softDeleteMode?s.softDeleteMode.field:void 0;if(!(E&&(!h||h[E]!==null&&h[E]!==void 0))){if(H(o,"before","delete")&&await K("before","delete",{id:t,op:"delete",previous:h??void 0,table:o}),await an({deletedId:t,deletedReference:d=>h?.[d],findHolders:async(d,w,y)=>{if($.tables[d]?.shardMode?.kind==="shardBy")throw new T("INTERNAL",`cross-backend cascade from global '${o}' into shardBy '${d}' is not supported — would require Query Coordinator fan-out across shards`);return(await x.findMany(d,{includeDeleted:l,where:{[w]:y}})).page},onCascade:(d,w)=>x.delete(w,void 0,i),onRestrict:d=>{throw new Ce(d,"restrict")},onSetNull:(d,w,y)=>x.patch(w,{[y]:null}),schema:$,tableName:o}),await ye(o),await pe(o),E&&h){const d={...h,[E]:m()},w=e.join(Object.keys(s.shape).map(y=>e`${e.identifier(y)} = ${be(s,y,d[y]??null)}`),e`, `);await Se(o,"UPDATE",w,f),await oe(o,h,d),await X(o,t,h,void 0),await se(o,t,d,h),await P(o,t,"update",d),H(o,"after","delete")&&await K("after","delete",{id:t,op:"delete",previous:h,table:o});return}await Se(o,"DELETE",void 0,f),Le.delete(t),await oe(o,h??void 0,void 0),await X(o,t,h??void 0,void 0),await se(o,t,void 0),await P(o,t,"delete"),H(o,"after","delete")&&await K("after","delete",{id:t,op:"delete",previous:h??void 0,table:o})}},async findFirst(t,n={}){return(await x.findMany(t,{...n,limit:1})).page[0]??null},async findFirstOrThrow(t,n={}){const i=await x.findFirst(t,n);if(i===null)throw new sn(`findFirstOrThrow: no "${t}" document matched`);return i},async findMany(t,n={}){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=tn(n.orderBy,i.shape),s=n.cursor?nn(o,on(n.cursor)):void 0,f=Ye,h=async(L,B,j,Te)=>{if(Ge(L))return c?_n(c,L,B,j,Te):Ve(L);const ve=$.tables[L];if(!ve)throw new T("INTERNAL",`unknown table: ${L}`);const me=ue(ve.softDeleteMode,void 0),z={[B]:{in:j}},D=q(q(z,Te),me),ae=await ge(D,L,void 0),Xe=fe(ae,b),ze=O(B);let ce=e`SELECT ${ze} AS __fk__, COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(L)}`;Xe&&(ce=e`${ce} WHERE ${Xe}`),ce=e`${ce} GROUP BY ${ze}`;const Bt=await I(r,u,ce),Qe=new Map;for(const Je of Bt)Qe.set(Je.__fk__,Number(Je.count??0));return Qe};let l=q(n.baseWhere,n.where);l=q(l,ue(i.softDeleteMode,n.includeDeleted)),l=await it(l,{fetcher:f,maxRelationKeys:p,relationBaseWhere:n.relationBaseWhere,schema:$,tableName:t}),s&&(l=l?{AND:[l,s]}:s);const E=fe(l,b),d=bn(o,u);let w=e`SELECT * FROM ${e.identifier(t)}`;E&&(w=e`${w} WHERE ${E}`),w=e`${w} ORDER BY ${d}`;const y=typeof n.limit=="number"?Math.max(0,Math.floor(n.limit)):void 0;y!==void 0&&(w=e`${w} LIMIT ${e.raw(String(y+1))}`);const R=await I(r,u,w),S=dt(i,R);if(y===void 0)return n.with&&await ot({fetcher:f,groupedCounter:h,parents:S,...rt(n),schema:$,tableName:t,with:n.with}),{continueCursor:null,isDone:!0,page:st(S,n.select,n.with)};const C=S.length>y,A=C?S.slice(0,y):S,F=A.at(-1);return n.with&&await ot({fetcher:f,groupedCounter:h,parents:A,...rt(n),schema:$,tableName:t,with:n.with}),{continueCursor:C&&F?rn(F,o):null,isDone:!C,page:st(A,n.select,n.with)}},async get(t,n){const i=await ne(t,n);if(!i)return null;const o=$.tables[i];return o?Z(o,await Dt.load(i,t)):null},async groupBy(t,n){const i=$.tables[t];if(!i)throw new T("INTERNAL",`unknown table: ${t}`);await U();const o=n.agg??{op:"count"};if(le(o.op),o.op!=="count"&&!o.field)throw new T("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);const s=ue(i.softDeleteMode,void 0),f=q(q(n.baseWhere,n.where),s),h=await ge(f,t,n.relationBaseWhere),l=h!==f;if(i.aggregateIndexes&&!n.baseWhere&&!l&&!s){const S=await Mt(t,i.aggregateIndexes,o,n);if(S!==void 0)return S}Un(i,t,n.by,o);const E=fe(h,b),d=n.by.map(S=>e`${O(S)} AS ${e.identifier(S)}`);if(o.op==="count")d.push(e`COUNT(*) AS ${e.identifier("value")}`);else{if(!o.field)throw new T("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);d.push(e`${e.raw(le(o.op))}(${O(o.field)}) AS ${e.identifier("value")}`)}const w=e.join(n.by.map(S=>O(S)),e`, `);let y=e`SELECT ${e.join(d,e`, `)} FROM ${e.identifier(t)}`;E&&(y=e`${y} WHERE ${E}`),y=e`${y} GROUP BY ${w}`;const R=await I(r,u,y);return Mn(n.by,R)},async insert(t,n,i){const o=$.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);await U();const s=Cn(o,n,V);Ne(o,s);let f,h=!0;i?.clientId!==void 0?(en(i.clientId),f=i.clientId):i?.allowExplicitId&&typeof s._id=="string"?f=s._id:(f=M(),h=!1);const l=i?.allowExplicitId&&typeof s._creationTime=="number"?s._creationTime:m(),E={...s,_creationTime:l,_id:f};H(t,"before","insert")&&await K("before","insert",{doc:{...E},id:f,op:"insert",table:t}),await ye(t),await pe(t);const{columns:d,values:w}=kt(o,f,l,s);return await Lt(t,e`INSERT INTO ${e.identifier(t)} (${J(d)}) VALUES (${te(w)})`),h&&Le.set(f,t),await oe(t,void 0,E),await X(t,f,void 0,E),await se(t,f,E),await P(t,f,"insert",E),H(t,"after","insert")&&await K("after","insert",{doc:E,id:f,op:"insert",table:t}),f},normalizeId(t,n){return Zt($,t,n)},async patch(t,n,i){Ze("patch",n);const o=await ne(t,i);if(!o)throw new T("INTERNAL",`document not found: ${t}`);const s=$.tables[o];if(!s)throw new T("INTERNAL",`document not found: ${t}`);const f=await Re(o,t),h=Z(s,f);if(!h)throw new T("INTERNAL",`document not found: ${t}`);const l={...h,...n,_id:t};_t(s,n,l,V),Ne(s,l,!0),H(o,"before","update")&&await K("before","update",{doc:{...l},id:t,op:"update",previous:h,table:o}),await ye(o),await pe(o);const E=Object.keys(s.shape),d=e.join(E.map(w=>e`${e.identifier(w)} = ${be(s,w,l[w]??null)}`),e`, `);await Se(o,"UPDATE",d,f),await oe(o,h,l),await X(o,t,h,l),await se(o,t,l,h),await P(o,t,"update",l),H(o,"after","update")&&await K("after","update",{doc:l,id:t,op:"update",previous:h,table:o})},async restore(t,n){const i=await ne(t,n);if(!i)throw new T("INTERNAL",`document not found: ${t}`);const o=$.tables[i],s=o?.softDeleteMode?.field;if(!o||!s)throw new T("INTERNAL",`ctx.db.restore: table "${i}" is not a .softDelete() table`);const f=await Re(i,t),h=f?.[s]!==null&&f?.[s]!==void 0;if(await x.patch(t,{[s]:null},n),h){const l=Z(o,f);l!==null&&await X(i,t,void 0,{...l,[s]:null})}},query(t){const n=$.tables[t];if(!n)throw new T("INTERNAL",`unknown table: ${t}`);const i="the legacy query()/withIndex() reader is not available on the D1 (global) backend; use findMany",o=[],s=l=>o.every(E=>E(l)),f=async(l,E)=>{await U();const d=o.length>0,w=await Ht(r,u,n,t,l,Kt(d?void 0:E));if(!d)return E===void 0&&jt(w),w;const y=w.filter(R=>s(R));return E===void 0?y:y.slice(0,E)},h=l=>{const E={async*[Symbol.asyncIterator](){if(!l)throw new T("INTERNAL",i);yield*await f(l,void 0)},async collect(){if(!l)throw new T("INTERNAL",i);return f(l,void 0)},async collectWithScores(){throw new T("GLOBAL_SEARCH_SCORES_UNSUPPORTED",`collectWithScores() is not supported on \`.global()\` tables (table "${t}") — relevance scores are not yet surfaced on this backend; use .collect() instead`)},filter(d){return o.push(d),E},async first(){if(!l)throw new T("INTERNAL",i);return(await f(l,1))[0]??null},order(){return E},async paginate(d){if(!l)throw new T("INTERNAL",i);const w=xt(d);return Pt(await f(l,Gt(w)),w)},async take(d){if(!l)throw new T("INTERNAL",i);return f(l,d)},async unique(){if(!l)throw new T("INTERNAL",i);const d=await f(l,2);if(d.length>1)throw new En(`unique() on table "${t}" matched ${String(d.length)} documents; expected at most one`);return d[0]??null},withGeoIndex(){throw new T("INTERNAL",`geo indexes are not supported on \`.global()\` tables (table "${t}") — geospatial queries run only on sharded tables`)},withIndex(){throw new T("INTERNAL",i)},withSearchIndex(d,w){const y=(n.searchIndexes??[]).find(S=>S.name===d);if(!y)throw new T("INTERNAL",`unknown search index "${d}" on table "${t}"`);const R={definition:y,field:y.field,filters:[],hasQuery:!1,indexName:d,query:""};if(w(Ut(R,t,Ot(y.language))),!R.hasQuery)throw new T("INTERNAL",`search index "${d}" on table "${t}" requires a .search(field, query) call`);return h(R)}};return E};return h(void 0)},async rank(t,n,i){const o=$.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(B=>B.name===n);if(!s)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(i.restrictsCounts)throw new nt(t);if(await U(),!await Me(t,s))return null;const h=typeof i.row=="string"?i.row:i.row._id;if(!h)return null;const l=de(t,s.name),E=s.sortBy.map((B,j)=>$e(j)),d=J(["__partition__",...E]),y=(await I(r,u,e`SELECT ${d} FROM ${e.identifier(l)} WHERE ${e.identifier("__id__")} = ${h}`))[0];if(!y)return null;const R=y.__partition__,S=q(i.baseWhere,i.where);et(S,$,t,"rank");const C=tt(s,S);if(C&&Ie(s.partitionBy??[],C)!==R)return null;const A=Dn(u.name,s,E,y,h),F=await I(r,u,e`SELECT COUNT(*) AS c FROM ${e.identifier(l)} WHERE ${e.identifier("__partition__")} = ${R} AND (${A})`),L=await I(r,u,e`SELECT COUNT(*) AS c FROM ${e.identifier(l)} WHERE ${e.identifier("__partition__")} = ${R}`);return{position:Number(F[0]?.c??0)+1,total:Number(L[0]?.c??0)}},async rankPage(t,n,i={}){et(q(i.baseWhere,i.where),$,t,"rankPage");const o=$.tables[t];if(!o)throw new T("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(D=>D.name===n);if(!s)throw new T("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(await U(),!await Me(t,s))return{continueCursor:null,isDone:!0,page:[]};const h=de(t,s.name),l=s.sortBy.map((D,ae)=>$e(ae)),E=Math.max(1,Math.min(1e3,Math.floor(i.take??100))),d=q(i.baseWhere,i.where),w=tt(s,d),y=qn(s,l),R=e.join(y.map(D=>e`${e.identifier(D.column)} ${e.raw(`${D.direction==="desc"?"DESC":"ASC"}${gt(u,{direction:D.direction,nullable:D.nullable})}`)}`),e`, `),S=[];typeof i.partitionKey=="string"?S.push(e`${e.identifier("__partition__")} = ${i.partitionKey}`):w&&S.push(e`${e.identifier("__partition__")} = ${Ie(s.partitionBy??[],w)}`);const C=Jt(i);if(C!==void 0){const D=Bn(u.name,y,C);D!==void 0&&S.push(D)}const A=J([_e,"__partition__",...l]);let F=e`SELECT ${A} FROM ${e.identifier(h)}`;S.length>0&&(F=e`${F} WHERE ${e.join(S,e` AND `)}`),F=e`${F} ORDER BY ${R} LIMIT ${e.raw(String(E+1))}`;const L=await I(r,u,F),B=L.length>E,j=B?L.slice(0,E):L,Te=j.map(D=>D[_e]),ve=await On(r,u,o,t,Te);let me=null;const z=j.at(-1);if(B&&z!==void 0){const D=[z.__partition__,...l.map(ae=>z[ae]),z[_e]];me=Wn(D)}return{continueCursor:me,isDone:!B,page:ve}},async replace(t,n,i,o){Ze("replace",n);const s=await ne(t,i);if(!s)throw new T("INTERNAL",`document not found: ${t}`);const f=$.tables[s];if(!f)throw new T("INTERNAL",`document not found: ${t}`);const h=await Re(s,t);if(h===void 0)throw new T("INTERNAL",`document not found: ${t}`);const E=Qt($,s,"update")||(f.aggregateIndexes??[]).length>0||(f.rankIndexes??[]).length>0?Z(f,h)??void 0:void 0,d=o?.allowExplicitId&&typeof n._creationTime=="number"?n._creationTime:m(),w={...n,_creationTime:d,_id:t};_t(f,n,w,V),Ne(f,w),H(s,"before","update")&&await K("before","update",{doc:{...w},id:t,op:"update",previous:E,table:s}),await ye(s),await pe(s);const y=Object.keys(f.shape),R=e.join([e`${e.identifier("_creationTime")} = ${d}`,...y.map(S=>e`${e.identifier(S)} = ${be(f,S,w[S]??null)}`)],e`, `);await Se(s,"UPDATE",R,h),await oe(s,E,w),await X(s,t,E,w),await se(s,t,w,E),await P(s,t,"update",w),H(s,"after","update")&&await K("after","update",{doc:w,id:t,op:"update",previous:E,table:s})}};return je={db:x,scheduler:Ae},x};export{fi as backfillSqlSearchIndexes,di as createSqlCtxDb,$i as decodeGlobalRow,Xn as readSqlCdcChangedTables,ci as readSqlCdcChanges,mt as readSqlCdcFloor,Kn as runSqlCdcMigration,Ft as runSqlSearchMigrations,Gn as sweepSqlCdcRetention};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as l}from"@lunora/errors";import{D as y,c as Z,m as tt,r as R,a as nt,l as W,w as N,b as et}from"./ctx-db-search-state-DRx8DyOa.mjs";import{sql as e}from"drizzle-orm";import{unionAll as it}from"@lunora/shard-engine";import{queryRun as E,decodeRows as Q,queryAll as A,queryBatch as rt,createIndexIfNotExists as q,columnRefSql as M,serializeColumnValue as ot,forEachRowPaged as G}from"./decodeGlobalRow-C76NMu9P.mjs";const st=t=>{const n=atob(t),i=Uint8Array.from(n,r=>r.codePointAt(0)??0);return new TextDecoder().decode(i)},at=()=>new l("BAD_REQUEST","invalid cursor"),F=16,k=8,h=1024,v=(t,n)=>n.query(t),ft=(t,n)=>{if(!n)return{exact:!0,lower:t,upper:t};const i=[...t].at(-1)??"",r=(i.codePointAt(0)??0)+1;if(r>=55296&&r<=57343||r>1114111)return{exact:!0,lower:t,upper:t};const o=t.slice(0,t.length-i.length);return{exact:!1,lower:t,upper:o+String.fromCodePoint(r)}},Lt=(t,n,i)=>{const r={eq:(o,s)=>{if(!t.definition.filterFields?.includes(o))throw new l("INTERNAL",`field "${o}" is not a filter field of search index "${t.indexName}" on table "${n}"`);if(t.filters.length>=k)throw new l("BAD_REQUEST",`search index "${t.indexName}" on table "${n}": at most ${String(k)} .eq() filters are supported per search query`);return t.filters.push({field:o,value:s}),r},search:(o,s)=>{const a=t;if(o!==a.definition.field)throw new l("INTERNAL",`search index "${a.indexName}" on table "${n}" indexes "${a.definition.field}", not "${o}"`);const f=v(s,i).length;if(f>F)throw new l("BAD_REQUEST",`search index "${a.indexName}" on table "${n}": at most ${String(F)} search terms are supported (got ${String(f)})`);return a.field=o,a.query=s,a.hasQuery=!0,r}};return r},Ot=t=>{if(t.length>h)throw new l("BAD_REQUEST",`more than ${String(h)} documents match this search — narrow it with filters or read it a page at a time with .paginate()`)},xt=t=>Math.min(t.offset+t.numItems+1,h),ct=t=>btoa(`search:${String(t)}`),ut=t=>{let n;try{n=st(t)}catch{return}if(!n.startsWith("search:"))return;const i=Number(n.slice(7));return Number.isInteger(i)&&i>=0?i:void 0},Ut=t=>{if(typeof t.endCursor=="string")throw new l("BAD_REQUEST","bounded (endCursor) pagination is not supported on search queries — relevance order has no stable range boundary");if(!Number.isFinite(t.numItems))throw new l("BAD_REQUEST",`search pagination needs a finite numItems, got ${String(t.numItems)}`);const n=Math.max(0,Math.floor(t.numItems)),i=t.cursor?ut(t.cursor):0;if(i===void 0)throw at();if(i+n>=h)throw new l("BAD_REQUEST",`search pagination reaches the ${String(h)}-document limit (offset ${String(i)} + ${String(n)} requested) — a page must end below the cap so the probe row that answers \`hasMore\` still fits: retry with numItems ${String(Math.max(1,h-i-1))} or fewer, or narrow the query or the filters instead`);return{numItems:n,offset:i}},Bt=(t,n)=>{const i=n.offset+n.numItems,r=n.numItems>0&&t.length>i;return{continueCursor:r?ct(i):null,isDone:!r,page:t.slice(n.offset,i)}},qt=t=>{if(t===void 0)return h+1;if(!Number.isFinite(t))return h;const n=Math.max(0,Math.floor(t));if(n>h)throw new l("BAD_REQUEST",`search returns at most ${String(h)} documents (asked for ${String(n)}) — narrow the query or paginate instead`);return n},dt=(t,n)=>{if(!n.includes("."))return t[n];let i=t;for(const r of n.split(".")){if(i===null||typeof i!="object"||Array.isArray(i))return;i=i[r]}return i},b=(t,n)=>`${t}__fts_${n}`,$t="__text__",$="__id__",g="__token__",O="__n__",mt=1e3,V=(t,n)=>n.document(t),x=(t,n)=>dt(t,n),lt=t=>typeof t=="string"?t:t==null?"":typeof t=="number"||typeof t=="bigint"||typeof t=="boolean"?String(t):JSON.stringify(t)??"",St=(t,n,i)=>t!==void 0&&n!==void 0&&x(t,i.field)===x(n,i.field),ht=(t,n)=>V(lt(x(t,n.field)),y(n.language)).slice(0,mt),U=(t,n)=>ht(t,n).join(" "),wt=(t,n)=>{const i=new Map;for(const r of V(t,n))i.set(r,(i.get(r)??0)+1);return i},C=async(t,n,i,r)=>{await E(t,n,e`DELETE FROM ${e.identifier(i)} WHERE ${e.identifier($)} = ${r}`)},L=(t,n)=>t.name==="mysql"?e`${e.identifier(n)}(191)`:t.textPatternOperatorClass===void 0?e`${e.identifier(n)}`:e`${e.identifier(n)} ${e.raw(t.textPatternOperatorClass)}`,H=50,Et=(t,n)=>n?e`${e.identifier(g)} LIKE ${`${t}%`}`:e`${e.identifier(g)} = ${t}`,X=(t,n)=>{const i=n.filters.map(r=>e`m.${M(r.field)} = ${ot(r.value)}`);return t.softDeleteMode&&i.push(e`m.${M(t.softDeleteMode.field)} IS NULL`),i},J=async(t,n,i,r,o,s,a)=>{const f=X(i,o);let c=e`SELECT m.* FROM (${a}) s JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = s.${e.identifier($)}`;return f.length>0&&(c=e`${c} WHERE ${e.join(f,e` AND `)}`),c=e`${c} ORDER BY s.${e.identifier("__score__")} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`,Q(i,await A(t,n,c))},pt=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=a.length-1,u=a.map((d,_)=>Et(d,_===c)),m=e.join(u,e` OR `),S=u.map(d=>e`SUM(CASE WHEN ${d} THEN ${e.identifier(O)} ELSE 0 END)`),p=e.join(S.map(d=>e`${d} > 0`),e` AND `),w=e`SELECT ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM ${e.identifier(f)} WHERE ${m} GROUP BY ${e.identifier($)} HAVING ${p}`;return J(t,n,i,r,o,s,w)},_t=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=`${f}__vocab`,u=a.length-1,m=a.map((w,d)=>{const _=ft(w,d===u),z=_.exact?e`${e.identifier("term")} = ${_.lower}`:e`${e.identifier("term")} >= ${_.lower} AND ${e.identifier("term")} < ${_.upper}`;return e`SELECT ${e.identifier("doc")}, ${e.raw(String(d))} AS ${e.identifier("__term__")}, COUNT(*) AS ${e.identifier("__n__")} FROM ${e.identifier(c)} WHERE ${z} GROUP BY ${e.identifier("doc")}`}),S=a.map((w,d)=>e`SUM(CASE WHEN u.${e.identifier("__term__")} = ${e.raw(String(d))} THEN u.${e.identifier("__n__")} ELSE 0 END)`),p=e`SELECT f.${e.identifier($)} AS ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM (${it(m)}) u JOIN ${e.identifier(f)} f ON f.rowid = u.${e.identifier("doc")} GROUP BY f.${e.identifier($)} HAVING ${e.join(S.map(w=>e`${w} > 0`),e` AND `)}`;return J(t,n,i,r,o,s,p)},gt=async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch,f=v(o.query,y(o.definition.language));if(!a||f.length===0)return[];const c=b(r,o.indexName),u=[a.matches(c,f),...X(i,o)],m=e`SELECT m.* FROM ${e.identifier(c)} JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = ${e.identifier(c)}.${e.identifier($)} WHERE ${e.join(u,e` AND `)} ORDER BY ${a.rank(c,f)} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`;return Q(i,await A(t,n,m))},yt={ensureCompanion:async(t,n,i)=>{const{integer:r,key:o}=n.companionTypes;await E(t,n,e`CREATE TABLE IF NOT EXISTS ${e.identifier(i)} (${e.identifier(g)} ${e.raw(o)} NOT NULL, ${e.identifier($)} ${e.raw(o)} NOT NULL, ${e.identifier(O)} ${e.raw(r)} NOT NULL)`),await q(t,n,{columns:e`${L(n,g)}, ${L(n,$)}`,name:`${i}__btree`,table:i,unique:!1}),await q(t,n,{columns:L(n,$),name:`${i}__by_id`,table:i,unique:!1})},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r);const a=[...wt(U(o,s),y(s.language))],f=e.join([g,$,O].map(u=>e.identifier(u)),e`, `),c=[];for(let u=0;u<a.length;u+=H){const m=e.join(a.slice(u,u+H).map(([S,p])=>e`(${S}, ${r}, ${p})`),e`, `);c.push(e`INSERT INTO ${e.identifier(i)} (${f}) VALUES ${m}`)}await rt(t,n,c)},name:"inverted",runSearch:pt},Tt={ensureCompanion:async(t,n,i)=>{await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(i)} USING fts5(${e.identifier($t)}, ${e.identifier($)} UNINDEXED)`),await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(`${i}__vocab`)} USING fts5vocab(${e.identifier(i)}, ${e.raw("instance")})`)},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r),await E(t,n,e`INSERT INTO ${e.identifier(i)} (${e.identifier("__text__")}, ${e.identifier($)}) VALUES (${U(o,s)}, ${r})`)},name:"fts5",runSearch:_t},It={ensureCompanion:async(t,n,i)=>{const r=n.nativeTextSearch;if(r){await E(t,n,r.createCompanion(i,n.companionTypes.key));for(const o of r.createIndexes(i))await E(t,n,o)}},indexDocument:async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch;a&&(await C(t,n,i,r),await E(t,n,a.indexDocument(i,r,U(o,s))))},name:"native",runSearch:gt},T=(t,n)=>t.strategy==="native"&&n.nativeTextSearch!==void 0?It:n.supportsFts5?Tt:yt,D=(t,n)=>`${Z(t)}/${T(t,n).name}`,B=function*(t){for(const[n,i]of Object.entries(t.tables)){const r=i.searchIndexes;if(!(i.shardMode!==void 0&&i.shardMode.kind!=="global"||!r))for(const o of r)yield[n,i,o]}},I=(t,n)=>b(t,n.name),Nt=async(t,n,i,r)=>{const o=I(i,r);return W(await R(t,n,o),D(r,n)).finished?!0:et(t,n,o)},Mt=async(t,n,i,r,o,s)=>{if(!await Nt(t,n,r,o.definition))throw new l("SEARCH_INDEX_BUILDING",`search index "${o.indexName}" on table "${r}" is still backfilling and currently covers only part of the table — retry once it finishes, or run the backfillSearch admin operation to complete it now`);return T(o.definition,n).runSearch(t,n,i,r,o,s)},P=t=>t.slice(t.lastIndexOf("/")+1),j=200,Y=async(t,n,i,r,o)=>{const s=I(r,o),a=T(o,n),f=D(o,n),c=W(await R(t,n,s),f);if(c.finished)return!0;if(c.wipe&&await N(t,n,s,void 0,!1,f),(await A(t,n,n.tableExists(r))).length===0)return await N(t,n,s,void 0,!0,f),!0;let m=0,S=c.cursor;await G(t,n,i,r,async w=>{m+=1;const d=w._id;typeof d=="string"&&(S=d,await a.indexDocument(t,n,s,d,w,o))},{after:c.cursor,limit:j});const p=m<j;return await N(t,n,s,S,p,f),p},K=async(t,n,i)=>{await tt(t,i);for(const[r,,o]of B(n)){const s=I(r,o),a=D(o,i),f=await R(t,i,s);f.profile!==void 0&&P(f.profile)!==P(a)&&(await E(t,i,e`DROP TABLE IF EXISTS ${e.identifier(s)}`),await nt(t,i,s)),await T(o,i).ensureCompanion(t,i,s)}},Rt=async(t,n,i,r,o)=>{const s=I(r,o);if((await R(t,n,s)).profile!==void 0)return;let f=0;(await A(t,n,n.tableExists(r))).length>0&&await G(t,n,i,r,()=>{f+=1},{limit:1}),await N(t,n,s,void 0,f===0,D(o,n))},Ft=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){if(s.staged){await Rt(t,i,o,r,s);continue}await Y(t,i,o,r,s)}},kt=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){let a=!1;for(;!a;)a=await Y(t,i,o,r,s)}},Ht=t=>{const{dialect:n,exec:i,schema:r}=t;return async(o,s,a,f)=>{const c=r.tables[o]?.searchIndexes;if(!(!c||c.length===0))for(const u of c){if(St(f,a,u))continue;const m=I(o,u);if(a){await T(u,n).indexDocument(i,n,m,s,a,u);continue}await C(i,n,m,s)}}};export{Lt as A,Ut as C,Bt as R,qt as T,Mt as a,xt as b,Ht as c,kt as d,Ft as r,Ot as x};
|
|
1
|
+
import{LunoraError as l}from"@lunora/errors";import{D as y,c as Z,m as tt,r as R,a as nt,l as W,w as N,b as et}from"./ctx-db-search-state-C0hUwzYJ.mjs";import{sql as e}from"drizzle-orm";import{unionAll as it}from"@lunora/shard-engine";import{queryRun as E,decodeRows as Q,queryAll as A,queryBatch as rt,createIndexIfNotExists as q,columnRefSql as M,serializeColumnValue as ot,forEachRowPaged as G}from"./decodeGlobalRow-B6zmyAi5.mjs";const st=t=>{const n=atob(t),i=Uint8Array.from(n,r=>r.codePointAt(0)??0);return new TextDecoder().decode(i)},at=()=>new l("BAD_REQUEST","invalid cursor"),F=16,k=8,h=1024,v=(t,n)=>n.query(t),ft=(t,n)=>{if(!n)return{exact:!0,lower:t,upper:t};const i=[...t].at(-1)??"",r=(i.codePointAt(0)??0)+1;if(r>=55296&&r<=57343||r>1114111)return{exact:!0,lower:t,upper:t};const o=t.slice(0,t.length-i.length);return{exact:!1,lower:t,upper:o+String.fromCodePoint(r)}},Lt=(t,n,i)=>{const r={eq:(o,s)=>{if(!t.definition.filterFields?.includes(o))throw new l("INTERNAL",`field "${o}" is not a filter field of search index "${t.indexName}" on table "${n}"`);if(t.filters.length>=k)throw new l("BAD_REQUEST",`search index "${t.indexName}" on table "${n}": at most ${String(k)} .eq() filters are supported per search query`);return t.filters.push({field:o,value:s}),r},search:(o,s)=>{const a=t;if(o!==a.definition.field)throw new l("INTERNAL",`search index "${a.indexName}" on table "${n}" indexes "${a.definition.field}", not "${o}"`);const f=v(s,i).length;if(f>F)throw new l("BAD_REQUEST",`search index "${a.indexName}" on table "${n}": at most ${String(F)} search terms are supported (got ${String(f)})`);return a.field=o,a.query=s,a.hasQuery=!0,r}};return r},Ot=t=>{if(t.length>h)throw new l("BAD_REQUEST",`more than ${String(h)} documents match this search — narrow it with filters or read it a page at a time with .paginate()`)},xt=t=>Math.min(t.offset+t.numItems+1,h),ct=t=>btoa(`search:${String(t)}`),ut=t=>{let n;try{n=st(t)}catch{return}if(!n.startsWith("search:"))return;const i=Number(n.slice(7));return Number.isInteger(i)&&i>=0?i:void 0},Ut=t=>{if(typeof t.endCursor=="string")throw new l("BAD_REQUEST","bounded (endCursor) pagination is not supported on search queries — relevance order has no stable range boundary");if(!Number.isFinite(t.numItems))throw new l("BAD_REQUEST",`search pagination needs a finite numItems, got ${String(t.numItems)}`);const n=Math.max(0,Math.floor(t.numItems)),i=t.cursor?ut(t.cursor):0;if(i===void 0)throw at();if(i+n>=h)throw new l("BAD_REQUEST",`search pagination reaches the ${String(h)}-document limit (offset ${String(i)} + ${String(n)} requested) — a page must end below the cap so the probe row that answers \`hasMore\` still fits: retry with numItems ${String(Math.max(1,h-i-1))} or fewer, or narrow the query or the filters instead`);return{numItems:n,offset:i}},Bt=(t,n)=>{const i=n.offset+n.numItems,r=n.numItems>0&&t.length>i;return{continueCursor:r?ct(i):null,isDone:!r,page:t.slice(n.offset,i)}},qt=t=>{if(t===void 0)return h+1;if(!Number.isFinite(t))return h;const n=Math.max(0,Math.floor(t));if(n>h)throw new l("BAD_REQUEST",`search returns at most ${String(h)} documents (asked for ${String(n)}) — narrow the query or paginate instead`);return n},dt=(t,n)=>{if(!n.includes("."))return t[n];let i=t;for(const r of n.split(".")){if(i===null||typeof i!="object"||Array.isArray(i))return;i=i[r]}return i},b=(t,n)=>`${t}__fts_${n}`,$t="__text__",$="__id__",g="__token__",O="__n__",mt=1e3,V=(t,n)=>n.document(t),x=(t,n)=>dt(t,n),lt=t=>typeof t=="string"?t:t==null?"":typeof t=="number"||typeof t=="bigint"||typeof t=="boolean"?String(t):JSON.stringify(t)??"",St=(t,n,i)=>t!==void 0&&n!==void 0&&x(t,i.field)===x(n,i.field),ht=(t,n)=>V(lt(x(t,n.field)),y(n.language)).slice(0,mt),U=(t,n)=>ht(t,n).join(" "),wt=(t,n)=>{const i=new Map;for(const r of V(t,n))i.set(r,(i.get(r)??0)+1);return i},C=async(t,n,i,r)=>{await E(t,n,e`DELETE FROM ${e.identifier(i)} WHERE ${e.identifier($)} = ${r}`)},L=(t,n)=>t.name==="mysql"?e`${e.identifier(n)}(191)`:t.textPatternOperatorClass===void 0?e`${e.identifier(n)}`:e`${e.identifier(n)} ${e.raw(t.textPatternOperatorClass)}`,H=50,Et=(t,n)=>n?e`${e.identifier(g)} LIKE ${`${t}%`}`:e`${e.identifier(g)} = ${t}`,X=(t,n)=>{const i=n.filters.map(r=>e`m.${M(r.field)} = ${ot(r.value)}`);return t.softDeleteMode&&i.push(e`m.${M(t.softDeleteMode.field)} IS NULL`),i},J=async(t,n,i,r,o,s,a)=>{const f=X(i,o);let c=e`SELECT m.* FROM (${a}) s JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = s.${e.identifier($)}`;return f.length>0&&(c=e`${c} WHERE ${e.join(f,e` AND `)}`),c=e`${c} ORDER BY s.${e.identifier("__score__")} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`,Q(i,await A(t,n,c))},pt=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=a.length-1,u=a.map((d,_)=>Et(d,_===c)),m=e.join(u,e` OR `),S=u.map(d=>e`SUM(CASE WHEN ${d} THEN ${e.identifier(O)} ELSE 0 END)`),p=e.join(S.map(d=>e`${d} > 0`),e` AND `),w=e`SELECT ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM ${e.identifier(f)} WHERE ${m} GROUP BY ${e.identifier($)} HAVING ${p}`;return J(t,n,i,r,o,s,w)},_t=async(t,n,i,r,o,s)=>{const a=v(o.query,y(o.definition.language));if(a.length===0)return[];const f=b(r,o.indexName),c=`${f}__vocab`,u=a.length-1,m=a.map((w,d)=>{const _=ft(w,d===u),z=_.exact?e`${e.identifier("term")} = ${_.lower}`:e`${e.identifier("term")} >= ${_.lower} AND ${e.identifier("term")} < ${_.upper}`;return e`SELECT ${e.identifier("doc")}, ${e.raw(String(d))} AS ${e.identifier("__term__")}, COUNT(*) AS ${e.identifier("__n__")} FROM ${e.identifier(c)} WHERE ${z} GROUP BY ${e.identifier("doc")}`}),S=a.map((w,d)=>e`SUM(CASE WHEN u.${e.identifier("__term__")} = ${e.raw(String(d))} THEN u.${e.identifier("__n__")} ELSE 0 END)`),p=e`SELECT f.${e.identifier($)} AS ${e.identifier($)}, ${e.join(S,e` + `)} AS ${e.identifier("__score__")} FROM (${it(m)}) u JOIN ${e.identifier(f)} f ON f.rowid = u.${e.identifier("doc")} GROUP BY f.${e.identifier($)} HAVING ${e.join(S.map(w=>e`${w} > 0`),e` AND `)}`;return J(t,n,i,r,o,s,p)},gt=async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch,f=v(o.query,y(o.definition.language));if(!a||f.length===0)return[];const c=b(r,o.indexName),u=[a.matches(c,f),...X(i,o)],m=e`SELECT m.* FROM ${e.identifier(c)} JOIN ${e.identifier(r)} m ON m.${e.identifier("id")} = ${e.identifier(c)}.${e.identifier($)} WHERE ${e.join(u,e` AND `)} ORDER BY ${a.rank(c,f)} DESC, m.${e.identifier("_creationTime")} DESC, m.${e.identifier("id")} ASC LIMIT ${e.raw(String(s))}`;return Q(i,await A(t,n,m))},yt={ensureCompanion:async(t,n,i)=>{const{integer:r,key:o}=n.companionTypes;await E(t,n,e`CREATE TABLE IF NOT EXISTS ${e.identifier(i)} (${e.identifier(g)} ${e.raw(o)} NOT NULL, ${e.identifier($)} ${e.raw(o)} NOT NULL, ${e.identifier(O)} ${e.raw(r)} NOT NULL)`),await q(t,n,{columns:e`${L(n,g)}, ${L(n,$)}`,name:`${i}__btree`,table:i,unique:!1}),await q(t,n,{columns:L(n,$),name:`${i}__by_id`,table:i,unique:!1})},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r);const a=[...wt(U(o,s),y(s.language))],f=e.join([g,$,O].map(u=>e.identifier(u)),e`, `),c=[];for(let u=0;u<a.length;u+=H){const m=e.join(a.slice(u,u+H).map(([S,p])=>e`(${S}, ${r}, ${p})`),e`, `);c.push(e`INSERT INTO ${e.identifier(i)} (${f}) VALUES ${m}`)}await rt(t,n,c)},name:"inverted",runSearch:pt},Tt={ensureCompanion:async(t,n,i)=>{await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(i)} USING fts5(${e.identifier($t)}, ${e.identifier($)} UNINDEXED)`),await E(t,n,e`CREATE VIRTUAL TABLE IF NOT EXISTS ${e.identifier(`${i}__vocab`)} USING fts5vocab(${e.identifier(i)}, ${e.raw("instance")})`)},indexDocument:async(t,n,i,r,o,s)=>{await C(t,n,i,r),await E(t,n,e`INSERT INTO ${e.identifier(i)} (${e.identifier("__text__")}, ${e.identifier($)}) VALUES (${U(o,s)}, ${r})`)},name:"fts5",runSearch:_t},It={ensureCompanion:async(t,n,i)=>{const r=n.nativeTextSearch;if(r){await E(t,n,r.createCompanion(i,n.companionTypes.key));for(const o of r.createIndexes(i))await E(t,n,o)}},indexDocument:async(t,n,i,r,o,s)=>{const a=n.nativeTextSearch;a&&(await C(t,n,i,r),await E(t,n,a.indexDocument(i,r,U(o,s))))},name:"native",runSearch:gt},T=(t,n)=>t.strategy==="native"&&n.nativeTextSearch!==void 0?It:n.supportsFts5?Tt:yt,D=(t,n)=>`${Z(t)}/${T(t,n).name}`,B=function*(t){for(const[n,i]of Object.entries(t.tables)){const r=i.searchIndexes;if(!(i.shardMode!==void 0&&i.shardMode.kind!=="global"||!r))for(const o of r)yield[n,i,o]}},I=(t,n)=>b(t,n.name),Nt=async(t,n,i,r)=>{const o=I(i,r);return W(await R(t,n,o),D(r,n)).finished?!0:et(t,n,o)},Mt=async(t,n,i,r,o,s)=>{if(!await Nt(t,n,r,o.definition))throw new l("SEARCH_INDEX_BUILDING",`search index "${o.indexName}" on table "${r}" is still backfilling and currently covers only part of the table — retry once it finishes, or run the backfillSearch admin operation to complete it now`);return T(o.definition,n).runSearch(t,n,i,r,o,s)},P=t=>t.slice(t.lastIndexOf("/")+1),j=200,Y=async(t,n,i,r,o)=>{const s=I(r,o),a=T(o,n),f=D(o,n),c=W(await R(t,n,s),f);if(c.finished)return!0;if(c.wipe&&await N(t,n,s,void 0,!1,f),(await A(t,n,n.tableExists(r))).length===0)return await N(t,n,s,void 0,!0,f),!0;let m=0,S=c.cursor;await G(t,n,i,r,async w=>{m+=1;const d=w._id;typeof d=="string"&&(S=d,await a.indexDocument(t,n,s,d,w,o))},{after:c.cursor,limit:j});const p=m<j;return await N(t,n,s,S,p,f),p},K=async(t,n,i)=>{await tt(t,i);for(const[r,,o]of B(n)){const s=I(r,o),a=D(o,i),f=await R(t,i,s);f.profile!==void 0&&P(f.profile)!==P(a)&&(await E(t,i,e`DROP TABLE IF EXISTS ${e.identifier(s)}`),await nt(t,i,s)),await T(o,i).ensureCompanion(t,i,s)}},Rt=async(t,n,i,r,o)=>{const s=I(r,o);if((await R(t,n,s)).profile!==void 0)return;let f=0;(await A(t,n,n.tableExists(r))).length>0&&await G(t,n,i,r,()=>{f+=1},{limit:1}),await N(t,n,s,void 0,f===0,D(o,n))},Ft=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){if(s.staged){await Rt(t,i,o,r,s);continue}await Y(t,i,o,r,s)}},kt=async(t,n,i)=>{await K(t,n,i);for(const[r,o,s]of B(n)){let a=!1;for(;!a;)a=await Y(t,i,o,r,s)}},Ht=t=>{const{dialect:n,exec:i,schema:r}=t;return async(o,s,a,f)=>{const c=r.tables[o]?.searchIndexes;if(!(!c||c.length===0))for(const u of c){if(St(f,a,u))continue;const m=I(o,u);if(a){await T(u,n).indexDocument(i,n,m,s,a,u);continue}await C(i,n,m,s)}}};export{Lt as A,Ut as C,Bt as R,qt as T,Mt as a,xt as b,Ht as c,kt as d,Ft as r,Ot as x};
|
package/dist/packem_shared/{ctx-db-search-state-DRx8DyOa.mjs → ctx-db-search-state-C0hUwzYJ.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{sql as e}from"drizzle-orm";import{queryRun as p,queryAll as m}from"./decodeGlobalRow-
|
|
1
|
+
import{sql as e}from"drizzle-orm";import{queryRun as p,queryAll as m}from"./decodeGlobalRow-B6zmyAi5.mjs";const y=["de","en","es","fr","it","nl","none","pt"],L=i=>y.includes(i),A="a an and are as at be but by for if in into is it no not of on or such that the their then there these they this to was will with",R="aber als am an auch auf aus bei bin bis bist da dass der den des dem die das denn dir du ein eine für hat ich im in ist mit nicht noch nur oder sich sie sind über und von vor war wie wir zu zum zur",F="a al como con de del el en es la las lo los mas no o para pero por que se su sus un una uno y ya",N="au aux avec ce ces dans de des du elle en et eux il je la le les leur lui ma mais me même mes moi mon ne nos notre nous on ou par pas pour qu que qui sa se ses son sur ta te tes toi ton tu un une vos votre vous y",_="a ai al alla anche che chi ci coi col come con da dal degli dei del della di do e ed gli ha hai hanno i il in la le lo ma mi ne nei nel non o per più quale quanto se si sono su sul tra un una uno vi",q="aan al als bij dan dat de der deze die dit door een en er het hij ij in is je kan me men met mij na naar niet nog nu of om ons ook op over te tot uit van voor was wat we wij zij zijn zo",x="a ao aos as até com como da das de do dos e em entre era essa esse esta este eu foi há isso já mais mas me mesmo meu na nas no nos num numa o os ou para pela pelo por qual que quem se sem seu só sua também te tem um uma você",C=new RegExp("(?<=\\p{Script=Cyrillic})[\\u0300-\\u0305\\u0307\\u0309-\\u036F]|(?<!\\p{Script=Cyrillic})[\\u0300-\\u036F]","gu"),O=/[\u0080-\u{10FFFF}]/u,b=/[\p{L}\p{N}]+/gu,H=/[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}\p{Script_Extensions=Hangul}]+/gu,g=/[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}\p{Script_Extensions=Hangul}]/u,D=i=>{if(!g.test(i))return[i];const n=[];let t=0;for(const r of i.matchAll(H)){const o=r.index,s=[...r[0]];if(o>t&&n.push(i.slice(t,o)),t=o+r[0].length,s.length===1){n.push(r[0]);continue}for(let a=0;a+1<s.length;a+=1)n.push(`${String(s[a])}${String(s[a+1])}`)}return t<i.length&&n.push(i.slice(t)),n},v=i=>O.test(i)?i.normalize("NFD").replaceAll(C,"").normalize("NFC").toLowerCase():i.toLowerCase(),f=i=>new Set(v(i).split(" ")),j={de:f(R),en:f(A),es:f(F),fr:f(N),it:f(_),nl:f(q),none:new Set,pt:f(x)},z=3,U=256,w=new Map,k=i=>{const n=i!==void 0&&L(i)?i:"none",t=w.get(n);if(t)return t;const r=j[n],o=a=>{const d=v(a),$=d.match(b)??[],u=(g.test(d)?$.flatMap(l=>D(l)):$).filter(l=>l.length<=U);return r.size===0?u:u.filter(l=>!r.has(l))},s={document:o,profile:`${n}-v${String(z)}`,query:a=>{const d=o(a);return d.filter(($,u)=>d.lastIndexOf($)===u)}};return w.set(n,s),s},W=i=>`${k(i.language).profile}:${i.field}`,S=i=>{const n=i.slice(i.indexOf(":")+1),t=n.lastIndexOf("/");return t===-1?n:n.slice(0,t)},V=(i,n)=>i.profile!==n?{cursor:void 0,finished:!1,wipe:i.cursor!==void 0||i.done}:{cursor:i.cursor,finished:i.done,wipe:!1},I=(i,n)=>typeof i=="string"&&S(i)===S(n),c="__lunora_search_state",K=async(i,n)=>{const{integer:t,key:r}=n.companionTypes,o=async(s,a)=>{try{await p(i,n,e`ALTER TABLE ${e.identifier(c)} ADD COLUMN ${e.identifier(s)} ${e.raw(a)}`)}catch{}};await p(i,n,e`CREATE TABLE IF NOT EXISTS ${e.identifier(c)} (${e.identifier("companion")} ${e.raw(r)} PRIMARY KEY, ${e.identifier("cursor")} ${e.raw(r)}, ${e.identifier("done")} ${e.raw(t)} NOT NULL DEFAULT 0, ${e.identifier("profile")} ${e.raw(r)}, ${e.identifier("covered")} ${e.raw(t)} NOT NULL DEFAULT 0)`),await o("profile",r),await o("covered",`${t} NOT NULL DEFAULT 0`),await p(i,n,e`UPDATE ${e.identifier(c)} SET ${e.identifier("covered")} = 1 WHERE ${e.identifier("done")} = 1 AND ${e.identifier("covered")} = 0`)},T=i=>i===1||i===!0||i==="1",P=async(i,n,t)=>{const r=await m(i,n,e`SELECT ${e.identifier("covered")} FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`);return T(r[0]?.covered)},Y=async(i,n,t)=>{await p(i,n,e`DELETE FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`)},X=async(i,n,t)=>{const o=(await m(i,n,e`SELECT ${e.identifier("cursor")}, ${e.identifier("done")}, ${e.identifier("profile")} FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`))[0];if(!o)return{cursor:void 0,done:!1,profile:void 0};const{cursor:s,profile:a}=o;return{cursor:typeof s=="string"?s:void 0,done:T(o.done),profile:typeof a=="string"?a:void 0}},G=async(i,n,t,r,o,s)=>{const a=r??null,d=await m(i,n,e`SELECT ${e.identifier("profile")} FROM ${e.identifier(c)} WHERE ${e.identifier("companion")} = ${t}`),$=I(d[0]?.profile,s),u=o?1:0,l=o||!$?e`${e.identifier("covered")} = ${u}`:e`${e.identifier("covered")} = ${e.identifier("covered")}`,h=e`UPDATE ${e.identifier(c)} SET ${e.identifier("cursor")} = ${a}, ${e.identifier("done")} = ${u}, ${e.identifier("profile")} = ${s}, ${l} WHERE ${e.identifier("companion")} = ${t}`;if(d.length>0){await p(i,n,h);return}try{await p(i,n,e`INSERT INTO ${e.identifier(c)} (${e.identifier("companion")}, ${e.identifier("cursor")}, ${e.identifier("done")}, ${e.identifier("profile")}, ${e.identifier("covered")}) VALUES (${t}, ${a}, ${o?1:0}, ${s}, ${o?1:0})`)}catch(E){if(!n.isUniqueViolation(E))throw E;await p(i,n,h)}};export{k as D,c as S,Y as a,P as b,W as c,V as l,K as m,X as r,G as w};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{bigintSqlKey as o}from"@lunora/shard-engine";import{B as a,c as i,e as n,a as r,s as c,t as d}from"./value-codec-
|
|
1
|
+
import{bigintSqlKey as o}from"@lunora/shard-engine";import{B as a,c as i,e as n,a as r,s as c,t as d}from"./value-codec-nCDJEXH_.mjs";export{a as BIGINT_KEY_LENGTH,o as bigintSqlKey,i as decodeBigint,n as effectiveColumnKind,r as sqliteDecode,c as sqliteEncode,d as tryJsonParse};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{renderSql as d}from"@lunora/shard-engine";import{sql as r}from"drizzle-orm";import{s as R,a as q,e as y}from"./value-codec-nCDJEXH_.mjs";const K="_version",O=n=>n==="_id"||n==="id"?"id":n,k=n=>r`${r.identifier(O(n))}`,D=n=>{const t=[];for(const[e,o]of Object.entries(n.shape)){const i=o._meta?.column;i&&t.push([e,i])}return t},b=(n,t,e)=>{const{params:o,sql:i}=d(t.name,e);return n.all(i,o)},u=(n,t,e)=>{const{params:o,sql:i}=d(t.name,e);return n.run(i,o)},B=async(n,t,e)=>{if(e.length!==0){if(!n.batch){for(const o of e)await u(n,t,o);return}await n.batch(e.map(o=>d(t.name,o)))}},F=async(n,t,e)=>{const o=e.unique?r`UNIQUE `:r``;if(t.name==="mysql"){try{await u(n,t,r`CREATE ${o}INDEX ${r.identifier(e.name)} ON ${r.identifier(e.table)} (${e.columns})`)}catch(i){const s=i;if(s.errno!==1061&&s.code!=="ER_DUP_KEYNAME"&&s.code!==1061)throw i}return}await u(n,t,r`CREATE ${o}INDEX IF NOT EXISTS ${r.identifier(e.name)} ON ${r.identifier(e.table)} (${e.columns})`)},L=R,h=n=>n?._meta?.column?.notNull===!1,g=n=>{if(n.kind!=="optional"||h(n))return!1;const t=n._meta?.inner;return!h(t)},w=new WeakMap,$=n=>{let t=w.get(n);return t===void 0&&(t=Object.entries(n.shape).map(([e,o])=>[e,y(o),g(o)]),w.set(n,t)),t},C=new WeakMap,M=(n,t)=>{let e=C.get(n);return e===void 0&&(e=new Map($(n).map(([o,i])=>[o,i])),C.set(n,e)),e.get(t)},U=(n,t,e)=>R(e,M(n,t)),S=(n,t)=>{const e={};for(const[o,i,s]of $(n)){const c=t[o];c===void 0||s&&c===null||(e[o]=q(c,i))}return e._id=t.id,e._creationTime=t._creationTime,e},_=(n,t)=>t?S(n,t):null,z=(n,t)=>{const e=[];for(const o of t){const i=_(n,o);i&&e.push(i)}return e},p=500,W=async(n,t,e,o,i,s={})=>{let c=s.after,a=s.limit,m=!0;for(;m&&(a===void 0||a>0);){const f=a===void 0?p:Math.min(p,a),I=c===void 0?r``:r` WHERE ${r.identifier("id")} > ${c}`,l=await b(n,t,r`SELECT * FROM ${r.identifier(o)}${I} ORDER BY ${r.identifier("id")} ASC LIMIT ${r.raw(String(f))}`);for(const N of l){const E=_(e,N);E&&await i(E)}c=l.at(-1)?.id,m=l.length===f,a=a===void 0?void 0:a-l.length}};export{p as BACKFILL_BATCH_SIZE,K as OCC_VERSION_COLUMN,k as columnRefSql,F as createIndexIfNotExists,S as decodeGlobalRow,_ as decodeRow,z as decodeRows,W as forEachRowPaged,O as physicalColumn,b as queryAll,B as queryBatch,u as queryRun,L as serializeColumnValue,U as serializeDocumentColumn,D as tableColumns};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as R}from"@lunora/errors";import{aggregateTableName as C,rankTableName as x,sortColumnName as A,renderSql as v,bigintSqlKey as D}from"@lunora/shard-engine";import{sql as n}from"drizzle-orm";import{queryRun as g,queryAll as w,createIndexIfNotExists as p,OCC_VERSION_COLUMN as T,tableColumns as M,columnRefSql as d,queryBatch as B}from"./decodeGlobalRow-B6zmyAi5.mjs";import{B as S,e as h}from"./value-codec-nCDJEXH_.mjs";const N=(t,i,e=!1)=>i.columnType(h(t),{unique:e||t._meta?.column?.unique===!0}),L=t=>{const i=new Set;for(const e of t.indexes)e.unique===!0&&e.fields.length===1&&i.add(e.fields[0]);return i},k=(t,i,e)=>{if(e.indexKeyPrefix!==void 0)for(const o of i.indexes){if(o.unique!==!0||o.fields.length<=1)continue;const r=o.fields.filter(s=>{const a=i.shape[s];return a!==void 0&&e.indexKeyPrefix?.(h(a))!==void 0});if(r.length>0)throw new Error(`${t}.${o.name}: a composite unique index cannot cover ${r.join(", ")} in full on this engine (its key limit forces a prefix, which would constrain the prefix rather than the value). Split it into single-column unique indexes, or store those fields as a bounded type.`)}},U=(t,i,e)=>{const o=[],r=L(i);for(const[l,f]of Object.entries(i.shape)){if(!f._meta?.column)continue;const c=f._meta.column.notNull&&f.kind!=="optional"?" NOT NULL":"";o.push(n`${n.identifier(l)} ${n.raw(`${N(f,e,r.has(l))}${c}`)}`)}const s=[...e.frameworkColumns().map(l=>n`${n.identifier(l.name)} ${n.raw(l.type)}`),n`${n.identifier(T)} ${n.raw(e.companionTypes.integer)}`],a=s.length+o.length,u=e.maxTableColumns;if(u!==void 0&&a>u){const l=a-1===u,f="Move one or more fields into a single object field, or split the table — either way the existing rows need a data migration (`defineMigration` + `lunora migrate up`); there is no in-place path.";throw new R("VALIDATION_ERROR",`@lunora/sql-store: global table "${t}" needs ${String(a)} columns, over this engine's ${String(u)}-column limit. ${l?`One of them is "${T}", the row version every guarded write reads, which caps declared fields at ${String(u-s.length)}. A table provisioned at ${String(u)} columns before that column existed cannot be widened to hold it. `:""}${f}`)}return n.join([...s,...o],n`, `)},j=t=>{if(t.indexKeyPrefix?.("string")===void 0)return n`${d("_creationTime")}, ${d("id")}`},F=async(t,i,e)=>{if(i.name!=="sqlite")return;const r=(await w(t,i,n`SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ${e.name} AND tbl_name = ${e.table}`))[0]?.sql;if(typeof r!="string")return;const s=l=>{const f=l.indexOf("(");return f===-1?void 0:l.slice(f,l.lastIndexOf(")")+1)},a=e.unique?n`UNIQUE `:n``,u=s(v(i.name,n`CREATE ${a}INDEX ${n.identifier(e.name)} ON ${n.identifier(e.table)} (${e.columns})`).sql);if(!(u===void 0||u===s(r))){if(e.unique){const l=n.join(e.refs.map(c=>n`${c} IS NOT NULL`),n` AND `);if((await w(t,i,n`SELECT ${e.columns} FROM ${n.identifier(e.table)} WHERE ${l} GROUP BY ${e.columns} HAVING COUNT(*) > 1 LIMIT 1`)).length>0)throw new R("INTERNAL",`unique index "${e.name}" on "${e.table}" cannot be re-created with its new column list: existing rows are duplicates under it. De-duplicate the table with a data migration first; the previous index is left in place.`)}await g(t,i,n`DROP INDEX IF EXISTS ${n.identifier(e.name)}`)}},P=async(t,i,e,o)=>{k(i,e,o);const r=L(e),s=u=>{const l=d(u),f=e.shape[u],c=f&&o.indexKeyPrefix&&f._meta?.column?.unique!==!0&&!r.has(u)?o.indexKeyPrefix(h(f)):void 0;return c===void 0?l:n`${l}(${n.raw(String(c))})`},a=j(o);for(const u of e.indexes){const l=u.fields.map($=>s($)),f=n.join(l,n`, `),c=u.unique??!1,m={columns:c||a===void 0?f:n`${f}, ${a}`,name:`${i}_${u.name}`,refs:l,table:i,unique:c};await F(t,o,m),await p(t,o,m)}a!==void 0&&await p(t,o,{columns:a,name:`${i}__by_creation`,table:i,unique:!1});for(const[u,l]of M(e))l.unique&&await p(t,o,{columns:s(u),name:`${i}_unique_${u}`,table:i,unique:!0})},_="__lunora_migration_state",K=async(t,i)=>{await g(t,i,n`CREATE TABLE IF NOT EXISTS ${n.identifier(_)} (${n.identifier("name")} ${n.raw(i.companionTypes.key)} PRIMARY KEY)`)},G=async(t,i,e)=>(await w(t,i,n`SELECT ${n.identifier("name")} FROM ${n.identifier(_)} WHERE ${n.identifier("name")} = ${e}`)).length>0,Y=async(t,i,e,o,r)=>{r>0&&console.warn(`[@lunora/sql-store] bigint storage migration on "${o}": ${String(r)} row(s) hold a value the order-preserving key cannot represent (non-numeric text, or a magnitude past 39 digits) and were left as stored — range filters and ORDER BY over them stay wrong until they are corrected by hand.`);try{await g(t,i,n`INSERT INTO ${n.identifier(_)} (${n.identifier("name")}) VALUES (${e})`)}catch(s){if(!i.isUniqueViolation(s))throw s}},H=100,V=t=>Object.entries(t.shape).filter(([,i])=>i._meta?.column!==void 0&&h(i)==="bigint").map(([i])=>i),X=(t,i)=>{const e=[],o=[];for(const r of i){const s=t[r];if(!(typeof s!="string"||s.length===S))try{const a=D(BigInt(s));e.push(n`${d(r)} = ${a}`),o.push(n`${d(r)} = ${s}`)}catch{}}return{assignments:e,guards:o}},W=async(t,i,e,o)=>{const r=V(e);if(r.length===0)return;await K(t,o);const s=`bigint-key:${i}`;if(await G(t,o,s))return;const a=n.join(r.map(c=>n`(${d(c)} IS NOT NULL AND LENGTH(${d(c)}) <> ${S})`),n` OR `),u=n.join([d("id"),...r.map(c=>d(c))],n`, `);let l="",f=0;for(;;){const c=await w(t,o,n`SELECT ${u} FROM ${n.identifier(i)} WHERE (${a}) AND ${d("id")} > ${l} ORDER BY ${d("id")} LIMIT ${n.raw(String(H))}`);if(c.length===0){await Y(t,o,s,i,f);return}const m=[];for(const y of c){const{id:I}=y,{assignments:b,guards:q}=typeof I=="string"?X(y,r):{assignments:[],guards:[]};if(b.length===0){f+=1;continue}m.push(n`UPDATE ${n.identifier(i)} SET ${n.join(b,n`, `)} WHERE ${d("id")} = ${I} AND ${n.join(q,n` AND `)}`)}await B(t,o,m);const $=c.at(-1)?.id;if(typeof $!="string")return;l=$}},Q=async(t,i,e,o)=>{const r=[[T,o.companionTypes.integer],...Object.entries(e.shape).filter(([,a])=>a._meta?.column!==void 0).map(([a,u])=>[a,N(u,o)])],s=async a=>{try{return await w(t,o,n`SELECT ${n.join(a.map(u=>d(u)),n`, `)} FROM ${n.identifier(i)} LIMIT 0`),!0}catch{return!1}};if(!await s(r.map(([a])=>a)))for(const[a,u]of r)await s([a])||await g(t,o,n`ALTER TABLE ${n.identifier(i)} ADD COLUMN ${n.identifier(a)} ${n.raw(u)}`)},rn=async(t,i,e)=>{for(const[o,r]of Object.entries(i.tables)){if(r.shardMode?.kind!=="global")continue;const s=U(o,r,e);await g(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(o)} (${s})`),await Q(t,o,r,e),await P(t,o,r,e),await W(t,o,r,e)}},O=t=>t.shardMode===void 0||t.shardMode.kind==="global",sn=async(t,i,e)=>{const{integer:o,key:r,real:s}=e.companionTypes;for(const[a,u]of Object.entries(i.tables)){const l=u.aggregateIndexes;if(!(!l||l.length===0||!O(u)))for(const f of l){const c=C(a,f.name);await g(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(c)} (${n.identifier("__key__")} ${n.raw(r)} PRIMARY KEY, ${n.identifier("__value__")} ${n.raw(s)}, ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0)`),e.name==="sqlite"&&((await w(t,e,n`PRAGMA table_info(${n.identifier(c)})`)).some($=>$.name==="__count__")||await g(t,e,n`ALTER TABLE ${n.identifier(c)} ADD COLUMN ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0`))}}},E=(t,i,e,o)=>{const r=t.name==="mysql"&&o?n`${n.identifier(i)}(191)`:n`${n.identifier(i)}`;return n`${r} ${n.raw(e)}`},z=(t,i,e)=>{const o=[E(t,"__partition__","ASC",!0)];for(const[r,s]of i.sortBy.entries()){const a=e.shape[s.field],u=a!==void 0&&t.indexKeyPrefix?.(h(a))!==void 0;o.push(E(t,A(r),s.direction==="desc"?"DESC":"ASC",u))}return o.push(E(t,"__id__","ASC",!0)),o},J=(t,i,e)=>i.sortBy.map((o,r)=>{const s=e.shape[o.field],a=t.columnType(s?h(s):void 0);return n`${n.identifier(A(r))} ${n.raw(a)}`}),an=async(t,i,e)=>{const{key:o}=e.companionTypes;for(const[r,s]of Object.entries(i.tables)){const a=s.rankIndexes;if(!(!a||a.length===0||!O(s)))for(const u of a){const l=x(r,u.name),f=J(e,u,s),c=f.length>0?n`, ${n.join(f,n`, `)}`:n``;await g(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(l)} (${n.identifier("__id__")} ${n.raw(o)} PRIMARY KEY, ${n.identifier("__partition__")} ${n.raw(o)} NOT NULL${c})`);const m=z(e,u,s),$=`${r}__rank_${u.name}__btree`;await p(t,e,{columns:n.join(m,n`, `),name:$,table:l,unique:!1})}}};export{sn as runSqlAggregateMigrations,rn as runSqlGlobalTableMigrations,an as runSqlRankMigrations};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as T}from"@lunora/errors";import{decodeBigintSqlKey as I,bigintSqlKey as N,BIGINT_KEY_DIGITS as U}from"@lunora/shard-engine";const B=r=>{if(r.kind!=="optional")return r.kind;const t=r._meta?.inner;return t?B(t):r.kind},A=r=>{let t="";for(let o=0;o<r.length;o+=32768)t+=String.fromCharCode(...r.subarray(o,o+32768));return btoa(t)},k=r=>{const t=atob(r),i=new Uint8Array(t.length);for(let o=0;o<t.length;o+=1)i[o]=t.codePointAt(o)??0;return i},s="$lunora.wire$",m=64,l=1024,g="__proto__",h={BigInt64Array,BigUint64Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array},O={Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},S=r=>{if(r===null||typeof r!="object")return!1;const t=Object.getPrototypeOf(r);return t===null||t===Object.prototype},E=(r,t=0)=>{if(t>m)return!0;if(r===null)return!1;const i=typeof r;if(i==="string"||i==="boolean")return!1;if(i==="number")return!Number.isFinite(r);if(i!=="object")return!0;if(Array.isArray(r))return r.length>0&&r[0]===s?!0:r.some(o=>o===void 0||E(o,t+1));if(!S(r)||Object.hasOwn(r,g))return!0;for(const o of Object.keys(r)){const f=r[o];if(f!==void 0&&E(f,t+1))return!0}return!1},b=(r,t=0)=>{if(t>m)throw new RangeError(`wire-codec: value nesting exceeds the ${m}-level limit`);if(r===void 0)return[s,"undefined"];if(r===null)return null;const i=typeof r;if(i==="bigint")return[s,"bigint",r.toString()];if(i==="number"){const e=r;return Number.isNaN(e)?[s,"nan"]:e===1/0?[s,"inf"]:e===-1/0?[s,"-inf"]:e}if(i!=="object")return r;if(r instanceof Date)return[s,"date",b(r.getTime(),t+1)];if(r instanceof Error){const e=r,n={};for(const c of Object.keys(e)){if(e[c]===void 0)continue;const a=b(e[c],t+1);c===g?Object.defineProperty(n,c,{configurable:!0,enumerable:!0,value:a,writable:!0}):n[c]=a}const y=[s,"error",e.name,e.message,n];return e.cause!==void 0&&y.push(b(e.cause,t+1)),y}if(r instanceof URL)return[s,"url",r.href];if(r instanceof Map)return[s,"map",[...r.entries()].map(([e,n])=>[b(e,t+1),b(n,t+1)])];if(r instanceof Set)return[s,"set",[...r].map(e=>b(e,t+1))];if(r instanceof ArrayBuffer)return[s,"bytes",A(new Uint8Array(r)),"ArrayBuffer"];if(ArrayBuffer.isView(r)){const e=r,n=e.constructor.name,y=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return n==="Uint8Array"?[s,"bytes",A(y)]:[s,"bytes",A(y),n]}if(Array.isArray(r)){const e=r.map(n=>b(n,t+1));return e.length>0&&e[0]===s?[s,"arr",e]:e}if(!S(r)){const e=r.constructor?.name??"value";throw new TypeError(`wire-codec: cannot encode a ${e} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`)}const o=r,f={};for(const e of Object.keys(o)){const n=o[e];if(n===void 0)continue;const y=b(n,t+1);e===g?Object.defineProperty(f,e,{configurable:!0,enumerable:!0,value:y,writable:!0}):f[e]=y}return f},u=(r,t=0)=>{if(t>m)throw new RangeError(`wire-codec: value nesting exceeds the ${m}-level limit`);if(r===null||typeof r!="object")return r;if(Array.isArray(r)){if(r[0]===s)switch(r[1]){case"-inf":return-1/0;case"arr":return r[2].map(e=>u(e,t+1));case"bigint":{const e=r[2];if(typeof e!="string"||e.length>l||!/^-?\d+$/.test(e))throw new RangeError(`wire-codec: invalid or over-long bigint (max ${l} digits)`);return BigInt(e)}case"date":{const e=u(r[2],t+1);if(typeof e!="number")throw new TypeError("wire-codec: malformed date — epoch must be a number");return new Date(e)}case"map":{const e=r[2];return new Map(e.map(n=>{if(!Array.isArray(n)||n.length!==2)throw new TypeError("wire-codec: malformed map entry — expected a [key, value] pair");return[u(n[0],t+1),u(n[1],t+1)]}))}case"set":return new Set(r[2].map(e=>u(e,t+1)));case"url":{const e=r[2];if(typeof e!="string")throw new TypeError("wire-codec: malformed url — href must be a string");return new URL(e)}case"error":{const e=r[2],n=r[3],y=(Object.hasOwn(O,e)?O[e]:void 0)??Error,c=new y(n);c.name!==e&&Object.defineProperty(c,"name",{configurable:!0,value:e,writable:!0});const a=u(r[4],t+1);if(a===null||typeof a!="object"||Array.isArray(a))throw new TypeError("wire-codec: malformed error — props must be an object");for(const d of Object.keys(a))d===g?Object.defineProperty(c,d,{configurable:!0,enumerable:!0,value:a[d],writable:!0}):c[d]=a[d];return r.length>5&&Object.defineProperty(c,"cause",{configurable:!0,value:u(r[5],t+1),writable:!0}),c}case"bytes":{const e=r[2];if(typeof e!="string")throw new TypeError("wire-codec: malformed bytes — payload must be a base64 string");const n=k(e),y=r[3]??"Uint8Array";if(y==="ArrayBuffer")return n.buffer.byteLength===n.byteLength?n.buffer:n.slice().buffer;const c=Object.hasOwn(h,y)?h[y]:void 0;return c?new c(n.slice().buffer):n}case"inf":return 1/0;case"nan":return Number.NaN;case"undefined":return;default:return r.map(e=>u(e,t+1))}return r.map(f=>u(f,t+1))}const i=r,o={};for(const f of Object.keys(i)){const e=u(i[f],t+1);f===g?Object.defineProperty(o,f,{configurable:!0,enumerable:!0,value:e,writable:!0}):o[f]=e}return o},p=s,w=(r,t)=>r.startsWith(p)?u(t(r.slice(p.length))):t(r),R=new Set(["any","from","union"]),L=(r,t)=>{if(t!==void 0&&(typeof r=="number"||typeof r=="boolean")&&R.has(t))return p+JSON.stringify(r);if(typeof r=="boolean")return r?1:0;if(r===null||typeof r=="string"||typeof r=="number")return r;if(typeof r=="bigint")return N(r);if(r instanceof Uint8Array)return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);try{return E(r)?p+JSON.stringify(b(r)):JSON.stringify(r)}catch(i){throw new T("BAD_REQUEST",`this value cannot be stored: ${i instanceof Error?i.message:String(i)}`)}},j=r=>{try{return JSON.parse(r)}catch{return r}},_=r=>{if(typeof r!="string")return r;const t=I(r);if(t!==void 0)return t;try{return BigInt(r)}catch{return r}},C=r=>B(r),x=(r,t)=>{if(r===null)return r;switch(t){case"any":case"from":case"union":return typeof r!="string"?r:r.startsWith("{")||r.startsWith("[")||r.startsWith(p)?w(r,j):I(r)??r;case"array":case"geoPoint":case"object":case"record":return typeof r=="string"?w(r,j):r;case"bigint":return _(r);case"boolean":return r===0||r===1?r===1:r;case"bytes":return r instanceof ArrayBuffer?r:ArrayBuffer.isView(r)?new Uint8Array(r.buffer,r.byteOffset,r.byteLength).slice().buffer:r;default:return r}},K=U+1;export{K as B,x as a,b,_ as c,u as d,C as e,E as n,L as s,j as t};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/sql-store",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.109",
|
|
4
4
|
"description": "Internal dialect-parameterized SQL store core for Lunora .global() backends (D1, PlanetScale)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
54
|
-
"@lunora/shard-engine": "1.0.0-alpha.
|
|
53
|
+
"@lunora/errors": "1.0.0-alpha.32",
|
|
54
|
+
"@lunora/shard-engine": "1.0.0-alpha.56",
|
|
55
55
|
"drizzle-orm": "^0.45.2"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const o="_version";export{o as OCC_VERSION_COLUMN};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import"./ctx-db-search-state-DRx8DyOa.mjs";import{d as S,c as s,a as i,r as l}from"./ctx-db-search-BT7H8AM6.mjs";import"./decodeGlobalRow-C76NMu9P.mjs";export{S as backfillSqlSearchIndexes,s as createSearchSync,i as runSqlSearch,l as runSqlSearchMigrations};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import{LunoraError as g}from"@lunora/errors";import{m as bt,D as Ct}from"./ctx-db-search-state-DRx8DyOa.mjs";import{c as At,r as Lt,A as kt,C as Nt,R as Mt,a as Dt,T as qt,x as Bt,b as Ot}from"./ctx-db-search-BT7H8AM6.mjs";import{d as ai}from"./ctx-db-search-BT7H8AM6.mjs";import{literalInList as Wt,throwingScheduler as Ft,CDC_LOG_TABLE as V,cursorBelowRetainedFloor as Ut,cdcTrimmedError as xt,assertNoExplicitUndefined as Qe,hasTrigger as Pt,runRowValidators as Ne,assertFlatPredicate as ze,mergeWhere as q,rankTableName as le,sortColumnName as $e,resolveRankPartition as Je,encodePartitionKey as Ie,resolveRankSeekTuple as Ht,RANK_TIEBREAK as _e,CountRlsUnsupportedError as Ze,normalizeIdStructurally as Kt,assertValidClientId as jt,aggregateSqlFunction as fe,softDeleteScope as ue,compileWhereSql as we,normalizeOrderKeys as Gt,buildSeekWhere as Vt,decodeCursor as Yt,resolveRelationPredicates as et,resolveWith as tt,relationHooks as nt,applySelect as it,encodeCursor as Xt,NotFoundError as Qt,applyOnDelete as zt,ConflictError as be,normalizeCountArgument as Jt,selectIndexForCount as Zt,encodeAggregateKey as X,aggregateTableName as J,selectIndexForAggregate as en,readAggregateValue as Me,runTriggers as tn,matchesRankStaticWhere as ot,CURSOR_PREFIX as nn,selectIndexForGroupBy as on,tiebreakDirectionFor as rn,fanOutScalarCounts as sn,matchesStaticWhere as De,foldAggregateTally as an,coerceAggregateNumber as qe,CDC_LOG_TABLE_SEQ_INDEX as cn,NotUniqueError as dn}from"@lunora/shard-engine";import{sql as e}from"drizzle-orm";import{d as wt,e as ln,n as fn,b as un}from"./value-codec-DMP5JYuK.mjs";import{runSqlGlobalTableMigrations as wn,runSqlAggregateMigrations as $n,runSqlRankMigrations as _n}from"./runSqlAggregateMigrations-BAp7JaB-.mjs";import{queryAll as I,queryRun as k,createIndexIfNotExists as hn,decodeRow as ee,serializeColumnValue as G,columnRefSql as W,decodeRows as En,tableColumns as $t,forEachRowPaged as rt,queryBatch as st}from"./decodeGlobalRow-C76NMu9P.mjs";import{decodeGlobalRow as di}from"./decodeGlobalRow-C76NMu9P.mjs";import{OCC_VERSION_COLUMN as at}from"./OCC_VERSION_COLUMN-ByC64Jgx.mjs";const yn=(a,c)=>{if(a.size<c)return;const $=a.keys().next().value;$!==void 0&&a.delete($)},ct=50,pn=(a,c={})=>{const $=c.maxBatch??ct,r=Number.isInteger($)&&$>0?$:ct,p=new Map;let w=!1;const l=async(S,m)=>{const v=[...m.keys()];for(let N=0;N<v.length;N+=r){const B=v.slice(N,N+r);try{const F=await a(S,B);for(const Y of B){const P=F.get(Y);for(const Ce of m.get(Y)??[])Ce.resolve(P)}}catch(F){for(const Y of B)for(const P of m.get(Y)??[])P.reject(F)}}},b=()=>{w=!1;const S=[...p.entries()];p.clear();for(const[m,v]of S)l(m,v).catch(()=>{})};return{async load(S,m){return new Promise((v,N)=>{let B=p.get(S);B||(B=new Map,p.set(S,B));const F=B.get(m);F?F.push({reject:N,resolve:v}):B.set(m,[{reject:N,resolve:v}]),w||(w=!0,queueMicrotask(b))})}}},Rn=new Set(["_id","id"]),We=(a,c,$)=>a==="postgres"?e`${c} IS NOT DISTINCT FROM ${$}`:a==="mysql"?e`${c} <=> ${$}`:e`${c} IS ${$}`,Sn=(a,c)=>a.name!=="postgres"||c.nullable!==!0?"":c.direction==="desc"?" NULLS LAST":" NULLS FIRST",Tn=(a,c)=>{const $=a.map(r=>e`${W(r.field)} ${e.raw(`${r.direction==="desc"?"DESC":"ASC"}${Sn(c,r)}`)}`);if(!a.some(r=>Rn.has(r.field))){const r=rn(a);$.push(e`${W("id")} ${e.raw(r==="desc"?"DESC":"ASC")}`)}return e.join($,e`, `)},dt=50,gn=(a,c,$)=>{const r={...c};for(const[p,w]of $t(a)){if(w.serverDefault){r[p]=w.serverDefault({auth:$});continue}r[p]===void 0&&(w.defaultFn?r[p]=w.defaultFn():"defaultValue"in w&&(r[p]=w.defaultValue))}return r},lt=(a,c,$,r)=>{for(const[p,w]of $t(a)){if(w.serverDefault){p in c&&($[p]=w.serverDefault({auth:r}));continue}w.onUpdateFn&&!(p in c)&&($[p]=w.onUpdateFn())}},vn=128,mn=()=>{const a=new Map;return{delete:c=>{a.delete(c)},get:c=>{const $=a.get(c);if($!==void 0)return a.delete(c),a.set(c,$),$},set:(c,$)=>{a.has(c)?a.delete(c):yn(a,vn),a.set(c,$)}}},In=async(a,c,$,r,p)=>{const w=p.get(r);if(w!==void 0)return w;const l=Object.entries($.tables).filter(([,S])=>S.shardMode===void 0||S.shardMode.kind==="global").map(([S])=>S),b=await Promise.all(l.map(async S=>({found:(await I(a,c,e`SELECT 1 FROM ${e.identifier(S)} WHERE ${e.identifier("id")} = ${r} LIMIT 1`)).length>0,tableName:S})));for(const S of b)if(S.found)return p.set(r,S.tableName),S.tableName},te=a=>a==null?null:Number(a),bn=(a,c)=>c.map($=>{const r={};for(const p of a)r[p]=$[p]??null;return{key:r,value:te($.value)}}),Cn=(a,c,$)=>[...a.partitionBy??[],...a.sortBy.map(p=>p.field),...a.where?Object.keys(a.where):[]].every(p=>c[p]===$[p]),_t=a=>a.length===1?a[0]:e`(${e.join(a,e` AND `)})`,Z=a=>e.join(a.map(c=>e`${e.identifier(c)}`),e`, `),ne=a=>e.join(a.map(c=>e`${c}`),e`, `),An=(a,c,$,r,p)=>{const w=[];for(let l=0;l<$.length+1;l+=1){const b=$.slice(0,l).map(v=>We(a,e`${e.identifier(v)}`,r[v])),S=$[l],m=c.sortBy[l];l<$.length&&S!==void 0&&m!==void 0?b.push(e`${e.identifier(S)} ${e.raw(m.direction==="desc"?">":"<")} ${r[S]}`):b.push(e`${e.identifier(_e)} < ${p}`),w.push(_t(b))}return w.length>0?e.join(w,e` OR `):void 0},Ln=(a,c,$)=>{if($.length!==c.length)return;const r=[];for(const[p,w]of c.entries()){const l=[];for(let b=0;b<p;b+=1){const S=c[b];S!==void 0&&l.push(We(a,e`${e.identifier(S.column)}`,$[b]))}l.push(e`${e.identifier(w.column)} ${e.raw(w.direction==="desc"?"<":">")} ${$[p]}`),r.push(_t(l))}return e`(${e.join(r,e` OR `)})`},kn=(a,c)=>{if(a.sortBy.length===0)throw new g("INTERNAL",`rankIndex "${a.name}" requires at least one "sortBy" column for stable pagination`);const $=[{column:"__partition__",direction:"asc"}];for(const[r,p]of a.sortBy.entries())$.push({column:c[r]??$e(r),direction:p.direction});return $.push({column:_e,direction:"asc"}),$},Nn=async(a,c,$,r,p)=>{const l=[];for(let v=0;v<p.length;v+=50)l.push(p.slice(v,v+50));const b=await Promise.all(l.map(async v=>I(a,c,e`SELECT * FROM ${e.identifier(r)} WHERE ${e.identifier("id")} IN (${ne(v)})`))),S=new Map;for(const v of b)for(const N of v)S.set(N.id,N);const m=[];for(const v of p){const N=ee($,S.get(v));N&&m.push(N)}return m},Mn=a=>{const c=JSON.stringify(a),$=new TextEncoder().encode(c);let r="";for(const p of $)r+=String.fromCodePoint(p);return nn+btoa(r)},Be=(a,c,$)=>{const r=a.shape[c];if(r!==void 0&&ln(r)==="bigint")throw new g("BAD_REQUEST",`${$}: "${c}" is stored as an order-preserving key, which SQL cannot reduce or group — declare an aggregateIndex covering this (by, field, op) so the maintained companion answers it instead (its running total is a REAL, so it stays exact only while the total is inside 2^53)`)},Dn=(a,c,$,r)=>{for(const p of $)Be(a,p,`groupBy(${c}, { by: [..."${p}"] })`);r.field!==void 0&&Be(a,r.field,`groupBy(${c}, { agg: { op: "${r.op}", field: "${r.field}" } })`)},Oe="__cdc_sweep",ht="global",qn=6e4,Bn=1e4,On=3e4;let ft=0;const Wn=async(a,c)=>{const{autoincrementPrimaryKey:$,key:r,real:p,text:w}=c.companionTypes;await k(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(V)} (${e.identifier("seq")} ${e.raw($)}, ${e.identifier("ts")} ${e.raw(p)} NOT NULL, ${e.identifier("table")} ${e.raw(r)} NOT NULL, ${e.identifier("id")} ${e.raw(r)} NOT NULL, ${e.identifier("op")} ${e.raw(r)} NOT NULL, ${e.identifier("doc")} ${e.raw(w)})`);const l=c.indexKeyPrefix?.("string");await hn(a,c,{columns:e`${l===void 0?e`${e.identifier("table")}`:e`${e.identifier("table")}(${e.raw(String(l))})`}, ${e.identifier("seq")}`,name:cn,table:V,unique:!1}),await k(a,c,e`CREATE TABLE IF NOT EXISTS ${e.identifier(Oe)} (${e.identifier("id")} ${e.raw(r)} NOT NULL PRIMARY KEY, ${e.identifier("lease_until")} ${e.raw(p)} NOT NULL)`),await k(a,c,e`INSERT INTO ${e.identifier(Oe)} (${e.identifier("id")}, ${e.identifier("lease_until")}) VALUES (${ht}, ${0}) ${c.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${e.identifier("id")} = ${e.identifier("id")}`:e`ON CONFLICT(${e.identifier("id")}) DO NOTHING`}`)},Fn=async(a,c,$)=>{const r=e`UPDATE ${e.identifier(Oe)} SET ${e.identifier("lease_until")} = ${$+qn} WHERE ${e.identifier("id")} = ${ht} AND ${e.identifier("lease_until")} <= ${$}`;if(c.supportsReturning)return(await I(a,c,e`${r} RETURNING ${e.identifier("id")}`)).length>0;const p=await k(a,c,r);return(c.affectedRows?c.affectedRows(p??{rowsAffected:0}):0)>0},Un=async(a,c,$,r)=>{if(!await Fn(a,c,r))return;const p=r-$;await k(a,c,e`
|
|
2
|
-
DELETE FROM ${e.identifier(V)} WHERE ${e.identifier("seq")} IN (
|
|
3
|
-
SELECT ${e.identifier("seq")} FROM (
|
|
4
|
-
SELECT ${e.identifier("seq")} FROM ${e.identifier(V)} WHERE ${e.identifier("ts")} <= ${p} ORDER BY ${e.identifier("seq")} ASC LIMIT ${e.raw(String(Bn))}
|
|
5
|
-
) AS ${e.identifier("expired")}
|
|
6
|
-
)
|
|
7
|
-
`)},Et=async(a,c)=>{const $=await I(a,c,e`SELECT MIN(${e.identifier("seq")}) AS ${e.identifier("seq")} FROM ${e.identifier(V)}`),r=Number($[0]?.seq??Number.NaN);return Number.isFinite(r)?r:void 0},xn=a=>JSON.stringify(fn(a)?un(a):a),Pn=async(a,c,$,r,p,w,l)=>{await k(a,l,e`INSERT INTO ${e.identifier(V)} (${e.identifier("ts")}, ${e.identifier("table")}, ${e.identifier("id")}, ${e.identifier("op")}, ${e.identifier("doc")}) VALUES (${c}, ${$}, ${r}, ${p}, ${w===void 0?null:xn(w)})`)},ii=async(a,c,$)=>{const r=c.sinceSeq??0,p=Math.max(1,Math.min(c.limit??1e3,1e4)),w=await Et(a,$);if(w!==void 0&&Ut(w,r))throw xt(w,r,"global");const b=(await I(a,$,e`SELECT seq, ts, ${e.identifier("table")}, id, op, doc FROM ${e.identifier(V)} WHERE seq > ${r} ORDER BY seq ASC LIMIT ${e.raw(String(p))}`)).map(S=>{const{doc:m}=S,v={id:String(S.id),op:String(S.op),seq:Number(S.seq),table:String(S.table),ts:Number(S.ts)};return typeof m=="string"?{...v,doc:wt(JSON.parse(m))}:v});return{changes:b,cursor:b.at(-1)?.seq??r}},Hn=async(a,c,$,r={})=>{if(r.cursorOnly){const S=await I(a,$,e`SELECT MAX(seq) AS seq FROM ${e.identifier(V)}`),m=Number(S[0]?.seq??c);return{cursor:Number.isFinite(m)?m:c,tables:[]}}const p=await I(a,$,e`SELECT ${e.identifier("table")} AS ${e.identifier("table")}, MAX(seq) AS seq FROM ${e.identifier(V)} WHERE seq > ${c} GROUP BY ${e.identifier("table")}`);let w=c;const l=[];for(const S of p){const m=Number(S.seq);Number.isFinite(m)&&m>w&&(w=m),l.push(String(S.table))}const b=r.retained===!0?await Et(a,$):void 0;return{cursor:w,tables:l,...b===void 0?{}:{floor:b}}},Kn=/string or blob too big/iu,jn=/row is too big/iu,Gn=(a,c)=>{const{code:$,errno:r}=c,p=c instanceof Error?c.message:"";switch(a.name){case"mysql":return r===1118||$==="ER_TOO_BIG_ROWSIZE"?"InnoDB's per-row ceiling — roughly 8 KB, half a 16 KB page, for the part of the row stored inline":void 0;case"postgres":return jn.test(p)?"the 8 KB heap page a tuple must fit once its wide columns have been TOASTed out":void 0;default:return Kn.test(p)?"the storage engine's per-row ceiling (2 MB on D1)":void 0}},Vn=(a,c,$)=>{const r=Gn(a,c);if(r!==void 0)throw new g("PAYLOAD_TOO_LARGE",`document is too large to store in "${$}": a single row cannot exceed ${r}. The limit is on the STORED bytes, which are UTF-8. Keep the payload in R2 (ctx.storage) and store a reference on the row.`)},ut=(a,c,$)=>{throw a.isUniqueViolation(c)?new be(`unique constraint violation on "${$}"`,"unique"):(Vn(a,c,$),c)},oi=a=>{const{crossShardCounter:c,crossShardReader:$,exec:r,maxRelationKeys:p,schema:w}=a,{dialect:l}=a,b={fieldRef:W,serialize:G,...l.name==="mysql"?{containsExpr:(t,n)=>e`LOCATE(${n}, ${t}) > 0`}:{},...l.name==="postgres"?{containsExpr:(t,n)=>e`strpos(${t}, ${n}) > 0`}:{},...l.name==="sqlite"?{}:{inList:Wt}},S=(t,n)=>We(l.name,t,n),m=t=>{const n=Z(t.columns),i=ne(t.values),o=f=>l.name==="mysql"?e`VALUES(${e.identifier(f)})`:e`excluded.${e.identifier(f)}`,s=f=>l.name==="postgres"?e`${e.identifier(t.table)}.${e.identifier(f)}`:e`${e.identifier(f)}`,u=e.join(Object.entries(t.set(o,s)).map(([f,h])=>e`${e.identifier(f)} = ${h}`),e`, `),E=l.name==="mysql"?e`ON DUPLICATE KEY UPDATE ${u}`:e`ON CONFLICT(${e.identifier(t.conflictKey)}) DO UPDATE SET ${u}`;return e`INSERT INTO ${e.identifier(t.table)} (${n}) VALUES (${i}) ${E}`},v=a.clock??(()=>Date.now()),N=a.idGenerator??(()=>crypto.randomUUID()),B=a.cdc??!1,F=typeof a.cdcRetentionMs=="number"&&Number.isFinite(a.cdcRetentionMs)&&a.cdcRetentionMs>0?a.cdcRetentionMs:void 0,Y=a.auth??{identity:null,userId:null},P=async(t,n,i,o)=>{if(!B||(await Pn(r,v(),t,n,i,o,l),F===void 0))return;const s=v();if(!(s-ft<On)){ft=s;try{await Un(r,l,F,s)}catch{}}},Ce=a.scheduler??Ft,Ae=mn();let he=0,Le;const U=async()=>(Le??=(async()=>{await wn(r,w,l),await $n(r,w,l),await _n(r,w,l),await bt(r,l),await Lt(r,w,l),B&&await Wn(r,l)})().catch(t=>{throw Le=void 0,t}),Le),ie=async(t,n)=>{await U();const i=await In(r,l,w,t,Ae);if(!(n!==void 0&&i!==n))return i},oe=new Map,Fe=async(t,n)=>{const i=J(t,n);return(await I(r,l,l.tableExists(i))).length>0},Ee=async(t,n)=>{const i=`${t}::${n.name}`,o=oe.get(i);if(o!==void 0)return o;if(!await Fe(t,n.name))return oe.set(i,!1),!1;const u=w.tables[t];if(!u)return oe.set(i,!1),!1;const E=n.by??[],f=new Map;await rt(r,l,u,t,_=>{if(n.where&&!De(_,n.where))return;const y=X(E,_);an(f,y,n,_)});const h=J(t,n.name);await k(r,l,e`DELETE FROM ${e.identifier(h)}`);const d=[];for(const[_,y]of f)d.push(e`INSERT INTO ${e.identifier(h)} (${e.identifier("__key__")}, ${e.identifier("__value__")}, ${e.identifier("__count__")}) VALUES (${_}, ${y.value}, ${y.count})`);return await st(r,l,d),oe.set(i,!0),!0},yt=async(t,n,i)=>{const o=fe(n.op),s=n.field??"",u=[];for(const h of n.by??[]){const d=G(i[h]??null);u.push(d===null?e`${W(h)} IS NULL`:e`${W(h)} = ${d}`)}for(const[h,d]of Object.entries(n.where??{})){const _=d!==null&&typeof d=="object"&&!Array.isArray(d)?d.eq:d,y=G(_);u.push(y===null?e`${W(h)} IS NULL`:e`${W(h)} = ${y}`)}const E=e`SELECT ${e.raw(o)}(${W(s)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}`,f=await I(r,l,u.length>0?e`${E} WHERE ${e.join(u,e` AND `)}`:E);return te(f[0]?.value)},pt=async(t,n)=>{await k(r,l,e`DELETE FROM ${e.identifier(t)} WHERE ${e.identifier("__key__")} = ${n} AND ${e.identifier("__count__")} <= 0`)},Rt=async(t,n,i,o)=>{const s=J(t,n.name),{op:u}=n,E=n.field??"",f=i&&(!n.where||De(i,n.where))?i:void 0,h=o&&(!n.where||De(o,n.where))?o:void 0;if(!(!f&&!h)){if(u==="count"||u==="sum"||u==="avg"){const d=new Set;for(const[_,y]of[[f,-1],[h,1]]){if(!_)continue;const T=u==="count"?1:qe(_[E]);if(T===void 0)continue;const R=X(n.by??[],_);d.add(R),await k(r,l,m({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(C,A)=>({__count__:e`${A("__count__")} + ${C("__count__")}`,__value__:u==="count"?e`${A("__value__")} + ${C("__value__")}`:e`COALESCE(${A("__value__")}, 0) + ${C("__value__")}`}),table:s,values:[R,y*T,y]}))}for(const _ of d)await pt(s,_);return}if(f){const d=X(n.by??[],f),_=qe(f[E]),T=(await I(r,l,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${d}`))[0],R=te(T?.value),C=(T?.count??0)-1;if(C<=0)await k(r,l,e`DELETE FROM ${e.identifier(s)} WHERE ${e.identifier("__key__")} = ${d}`);else if(T&&_!==void 0&&R!==null&&_===R){const A=await yt(t,n,f);await k(r,l,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__value__")} = ${A}, ${e.identifier("__count__")} = ${C} WHERE ${e.identifier("__key__")} = ${d}`)}else await k(r,l,e`UPDATE ${e.identifier(s)} SET ${e.identifier("__count__")} = ${e.identifier("__count__")} - 1 WHERE ${e.identifier("__key__")} = ${d}`)}if(h){const d=X(n.by??[],h),_=qe(h[E]);if(_===void 0)await k(r,l,m({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(y,T)=>({__count__:e`${T("__count__")} + 1`}),table:s,values:[d,null,1]}));else{const y=u==="min"?"MIN":"MAX";await k(r,l,m({columns:["__key__","__value__","__count__"],conflictKey:"__key__",set:(T,R)=>({__count__:e`${R("__count__")} + 1`,__value__:e`${e.raw(y)}(COALESCE(${R("__value__")}, ${T("__value__")}), ${T("__value__")})`}),table:s,values:[d,_,1]}))}}}},ye=async t=>{const n=w.tables[t]?.aggregateIndexes;if(!(!n||n.length===0))for(const i of n)await Ee(t,i)},re=async(t,n,i)=>{const o=w.tables[t]?.aggregateIndexes;if(!(!o||o.length===0))for(const s of o){const u=`${t}::${s.name}`;(oe.get(u)??await Fe(t,s.name))&&await Rt(t,s,n,i)}},se=new Map,Ue=async(t,n)=>{const i=le(t,n);return(await I(r,l,l.tableExists(i))).length>0},ke=async(t,n)=>{const i=`${t}::rank::${n.name}`,o=se.get(i);if(o!==void 0)return o;if(!await Ue(t,n.name))return se.set(i,!1),!1;const u=w.tables[t];if(!u)return se.set(i,!1),!1;const E=le(t,n.name);await k(r,l,e`DELETE FROM ${e.identifier(E)}`);const f=n.sortBy.map((y,T)=>$e(T)),h=Z(["__id__","__partition__",...f]),d=[];await rt(r,l,u,t,y=>{if(n.where&&!ot(y,n.where))return;const T=Ie(n.partitionBy??[],y),R=n.sortBy.map(C=>G(y[C.field]??null));d.push([y._id,T,...R])});const _=d.map(y=>e`INSERT INTO ${e.identifier(E)} (${h}) VALUES (${ne(y)})`);return await st(r,l,_),se.set(i,!0),!0},pe=async t=>{const n=w.tables[t]?.rankIndexes;if(!(!n||n.length===0))for(const i of n)await ke(t,i)},Q=async(t,n,i,o)=>{const s=w.tables[t],u=s?.rankIndexes;if(!u||u.length===0)return;const E=s?.softDeleteMode?.field,f=E===void 0||o===void 0?void 0:o[E],h=f!=null;for(const d of u){if(i!==void 0&&o!==void 0&&Cn(d,i,o))continue;const _=`${t}::rank::${d.name}`;if(!(se.get(_)??await Ue(t,d.name)))continue;const R=le(t,d.name);if(i&&await k(r,l,e`DELETE FROM ${e.identifier(R)} WHERE ${e.identifier("__id__")} = ${n}`),o&&!h){if(d.where&&!ot(o,d.where))continue;const C=d.sortBy.map((M,j)=>$e(j)),A=Z(["__id__","__partition__",...C]),O=Ie(d.partitionBy??[],o),L=d.sortBy.map(M=>G(o[M.field]??null));await k(r,l,e`INSERT INTO ${e.identifier(R)} (${A}) VALUES (${ne([n,O,...L])})`)}}},ae=At({dialect:l,exec:r,schema:w}),xe=new Set;for(const[t,n]of Object.entries(w.tables))for(const i of Object.values(n.triggerMap??{}))xe.add(`${t} ${i.timing} ${i.op}`);const H=(t,n,i)=>xe.has(`${t} ${n} ${i}`);let Pe;const K=async(t,n,i)=>{if(he+=1,he>dt)throw he-=1,new be(`trigger recursion exceeded ${String(dt)} levels on "${i.table}" — check for a self-triggering write`,"trigger");try{await tn({ctx:Pe,event:i,op:n,schema:w,tableName:i.table,timing:t})}finally{he-=1}},St=async(t,n)=>{try{await k(r,l,n)}catch(i){ut(l,i,t)}},Re=async(t,n)=>(await I(r,l,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} = ${n}`))[0],Se=async(t,n,i,o)=>{if(o===void 0)return;const s=e`${e.identifier(at)}`,u=e.join([S(e`${e.identifier("id")}`,o.id),S(s,o[at]??null)],e` AND `),E=n==="UPDATE"?e`UPDATE ${e.identifier(t)} SET ${i}, ${s} = COALESCE(${s}, 0) + 1 WHERE ${u}`:e`DELETE FROM ${e.identifier(t)} WHERE ${u}`,f=()=>{throw new be(`optimistic concurrency conflict on "${t}" — the row changed during this mutation; refetch and retry`,"occ")};try{if(l.supportsReturning)(await I(r,l,e`${E} RETURNING ${e.identifier("id")}`)).length===0&&f();else{const h=await k(r,l,E);(l.affectedRows?l.affectedRows(h??{rowsAffected:0}):0)===0&&f()}}catch(h){ut(l,h,t)}},Tt=(t,n,i,o)=>{const s=Object.keys(t.shape);return{columns:["id","_creationTime",...s],values:[n,i,...s.map(u=>G(o[u]??null))]}},gt=async(t,n,i,o)=>{const s=on(n,i.op,i.field,o.by,o.where);if(!s||!await Ee(t,s.index))return;const E=J(t,s.index.name),f=Object.keys(s.partial);if(f.length===(s.index.by??[]).length&&f.length>0){const d=X(s.index.by??[],s.partial),_=await I(r,l,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(E)} WHERE ${e.identifier("__key__")} = ${d}`);if(_.length===0)return[];const y=_[0];return[{key:{...s.partial},value:Me(i.op,{count:y.count,value:te(y.value)})}]}return f.length>0?void 0:(await I(r,l,e`SELECT ${e.identifier("__key__")} AS ${e.identifier("key")}, ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(E)}`)).map(d=>{const _=d;return{key:wt(JSON.parse(_.key)),value:Me(i.op,{count:_.count,value:te(_.value)})}})},He=t=>{const n=w.tables[t]?.shardMode?.kind;return n==="shardBy"||n==="root"},Ke=t=>{throw new g("INTERNAL",`cross-backend relation: a global table cannot load the shard-local relation '${t}' (it spans every shard) — wire a cross-shard reader to support it`)},vt=(t,n)=>{if(n?.relationMask!==void 0&&n.with!==void 0&&Object.keys(n.with).length>0)throw new g("MASK_UNSUPPORTED",`masking cannot follow a nested \`with\` across the cross-shard relation hop to "${t}" — the nested rows are hydrated on the serving shard, where the mask policy does not exist. Read the nested relation at its own (masked) call site instead.`);const i={};if(n?.relationBaseWhere)for(const o of Object.keys(w.tables)){const s=n.relationBaseWhere(o);s!==void 0&&(i[o]=s)}return{orderBy:n?.orderBy,relationPolicies:i,where:q(n?.baseWhere,n?.where),with:n?.with}},je=(t,n)=>He(t)?$?$(t,vt(t,n)):Ke(t):x.findMany(t,n),Te=(t,n,i)=>et(t,{fetcher:je,maxRelationKeys:p,relationBaseWhere:i,schema:w,tableName:n}),mt=pn(async(t,n)=>{const i=await I(r,l,e`SELECT * FROM ${e.identifier(t)} WHERE ${e.identifier("id")} IN (${ne(n)})`),o=new Map;for(const s of i){const{id:u}=s;typeof u=="string"&&o.set(u,s)}return o}),x={async cdcChangedTables(t,n){if(B)try{return await U(),await Hn(r,t,l,{...n,retained:F!==void 0})}catch{return}},async aggregate(t,n){const i=w.tables[t];if(!i)throw new g("INTERNAL",`unknown table: ${t}`);if(await U(),fe(n.op),n.op==="count")return x.count(t,{baseWhere:n.baseWhere,relationBaseWhere:n.relationBaseWhere,restrictsCounts:n.restrictsCounts,where:n.where});if(!n.field)throw new g("INTERNAL",`aggregate(${t}, { op: "${n.op}" }): "field" is required for non-count reducers`);const o=ue(i.softDeleteMode,void 0),s=q(q(n.baseWhere,n.where),o),u=await Te(s,t,n.relationBaseWhere),E=u!==s;if(i.aggregateIndexes&&!n.baseWhere&&!E&&!o){const T=en(i.aggregateIndexes,n.op,n.field,n.where);if(T&&await Ee(t,T.index)){const C=X(T.index.by??[],T.key),A=J(t,T.index.name),L=(await I(r,l,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")}, ${e.identifier("__count__")} AS ${e.identifier("count")} FROM ${e.identifier(A)} WHERE ${e.identifier("__key__")} = ${C}`))[0];return Me(n.op,L===void 0?void 0:{count:L.count,value:te(L.value)})}}Be(i,n.field,`aggregate(${t}, { op: "${n.op}", field: "${n.field}" })`);const f=we(u,b),h=e.raw(fe(n.op)),d=e`SELECT ${h}(${W(n.field)}) AS ${e.identifier("value")} FROM ${e.identifier(t)}`,y=(await I(r,l,f?e`${d} WHERE ${f}`:d))[0]?.value;return y==null?null:Number(y)},async count(t,n){const i=w.tables[t];if(!i)throw new g("INTERNAL",`unknown table: ${t}`);await U();const o=Jt(n);if(o.restrictsCounts)throw new Ze(t);const s=ue(i.softDeleteMode,void 0),u=q(q(o.baseWhere,o.where),s),E=await Te(u,t,o.relationBaseWhere),f=E!==u;if(i.aggregateIndexes&&!o.baseWhere&&!f&&!s){const y=Zt(i.aggregateIndexes,o.where);if(y&&await Ee(t,y.index)){const R=X(y.index.by??[],y.key),C=J(t,y.index.name),A=await I(r,l,e`SELECT ${e.identifier("__value__")} AS ${e.identifier("value")} FROM ${e.identifier(C)} WHERE ${e.identifier("__key__")} = ${R}`);return Number(A[0]?.value??0)}}const h=we(E,b),d=e`SELECT COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(t)}`,_=await I(r,l,h?e`${d} WHERE ${h}`:d);return Number(_[0]?.count??0)},async delete(t,n,i){const o=await ie(t,n);if(!o)return;const s=w.tables[o];if(!s)return;const u=await Re(o,t),E=ee(s,u),f=i?.hard===!0,h=!f&&s.softDeleteMode?s.softDeleteMode.field:void 0;if(!(h&&(!E||E[h]!==null&&E[h]!==void 0))){if(H(o,"before","delete")&&await K("before","delete",{id:t,op:"delete",previous:E??void 0,table:o}),await zt({deletedId:t,deletedReference:d=>E?.[d],findHolders:async(d,_,y)=>{if(w.tables[d]?.shardMode?.kind==="shardBy")throw new g("INTERNAL",`cross-backend cascade from global '${o}' into shardBy '${d}' is not supported — would require Query Coordinator fan-out across shards`);return(await x.findMany(d,{includeDeleted:f,where:{[_]:y}})).page},onCascade:(d,_)=>x.delete(_,void 0,i),onRestrict:d=>{throw new be(d,"restrict")},onSetNull:(d,_,y)=>x.patch(_,{[y]:null}),schema:w,tableName:o}),await ye(o),await pe(o),h&&E){const d={...E,[h]:v()},_=e.join(Object.keys(s.shape).map(y=>e`${e.identifier(y)} = ${G(d[y]??null)}`),e`, `);await Se(o,"UPDATE",_,u),await re(o,E,d),await Q(o,t,E,void 0),await ae(o,t,d,E),await P(o,t,"update",d),H(o,"after","delete")&&await K("after","delete",{id:t,op:"delete",previous:E,table:o});return}await Se(o,"DELETE",void 0,u),Ae.delete(t),await re(o,E??void 0,void 0),await Q(o,t,E??void 0,void 0),await ae(o,t,void 0),await P(o,t,"delete"),H(o,"after","delete")&&await K("after","delete",{id:t,op:"delete",previous:E??void 0,table:o})}},async findFirst(t,n={}){return(await x.findMany(t,{...n,limit:1})).page[0]??null},async findFirstOrThrow(t,n={}){const i=await x.findFirst(t,n);if(i===null)throw new Qt(`findFirstOrThrow: no "${t}" document matched`);return i},async findMany(t,n={}){const i=w.tables[t];if(!i)throw new g("INTERNAL",`unknown table: ${t}`);await U();const o=Gt(n.orderBy,i.shape),s=n.cursor?Vt(o,Yt(n.cursor)):void 0,u=je,E=async(L,M,j,ge)=>{if(He(L))return c?sn(c,L,M,j,ge):Ke(L);const ve=w.tables[L];if(!ve)throw new g("INTERNAL",`unknown table: ${L}`);const me=ue(ve.softDeleteMode,void 0),z={[M]:{in:j}},D=q(q(z,ge),me),ce=await Te(D,L,void 0),Ge=we(ce,b),Ve=W(M);let de=e`SELECT ${Ve} AS __fk__, COUNT(*) AS ${e.identifier("count")} FROM ${e.identifier(L)}`;Ge&&(de=e`${de} WHERE ${Ge}`),de=e`${de} GROUP BY ${Ve}`;const It=await I(r,l,de),Ye=new Map;for(const Xe of It)Ye.set(Xe.__fk__,Number(Xe.count??0));return Ye};let f=q(n.baseWhere,n.where);f=q(f,ue(i.softDeleteMode,n.includeDeleted)),f=await et(f,{fetcher:u,maxRelationKeys:p,relationBaseWhere:n.relationBaseWhere,schema:w,tableName:t}),s&&(f=f?{AND:[f,s]}:s);const h=we(f,b),d=Tn(o,l);let _=e`SELECT * FROM ${e.identifier(t)}`;h&&(_=e`${_} WHERE ${h}`),_=e`${_} ORDER BY ${d}`;const y=typeof n.limit=="number"?Math.max(0,Math.floor(n.limit)):void 0;y!==void 0&&(_=e`${_} LIMIT ${e.raw(String(y+1))}`);const T=await I(r,l,_),R=En(i,T);if(y===void 0)return n.with&&await tt({fetcher:u,groupedCounter:E,parents:R,...nt(n),schema:w,tableName:t,with:n.with}),{continueCursor:null,isDone:!0,page:it(R,n.select,n.with)};const C=R.length>y,A=C?R.slice(0,y):R,O=A.at(-1);return n.with&&await tt({fetcher:u,groupedCounter:E,parents:A,...nt(n),schema:w,tableName:t,with:n.with}),{continueCursor:C&&O?Xt(O,o):null,isDone:!C,page:it(A,n.select,n.with)}},async get(t,n){const i=await ie(t,n);if(!i)return null;const o=w.tables[i];return o?ee(o,await mt.load(i,t)):null},async groupBy(t,n){const i=w.tables[t];if(!i)throw new g("INTERNAL",`unknown table: ${t}`);await U();const o=n.agg??{op:"count"};if(fe(o.op),o.op!=="count"&&!o.field)throw new g("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);const s=ue(i.softDeleteMode,void 0),u=q(q(n.baseWhere,n.where),s),E=await Te(u,t,n.relationBaseWhere),f=E!==u;if(i.aggregateIndexes&&!n.baseWhere&&!f&&!s){const R=await gt(t,i.aggregateIndexes,o,n);if(R!==void 0)return R}Dn(i,t,n.by,o);const h=we(E,b),d=n.by.map(R=>e`${W(R)} AS ${e.identifier(R)}`);if(o.op==="count")d.push(e`COUNT(*) AS ${e.identifier("value")}`);else{if(!o.field)throw new g("INTERNAL",`groupBy(${t}, { agg: { op: "${o.op}" } }): "field" is required for non-count reducers`);d.push(e`${e.raw(fe(o.op))}(${W(o.field)}) AS ${e.identifier("value")}`)}const _=e.join(n.by.map(R=>W(R)),e`, `);let y=e`SELECT ${e.join(d,e`, `)} FROM ${e.identifier(t)}`;h&&(y=e`${y} WHERE ${h}`),y=e`${y} GROUP BY ${_}`;const T=await I(r,l,y);return bn(n.by,T)},async insert(t,n,i){const o=w.tables[t];if(!o)throw new g("INTERNAL",`unknown table: ${t}`);await U();const s=gn(o,n,Y);Ne(o,s);let u,E=!0;i?.clientId!==void 0?(jt(i.clientId),u=i.clientId):i?.allowExplicitId&&typeof s._id=="string"?u=s._id:(u=N(),E=!1);const f=i?.allowExplicitId&&typeof s._creationTime=="number"?s._creationTime:v(),h={...s,_creationTime:f,_id:u};H(t,"before","insert")&&await K("before","insert",{doc:{...h},id:u,op:"insert",table:t}),await ye(t),await pe(t);const{columns:d,values:_}=Tt(o,u,f,s);return await St(t,e`INSERT INTO ${e.identifier(t)} (${Z(d)}) VALUES (${ne(_)})`),E&&Ae.set(u,t),await re(t,void 0,h),await Q(t,u,void 0,h),await ae(t,u,h),await P(t,u,"insert",h),H(t,"after","insert")&&await K("after","insert",{doc:h,id:u,op:"insert",table:t}),u},normalizeId(t,n){return Kt(w,t,n)},async patch(t,n,i){Qe("patch",n);const o=await ie(t,i);if(!o)throw new g("INTERNAL",`document not found: ${t}`);const s=w.tables[o];if(!s)throw new g("INTERNAL",`document not found: ${t}`);const u=await Re(o,t),E=ee(s,u);if(!E)throw new g("INTERNAL",`document not found: ${t}`);const f={...E,...n,_id:t};lt(s,n,f,Y),Ne(s,f,!0),H(o,"before","update")&&await K("before","update",{doc:{...f},id:t,op:"update",previous:E,table:o}),await ye(o),await pe(o);const h=Object.keys(s.shape),d=e.join(h.map(_=>e`${e.identifier(_)} = ${G(f[_]??null)}`),e`, `);await Se(o,"UPDATE",d,u),await re(o,E,f),await Q(o,t,E,f),await ae(o,t,f,E),await P(o,t,"update",f),H(o,"after","update")&&await K("after","update",{doc:f,id:t,op:"update",previous:E,table:o})},async restore(t,n){const i=await ie(t,n);if(!i)throw new g("INTERNAL",`document not found: ${t}`);const o=w.tables[i],s=o?.softDeleteMode?.field;if(!o||!s)throw new g("INTERNAL",`ctx.db.restore: table "${i}" is not a .softDelete() table`);const u=await Re(i,t),E=u?.[s]!==null&&u?.[s]!==void 0;if(await x.patch(t,{[s]:null},n),E){const f=ee(o,u);f!==null&&await Q(i,t,void 0,{...f,[s]:null})}},query(t){const n=w.tables[t];if(!n)throw new g("INTERNAL",`unknown table: ${t}`);const i="the legacy query()/withIndex() reader is not available on the D1 (global) backend; use findMany",o=[],s=f=>o.every(h=>h(f)),u=async(f,h)=>{await U();const d=o.length>0,_=await Dt(r,l,n,t,f,qt(d?void 0:h));if(!d)return h===void 0&&Bt(_),_;const y=_.filter(T=>s(T));return h===void 0?y:y.slice(0,h)},E=f=>{const h={async*[Symbol.asyncIterator](){if(!f)throw new g("INTERNAL",i);yield*await u(f,void 0)},async collect(){if(!f)throw new g("INTERNAL",i);return u(f,void 0)},async collectWithScores(){throw new g("GLOBAL_SEARCH_SCORES_UNSUPPORTED",`collectWithScores() is not supported on \`.global()\` tables (table "${t}") — relevance scores are not yet surfaced on this backend; use .collect() instead`)},filter(d){return o.push(d),h},async first(){if(!f)throw new g("INTERNAL",i);return(await u(f,1))[0]??null},order(){return h},async paginate(d){if(!f)throw new g("INTERNAL",i);const _=Nt(d);return Mt(await u(f,Ot(_)),_)},async take(d){if(!f)throw new g("INTERNAL",i);return u(f,d)},async unique(){if(!f)throw new g("INTERNAL",i);const d=await u(f,2);if(d.length>1)throw new dn(`unique() on table "${t}" matched ${String(d.length)} documents; expected at most one`);return d[0]??null},withGeoIndex(){throw new g("INTERNAL",`geo indexes are not supported on \`.global()\` tables (table "${t}") — geospatial queries run only on sharded tables`)},withIndex(){throw new g("INTERNAL",i)},withSearchIndex(d,_){const y=(n.searchIndexes??[]).find(R=>R.name===d);if(!y)throw new g("INTERNAL",`unknown search index "${d}" on table "${t}"`);const T={definition:y,field:y.field,filters:[],hasQuery:!1,indexName:d,query:""};if(_(kt(T,t,Ct(y.language))),!T.hasQuery)throw new g("INTERNAL",`search index "${d}" on table "${t}" requires a .search(field, query) call`);return E(T)}};return h};return E(void 0)},async rank(t,n,i){const o=w.tables[t];if(!o)throw new g("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(M=>M.name===n);if(!s)throw new g("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(i.restrictsCounts)throw new Ze(t);if(await U(),!await ke(t,s))return null;const E=typeof i.row=="string"?i.row:i.row._id;if(!E)return null;const f=le(t,s.name),h=s.sortBy.map((M,j)=>$e(j)),d=Z(["__partition__",...h]),y=(await I(r,l,e`SELECT ${d} FROM ${e.identifier(f)} WHERE ${e.identifier("__id__")} = ${E}`))[0];if(!y)return null;const T=y.__partition__,R=q(i.baseWhere,i.where);ze(R,w,t,"rank");const C=Je(s,R);if(C&&Ie(s.partitionBy??[],C)!==T)return null;const A=An(l.name,s,h,y,E),O=await I(r,l,e`SELECT COUNT(*) AS c FROM ${e.identifier(f)} WHERE ${e.identifier("__partition__")} = ${T}${A?e` AND (${A})`:e``}`),L=await I(r,l,e`SELECT COUNT(*) AS c FROM ${e.identifier(f)} WHERE ${e.identifier("__partition__")} = ${T}`);return{position:Number(O[0]?.c??0)+1,total:Number(L[0]?.c??0)}},async rankPage(t,n,i={}){ze(q(i.baseWhere,i.where),w,t,"rankPage");const o=w.tables[t];if(!o)throw new g("INTERNAL",`unknown table: ${t}`);const s=o.rankIndexes?.find(D=>D.name===n);if(!s)throw new g("INTERNAL",`unknown rankIndex "${n}" on table "${t}"`);if(await U(),!await ke(t,s))return{continueCursor:null,isDone:!0,page:[]};const E=le(t,s.name),f=s.sortBy.map((D,ce)=>$e(ce)),h=Math.max(1,Math.min(1e3,Math.floor(i.take??100))),d=q(i.baseWhere,i.where),_=Je(s,d),y=kn(s,f),T=e.join(y.map(D=>e`${e.identifier(D.column)} ${e.raw(D.direction==="desc"?"DESC":"ASC")}`),e`, `),R=[];typeof i.partitionKey=="string"?R.push(e`${e.identifier("__partition__")} = ${i.partitionKey}`):_&&R.push(e`${e.identifier("__partition__")} = ${Ie(s.partitionBy??[],_)}`);const C=Ht(i);if(C!==void 0){const D=Ln(l.name,y,C);D!==void 0&&R.push(D)}const A=Z([_e,"__partition__",...f]);let O=e`SELECT ${A} FROM ${e.identifier(E)}`;R.length>0&&(O=e`${O} WHERE ${e.join(R,e` AND `)}`),O=e`${O} ORDER BY ${T} LIMIT ${e.raw(String(h+1))}`;const L=await I(r,l,O),M=L.length>h,j=M?L.slice(0,h):L,ge=j.map(D=>D[_e]),ve=await Nn(r,l,o,t,ge);let me=null;const z=j.at(-1);if(M&&z!==void 0){const D=[z.__partition__,...f.map(ce=>z[ce]),z[_e]];me=Mn(D)}return{continueCursor:me,isDone:!M,page:ve}},async replace(t,n,i,o){Qe("replace",n);const s=await ie(t,i);if(!s)throw new g("INTERNAL",`document not found: ${t}`);const u=w.tables[s];if(!u)throw new g("INTERNAL",`document not found: ${t}`);const E=await Re(s,t);if(E===void 0)throw new g("INTERNAL",`document not found: ${t}`);const h=Pt(w,s,"update")||(u.aggregateIndexes??[]).length>0||(u.rankIndexes??[]).length>0?ee(u,E)??void 0:void 0,d=o?.allowExplicitId&&typeof n._creationTime=="number"?n._creationTime:v(),_={...n,_creationTime:d,_id:t};lt(u,n,_,Y),Ne(u,_),H(s,"before","update")&&await K("before","update",{doc:{..._},id:t,op:"update",previous:h,table:s}),await ye(s),await pe(s);const y=Object.keys(u.shape),T=e.join([e`${e.identifier("_creationTime")} = ${d}`,...y.map(R=>e`${e.identifier(R)} = ${G(_[R]??null)}`)],e`, `);await Se(s,"UPDATE",T,E),await re(s,h,_),await Q(s,t,h,_),await ae(s,t,_,h),await P(s,t,"update",_),H(s,"after","update")&&await K("after","update",{doc:_,id:t,op:"update",previous:h,table:s})}};return Pe={db:x,scheduler:Ce},x};export{ai as backfillSqlSearchIndexes,oi as createSqlCtxDb,di as decodeGlobalRow,Hn as readSqlCdcChangedTables,ii as readSqlCdcChanges,Et as readSqlCdcFloor,Wn as runSqlCdcMigration,Lt as runSqlSearchMigrations,Un as sweepSqlCdcRetention};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{renderSql as d}from"@lunora/shard-engine";import{sql as i}from"drizzle-orm";import{s as I,a as N,e as _}from"./value-codec-DMP5JYuK.mjs";import{OCC_VERSION_COLUMN as F}from"./OCC_VERSION_COLUMN-ByC64Jgx.mjs";const q=n=>n==="_id"||n==="id"?"id":n,M=n=>i`${i.identifier(q(n))}`,v=n=>{const t=[];for(const[e,o]of Object.entries(n.shape)){const r=o._meta?.column;r&&t.push([e,r])}return t},O=(n,t,e)=>{const{params:o,sql:r}=d(t.name,e);return n.all(r,o)},u=(n,t,e)=>{const{params:o,sql:r}=d(t.name,e);return n.run(r,o)},k=async(n,t,e)=>{if(e.length!==0){if(!n.batch){for(const o of e)await u(n,t,o);return}await n.batch(e.map(o=>d(t.name,o)))}},D=async(n,t,e)=>{const o=e.unique?i`UNIQUE `:i``;if(t.name==="mysql"){try{await u(n,t,i`CREATE ${o}INDEX ${i.identifier(e.name)} ON ${i.identifier(e.table)} (${e.columns})`)}catch(r){const s=r;if(s.errno!==1061&&s.code!=="ER_DUP_KEYNAME"&&s.code!==1061)throw r}return}await u(n,t,i`CREATE ${o}INDEX IF NOT EXISTS ${i.identifier(e.name)} ON ${i.identifier(e.table)} (${e.columns})`)},K=I,h=n=>n?._meta?.column?.notNull===!1,b=n=>{if(n.kind!=="optional"||h(n))return!1;const t=n._meta?.inner;return!h(t)},w=new WeakMap,y=n=>{let t=w.get(n);return t===void 0&&(t=Object.entries(n.shape).map(([e,o])=>[e,_(o),b(o)]),w.set(n,t)),t},S=(n,t)=>{const e={};for(const[o,r,s]of y(n)){const c=t[o];c===void 0||s&&c===null||(e[o]=N(c,r))}return e._id=t.id,e._creationTime=t._creationTime,e},$=(n,t)=>t?S(n,t):null,L=(n,t)=>{const e=[];for(const o of t){const r=$(n,o);r&&e.push(r)}return e},R=500,B=async(n,t,e,o,r,s={})=>{let c=s.after,a=s.limit,m=!0;for(;m&&(a===void 0||a>0);){const f=a===void 0?R:Math.min(R,a),C=c===void 0?i``:i` WHERE ${i.identifier("id")} > ${c}`,l=await O(n,t,i`SELECT * FROM ${i.identifier(o)}${C} ORDER BY ${i.identifier("id")} ASC LIMIT ${i.raw(String(f))}`);for(const p of l){const E=$(e,p);E&&await r(E)}c=l.at(-1)?.id,m=l.length===f,a=a===void 0?void 0:a-l.length}};export{R as BACKFILL_BATCH_SIZE,F as OCC_VERSION_COLUMN,M as columnRefSql,D as createIndexIfNotExists,S as decodeGlobalRow,$ as decodeRow,L as decodeRows,B as forEachRowPaged,q as physicalColumn,O as queryAll,k as queryBatch,u as queryRun,K as serializeColumnValue,v as tableColumns};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as R}from"@lunora/errors";import{aggregateTableName as C,rankTableName as q,sortColumnName as A,renderSql as v,bigintSqlKey as D}from"@lunora/shard-engine";import{sql as n}from"drizzle-orm";import{queryRun as d,queryAll as w,createIndexIfNotExists as E,tableColumns as M,columnRefSql as m,queryBatch as B}from"./decodeGlobalRow-C76NMu9P.mjs";import{B as N,e as T}from"./value-codec-DMP5JYuK.mjs";import{OCC_VERSION_COLUMN as p}from"./OCC_VERSION_COLUMN-ByC64Jgx.mjs";const S=(t,i)=>i.columnType(T(t)),k=(t,i,e)=>{const o=[];for(const[f,l]of Object.entries(i.shape)){if(!l._meta?.column)continue;const u=l._meta.column.notNull&&l.kind!=="optional"?" NOT NULL":"";o.push(n`${n.identifier(f)} ${n.raw(`${S(l,e)}${u}`)}`)}const s=[...e.frameworkColumns().map(f=>n`${n.identifier(f.name)} ${n.raw(f.type)}`),n`${n.identifier(p)} ${n.raw(e.companionTypes.integer)}`],a=s.length+o.length,r=e.maxTableColumns;if(r!==void 0&&a>r){const f=a-1===r,l="Move one or more fields into a single object field, or split the table — either way the existing rows need a data migration (`defineMigration` + `lunora migrate up`); there is no in-place path.";throw new R("VALIDATION_ERROR",`@lunora/sql-store: global table "${t}" needs ${String(a)} columns, over this engine's ${String(r)}-column limit. ${f?`One of them is "${p}", the row version every guarded write reads, which caps declared fields at ${String(r-s.length)}. A table provisioned at ${String(r)} columns before that column existed cannot be widened to hold it. `:""}${l}`)}return n.join([...s,...o],n`, `)},x=t=>{if(t.indexKeyPrefix?.("string")===void 0)return n`${m("_creationTime")}, ${m("id")}`},U=async(t,i,e)=>{if(i.name!=="sqlite")return;const s=(await w(t,i,n`SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ${e.name} AND tbl_name = ${e.table}`))[0]?.sql;if(typeof s!="string")return;const a=l=>{const u=l.indexOf("(");return u===-1?void 0:l.slice(u,l.lastIndexOf(")")+1)},r=e.unique?n`UNIQUE `:n``,f=a(v(i.name,n`CREATE ${r}INDEX ${n.identifier(e.name)} ON ${n.identifier(e.table)} (${e.columns})`).sql);if(!(f===void 0||f===a(s))){if(e.unique){const l=n.join(e.refs.map(c=>n`${c} IS NOT NULL`),n` AND `);if((await w(t,i,n`SELECT ${e.columns} FROM ${n.identifier(e.table)} WHERE ${l} GROUP BY ${e.columns} HAVING COUNT(*) > 1 LIMIT 1`)).length>0)throw new R("INTERNAL",`unique index "${e.name}" on "${e.table}" cannot be re-created with its new column list: existing rows are duplicates under it. De-duplicate the table with a data migration first; the previous index is left in place.`)}await d(t,i,n`DROP INDEX IF EXISTS ${n.identifier(e.name)}`)}},j=async(t,i,e,o)=>{const s=r=>{const f=m(r),l=e.shape[r],u=l&&o.indexKeyPrefix?o.indexKeyPrefix(T(l)):void 0;return u===void 0?f:n`${f}(${n.raw(String(u))})`},a=x(o);for(const r of e.indexes){const f=r.fields.map($=>s($)),l=n.join(f,n`, `),u=r.unique??!1,c={columns:u||a===void 0?l:n`${l}, ${a}`,name:`${i}_${r.name}`,refs:f,table:i,unique:u};await U(t,o,c),await E(t,o,c)}a!==void 0&&await E(t,o,{columns:a,name:`${i}__by_creation`,table:i,unique:!1});for(const[r,f]of M(e))f.unique&&await E(t,o,{columns:s(r),name:`${i}_unique_${r}`,table:i,unique:!0})},y="__lunora_migration_state",P=async(t,i)=>{await d(t,i,n`CREATE TABLE IF NOT EXISTS ${n.identifier(y)} (${n.identifier("name")} ${n.raw(i.companionTypes.key)} PRIMARY KEY)`)},F=async(t,i,e)=>(await w(t,i,n`SELECT ${n.identifier("name")} FROM ${n.identifier(y)} WHERE ${n.identifier("name")} = ${e}`)).length>0,K=async(t,i,e,o,s)=>{s>0&&console.warn(`[@lunora/sql-store] bigint storage migration on "${o}": ${String(s)} row(s) hold a value the order-preserving key cannot represent (non-numeric text, or a magnitude past 39 digits) and were left as stored — range filters and ORDER BY over them stay wrong until they are corrected by hand.`);try{await d(t,i,n`INSERT INTO ${n.identifier(y)} (${n.identifier("name")}) VALUES (${e})`)}catch(a){if(!i.isUniqueViolation(a))throw a}},G=100,Y=t=>Object.entries(t.shape).filter(([,i])=>i._meta?.column!==void 0&&T(i)==="bigint").map(([i])=>i),H=(t,i)=>{const e=[],o=[];for(const s of i){const a=t[s];if(!(typeof a!="string"||a.length===N))try{const r=D(BigInt(a));e.push(n`${m(s)} = ${r}`),o.push(n`${m(s)} = ${a}`)}catch{}}return{assignments:e,guards:o}},X=async(t,i,e,o)=>{const s=Y(e);if(s.length===0)return;await P(t,o);const a=`bigint-key:${i}`;if(await F(t,o,a))return;const r=n.join(s.map(c=>n`(${m(c)} IS NOT NULL AND LENGTH(${m(c)}) <> ${N})`),n` OR `),f=n.join([m("id"),...s.map(c=>m(c))],n`, `);let l="",u=0;for(;;){const c=await w(t,o,n`SELECT ${f} FROM ${n.identifier(i)} WHERE (${r}) AND ${m("id")} > ${l} ORDER BY ${m("id")} LIMIT ${n.raw(String(G))}`);if(c.length===0){await K(t,o,a,i,u);return}const $=[];for(const h of c){const{id:b}=h,{assignments:I,guards:O}=typeof b=="string"?H(h,s):{assignments:[],guards:[]};if(I.length===0){u+=1;continue}$.push(n`UPDATE ${n.identifier(i)} SET ${n.join(I,n`, `)} WHERE ${m("id")} = ${b} AND ${n.join(O,n` AND `)}`)}await B(t,o,$);const g=c.at(-1)?.id;if(typeof g!="string")return;l=g}},V=async(t,i,e,o)=>{const s=[[p,o.companionTypes.integer],...Object.entries(e.shape).filter(([,r])=>r._meta?.column!==void 0).map(([r,f])=>[r,S(f,o)])],a=async r=>{try{return await w(t,o,n`SELECT ${n.join(r.map(f=>m(f)),n`, `)} FROM ${n.identifier(i)} LIMIT 0`),!0}catch{return!1}};if(!await a(s.map(([r])=>r)))for(const[r,f]of s)await a([r])||await d(t,o,n`ALTER TABLE ${n.identifier(i)} ADD COLUMN ${n.identifier(r)} ${n.raw(f)}`)},on=async(t,i,e)=>{for(const[o,s]of Object.entries(i.tables)){if(s.shardMode?.kind!=="global")continue;const a=k(o,s,e);await d(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(o)} (${a})`),await V(t,o,s,e),await j(t,o,s,e),await X(t,o,s,e)}},L=t=>t.shardMode===void 0||t.shardMode.kind==="global",rn=async(t,i,e)=>{const{integer:o,key:s,real:a}=e.companionTypes;for(const[r,f]of Object.entries(i.tables)){const l=f.aggregateIndexes;if(!(!l||l.length===0||!L(f)))for(const u of l){const c=C(r,u.name);await d(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(c)} (${n.identifier("__key__")} ${n.raw(s)} PRIMARY KEY, ${n.identifier("__value__")} ${n.raw(a)}, ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0)`),e.name==="sqlite"&&((await w(t,e,n`PRAGMA table_info(${n.identifier(c)})`)).some(g=>g.name==="__count__")||await d(t,e,n`ALTER TABLE ${n.identifier(c)} ADD COLUMN ${n.identifier("__count__")} ${n.raw(o)} NOT NULL DEFAULT 0`))}}},_=(t,i,e,o)=>{const s=t.name==="mysql"&&o?n`${n.identifier(i)}(191)`:n`${n.identifier(i)}`;return n`${s} ${n.raw(e)}`},W=(t,i,e)=>{const o=[_(t,"__partition__","ASC",!0)];for(const[s,a]of i.sortBy.entries()){const r=e.shape[a.field],f=r!==void 0&&t.indexKeyPrefix?.(T(r))!==void 0;o.push(_(t,A(s),a.direction==="desc"?"DESC":"ASC",f))}return o.push(_(t,"__id__","ASC",!0)),o},Q=(t,i,e)=>i.sortBy.map((o,s)=>{const a=e.shape[o.field],r=t.columnType(a?T(a):void 0);return n`${n.identifier(A(s))} ${n.raw(r)}`}),sn=async(t,i,e)=>{const{key:o}=e.companionTypes;for(const[s,a]of Object.entries(i.tables)){const r=a.rankIndexes;if(!(!r||r.length===0||!L(a)))for(const f of r){const l=q(s,f.name),u=Q(e,f,a),c=u.length>0?n`, ${n.join(u,n`, `)}`:n``;await d(t,e,n`CREATE TABLE IF NOT EXISTS ${n.identifier(l)} (${n.identifier("__id__")} ${n.raw(o)} PRIMARY KEY, ${n.identifier("__partition__")} ${n.raw(o)} NOT NULL${c})`);const $=W(e,f,a),g=`${s}__rank_${f.name}__btree`;await E(t,e,{columns:n.join($,n`, `),name:g,table:l,unique:!1})}}};export{rn as runSqlAggregateMigrations,on as runSqlGlobalTableMigrations,sn as runSqlRankMigrations};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as k}from"@lunora/errors";import{decodeBigintSqlKey as T,bigintSqlKey as R,BIGINT_KEY_DIGITS as U}from"@lunora/shard-engine";const I=r=>{if(r.kind!=="optional")return r.kind;const t=r._meta?.inner;return t?I(t):r.kind},A=r=>{let t="";for(let o=0;o<r.length;o+=32768)t+=String.fromCharCode(...r.subarray(o,o+32768));return btoa(t)},S=r=>{const t=atob(r),s=new Uint8Array(t.length);for(let o=0;o<t.length;o+=1)s[o]=t.codePointAt(o)??0;return s},i="$lunora.wire$",g=64,l=1024,m="__proto__",h={BigInt64Array,BigUint64Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array},O={Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},B=r=>{if(r===null||typeof r!="object")return!1;const t=Object.getPrototypeOf(r);return t===null||t===Object.prototype},E=(r,t=0)=>{if(t>g)return!0;if(r===null)return!1;const s=typeof r;if(s==="string"||s==="boolean")return!1;if(s==="number")return!Number.isFinite(r);if(s!=="object")return!0;if(Array.isArray(r))return r.length>0&&r[0]===i?!0:r.some(o=>o===void 0||E(o,t+1));if(!B(r)||Object.hasOwn(r,m))return!0;for(const o of Object.keys(r)){const c=r[o];if(c!==void 0&&E(c,t+1))return!0}return!1},u=(r,t=0)=>{if(t>g)throw new RangeError(`wire-codec: value nesting exceeds the ${g}-level limit`);if(r===void 0)return[i,"undefined"];if(r===null)return null;const s=typeof r;if(s==="bigint")return[i,"bigint",r.toString()];if(s==="number"){const e=r;return Number.isNaN(e)?[i,"nan"]:e===1/0?[i,"inf"]:e===-1/0?[i,"-inf"]:e}if(s!=="object")return r;if(r instanceof Date)return[i,"date",u(r.getTime(),t+1)];if(r instanceof Error){const e=r,n={};for(const y of Object.keys(e))e[y]!==void 0&&(n[y]=u(e[y],t+1));const f=[i,"error",e.name,e.message,n];return e.cause!==void 0&&f.push(u(e.cause,t+1)),f}if(r instanceof URL)return[i,"url",r.href];if(r instanceof Map)return[i,"map",[...r.entries()].map(([e,n])=>[u(e,t+1),u(n,t+1)])];if(r instanceof Set)return[i,"set",[...r].map(e=>u(e,t+1))];if(r instanceof ArrayBuffer)return[i,"bytes",A(new Uint8Array(r)),"ArrayBuffer"];if(ArrayBuffer.isView(r)){const e=r,n=e.constructor.name,f=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return n==="Uint8Array"?[i,"bytes",A(f)]:[i,"bytes",A(f),n]}if(Array.isArray(r)){const e=r.map(n=>u(n,t+1));return e.length>0&&e[0]===i?[i,"arr",e]:e}if(!B(r)){const e=r.constructor?.name??"value";throw new TypeError(`wire-codec: cannot encode a ${e} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`)}const o=r,c={};for(const e of Object.keys(o)){const n=o[e];if(n===void 0)continue;const f=u(n,t+1);e===m?Object.defineProperty(c,e,{configurable:!0,enumerable:!0,value:f,writable:!0}):c[e]=f}return c},a=(r,t=0)=>{if(t>g)throw new RangeError(`wire-codec: value nesting exceeds the ${g}-level limit`);if(r===null||typeof r!="object")return r;if(Array.isArray(r)){if(r[0]===i)switch(r[1]){case"-inf":return-1/0;case"arr":return r[2].map(e=>a(e,t+1));case"bigint":{const e=r[2];if(typeof e!="string"||e.length>l||!/^-?\d+$/.test(e))throw new RangeError(`wire-codec: invalid or over-long bigint (max ${l} digits)`);return BigInt(e)}case"date":{const e=a(r[2],t+1);if(typeof e!="number")throw new TypeError("wire-codec: malformed date — epoch must be a number");return new Date(e)}case"map":{const e=r[2];return new Map(e.map(n=>{if(!Array.isArray(n)||n.length!==2)throw new TypeError("wire-codec: malformed map entry — expected a [key, value] pair");return[a(n[0],t+1),a(n[1],t+1)]}))}case"set":return new Set(r[2].map(e=>a(e,t+1)));case"url":{const e=r[2];if(typeof e!="string")throw new TypeError("wire-codec: malformed url — href must be a string");return new URL(e)}case"error":{const e=r[2],n=r[3],f=(Object.hasOwn(O,e)?O[e]:void 0)??Error,y=new f(n);y.name!==e&&Object.defineProperty(y,"name",{configurable:!0,value:e,writable:!0});const b=a(r[4],t+1);if(b===null||typeof b!="object"||Array.isArray(b))throw new TypeError("wire-codec: malformed error — props must be an object");for(const d of Object.keys(b))d===m?Object.defineProperty(y,d,{configurable:!0,enumerable:!0,value:b[d],writable:!0}):y[d]=b[d];return r.length>5&&Object.defineProperty(y,"cause",{configurable:!0,value:a(r[5],t+1),writable:!0}),y}case"bytes":{const e=r[2];if(typeof e!="string")throw new TypeError("wire-codec: malformed bytes — payload must be a base64 string");const n=S(e),f=r[3]??"Uint8Array";if(f==="ArrayBuffer")return n.buffer.byteLength===n.byteLength?n.buffer:n.slice().buffer;const y=Object.hasOwn(h,f)?h[f]:void 0;return y?new y(n.slice().buffer):n}case"inf":return 1/0;case"nan":return Number.NaN;case"undefined":return;default:return r.map(e=>a(e,t+1))}return r.map(c=>a(c,t+1))}const s=r,o={};for(const c of Object.keys(s)){const e=a(s[c],t+1);c===m?Object.defineProperty(o,c,{configurable:!0,enumerable:!0,value:e,writable:!0}):o[c]=e}return o},p=i,w=(r,t)=>r.startsWith(p)?a(t(r.slice(p.length))):t(r),L=r=>{if(typeof r=="boolean")return r?1:0;if(r===null||typeof r=="string"||typeof r=="number")return r;if(typeof r=="bigint")return R(r);if(r instanceof Uint8Array)return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);try{return E(r)?p+JSON.stringify(u(r)):JSON.stringify(r)}catch(t){throw new k("BAD_REQUEST",`this value cannot be stored: ${t instanceof Error?t.message:String(t)}`)}},j=r=>{try{return JSON.parse(r)}catch{return r}},_=r=>{if(typeof r!="string")return r;const t=T(r);if(t!==void 0)return t;try{return BigInt(r)}catch{return r}},C=r=>I(r),D=(r,t)=>{if(r===null)return r;switch(t){case"any":case"from":case"union":return typeof r=="string"&&(r.startsWith("{")||r.startsWith("[")||r.startsWith(p))?w(r,j):r;case"array":case"geoPoint":case"object":case"record":return typeof r=="string"?w(r,j):r;case"bigint":return _(r);case"boolean":return r===0||r===1?r===1:r;case"bytes":return r instanceof ArrayBuffer?r:ArrayBuffer.isView(r)?new Uint8Array(r.buffer,r.byteOffset,r.byteLength).slice().buffer:r;default:return r}},x=U+1;export{x as B,D as a,u as b,_ as c,a as d,C as e,E as n,L as s,j as t};
|