@lunora/sql-store 1.0.0-alpha.9 → 1.0.0-alpha.91
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +6 -0
- package/README.md +2 -1
- package/dist/dialect.d.mts +136 -38
- package/dist/dialect.d.ts +136 -38
- package/dist/dialect.mjs +0 -1
- package/dist/index.d.mts +264 -121
- package/dist/index.d.ts +264 -121
- package/dist/index.mjs +1 -2
- package/dist/packem_shared/SEARCH_STATE_TABLE-DA3LgCT0.mjs +1 -0
- package/dist/packem_shared/backfillSqlSearchIndexes-CWhgwSCR.mjs +1 -0
- package/dist/packem_shared/createSqlCtxDb-Bn3b-5Wa.mjs +7 -0
- package/dist/packem_shared/ctx-db-search-DlItpO6A.mjs +1 -0
- package/dist/packem_shared/decodeBigint-B4wY6fcF.mjs +1 -0
- package/dist/packem_shared/decodeGlobalRow-BKIjk_Zk.mjs +1 -0
- package/package.json +3 -2
- package/dist/packem_shared/createSqlCtxDb-DYqDyPq8.mjs +0 -1754
- package/dist/packem_shared/decodeBigint-Dedu92k4.mjs +0 -69
package/LICENSE.md
CHANGED
|
@@ -103,3 +103,9 @@ Unless required by applicable law or agreed to in writing, software distributed
|
|
|
103
103
|
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
104
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
105
|
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
<!-- /DEPENDENCIES -->
|
|
109
|
+
|
|
110
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
111
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
CHANGED
|
@@ -83,7 +83,8 @@ seam from the root); consumers import everything from the root `@lunora/sql-stor
|
|
|
83
83
|
- Migration runners (each takes `(exec, schema, dialect)`, or `(exec, dialect)`
|
|
84
84
|
for CDC): `runSqlGlobalTableMigrations`, `runSqlAggregateMigrations`,
|
|
85
85
|
`runSqlRankMigrations`, `runSqlSearchMigrations`, `runSqlCdcMigration`.
|
|
86
|
-
- CDC log helpers: `readSqlCdcChanges`, `
|
|
86
|
+
- CDC log helpers: `readSqlCdcChanges`, `readSqlCdcChangedTables`,
|
|
87
|
+
`readSqlCdcFloor`, `sweepSqlCdcRetention`.
|
|
87
88
|
- Value codec building blocks a dialect reuses for `encode`/`decode`:
|
|
88
89
|
`sqliteEncode`, `sqliteDecode`, `decodeBigint`, `tryJsonParse`,
|
|
89
90
|
`effectiveColumnKind`.
|
package/dist/dialect.d.mts
CHANGED
|
@@ -5,20 +5,45 @@ interface SqlRunResult {
|
|
|
5
5
|
rowsAffected: number;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
* The async SQL surface the store core consumes. Satisfied by a
|
|
9
|
-
* `D1Session`/`D1Client` (D1), a `node:sqlite` adapter (tests), or a
|
|
10
|
-
* Hyperdrive-backed `postgres`/`pg`/`mysql2` driver (PlanetScale).
|
|
11
|
-
*
|
|
12
|
-
* `all` runs a row-returning statement (incl. `... RETURNING ...`); `run` runs a
|
|
13
|
-
* write and reports `rowsAffected` — the matched-rows count that drives the
|
|
14
|
-
* MySQL optimistic-concurrency guard (which has no `RETURNING`).
|
|
15
|
-
*
|
|
16
|
-
* Note: companion writes (aggregate/rank/FTS/CDC) run as separate sequential
|
|
17
|
-
* statements after the row write on every engine, so they share D1's
|
|
18
|
-
* at-least-once caveat — there is no cross-statement transaction here.
|
|
19
|
-
*/
|
|
8
|
+
* The async SQL surface the store core consumes. Satisfied by a
|
|
9
|
+
* `D1Session`/`D1Client` (D1), a `node:sqlite` adapter (tests), or a
|
|
10
|
+
* Hyperdrive-backed `postgres`/`pg`/`mysql2` driver (PlanetScale).
|
|
11
|
+
*
|
|
12
|
+
* `all` runs a row-returning statement (incl. `... RETURNING ...`); `run` runs a
|
|
13
|
+
* write and reports `rowsAffected` — the matched-rows count that drives the
|
|
14
|
+
* MySQL optimistic-concurrency guard (which has no `RETURNING`).
|
|
15
|
+
*
|
|
16
|
+
* Note: companion writes (aggregate/rank/FTS/CDC) run as separate sequential
|
|
17
|
+
* statements after the row write on every engine, so they share D1's
|
|
18
|
+
* at-least-once caveat — there is no cross-statement transaction here.
|
|
19
|
+
*/
|
|
20
20
|
interface SqlExec {
|
|
21
21
|
all: (sql: string, params: ReadonlyArray<unknown>) => Promise<Record<string, unknown>[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Optional: run several write statements as one round trip instead of
|
|
24
|
+
* `run()` called once per statement in sequence. Statements MUST be
|
|
25
|
+
* mutually independent — an implementation MAY reorder or parallelize
|
|
26
|
+
* across elements, so callers must never rely on array order between
|
|
27
|
+
* elements (e.g. a purge-then-insert pair belongs in separate sequential
|
|
28
|
+
* `queryRun`/`run` calls, not one `batch`).
|
|
29
|
+
*
|
|
30
|
+
* Absent, the store core falls back to its historical sequential `run()`
|
|
31
|
+
* loop, so an exec that doesn't implement this keeps working unchanged.
|
|
32
|
+
*
|
|
33
|
+
* D1's `client.batch` executes the whole array atomically (all-or-nothing)
|
|
34
|
+
* in one request, preserving array order — an actual atomicity improvement
|
|
35
|
+
* over the sequential fallback, not just a round-trip one. The Hyperdrive
|
|
36
|
+
* `postgres`/`pg` adapters instead dispatch every statement concurrently
|
|
37
|
+
* (`Promise.all`) over the same connection/pool rather than awaiting each
|
|
38
|
+
* in turn — still "at-least-once, non-atomic" like the fallback, but no
|
|
39
|
+
* longer serialized one full RTT at a time, and with no ordering between
|
|
40
|
+
* elements. Safe only for statements whose effects don't depend on each
|
|
41
|
+
* other (distinct-keyed rows), which is what every current caller batches.
|
|
42
|
+
*/
|
|
43
|
+
batch?: (statements: ReadonlyArray<{
|
|
44
|
+
params: ReadonlyArray<unknown>;
|
|
45
|
+
sql: string;
|
|
46
|
+
}>) => Promise<void>;
|
|
22
47
|
run: (sql: string, params: ReadonlyArray<unknown>) => Promise<SqlRunResult>;
|
|
23
48
|
}
|
|
24
49
|
/** Everything engine-specific the store core needs. One value per engine; `sqliteDialect` (in `@lunora/d1`) is the reference, Postgres/MySQL live in `@lunora/hyperdrive/global`. */
|
|
@@ -26,19 +51,19 @@ interface SqlDialect {
|
|
|
26
51
|
/** Affected-rows extractor for the OCC fallback when `supportsReturning` is false (MySQL). */
|
|
27
52
|
affectedRows?: (result: SqlRunResult) => number;
|
|
28
53
|
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
54
|
+
* Storage SQL column type for a validator `kind`. SQLite affinity
|
|
55
|
+
* (`TEXT`/`INTEGER`/`REAL`/`BLOB`); Postgres `TEXT`/`DOUBLE PRECISION`/
|
|
56
|
+
* `BOOLEAN`/`JSONB`/`BYTEA`; MySQL `VARCHAR(255)`/`TEXT`/`DOUBLE`/
|
|
57
|
+
* `TINYINT(1)`/`JSON`/`LONGBLOB`.
|
|
58
|
+
*/
|
|
34
59
|
columnType: (kind: string | undefined) => string;
|
|
35
60
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
61
|
+
* Engine SQL types for the **internal companion tables** (aggregate / rank /
|
|
62
|
+
* CDC), which are built from raw SQL types, not validator kinds. SQLite uses
|
|
63
|
+
* `TEXT`/`REAL`/`INTEGER`/`BLOB` and `INTEGER PRIMARY KEY AUTOINCREMENT`;
|
|
64
|
+
* Postgres `TEXT`/`DOUBLE PRECISION`/`INTEGER`/`BYTEA` + `BIGSERIAL`; MySQL
|
|
65
|
+
* needs a bounded `VARCHAR` key, `DOUBLE`, `LONGBLOB`, `AUTO_INCREMENT`.
|
|
66
|
+
*/
|
|
42
67
|
companionTypes: {
|
|
43
68
|
autoincrementPrimaryKey: string;
|
|
44
69
|
integer: string;
|
|
@@ -46,9 +71,19 @@ interface SqlDialect {
|
|
|
46
71
|
real: string;
|
|
47
72
|
text: string;
|
|
48
73
|
};
|
|
49
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* Map a stored value back to its JS form, by effective validator `kind`
|
|
76
|
+
* (inverse of `encode`). NOTE: currently **unused** by the store core, which
|
|
77
|
+
* hard-codes `sqliteDecode` in `decodeGlobalRow` on every engine. Kept on the
|
|
78
|
+
* seam for a future engine-native codec; an override here does not run today.
|
|
79
|
+
*/
|
|
50
80
|
decode: (value: unknown, kind: string | undefined) => unknown;
|
|
51
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* Map a JS value to its bound storage form (boolean→1/0, bigint→string,
|
|
83
|
+
* object→JSON on SQLite). NOTE: currently **unused** by the store core, which
|
|
84
|
+
* hard-codes `sqliteEncode` as `serializeColumnValue` on every engine. Kept on
|
|
85
|
+
* the seam for a future engine-native codec; an override here does not run today.
|
|
86
|
+
*/
|
|
52
87
|
encode: (value: unknown) => unknown;
|
|
53
88
|
/** The framework columns every global table carries — the `id` primary key and `_creationTime` — as `{ name, type }` so the DDL builder can quote each name through the engine's dialect. */
|
|
54
89
|
frameworkColumns: () => ReadonlyArray<{
|
|
@@ -56,27 +91,90 @@ interface SqlDialect {
|
|
|
56
91
|
type: string;
|
|
57
92
|
}>;
|
|
58
93
|
/**
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
94
|
+
* Optional: the key-prefix length an indexed column of this `kind` needs.
|
|
95
|
+
* MySQL/InnoDB can't index a `TEXT`/`LONGTEXT`/`BLOB` column without a prefix
|
|
96
|
+
* (the store appends `(<n>)` to the column reference); SQLite/Postgres index
|
|
97
|
+
* text columns directly and omit this hook (or return `undefined`). `kind` is
|
|
98
|
+
* the column's effective validator kind.
|
|
99
|
+
*/
|
|
65
100
|
indexKeyPrefix?: (kind: string | undefined) => number | undefined;
|
|
66
101
|
/** True when an `error` thrown by a write is a UNIQUE-constraint breach (mapped to a 409 ConflictError). */
|
|
67
102
|
isUniqueViolation: (error: unknown) => boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Most columns one table may carry on this engine, framework columns
|
|
105
|
+
* included. Omit it and the DDL builder does not check — the right answer
|
|
106
|
+
* for an engine whose ceiling is high enough that no real schema reaches it.
|
|
107
|
+
*
|
|
108
|
+
* Declared because the ceilings differ by more than an order of magnitude:
|
|
109
|
+
* D1 runs Workerd's SQLite build, which caps a table at 100 columns, where
|
|
110
|
+
* Postgres allows 1,600 and MySQL 4,096. A fixed number here would either
|
|
111
|
+
* miss the D1 failure or reject schemas the other two engines run happily.
|
|
112
|
+
*/
|
|
113
|
+
maxTableColumns?: number;
|
|
68
114
|
/** A short engine tag for diagnostics/branching (`"sqlite" | "postgres" | "mysql"`). The store core selects drizzle's matching dialect for rendering off this. */
|
|
69
115
|
readonly name: "mysql" | "postgres" | "sqlite";
|
|
116
|
+
/**
|
|
117
|
+
* Optional: the engine's own full-text index, opted into per search index
|
|
118
|
+
* with `.searchIndex({ strategy: "native" })`.
|
|
119
|
+
*
|
|
120
|
+
* Only Postgres supplies one today (`tsvector` + GIN + `to_tsquery`). It
|
|
121
|
+
* scales sublinearly where the portable inverted companion aggregates every
|
|
122
|
+
* matching token row, but it ranks with the engine's formula rather than the
|
|
123
|
+
* shared scorer — which is why it is opt-in and why the parity suite asserts
|
|
124
|
+
* matching, not order, for indexes that use it.
|
|
125
|
+
*
|
|
126
|
+
* Recall still matches the portable path: the stored form is built from the
|
|
127
|
+
* tokens Lunora's analyzer already produced, under a configuration that adds
|
|
128
|
+
* no stemming or stopwords of the engine's own.
|
|
129
|
+
*
|
|
130
|
+
* Every member returns a *statement*, not a fragment, so no engine grammar
|
|
131
|
+
* reaches the store core: Postgres matches with `@@` against a `tsvector`
|
|
132
|
+
* column while MySQL would use `MATCH … AGAINST` against a text column, and
|
|
133
|
+
* both fit here without the caller knowing which.
|
|
134
|
+
*/
|
|
135
|
+
nativeTextSearch?: {
|
|
136
|
+
/** DDL for the companion table holding the engine's indexed form, keyed by document id. */
|
|
137
|
+
createCompanion: (companion: string, keyType: string) => SQL;
|
|
138
|
+
/** DDL for the indexes that make the match fast. */
|
|
139
|
+
createIndexes: (companion: string) => SQL[];
|
|
140
|
+
/** Replace one document's row, given its already-analyzed token stream. */
|
|
141
|
+
indexDocument: (companion: string, id: string, analyzed: string) => SQL;
|
|
142
|
+
/** The `WHERE` predicate matching a query's analyzed terms, final term as a prefix. */
|
|
143
|
+
matches: (companion: string, terms: ReadonlyArray<string>) => SQL;
|
|
144
|
+
/** The `ORDER BY` expression, best first. */
|
|
145
|
+
rank: (companion: string, terms: ReadonlyArray<string>) => SQL;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* True when the engine ships SQLite's FTS5 module, which decides whether a
|
|
149
|
+
* search index is stored as an FTS5 shadow or as the portable inverted
|
|
150
|
+
* companion.
|
|
151
|
+
*
|
|
152
|
+
* A static property of the engine, so it is declared rather than probed:
|
|
153
|
+
* the previous `CREATE VIRTUAL TABLE` capability probe spent a round trip
|
|
154
|
+
* (and an error in the database's log) on every fresh connection to
|
|
155
|
+
* rediscover something the dialect already knows. Tests that want the
|
|
156
|
+
* portable layout override this instead of intercepting SQL strings.
|
|
157
|
+
*/
|
|
158
|
+
supportsFts5: boolean;
|
|
70
159
|
/** True when the engine supports `UPDATE/DELETE ... RETURNING` (SQLite/PG yes, MySQL no → use `affectedRows`). */
|
|
71
160
|
supportsReturning: boolean;
|
|
72
161
|
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
162
|
+
* The catalog probe for whether a physical `table` exists — backs the opt-in
|
|
163
|
+
* companion-table (`__agg_`/`__rank_`) existence checks. Returns a drizzle
|
|
164
|
+
* {@link SQL} (a non-empty result ⇒ the table exists) so it renders through
|
|
165
|
+
* the same per-engine path as every other statement, never a hand-built
|
|
166
|
+
* placeholder string. SQLite reads `sqlite_master`; Postgres/MySQL read
|
|
167
|
+
* `information_schema.tables`.
|
|
168
|
+
*/
|
|
80
169
|
tableExists: (table: string) => SQL;
|
|
170
|
+
/**
|
|
171
|
+
* Optional: btree operator class appended to an indexed text column so a
|
|
172
|
+
* `LIKE 'prefix%'` scan can use the index whatever the database collation.
|
|
173
|
+
* Postgres needs `text_pattern_ops` (a default `text_ops` btree built under
|
|
174
|
+
* e.g. `en_US.UTF-8` is useless to `LIKE`); SQLite and MySQL index prefix
|
|
175
|
+
* matches off the plain index and omit this. Only the search companion's
|
|
176
|
+
* token index reads it.
|
|
177
|
+
*/
|
|
178
|
+
textPatternOperatorClass?: string;
|
|
81
179
|
}
|
|
82
180
|
export { SqlDialect, SqlExec, SqlRunResult };
|
package/dist/dialect.d.ts
CHANGED
|
@@ -5,20 +5,45 @@ interface SqlRunResult {
|
|
|
5
5
|
rowsAffected: number;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
* The async SQL surface the store core consumes. Satisfied by a
|
|
9
|
-
* `D1Session`/`D1Client` (D1), a `node:sqlite` adapter (tests), or a
|
|
10
|
-
* Hyperdrive-backed `postgres`/`pg`/`mysql2` driver (PlanetScale).
|
|
11
|
-
*
|
|
12
|
-
* `all` runs a row-returning statement (incl. `... RETURNING ...`); `run` runs a
|
|
13
|
-
* write and reports `rowsAffected` — the matched-rows count that drives the
|
|
14
|
-
* MySQL optimistic-concurrency guard (which has no `RETURNING`).
|
|
15
|
-
*
|
|
16
|
-
* Note: companion writes (aggregate/rank/FTS/CDC) run as separate sequential
|
|
17
|
-
* statements after the row write on every engine, so they share D1's
|
|
18
|
-
* at-least-once caveat — there is no cross-statement transaction here.
|
|
19
|
-
*/
|
|
8
|
+
* The async SQL surface the store core consumes. Satisfied by a
|
|
9
|
+
* `D1Session`/`D1Client` (D1), a `node:sqlite` adapter (tests), or a
|
|
10
|
+
* Hyperdrive-backed `postgres`/`pg`/`mysql2` driver (PlanetScale).
|
|
11
|
+
*
|
|
12
|
+
* `all` runs a row-returning statement (incl. `... RETURNING ...`); `run` runs a
|
|
13
|
+
* write and reports `rowsAffected` — the matched-rows count that drives the
|
|
14
|
+
* MySQL optimistic-concurrency guard (which has no `RETURNING`).
|
|
15
|
+
*
|
|
16
|
+
* Note: companion writes (aggregate/rank/FTS/CDC) run as separate sequential
|
|
17
|
+
* statements after the row write on every engine, so they share D1's
|
|
18
|
+
* at-least-once caveat — there is no cross-statement transaction here.
|
|
19
|
+
*/
|
|
20
20
|
interface SqlExec {
|
|
21
21
|
all: (sql: string, params: ReadonlyArray<unknown>) => Promise<Record<string, unknown>[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Optional: run several write statements as one round trip instead of
|
|
24
|
+
* `run()` called once per statement in sequence. Statements MUST be
|
|
25
|
+
* mutually independent — an implementation MAY reorder or parallelize
|
|
26
|
+
* across elements, so callers must never rely on array order between
|
|
27
|
+
* elements (e.g. a purge-then-insert pair belongs in separate sequential
|
|
28
|
+
* `queryRun`/`run` calls, not one `batch`).
|
|
29
|
+
*
|
|
30
|
+
* Absent, the store core falls back to its historical sequential `run()`
|
|
31
|
+
* loop, so an exec that doesn't implement this keeps working unchanged.
|
|
32
|
+
*
|
|
33
|
+
* D1's `client.batch` executes the whole array atomically (all-or-nothing)
|
|
34
|
+
* in one request, preserving array order — an actual atomicity improvement
|
|
35
|
+
* over the sequential fallback, not just a round-trip one. The Hyperdrive
|
|
36
|
+
* `postgres`/`pg` adapters instead dispatch every statement concurrently
|
|
37
|
+
* (`Promise.all`) over the same connection/pool rather than awaiting each
|
|
38
|
+
* in turn — still "at-least-once, non-atomic" like the fallback, but no
|
|
39
|
+
* longer serialized one full RTT at a time, and with no ordering between
|
|
40
|
+
* elements. Safe only for statements whose effects don't depend on each
|
|
41
|
+
* other (distinct-keyed rows), which is what every current caller batches.
|
|
42
|
+
*/
|
|
43
|
+
batch?: (statements: ReadonlyArray<{
|
|
44
|
+
params: ReadonlyArray<unknown>;
|
|
45
|
+
sql: string;
|
|
46
|
+
}>) => Promise<void>;
|
|
22
47
|
run: (sql: string, params: ReadonlyArray<unknown>) => Promise<SqlRunResult>;
|
|
23
48
|
}
|
|
24
49
|
/** Everything engine-specific the store core needs. One value per engine; `sqliteDialect` (in `@lunora/d1`) is the reference, Postgres/MySQL live in `@lunora/hyperdrive/global`. */
|
|
@@ -26,19 +51,19 @@ interface SqlDialect {
|
|
|
26
51
|
/** Affected-rows extractor for the OCC fallback when `supportsReturning` is false (MySQL). */
|
|
27
52
|
affectedRows?: (result: SqlRunResult) => number;
|
|
28
53
|
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
54
|
+
* Storage SQL column type for a validator `kind`. SQLite affinity
|
|
55
|
+
* (`TEXT`/`INTEGER`/`REAL`/`BLOB`); Postgres `TEXT`/`DOUBLE PRECISION`/
|
|
56
|
+
* `BOOLEAN`/`JSONB`/`BYTEA`; MySQL `VARCHAR(255)`/`TEXT`/`DOUBLE`/
|
|
57
|
+
* `TINYINT(1)`/`JSON`/`LONGBLOB`.
|
|
58
|
+
*/
|
|
34
59
|
columnType: (kind: string | undefined) => string;
|
|
35
60
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
61
|
+
* Engine SQL types for the **internal companion tables** (aggregate / rank /
|
|
62
|
+
* CDC), which are built from raw SQL types, not validator kinds. SQLite uses
|
|
63
|
+
* `TEXT`/`REAL`/`INTEGER`/`BLOB` and `INTEGER PRIMARY KEY AUTOINCREMENT`;
|
|
64
|
+
* Postgres `TEXT`/`DOUBLE PRECISION`/`INTEGER`/`BYTEA` + `BIGSERIAL`; MySQL
|
|
65
|
+
* needs a bounded `VARCHAR` key, `DOUBLE`, `LONGBLOB`, `AUTO_INCREMENT`.
|
|
66
|
+
*/
|
|
42
67
|
companionTypes: {
|
|
43
68
|
autoincrementPrimaryKey: string;
|
|
44
69
|
integer: string;
|
|
@@ -46,9 +71,19 @@ interface SqlDialect {
|
|
|
46
71
|
real: string;
|
|
47
72
|
text: string;
|
|
48
73
|
};
|
|
49
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* Map a stored value back to its JS form, by effective validator `kind`
|
|
76
|
+
* (inverse of `encode`). NOTE: currently **unused** by the store core, which
|
|
77
|
+
* hard-codes `sqliteDecode` in `decodeGlobalRow` on every engine. Kept on the
|
|
78
|
+
* seam for a future engine-native codec; an override here does not run today.
|
|
79
|
+
*/
|
|
50
80
|
decode: (value: unknown, kind: string | undefined) => unknown;
|
|
51
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* Map a JS value to its bound storage form (boolean→1/0, bigint→string,
|
|
83
|
+
* object→JSON on SQLite). NOTE: currently **unused** by the store core, which
|
|
84
|
+
* hard-codes `sqliteEncode` as `serializeColumnValue` on every engine. Kept on
|
|
85
|
+
* the seam for a future engine-native codec; an override here does not run today.
|
|
86
|
+
*/
|
|
52
87
|
encode: (value: unknown) => unknown;
|
|
53
88
|
/** The framework columns every global table carries — the `id` primary key and `_creationTime` — as `{ name, type }` so the DDL builder can quote each name through the engine's dialect. */
|
|
54
89
|
frameworkColumns: () => ReadonlyArray<{
|
|
@@ -56,27 +91,90 @@ interface SqlDialect {
|
|
|
56
91
|
type: string;
|
|
57
92
|
}>;
|
|
58
93
|
/**
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
94
|
+
* Optional: the key-prefix length an indexed column of this `kind` needs.
|
|
95
|
+
* MySQL/InnoDB can't index a `TEXT`/`LONGTEXT`/`BLOB` column without a prefix
|
|
96
|
+
* (the store appends `(<n>)` to the column reference); SQLite/Postgres index
|
|
97
|
+
* text columns directly and omit this hook (or return `undefined`). `kind` is
|
|
98
|
+
* the column's effective validator kind.
|
|
99
|
+
*/
|
|
65
100
|
indexKeyPrefix?: (kind: string | undefined) => number | undefined;
|
|
66
101
|
/** True when an `error` thrown by a write is a UNIQUE-constraint breach (mapped to a 409 ConflictError). */
|
|
67
102
|
isUniqueViolation: (error: unknown) => boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Most columns one table may carry on this engine, framework columns
|
|
105
|
+
* included. Omit it and the DDL builder does not check — the right answer
|
|
106
|
+
* for an engine whose ceiling is high enough that no real schema reaches it.
|
|
107
|
+
*
|
|
108
|
+
* Declared because the ceilings differ by more than an order of magnitude:
|
|
109
|
+
* D1 runs Workerd's SQLite build, which caps a table at 100 columns, where
|
|
110
|
+
* Postgres allows 1,600 and MySQL 4,096. A fixed number here would either
|
|
111
|
+
* miss the D1 failure or reject schemas the other two engines run happily.
|
|
112
|
+
*/
|
|
113
|
+
maxTableColumns?: number;
|
|
68
114
|
/** A short engine tag for diagnostics/branching (`"sqlite" | "postgres" | "mysql"`). The store core selects drizzle's matching dialect for rendering off this. */
|
|
69
115
|
readonly name: "mysql" | "postgres" | "sqlite";
|
|
116
|
+
/**
|
|
117
|
+
* Optional: the engine's own full-text index, opted into per search index
|
|
118
|
+
* with `.searchIndex({ strategy: "native" })`.
|
|
119
|
+
*
|
|
120
|
+
* Only Postgres supplies one today (`tsvector` + GIN + `to_tsquery`). It
|
|
121
|
+
* scales sublinearly where the portable inverted companion aggregates every
|
|
122
|
+
* matching token row, but it ranks with the engine's formula rather than the
|
|
123
|
+
* shared scorer — which is why it is opt-in and why the parity suite asserts
|
|
124
|
+
* matching, not order, for indexes that use it.
|
|
125
|
+
*
|
|
126
|
+
* Recall still matches the portable path: the stored form is built from the
|
|
127
|
+
* tokens Lunora's analyzer already produced, under a configuration that adds
|
|
128
|
+
* no stemming or stopwords of the engine's own.
|
|
129
|
+
*
|
|
130
|
+
* Every member returns a *statement*, not a fragment, so no engine grammar
|
|
131
|
+
* reaches the store core: Postgres matches with `@@` against a `tsvector`
|
|
132
|
+
* column while MySQL would use `MATCH … AGAINST` against a text column, and
|
|
133
|
+
* both fit here without the caller knowing which.
|
|
134
|
+
*/
|
|
135
|
+
nativeTextSearch?: {
|
|
136
|
+
/** DDL for the companion table holding the engine's indexed form, keyed by document id. */
|
|
137
|
+
createCompanion: (companion: string, keyType: string) => SQL;
|
|
138
|
+
/** DDL for the indexes that make the match fast. */
|
|
139
|
+
createIndexes: (companion: string) => SQL[];
|
|
140
|
+
/** Replace one document's row, given its already-analyzed token stream. */
|
|
141
|
+
indexDocument: (companion: string, id: string, analyzed: string) => SQL;
|
|
142
|
+
/** The `WHERE` predicate matching a query's analyzed terms, final term as a prefix. */
|
|
143
|
+
matches: (companion: string, terms: ReadonlyArray<string>) => SQL;
|
|
144
|
+
/** The `ORDER BY` expression, best first. */
|
|
145
|
+
rank: (companion: string, terms: ReadonlyArray<string>) => SQL;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* True when the engine ships SQLite's FTS5 module, which decides whether a
|
|
149
|
+
* search index is stored as an FTS5 shadow or as the portable inverted
|
|
150
|
+
* companion.
|
|
151
|
+
*
|
|
152
|
+
* A static property of the engine, so it is declared rather than probed:
|
|
153
|
+
* the previous `CREATE VIRTUAL TABLE` capability probe spent a round trip
|
|
154
|
+
* (and an error in the database's log) on every fresh connection to
|
|
155
|
+
* rediscover something the dialect already knows. Tests that want the
|
|
156
|
+
* portable layout override this instead of intercepting SQL strings.
|
|
157
|
+
*/
|
|
158
|
+
supportsFts5: boolean;
|
|
70
159
|
/** True when the engine supports `UPDATE/DELETE ... RETURNING` (SQLite/PG yes, MySQL no → use `affectedRows`). */
|
|
71
160
|
supportsReturning: boolean;
|
|
72
161
|
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
162
|
+
* The catalog probe for whether a physical `table` exists — backs the opt-in
|
|
163
|
+
* companion-table (`__agg_`/`__rank_`) existence checks. Returns a drizzle
|
|
164
|
+
* {@link SQL} (a non-empty result ⇒ the table exists) so it renders through
|
|
165
|
+
* the same per-engine path as every other statement, never a hand-built
|
|
166
|
+
* placeholder string. SQLite reads `sqlite_master`; Postgres/MySQL read
|
|
167
|
+
* `information_schema.tables`.
|
|
168
|
+
*/
|
|
80
169
|
tableExists: (table: string) => SQL;
|
|
170
|
+
/**
|
|
171
|
+
* Optional: btree operator class appended to an indexed text column so a
|
|
172
|
+
* `LIKE 'prefix%'` scan can use the index whatever the database collation.
|
|
173
|
+
* Postgres needs `text_pattern_ops` (a default `text_ops` btree built under
|
|
174
|
+
* e.g. `en_US.UTF-8` is useless to `LIKE`); SQLite and MySQL index prefix
|
|
175
|
+
* matches off the plain index and omit this. Only the search companion's
|
|
176
|
+
* token index reads it.
|
|
177
|
+
*/
|
|
178
|
+
textPatternOperatorClass?: string;
|
|
81
179
|
}
|
|
82
180
|
export { SqlDialect, SqlExec, SqlRunResult };
|
package/dist/dialect.mjs
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|