@jini-ai/db 0.2.0
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/CHANGELOG.md +33 -0
- package/LICENSE.pglite-socket +176 -0
- package/NOTICE.md +27 -0
- package/README.md +210 -0
- package/dist/core/artifact-naming.d.ts +34 -0
- package/dist/core/artifact-naming.js +34 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.js +10 -0
- package/dist/core/messages.d.ts +43 -0
- package/dist/core/messages.js +26 -0
- package/dist/core/pg-types.d.ts +22 -0
- package/dist/core/pg-types.js +26 -0
- package/dist/core/ports.d.ts +70 -0
- package/dist/core/ports.js +22 -0
- package/dist/core/row-values.d.ts +32 -0
- package/dist/core/row-values.js +42 -0
- package/dist/core/socket.d.ts +8 -0
- package/dist/core/socket.js +8 -0
- package/dist/kernel/dialect.d.ts +83 -0
- package/dist/kernel/dialect.js +191 -0
- package/dist/kernel/index.d.ts +15 -0
- package/dist/kernel/index.js +15 -0
- package/dist/kernel/kernel-core.d.ts +43 -0
- package/dist/kernel/kernel-core.js +98 -0
- package/dist/kernel/ops.d.ts +48 -0
- package/dist/kernel/ops.js +49 -0
- package/dist/kernel/pg-types.d.ts +22 -0
- package/dist/kernel/pg-types.js +26 -0
- package/dist/kernel/pglite/dialect.d.ts +11 -0
- package/dist/kernel/pglite/dialect.js +88 -0
- package/dist/kernel/pglite/driver.d.ts +30 -0
- package/dist/kernel/pglite/driver.js +47 -0
- package/dist/kernel/pglite/index.d.ts +5 -0
- package/dist/kernel/pglite/index.js +5 -0
- package/dist/kernel/pglite/ops.d.ts +39 -0
- package/dist/kernel/pglite/ops.js +78 -0
- package/dist/kernel/port.d.ts +82 -0
- package/dist/kernel/port.js +11 -0
- package/dist/kernel/postgres/driver.d.ts +23 -0
- package/dist/kernel/postgres/driver.js +42 -0
- package/dist/kernel/postgres/index.d.ts +5 -0
- package/dist/kernel/postgres/index.js +5 -0
- package/dist/kernel/postgres/ops.d.ts +12 -0
- package/dist/kernel/postgres/ops.js +46 -0
- package/dist/kernel/postgres/pglite-socket.d.ts +23 -0
- package/dist/kernel/postgres/pglite-socket.js +52 -0
- package/dist/kernel/postgres-lock.d.ts +8 -0
- package/dist/kernel/postgres-lock.js +10 -0
- package/dist/kernel/schema-scope.d.ts +14 -0
- package/dist/kernel/schema-scope.js +20 -0
- package/dist/kernel/schema-shape.d.ts +26 -0
- package/dist/kernel/schema-shape.js +83 -0
- package/dist/kernel/socket.d.ts +8 -0
- package/dist/kernel/socket.js +8 -0
- package/dist/kernel/sqlite/driver.d.ts +71 -0
- package/dist/kernel/sqlite/driver.js +132 -0
- package/dist/kernel/sqlite/index.d.ts +4 -0
- package/dist/kernel/sqlite/index.js +4 -0
- package/dist/kernel/sqlite/ops.d.ts +12 -0
- package/dist/kernel/sqlite/ops.js +40 -0
- package/dist/kernel/store-copy.d.ts +82 -0
- package/dist/kernel/store-copy.js +240 -0
- package/dist/kernel/turn-lock.d.ts +21 -0
- package/dist/kernel/turn-lock.js +42 -0
- package/dist/kernel/typegen.d.ts +13 -0
- package/dist/kernel/typegen.js +76 -0
- package/dist/migrate/checksum.d.ts +11 -0
- package/dist/migrate/checksum.js +14 -0
- package/dist/migrate/index.d.ts +8 -0
- package/dist/migrate/index.js +8 -0
- package/dist/migrate/runner.d.ts +33 -0
- package/dist/migrate/runner.js +126 -0
- package/dist/migrate/step.d.ts +37 -0
- package/dist/migrate/step.js +31 -0
- package/dist/pglite/dialect.d.ts +11 -0
- package/dist/pglite/dialect.js +88 -0
- package/dist/pglite/driver.d.ts +30 -0
- package/dist/pglite/driver.js +46 -0
- package/dist/pglite/index.d.ts +5 -0
- package/dist/pglite/index.js +4 -0
- package/dist/pglite/ops.d.ts +39 -0
- package/dist/pglite/ops.js +78 -0
- package/dist/pglite/owner.d.ts +107 -0
- package/dist/pglite/owner.js +359 -0
- package/dist/pglite/socket-server.d.ts +38 -0
- package/dist/pglite/socket-server.js +375 -0
- package/dist/pglite/types.d.ts +9 -0
- package/dist/pglite/types.js +2 -0
- package/dist/postgres/driver.d.ts +23 -0
- package/dist/postgres/driver.js +41 -0
- package/dist/postgres/index.d.ts +3 -0
- package/dist/postgres/index.js +3 -0
- package/dist/postgres/ops.d.ts +12 -0
- package/dist/postgres/ops.js +46 -0
- package/dist/postgres/pglite-socket.d.ts +23 -0
- package/dist/postgres/pglite-socket.js +51 -0
- package/dist/postgres/types.d.ts +53 -0
- package/dist/postgres/types.js +14 -0
- package/dist/sqlite/db-ops.d.ts +59 -0
- package/dist/sqlite/db-ops.js +82 -0
- package/dist/sqlite/driver.d.ts +71 -0
- package/dist/sqlite/driver.js +132 -0
- package/dist/sqlite/index.d.ts +9 -0
- package/dist/sqlite/index.js +4 -0
- package/dist/sqlite/inspect.d.ts +60 -0
- package/dist/sqlite/inspect.js +153 -0
- package/dist/sqlite/open.d.ts +58 -0
- package/dist/sqlite/open.js +43 -0
- package/dist/sqlite/ops.d.ts +12 -0
- package/dist/sqlite/ops.js +40 -0
- package/dist/sqlite/restore-types.d.ts +42 -0
- package/dist/sqlite/restore-types.js +2 -0
- package/dist/sqlite/sync-types.d.ts +34 -0
- package/dist/sqlite/sync-types.js +2 -0
- package/dist/sqlite/types.d.ts +37 -0
- package/dist/sqlite/types.js +8 -0
- package/dist/tools/database-catalog.d.ts +61 -0
- package/dist/tools/database-catalog.js +201 -0
- package/dist/tools/destination-port.d.ts +27 -0
- package/dist/tools/destination-port.js +2 -0
- package/dist/tools/index.d.ts +13 -0
- package/dist/tools/index.js +11 -0
- package/dist/tools/plan-store.d.ts +61 -0
- package/dist/tools/plan-store.js +39 -0
- package/dist/tools/read-tools.d.ts +53 -0
- package/dist/tools/read-tools.js +97 -0
- package/dist/tools/registration.d.ts +18 -0
- package/dist/tools/registration.js +10 -0
- package/dist/tools/restore-points.d.ts +125 -0
- package/dist/tools/restore-points.js +54 -0
- package/dist/tools/surface-port.d.ts +43 -0
- package/dist/tools/surface-port.js +2 -0
- package/dist/tools/timeline.d.ts +63 -0
- package/dist/tools/timeline.js +46 -0
- package/dist/tools/transfer-catalog.d.ts +25 -0
- package/dist/tools/transfer-catalog.js +59 -0
- package/dist/tools/transfer-tools.d.ts +82 -0
- package/dist/tools/transfer-tools.js +346 -0
- package/dist/transfer/copy-engine.d.ts +129 -0
- package/dist/transfer/copy-engine.js +233 -0
- package/dist/transfer/index.d.ts +9 -0
- package/dist/transfer/index.js +9 -0
- package/dist/transfer/internal-ddl.d.ts +23 -0
- package/dist/transfer/internal-ddl.js +119 -0
- package/dist/transfer/plan.d.ts +28 -0
- package/dist/transfer/plan.js +19 -0
- package/dist/transfer/postgres-ddl.d.ts +36 -0
- package/dist/transfer/postgres-ddl.js +16 -0
- package/dist/transfer/postgres-target.d.ts +65 -0
- package/dist/transfer/postgres-target.js +95 -0
- package/dist/transfer/sqlite-source.d.ts +78 -0
- package/dist/transfer/sqlite-source.js +79 -0
- package/dist/transfer/table-catalog.d.ts +44 -0
- package/dist/transfer/table-catalog.js +109 -0
- package/dist/transfer/types.d.ts +47 -0
- package/dist/transfer/types.js +2 -0
- package/package.json +184 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file How every Postgres transport turns column text into JS values where the defaults differ
|
|
3
|
+
* from what the repos (and SQLite) expect. One table, used by the PGlite and node-postgres drivers,
|
|
4
|
+
* so a row reads the same on both.
|
|
5
|
+
*
|
|
6
|
+
* - `json`/`jsonb` stay JSON TEXT: the repos store and parse JSON themselves (SQLite has no JSON
|
|
7
|
+
* type), so a `string` column type holds on every dialect. The text is re-serialised compactly
|
|
8
|
+
* (`{"a":1}`, as `JSON.stringify` writes it) instead of Postgres's own spelling (`{"a": 1}`), so a
|
|
9
|
+
* value written compactly reads back byte-identical.
|
|
10
|
+
* Key order is jsonb's (it does not keep insertion order); compare parsed values, not text.
|
|
11
|
+
* - `int8` (`bigint`) is a `number` when it fits in a double exactly, else a `bigint` — PGlite's own
|
|
12
|
+
* rule, which node-postgres (default: string) is aligned to.
|
|
13
|
+
*/
|
|
14
|
+
export const PG_OID = { int8: 20, json: 114, jsonb: 3802 };
|
|
15
|
+
export function parseInt8(text) {
|
|
16
|
+
const value = BigInt(text);
|
|
17
|
+
return value < BigInt(Number.MIN_SAFE_INTEGER) || value > BigInt(Number.MAX_SAFE_INTEGER) ? value : Number(value);
|
|
18
|
+
}
|
|
19
|
+
const compactJson = (text) => JSON.stringify(JSON.parse(text));
|
|
20
|
+
/** Parser overrides by type oid. */
|
|
21
|
+
export const PG_PARSERS = {
|
|
22
|
+
[PG_OID.int8]: parseInt8,
|
|
23
|
+
[PG_OID.json]: compactJson,
|
|
24
|
+
[PG_OID.jsonb]: compactJson,
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=pg-types.js.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Driver-neutral persistence contracts.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* The vocabulary a host talks to its database through, with no statement about which database
|
|
6
|
+
* that is. Every declaration here is an `interface`/`type` that erases at compile time, so this
|
|
7
|
+
* module contributes zero bytes and zero `require()` calls to a consumer's runtime graph.
|
|
8
|
+
*
|
|
9
|
+
* How it relates to the package:
|
|
10
|
+
* `db/core` is the half of `@jini-ai/infra` that must stay installable with no native module
|
|
11
|
+
* present — a host running Supabase/Postgres pulls `@jini-ai/infra/db/core` and never compiles
|
|
12
|
+
* `better-sqlite3`. That property is not a convention here, it is enforced: `pnpm guard`'s
|
|
13
|
+
* R12 check walks this directory's transitive import closure and fails if it reaches a driver
|
|
14
|
+
* folder or a driver package. See `scripts/check-driver-isolation.ts`.
|
|
15
|
+
*
|
|
16
|
+
* Architectural role:
|
|
17
|
+
* Ports. Drivers under `db/<driver>/` implement these; host domain code depends on these and
|
|
18
|
+
* never on a driver. The direction is one-way — `db/sqlite` may import `db/core`, never the
|
|
19
|
+
* reverse — which is what lets a second driver land later without restructuring either side.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* How expensive a restore point is to capture, which is what lets a caller decide whether to
|
|
23
|
+
* take one inline or defer it. SQLite's whole-file online backup is `cheap`; a logical dump
|
|
24
|
+
* against a networked Postgres is not.
|
|
25
|
+
*/
|
|
26
|
+
export type RestoreCostClass = 'cheap' | 'expensive';
|
|
27
|
+
/**
|
|
28
|
+
* What a captured artifact physically is. `file-snapshot` is a byte-level copy restorable by
|
|
29
|
+
* swapping it into place; `logical-dump` is a replayable statement stream. The distinction
|
|
30
|
+
* matters to callers because only the former is guaranteed to round-trip a database's exact
|
|
31
|
+
* page state (indexes, FTS shadow tables, vacuum layout).
|
|
32
|
+
*/
|
|
33
|
+
export type RestoreKind = 'file-snapshot' | 'logical-dump';
|
|
34
|
+
export interface RestoreCapability {
|
|
35
|
+
readonly costClass: RestoreCostClass;
|
|
36
|
+
readonly kind: RestoreKind;
|
|
37
|
+
}
|
|
38
|
+
export interface RestorePoint {
|
|
39
|
+
/** Opaque handle the same adapter can later restore from — a filesystem path for file-snapshot drivers. */
|
|
40
|
+
readonly artifactRef: string;
|
|
41
|
+
/** The host's monotonic write counter at capture time, so a caller can tell how stale an artifact is. */
|
|
42
|
+
readonly watermarkAtCapture: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A host-supplied read of the current write watermark. Injected rather than imported so this
|
|
46
|
+
* package never needs to know the host's schema — the watermark lives in a host-owned table
|
|
47
|
+
* whose name this package deliberately does not know, and a driver here only needs its value.
|
|
48
|
+
* May be sync (a SQLite host reads it in-process) or async (a PGlite/Postgres host queries it);
|
|
49
|
+
* callers always await it.
|
|
50
|
+
*/
|
|
51
|
+
export type WatermarkReader = () => number | Promise<number>;
|
|
52
|
+
/**
|
|
53
|
+
* Database-level operations that sit beneath the ORM: capability discovery, restore-point
|
|
54
|
+
* capture, and physical restore. Deliberately narrow — anything expressible as a query belongs
|
|
55
|
+
* in a repository, not here.
|
|
56
|
+
*/
|
|
57
|
+
export interface DbOpsPort {
|
|
58
|
+
getCapabilities(): Promise<{
|
|
59
|
+
restorePoint: RestoreCapability;
|
|
60
|
+
}>;
|
|
61
|
+
captureRestorePoint(required: {
|
|
62
|
+
scopeId: string;
|
|
63
|
+
}): Promise<RestorePoint>;
|
|
64
|
+
restoreFromArtifact(required: {
|
|
65
|
+
artifactRef: string;
|
|
66
|
+
}): Promise<{
|
|
67
|
+
restartRequired: boolean;
|
|
68
|
+
}>;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=ports.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Driver-neutral persistence contracts.
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* The vocabulary a host talks to its database through, with no statement about which database
|
|
6
|
+
* that is. Every declaration here is an `interface`/`type` that erases at compile time, so this
|
|
7
|
+
* module contributes zero bytes and zero `require()` calls to a consumer's runtime graph.
|
|
8
|
+
*
|
|
9
|
+
* How it relates to the package:
|
|
10
|
+
* `db/core` is the half of `@jini-ai/infra` that must stay installable with no native module
|
|
11
|
+
* present — a host running Supabase/Postgres pulls `@jini-ai/infra/db/core` and never compiles
|
|
12
|
+
* `better-sqlite3`. That property is not a convention here, it is enforced: `pnpm guard`'s
|
|
13
|
+
* R12 check walks this directory's transitive import closure and fails if it reaches a driver
|
|
14
|
+
* folder or a driver package. See `scripts/check-driver-isolation.ts`.
|
|
15
|
+
*
|
|
16
|
+
* Architectural role:
|
|
17
|
+
* Ports. Drivers under `db/<driver>/` implement these; host domain code depends on these and
|
|
18
|
+
* never on a driver. The direction is one-way — `db/sqlite` may import `db/core`, never the
|
|
19
|
+
* reverse — which is what lets a second driver land later without restructuring either side.
|
|
20
|
+
*/
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=ports.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Generic legacy row shapes and tolerant JSON narrowing; no driver or SQL dependencies. */
|
|
2
|
+
export type DbRow = Record<string, any>;
|
|
3
|
+
export type JsonObject = Record<string, unknown>;
|
|
4
|
+
/** @module db/core/json
|
|
5
|
+
* Pure JSON parsing utility that converts SQLite TEXT columns to JS values without throwing.
|
|
6
|
+
* Imports no sibling subdirectory.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Safely parse a SQLite TEXT column value to a JS value.
|
|
10
|
+
* Returns `undefined` instead of throwing for non-string, empty, or malformed JSON —
|
|
11
|
+
* callers should narrow the return type before use.
|
|
12
|
+
* @param s - raw column value as stored in SQLite (typically `string | null`)
|
|
13
|
+
* @returns parsed value, or `undefined` if the input is absent or unparseable
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseJsonOrUndef(s: unknown): any;
|
|
16
|
+
/** @module db/core/rows
|
|
17
|
+
* Helpers that normalize raw better-sqlite3 query results to typed `DbRow` values.
|
|
18
|
+
* Imports no sibling subdirectory; depends only on `core/types`.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Narrow a single `db.prepare(…).get()` result to `DbRow | null`.
|
|
22
|
+
* Returns `null` for falsy or non-object returns so callers can distinguish "no row" from an empty row.
|
|
23
|
+
* @param value - raw return from better-sqlite3's `.get()`
|
|
24
|
+
*/
|
|
25
|
+
export declare function row(value: unknown): DbRow | null;
|
|
26
|
+
/**
|
|
27
|
+
* Map a `db.prepare(…).all()` result array to `DbRow[]`, replacing any falsy or non-object
|
|
28
|
+
* entries with an empty object so the array length is always preserved.
|
|
29
|
+
* @param value - raw return from better-sqlite3's `.all()`
|
|
30
|
+
*/
|
|
31
|
+
export declare function rows(value: unknown[]): DbRow[];
|
|
32
|
+
//# sourceMappingURL=row-values.d.ts.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** @module db/core/json
|
|
2
|
+
* Pure JSON parsing utility that converts SQLite TEXT columns to JS values without throwing.
|
|
3
|
+
* Imports no sibling subdirectory.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Safely parse a SQLite TEXT column value to a JS value.
|
|
7
|
+
* Returns `undefined` instead of throwing for non-string, empty, or malformed JSON —
|
|
8
|
+
* callers should narrow the return type before use.
|
|
9
|
+
* @param s - raw column value as stored in SQLite (typically `string | null`)
|
|
10
|
+
* @returns parsed value, or `undefined` if the input is absent or unparseable
|
|
11
|
+
*/
|
|
12
|
+
export function parseJsonOrUndef(s) {
|
|
13
|
+
if (typeof s !== 'string' || !s)
|
|
14
|
+
return undefined;
|
|
15
|
+
try {
|
|
16
|
+
return JSON.parse(s);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** @module db/core/rows
|
|
23
|
+
* Helpers that normalize raw better-sqlite3 query results to typed `DbRow` values.
|
|
24
|
+
* Imports no sibling subdirectory; depends only on `core/types`.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Narrow a single `db.prepare(…).get()` result to `DbRow | null`.
|
|
28
|
+
* Returns `null` for falsy or non-object returns so callers can distinguish "no row" from an empty row.
|
|
29
|
+
* @param value - raw return from better-sqlite3's `.get()`
|
|
30
|
+
*/
|
|
31
|
+
export function row(value) {
|
|
32
|
+
return value && typeof value === 'object' ? value : null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Map a `db.prepare(…).all()` result array to `DbRow[]`, replacing any falsy or non-object
|
|
36
|
+
* entries with an empty object so the array length is always preserved.
|
|
37
|
+
* @param value - raw return from better-sqlite3's `.all()`
|
|
38
|
+
*/
|
|
39
|
+
export function rows(value) {
|
|
40
|
+
return value.map((item) => row(item) ?? {});
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=row-values.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Where a PGlite owner's Unix socket lives inside its socket directory. Shared by the owner
|
|
3
|
+
* (`@jini-ai/db/pglite`) and the socket client (`@jini-ai/db/postgres`), which must agree on it and
|
|
4
|
+
* must not import each other.
|
|
5
|
+
*/
|
|
6
|
+
/** The Postgres client convention: a client given `host=<dir>, port=5432` connects to this file. */
|
|
7
|
+
export declare const PGLITE_SOCKET_FILE = ".s.PGSQL.5432";
|
|
8
|
+
//# sourceMappingURL=socket.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Where a PGlite owner's Unix socket lives inside its socket directory. Shared by the owner
|
|
3
|
+
* (`@jini-ai/db/pglite`) and the socket client (`@jini-ai/db/postgres`), which must agree on it and
|
|
4
|
+
* must not import each other.
|
|
5
|
+
*/
|
|
6
|
+
/** The Postgres client convention: a client given `host=<dir>, port=5432` connects to this file. */
|
|
7
|
+
export const PGLITE_SOCKET_FILE = ".s.PGSQL.5432";
|
|
8
|
+
//# sourceMappingURL=socket.js.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type Expression, type RawBuilder } from "kysely";
|
|
2
|
+
import type { StorageDialect, StorageKernel } from "./port.js";
|
|
3
|
+
/**
|
|
4
|
+
* A SCALAR at `path` inside a JSON text/jsonb column, as TEXT with the same spelling on both
|
|
5
|
+
* dialects: strings as themselves, integers in decimal, booleans as `'true'`/`'false'`, and SQL NULL
|
|
6
|
+
* for JSON `null` or a missing key. Compare numbers through an explicit cast of this text.
|
|
7
|
+
*
|
|
8
|
+
* Not for objects or arrays (each dialect prints those differently), nor for fractional numbers
|
|
9
|
+
* whose spelling matters (`1.50` reads `1.50` on Postgres, `1.5` on SQLite).
|
|
10
|
+
*/
|
|
11
|
+
export declare function jsonText(dialect: StorageDialect, column: Expression<unknown>, path: readonly string[]): RawBuilder<string | null>;
|
|
12
|
+
/**
|
|
13
|
+
* `column` at `path` equals the JSON scalar `value` (string, number or boolean), as a boolean
|
|
14
|
+
* expression. SQLite has no JSON boolean type — `json_extract` reads `true`/`false` as `1`/`0` — so
|
|
15
|
+
* a boolean is bound as `1`/`0` there; Postgres compares the two `jsonb` values, so `10` matches
|
|
16
|
+
* `10.0` and a string never matches a number.
|
|
17
|
+
*/
|
|
18
|
+
export declare function jsonScalarEquals(dialect: StorageDialect, column: Expression<unknown>, path: readonly string[], value: string | number | boolean): RawBuilder<boolean>;
|
|
19
|
+
/**
|
|
20
|
+
* A sort key for the value at `path`: SQLite's typed `json_extract`, Postgres' `jsonb` value, so
|
|
21
|
+
* numbers sort as numbers (`9` before `10`) — unlike {@link jsonText}, whose text sorts `'10'` first.
|
|
22
|
+
* A missing key is SQL NULL on both; pair with `nullsFirst()` (asc) / `nullsLast()` (desc) so NULLs
|
|
23
|
+
* land where SQLite puts them by default.
|
|
24
|
+
*/
|
|
25
|
+
export declare function jsonSortKey(dialect: StorageDialect, column: Expression<unknown>, path: readonly string[]): RawBuilder<unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* The column's JSON with `path` set to `value` (any JSON-serialisable value), creating the last key
|
|
28
|
+
* if it is missing. A NULL column counts as `{}`. The parent object must already exist.
|
|
29
|
+
*/
|
|
30
|
+
export declare function jsonSet(dialect: StorageDialect, column: Expression<unknown>, path: readonly string[], value: unknown): RawBuilder<string>;
|
|
31
|
+
/** The statement's current time as `Date#toISOString()` text (UTC, milliseconds, `Z`). */
|
|
32
|
+
export declare function nowIso(dialect: StorageDialect): RawBuilder<string>;
|
|
33
|
+
/** A boolean column as read: `true`/`false` on Postgres, `1`/`0` on SQLite. NULL stays null. */
|
|
34
|
+
export declare function toBool(value: boolean | number | null | undefined): boolean | null;
|
|
35
|
+
/**
|
|
36
|
+
* True iff `err` is a UNIQUE violation, from any driver: SQLite's `SQLITE_CONSTRAINT_UNIQUE` /
|
|
37
|
+
* "UNIQUE constraint failed", Postgres SQLSTATE `23505` (node-postgres and PGlite both set `code`).
|
|
38
|
+
* The message check also covers the in-memory test doubles, which throw SQLite's wording.
|
|
39
|
+
*/
|
|
40
|
+
export declare function isUniqueViolation(err: unknown): boolean;
|
|
41
|
+
/** A blob/bytea value as bytes, whichever shape the driver returned (Buffer, Uint8Array). */
|
|
42
|
+
export declare function toBytes(value: unknown): Uint8Array;
|
|
43
|
+
export interface ColumnInfo {
|
|
44
|
+
name: string;
|
|
45
|
+
/** The declared type, lower-case, in the dialect's own spelling (`text`, `integer`, `bigint`…). */
|
|
46
|
+
type: string;
|
|
47
|
+
notNull: boolean;
|
|
48
|
+
primaryKey: boolean;
|
|
49
|
+
}
|
|
50
|
+
/** User tables in the connection's current schema, sorted by name. Internal tables excluded. */
|
|
51
|
+
export declare function listTables<DB>(kernel: StorageKernel<DB>): Promise<string[]>;
|
|
52
|
+
export declare function tableExists<DB>(kernel: StorageKernel<DB>, table: string): Promise<boolean>;
|
|
53
|
+
/** Columns in declaration order; empty when the table does not exist. */
|
|
54
|
+
export declare function listColumns<DB>(kernel: StorageKernel<DB>, table: string): Promise<ColumnInfo[]>;
|
|
55
|
+
/** The four SQLite storage classes a portable column declares (plugin data modules use these). */
|
|
56
|
+
export type ColumnAffinity = "TEXT" | "INTEGER" | "REAL" | "BLOB";
|
|
57
|
+
/**
|
|
58
|
+
* The column type to write in DDL for `affinity`. SQLite keeps the affinity name itself, so a
|
|
59
|
+
* table created here reads back exactly as before; Postgres gets the same mapping as the frozen
|
|
60
|
+
* Postgres baseline. Lower-cased, it is also what {@link listColumns} reports for that column.
|
|
61
|
+
*/
|
|
62
|
+
export declare function columnTypeSql(dialect: StorageDialect, affinity: ColumnAffinity): string;
|
|
63
|
+
/** A generated integer primary key column's type and constraints (write the column name before it). */
|
|
64
|
+
export declare function autoIdColumnSql(dialect: StorageDialect): string;
|
|
65
|
+
/**
|
|
66
|
+
* Makes everything committed so far durable in the main database file. SQLite: a full WAL
|
|
67
|
+
* checkpoint (must run outside a write transaction on this connection: SQLite refuses it with
|
|
68
|
+
* "database table is locked"). Postgres: nothing to do, a commit is already durable.
|
|
69
|
+
*/
|
|
70
|
+
export declare function checkpointWal<DB>(kernel: StorageKernel<DB>): Promise<void>;
|
|
71
|
+
export interface IndexInfo {
|
|
72
|
+
name: string;
|
|
73
|
+
/** Indexed columns in key order. */
|
|
74
|
+
columns: string[];
|
|
75
|
+
unique: boolean;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The indexes created with `CREATE INDEX` on `table`, by name. Excluded: the ones a PRIMARY KEY or
|
|
79
|
+
* UNIQUE constraint makes on its own (SQLite origin `pk`/`u`, Postgres constraint-backed), which no
|
|
80
|
+
* `CREATE INDEX` declared. Empty when the table does not exist.
|
|
81
|
+
*/
|
|
82
|
+
export declare function listIndexes<DB>(kernel: StorageKernel<DB>, table: string): Promise<Map<string, IndexInfo>>;
|
|
83
|
+
//# sourceMappingURL=dialect.d.ts.map
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
/**
|
|
3
|
+
* @file Dialect helpers: the few places SQLite and Postgres spell the same thing differently, as
|
|
4
|
+
* Kysely `sql` fragments or kernel queries. A repo that needs one of these calls the helper with
|
|
5
|
+
* `kernel.dialect`; it never writes the dialect's own spelling (the raw-SQLite guard enforces the
|
|
6
|
+
* SQLite side). Idea from EmDash's `dialect-helpers.ts`, re-implemented here.
|
|
7
|
+
*
|
|
8
|
+
* Upserts need no helper: Kysely's `onConflict(oc => oc.column(…).doUpdateSet(…))` compiles to the
|
|
9
|
+
* same `ON CONFLICT … DO UPDATE` on both; "the value being inserted" is `eb.ref("excluded.col")`.
|
|
10
|
+
*/
|
|
11
|
+
const JSON_KEY = /^[A-Za-z0-9_]+$/;
|
|
12
|
+
function checkedPath(path) {
|
|
13
|
+
if (path.length === 0)
|
|
14
|
+
throw new Error("JSON path must name at least one key");
|
|
15
|
+
for (const key of path) {
|
|
16
|
+
if (!JSON_KEY.test(key))
|
|
17
|
+
throw new Error(`JSON path key '${key}' is not a plain identifier`);
|
|
18
|
+
}
|
|
19
|
+
return path;
|
|
20
|
+
}
|
|
21
|
+
const sqlitePath = (path) => `$.${checkedPath(path).join(".")}`;
|
|
22
|
+
const pgPath = (path) => `{${checkedPath(path).join(",")}}`;
|
|
23
|
+
/**
|
|
24
|
+
* A SCALAR at `path` inside a JSON text/jsonb column, as TEXT with the same spelling on both
|
|
25
|
+
* dialects: strings as themselves, integers in decimal, booleans as `'true'`/`'false'`, and SQL NULL
|
|
26
|
+
* for JSON `null` or a missing key. Compare numbers through an explicit cast of this text.
|
|
27
|
+
*
|
|
28
|
+
* Not for objects or arrays (each dialect prints those differently), nor for fractional numbers
|
|
29
|
+
* whose spelling matters (`1.50` reads `1.50` on Postgres, `1.5` on SQLite).
|
|
30
|
+
*/
|
|
31
|
+
export function jsonText(dialect, column, path) {
|
|
32
|
+
if (dialect === "postgres")
|
|
33
|
+
return sql `(${column}::jsonb #>> ${pgPath(path)})`;
|
|
34
|
+
const at = sqlitePath(path);
|
|
35
|
+
// json_extract hands back SQLite values (1/0 for booleans, typed numbers); spell them as JSON does.
|
|
36
|
+
return sql `(CASE json_type(${column}, ${at})
|
|
37
|
+
WHEN 'true' THEN 'true' WHEN 'false' THEN 'false'
|
|
38
|
+
ELSE CAST(json_extract(${column}, ${at}) AS TEXT) END)`;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `column` at `path` equals the JSON scalar `value` (string, number or boolean), as a boolean
|
|
42
|
+
* expression. SQLite has no JSON boolean type — `json_extract` reads `true`/`false` as `1`/`0` — so
|
|
43
|
+
* a boolean is bound as `1`/`0` there; Postgres compares the two `jsonb` values, so `10` matches
|
|
44
|
+
* `10.0` and a string never matches a number.
|
|
45
|
+
*/
|
|
46
|
+
export function jsonScalarEquals(dialect, column, path, value) {
|
|
47
|
+
if (dialect === "postgres")
|
|
48
|
+
return sql `((${column}::jsonb #> ${pgPath(path)}) = ${JSON.stringify(value)}::jsonb)`;
|
|
49
|
+
const bound = typeof value === "boolean" ? (value ? 1 : 0) : value;
|
|
50
|
+
return sql `(json_extract(${column}, ${sqlitePath(path)}) = ${bound})`;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A sort key for the value at `path`: SQLite's typed `json_extract`, Postgres' `jsonb` value, so
|
|
54
|
+
* numbers sort as numbers (`9` before `10`) — unlike {@link jsonText}, whose text sorts `'10'` first.
|
|
55
|
+
* A missing key is SQL NULL on both; pair with `nullsFirst()` (asc) / `nullsLast()` (desc) so NULLs
|
|
56
|
+
* land where SQLite puts them by default.
|
|
57
|
+
*/
|
|
58
|
+
export function jsonSortKey(dialect, column, path) {
|
|
59
|
+
return dialect === "postgres"
|
|
60
|
+
? sql `(${column}::jsonb #> ${pgPath(path)})`
|
|
61
|
+
: sql `json_extract(${column}, ${sqlitePath(path)})`;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The column's JSON with `path` set to `value` (any JSON-serialisable value), creating the last key
|
|
65
|
+
* if it is missing. A NULL column counts as `{}`. The parent object must already exist.
|
|
66
|
+
*/
|
|
67
|
+
export function jsonSet(dialect, column, path, value) {
|
|
68
|
+
const encoded = JSON.stringify(value);
|
|
69
|
+
return dialect === "sqlite"
|
|
70
|
+
? sql `json_set(coalesce(${column}, '{}'), ${sqlitePath(path)}, json(${encoded}))`
|
|
71
|
+
: sql `jsonb_set(coalesce(${column}::jsonb, '{}'::jsonb), ${pgPath(path)}, ${encoded}::jsonb, true)`;
|
|
72
|
+
}
|
|
73
|
+
/** The statement's current time as `Date#toISOString()` text (UTC, milliseconds, `Z`). */
|
|
74
|
+
export function nowIso(dialect) {
|
|
75
|
+
return dialect === "sqlite"
|
|
76
|
+
? sql `strftime('%Y-%m-%dT%H:%M:%fZ', 'now')`
|
|
77
|
+
: sql `to_char(statement_timestamp() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`;
|
|
78
|
+
}
|
|
79
|
+
/** A boolean column as read: `true`/`false` on Postgres, `1`/`0` on SQLite. NULL stays null. */
|
|
80
|
+
export function toBool(value) {
|
|
81
|
+
if (value === null || value === undefined)
|
|
82
|
+
return null;
|
|
83
|
+
return value === true || value === 1;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* True iff `err` is a UNIQUE violation, from any driver: SQLite's `SQLITE_CONSTRAINT_UNIQUE` /
|
|
87
|
+
* "UNIQUE constraint failed", Postgres SQLSTATE `23505` (node-postgres and PGlite both set `code`).
|
|
88
|
+
* The message check also covers the in-memory test doubles, which throw SQLite's wording.
|
|
89
|
+
*/
|
|
90
|
+
export function isUniqueViolation(err) {
|
|
91
|
+
if (!(err instanceof Error))
|
|
92
|
+
return false;
|
|
93
|
+
const code = err.code;
|
|
94
|
+
return code === "SQLITE_CONSTRAINT_UNIQUE" || code === "23505" || err.message.includes("UNIQUE constraint failed");
|
|
95
|
+
}
|
|
96
|
+
/** A blob/bytea value as bytes, whichever shape the driver returned (Buffer, Uint8Array). */
|
|
97
|
+
export function toBytes(value) {
|
|
98
|
+
if (value instanceof Uint8Array)
|
|
99
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
100
|
+
throw new Error(`expected a binary column value, got ${typeof value}`);
|
|
101
|
+
}
|
|
102
|
+
/** User tables in the connection's current schema, sorted by name. Internal tables excluded. */
|
|
103
|
+
export async function listTables(kernel) {
|
|
104
|
+
const rows = await kernel.query(kernel.dialect === "sqlite"
|
|
105
|
+
? sql `SELECT name FROM sqlite_schema WHERE type = 'table' AND substr(name, 1, 7) <> 'sqlite_' ORDER BY name`
|
|
106
|
+
: sql `SELECT table_name AS name FROM information_schema.tables
|
|
107
|
+
WHERE table_schema = current_schema() AND table_type = 'BASE TABLE' ORDER BY table_name`);
|
|
108
|
+
return rows.map((row) => row.name);
|
|
109
|
+
}
|
|
110
|
+
export async function tableExists(kernel, table) {
|
|
111
|
+
return (await listColumns(kernel, table)).length > 0;
|
|
112
|
+
}
|
|
113
|
+
/** Columns in declaration order; empty when the table does not exist. */
|
|
114
|
+
export async function listColumns(kernel, table) {
|
|
115
|
+
const rows = await kernel.query(kernel.dialect === "sqlite"
|
|
116
|
+
? sql `SELECT name, type, "notnull" AS not_null, pk FROM pragma_table_info(${table}) ORDER BY cid`
|
|
117
|
+
: sql `SELECT c.column_name AS name, c.data_type AS type, (c.is_nullable = 'NO') AS not_null,
|
|
118
|
+
EXISTS (
|
|
119
|
+
SELECT 1 FROM information_schema.table_constraints t
|
|
120
|
+
JOIN information_schema.key_column_usage k
|
|
121
|
+
ON k.constraint_name = t.constraint_name AND k.table_schema = t.table_schema
|
|
122
|
+
WHERE t.constraint_type = 'PRIMARY KEY' AND t.table_schema = c.table_schema
|
|
123
|
+
AND t.table_name = c.table_name AND k.column_name = c.column_name
|
|
124
|
+
) AS pk
|
|
125
|
+
FROM information_schema.columns c
|
|
126
|
+
WHERE c.table_schema = current_schema() AND c.table_name = ${table}
|
|
127
|
+
ORDER BY c.ordinal_position`);
|
|
128
|
+
return rows.map((row) => ({
|
|
129
|
+
name: row.name,
|
|
130
|
+
type: row.type.toLowerCase(),
|
|
131
|
+
notNull: row.not_null === true || Number(row.not_null) === 1,
|
|
132
|
+
primaryKey: row.pk === true || Number(row.pk) > 0,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
const PG_COLUMN_TYPES = {
|
|
136
|
+
TEXT: "text",
|
|
137
|
+
// SQLite INTEGER is 64-bit (it holds `Date.now()` stamps); Postgres `integer` is 32-bit.
|
|
138
|
+
INTEGER: "bigint",
|
|
139
|
+
REAL: "double precision",
|
|
140
|
+
BLOB: "bytea",
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* The column type to write in DDL for `affinity`. SQLite keeps the affinity name itself, so a
|
|
144
|
+
* table created here reads back exactly as before; Postgres gets the same mapping as the frozen
|
|
145
|
+
* Postgres baseline. Lower-cased, it is also what {@link listColumns} reports for that column.
|
|
146
|
+
*/
|
|
147
|
+
export function columnTypeSql(dialect, affinity) {
|
|
148
|
+
return dialect === "sqlite" ? affinity : PG_COLUMN_TYPES[affinity];
|
|
149
|
+
}
|
|
150
|
+
/** A generated integer primary key column's type and constraints (write the column name before it). */
|
|
151
|
+
export function autoIdColumnSql(dialect) {
|
|
152
|
+
return dialect === "sqlite" ? "INTEGER PRIMARY KEY AUTOINCREMENT" : "bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY";
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Makes everything committed so far durable in the main database file. SQLite: a full WAL
|
|
156
|
+
* checkpoint (must run outside a write transaction on this connection: SQLite refuses it with
|
|
157
|
+
* "database table is locked"). Postgres: nothing to do, a commit is already durable.
|
|
158
|
+
*/
|
|
159
|
+
export async function checkpointWal(kernel) {
|
|
160
|
+
if (kernel.dialect === "sqlite")
|
|
161
|
+
await kernel.query(sql `PRAGMA wal_checkpoint(FULL)`);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The indexes created with `CREATE INDEX` on `table`, by name. Excluded: the ones a PRIMARY KEY or
|
|
165
|
+
* UNIQUE constraint makes on its own (SQLite origin `pk`/`u`, Postgres constraint-backed), which no
|
|
166
|
+
* `CREATE INDEX` declared. Empty when the table does not exist.
|
|
167
|
+
*/
|
|
168
|
+
export async function listIndexes(kernel, table) {
|
|
169
|
+
const rows = await kernel.query(kernel.dialect === "sqlite"
|
|
170
|
+
? sql `SELECT il.name AS name, il."unique" AS is_unique, ii.name AS column_name
|
|
171
|
+
FROM pragma_index_list(${table}) il JOIN pragma_index_info(il.name) ii
|
|
172
|
+
WHERE il.origin = 'c' ORDER BY il.name, ii.seqno`
|
|
173
|
+
: sql `SELECT ic.relname AS name, ix.indisunique AS is_unique, a.attname AS column_name
|
|
174
|
+
FROM pg_index ix
|
|
175
|
+
JOIN pg_class t ON t.oid = ix.indrelid
|
|
176
|
+
JOIN pg_class ic ON ic.oid = ix.indexrelid
|
|
177
|
+
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
178
|
+
CROSS JOIN LATERAL unnest(ix.indkey::int2[]) WITH ORDINALITY AS k(attnum, ord)
|
|
179
|
+
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = k.attnum
|
|
180
|
+
WHERE n.nspname = current_schema() AND t.relname = ${table} AND NOT ix.indisprimary
|
|
181
|
+
AND NOT EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = ix.indexrelid)
|
|
182
|
+
ORDER BY ic.relname, k.ord`);
|
|
183
|
+
const indexes = new Map();
|
|
184
|
+
for (const row of rows) {
|
|
185
|
+
const index = indexes.get(row.name) ?? { name: row.name, columns: [], unique: row.is_unique === true || Number(row.is_unique) === 1 };
|
|
186
|
+
index.columns.push(row.column_name);
|
|
187
|
+
indexes.set(row.name, index);
|
|
188
|
+
}
|
|
189
|
+
return indexes;
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=dialect.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file `@jini-ai/db/kernel`: the storage kernel port, the kernel core every driver builds on, the
|
|
3
|
+
* dialect helpers and introspection, and the storage-ops contract. Imports kysely and Node builtins
|
|
4
|
+
* only — never a database driver (`import-isolation.test.ts` and `loads-without-driver.test.ts` prove it).
|
|
5
|
+
*/
|
|
6
|
+
export { type StorageCapabilities, type StorageCapability, type StorageDialect, type StorageKernel, type StorageTransport, UnsupportedCapabilityError, } from "./port.js";
|
|
7
|
+
export { buildKernel, type KernelDriver } from "./kernel-core.js";
|
|
8
|
+
export { TurnLock } from "./turn-lock.js";
|
|
9
|
+
export { autoIdColumnSql, checkpointWal, type ColumnAffinity, type ColumnInfo, columnTypeSql, type IndexInfo, isUniqueViolation, jsonScalarEquals, jsonSet, jsonSortKey, jsonText, listColumns, listIndexes, listTables, nowIso, tableExists, toBool, toBytes, } from "./dialect.js";
|
|
10
|
+
export { scopeToSchema } from "./schema-scope.js";
|
|
11
|
+
export { databaseFile, readSchemaShape, type SchemaShape, type TableShape } from "./schema-shape.js";
|
|
12
|
+
export { renderDatabaseTypes } from "./typegen.js";
|
|
13
|
+
export { outsideTransactionOf, StorageOpError, StorageOpNotSupportedError, type StorageOps, verifyLedgerReadBack, } from "./ops.js";
|
|
14
|
+
export { postgresLockKey } from "./postgres-lock.js";
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file `@jini-ai/db/kernel`: the storage kernel port, the kernel core every driver builds on, the
|
|
3
|
+
* dialect helpers and introspection, and the storage-ops contract. Imports kysely and Node builtins
|
|
4
|
+
* only — never a database driver (`import-isolation.test.ts` and `loads-without-driver.test.ts` prove it).
|
|
5
|
+
*/
|
|
6
|
+
export { UnsupportedCapabilityError, } from "./port.js";
|
|
7
|
+
export { buildKernel } from "./kernel-core.js";
|
|
8
|
+
export { TurnLock } from "./turn-lock.js";
|
|
9
|
+
export { autoIdColumnSql, checkpointWal, columnTypeSql, isUniqueViolation, jsonScalarEquals, jsonSet, jsonSortKey, jsonText, listColumns, listIndexes, listTables, nowIso, tableExists, toBool, toBytes, } from "./dialect.js";
|
|
10
|
+
export { scopeToSchema } from "./schema-scope.js";
|
|
11
|
+
export { databaseFile, readSchemaShape } from "./schema-shape.js";
|
|
12
|
+
export { renderDatabaseTypes } from "./typegen.js";
|
|
13
|
+
export { outsideTransactionOf, StorageOpError, StorageOpNotSupportedError, verifyLedgerReadBack, } from "./ops.js";
|
|
14
|
+
export { postgresLockKey } from "./postgres-lock.js";
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import { type StorageCapabilities, type StorageDialect, type StorageKernel, type StorageTransport } from "./port.js";
|
|
3
|
+
import { TurnLock } from "./turn-lock.js";
|
|
4
|
+
/**
|
|
5
|
+
* @file The transaction rules every driver shares, so they cannot differ by dialect: a test that
|
|
6
|
+
* passes on PGlite passes on SQLite for the same reason.
|
|
7
|
+
*
|
|
8
|
+
* The caller's async context (AsyncLocalStorage) records whether it is inside a `run()` body or a
|
|
9
|
+
* transaction, and which executor that is. Nested `transaction()` joins; nested `run()` reuses the
|
|
10
|
+
* executor; `transaction()` inside `run()` throws (it would wait on the caller's own turn).
|
|
11
|
+
*/
|
|
12
|
+
/** What a driver supplies. Everything else about a kernel is built here. */
|
|
13
|
+
export interface KernelDriver<DB> {
|
|
14
|
+
dialect: StorageDialect;
|
|
15
|
+
transport: StorageTransport;
|
|
16
|
+
capabilities: StorageCapabilities;
|
|
17
|
+
ready: Promise<void>;
|
|
18
|
+
base: Kysely<DB>;
|
|
19
|
+
/** One real transaction; `body` gets its executor. Commit on resolve, roll back on throw. */
|
|
20
|
+
begin<T>(body: (tx: Kysely<DB>) => Promise<T>): Promise<T>;
|
|
21
|
+
/** `StorageKernel.lockKey` on the open transaction's executor. */
|
|
22
|
+
lockKey(tx: Kysely<DB>, key: string): Promise<void>;
|
|
23
|
+
/** Share one connection between all callers: take turns (see `turn-lock.ts`). */
|
|
24
|
+
oneConnection: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* The turn lock to take turns on, when several connections in this process open the SAME
|
|
27
|
+
* database (two better-sqlite3 handles on one file): they must take turns as one, or one
|
|
28
|
+
* connection's `BEGIN IMMEDIATE` busy-waits, thread blocked, on a transaction that can then never
|
|
29
|
+
* commit. Omitted: a lock of this kernel's own.
|
|
30
|
+
*/
|
|
31
|
+
turnLock?: TurnLock | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* A transaction on the connection that this kernel did not open (a legacy `BEGIN IMMEDIATE` site
|
|
34
|
+
* not converted yet). While one is open, kernel calls join it — the same pass-through the legacy
|
|
35
|
+
* runners use — instead of failing on a nested `BEGIN`. Removed once no legacy site remains.
|
|
36
|
+
*/
|
|
37
|
+
foreignTransactionOpen?(): boolean;
|
|
38
|
+
/** `StorageKernel.backupTo`; required when `capabilities.backup` is true. */
|
|
39
|
+
backup?(destPath: string): Promise<void>;
|
|
40
|
+
close(): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
export declare function buildKernel<DB>(driver: KernelDriver<DB>): StorageKernel<DB>;
|
|
43
|
+
//# sourceMappingURL=kernel-core.d.ts.map
|