@jini-ai/db 0.2.0 → 0.3.1
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 +26 -10
- package/dist/core/artifact-naming.d.ts +4 -1
- package/dist/core/artifact-naming.js +3 -4
- package/dist/core/index.d.ts +1 -0
- package/dist/core/messages.d.ts +1 -1
- package/dist/core/pg-types.d.ts +52 -0
- package/dist/core/pg-types.js +8 -0
- package/dist/core/transfer-naming.d.ts +10 -0
- package/dist/core/transfer-naming.js +2 -0
- package/dist/kernel/dialect.d.ts +2 -1
- package/dist/kernel/dialect.js +5 -2
- package/dist/kernel/postgres/driver.d.ts +1 -1
- package/dist/kernel/postgres/driver.js +1 -1
- package/dist/kernel/postgres/pglite-socket.d.ts +1 -1
- package/dist/kernel/postgres/pglite-socket.js +1 -1
- package/dist/pg-fixture.d.ts +50 -0
- package/dist/pg-fixture.js +84 -0
- package/dist/pglite/owner.js +7 -0
- package/dist/postgres/index.d.ts +1 -1
- package/dist/postgres/index.js +1 -1
- package/dist/postgres/types.d.ts +1 -52
- package/dist/postgres/types.js +2 -13
- package/dist/recovery/deep-link.d.ts +60 -0
- package/dist/recovery/deep-link.js +16 -0
- package/dist/recovery/degraded-banners.d.ts +40 -0
- package/dist/recovery/degraded-banners.js +69 -0
- package/dist/recovery/disclosure.d.ts +58 -0
- package/dist/recovery/disclosure.js +55 -0
- package/dist/recovery/evaluate-boot-migration-policy.d.ts +41 -0
- package/dist/recovery/evaluate-boot-migration-policy.js +50 -0
- package/dist/recovery/hook-policy.d.ts +9 -0
- package/dist/recovery/hook-policy.js +2 -0
- package/dist/recovery/index.d.ts +15 -0
- package/dist/recovery/index.js +13 -0
- package/dist/recovery/lookup-adapters.d.ts +40 -0
- package/dist/recovery/lookup-adapters.js +44 -0
- package/dist/recovery/migrate-forward-execute.d.ts +65 -0
- package/dist/recovery/migrate-forward-execute.js +68 -0
- package/dist/recovery/migrate-forward-hooks.d.ts +118 -0
- package/dist/recovery/migrate-forward-hooks.js +85 -0
- package/dist/recovery/operation-lock.d.ts +78 -0
- package/dist/recovery/operation-lock.js +58 -0
- package/dist/recovery/reconcile-interrupted-migration.d.ts +73 -0
- package/dist/recovery/reconcile-interrupted-migration.js +47 -0
- package/dist/recovery/recovery-orchestrator.d.ts +204 -0
- package/dist/recovery/recovery-orchestrator.js +96 -0
- package/dist/recovery/restore-hooks.d.ts +105 -0
- package/dist/recovery/restore-hooks.js +154 -0
- package/dist/recovery/restore-point-create.d.ts +80 -0
- package/dist/recovery/restore-point-create.js +37 -0
- package/dist/sqlite/open.d.ts +1 -1
- package/dist/sqlite/open.js +3 -3
- package/dist/sqlite/restore-types.d.ts +7 -3
- package/dist/tools/read-tools.js +7 -4
- package/dist/tools/restore-points.d.ts +3 -0
- package/dist/tools/restore-points.js +2 -2
- package/dist/tools/surface-port.d.ts +11 -29
- package/dist/tools/timeline.d.ts +4 -0
- package/dist/tools/timeline.js +8 -2
- package/dist/tools/transfer-tools.js +36 -37
- package/dist/transfer/internal-ddl.d.ts +3 -0
- package/dist/transfer/internal-ddl.js +6 -2
- package/dist/transfer/types.d.ts +3 -9
- package/package.json +48 -6
- package/dist/kernel/pg-types.d.ts +0 -22
- package/dist/kernel/pg-types.js +0 -26
- package/dist/kernel/socket.d.ts +0 -8
- package/dist/kernel/socket.js +0 -8
- package/dist/pglite/dialect.d.ts +0 -11
- package/dist/pglite/dialect.js +0 -88
- package/dist/pglite/driver.d.ts +0 -30
- package/dist/pglite/driver.js +0 -46
- package/dist/pglite/ops.d.ts +0 -39
- package/dist/pglite/ops.js +0 -78
- package/dist/postgres/driver.d.ts +0 -23
- package/dist/postgres/driver.js +0 -41
- package/dist/postgres/ops.d.ts +0 -12
- package/dist/postgres/ops.js +0 -46
- package/dist/postgres/pglite-socket.d.ts +0 -23
- package/dist/postgres/pglite-socket.js +0 -51
- package/dist/sqlite/driver.d.ts +0 -71
- package/dist/sqlite/driver.js +0 -132
- package/dist/sqlite/ops.d.ts +0 -12
- package/dist/sqlite/ops.js +0 -40
package/dist/pglite/ops.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { existsSync, rmSync } from "node:fs";
|
|
3
|
-
import { readFile } from "node:fs/promises";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { sql } from "kysely";
|
|
6
|
-
import { outsideTransactionOf, verifyLedgerReadBack, } from "../kernel/index.js";
|
|
7
|
-
/**
|
|
8
|
-
* The ops for a PGlite kernel. Must be used outside a transaction (Postgres cannot `VACUUM` inside one).
|
|
9
|
-
*
|
|
10
|
-
* @param required.ledgerTable the migration ledger the compaction reads back (no default).
|
|
11
|
-
* @param optional.pgliteOwner the owner serving the data dir `kernel` is a socket client of.
|
|
12
|
-
* @throws {Error} a kernel that is neither in-process PGlite nor a socket client given its owner.
|
|
13
|
-
* @complexity each op is one full pass over the data dir, O(size).
|
|
14
|
-
*/
|
|
15
|
-
export function pgliteOps(kernel, required, optional = {}) {
|
|
16
|
-
const owner = optional.pgliteOwner;
|
|
17
|
-
const ownedSocket = kernel.transport === "pglite-socket" && owner !== undefined;
|
|
18
|
-
if (kernel.transport !== "pglite" && !ownedSocket) {
|
|
19
|
-
throw new Error(`pgliteOps: a ${kernel.transport} kernel is not PGlite in this process (a socket client needs pgliteOwner)`);
|
|
20
|
-
}
|
|
21
|
-
const outsideTransaction = outsideTransactionOf(kernel);
|
|
22
|
-
return {
|
|
23
|
-
async copyTo(targetPath) {
|
|
24
|
-
outsideTransaction("copyTo");
|
|
25
|
-
if (owner !== undefined)
|
|
26
|
-
return copyServedPgliteTo(owner, targetPath, required);
|
|
27
|
-
await restoreDataDir(targetPath, required, async () => {
|
|
28
|
-
const tarball = `${targetPath}.${randomUUID()}.tar`;
|
|
29
|
-
try {
|
|
30
|
-
await kernel.backupTo(tarball);
|
|
31
|
-
return new Blob([await readFile(tarball)]);
|
|
32
|
-
}
|
|
33
|
-
finally {
|
|
34
|
-
rmSync(tarball, { force: true });
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
},
|
|
38
|
-
async compactAndVerify() {
|
|
39
|
-
outsideTransaction("compactAndVerify");
|
|
40
|
-
if (owner !== undefined) {
|
|
41
|
-
// Two calls: a multi-statement string runs as one implicit transaction, which VACUUM refuses.
|
|
42
|
-
await owner.runExclusive(async (db) => {
|
|
43
|
-
await db.exec("VACUUM");
|
|
44
|
-
await db.exec("CHECKPOINT");
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
await kernel.execute(sql `VACUUM`);
|
|
49
|
-
await kernel.execute(sql `CHECKPOINT`);
|
|
50
|
-
}
|
|
51
|
-
await verifyLedgerReadBack(kernel, required.ledgerTable);
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* `copyTo` for a PGlite data dir served by `owner` (in this process): the dump is taken inside the
|
|
57
|
-
* owner's exclusive window, then restored into `targetPath` (which must not exist).
|
|
58
|
-
*/
|
|
59
|
-
export async function copyServedPgliteTo(owner, targetPath, required) {
|
|
60
|
-
await restoreDataDir(targetPath, required, () => owner.runExclusive((db) => db.dumpDataDir("none")));
|
|
61
|
-
}
|
|
62
|
-
/** Restores `dump()` into a new PGlite data dir at `targetPath`; removes a partial dir on failure. */
|
|
63
|
-
async function restoreDataDir(targetPath, required, dump) {
|
|
64
|
-
if (existsSync(targetPath))
|
|
65
|
-
throw new Error(`storage op copyTo: ${targetPath} already exists`);
|
|
66
|
-
const data = await dump();
|
|
67
|
-
try {
|
|
68
|
-
const restored = await required.PGlite.create({ dataDir: targetPath, loadDataDir: data });
|
|
69
|
-
await restored.close();
|
|
70
|
-
if (required.ownerLockFileName !== undefined)
|
|
71
|
-
rmSync(join(targetPath, required.ownerLockFileName), { force: true });
|
|
72
|
-
}
|
|
73
|
-
catch (err) {
|
|
74
|
-
rmSync(targetPath, { recursive: true, force: true });
|
|
75
|
-
throw err;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
//# sourceMappingURL=ops.js.map
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { type StorageKernel } from "../kernel/index.js";
|
|
2
|
-
import { type PgModule } from "./types.js";
|
|
3
|
-
/**
|
|
4
|
-
* @file The node-postgres driver: a connection POOL to any Postgres server (local, Supabase, Neon).
|
|
5
|
-
* Kysely's own Postgres dialect; each transaction checks out its own connection, so unrelated
|
|
6
|
-
* requests run concurrently (no turn lock) and `lockKey` is what serializes the sequences that
|
|
7
|
-
* must not interleave.
|
|
8
|
-
*
|
|
9
|
-
* This package never imports `pg`: the consumer passes its own module (`import pg from "pg"`).
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
|
-
* Opens a pooled kernel on the server at `connectionString`.
|
|
13
|
-
*
|
|
14
|
-
* @param required.pg the consumer's node-postgres module.
|
|
15
|
-
* @param optional.max pool size, default 10.
|
|
16
|
-
*/
|
|
17
|
-
export declare function openPostgresKernel<DB>(required: {
|
|
18
|
-
pg: PgModule;
|
|
19
|
-
connectionString: string;
|
|
20
|
-
}, optional?: {
|
|
21
|
-
max?: number;
|
|
22
|
-
}): StorageKernel<DB>;
|
|
23
|
-
//# sourceMappingURL=driver.d.ts.map
|
package/dist/postgres/driver.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { Kysely, PostgresDialect } from "kysely";
|
|
2
|
-
import { buildKernel, PG_PARSERS, postgresLockKey } from "../kernel/index.js";
|
|
3
|
-
import { pgTypesFor } from "./types.js";
|
|
4
|
-
/**
|
|
5
|
-
* @file The node-postgres driver: a connection POOL to any Postgres server (local, Supabase, Neon).
|
|
6
|
-
* Kysely's own Postgres dialect; each transaction checks out its own connection, so unrelated
|
|
7
|
-
* requests run concurrently (no turn lock) and `lockKey` is what serializes the sequences that
|
|
8
|
-
* must not interleave.
|
|
9
|
-
*
|
|
10
|
-
* This package never imports `pg`: the consumer passes its own module (`import pg from "pg"`).
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* Opens a pooled kernel on the server at `connectionString`.
|
|
14
|
-
*
|
|
15
|
-
* @param required.pg the consumer's node-postgres module.
|
|
16
|
-
* @param optional.max pool size, default 10.
|
|
17
|
-
*/
|
|
18
|
-
export function openPostgresKernel(required, optional = {}) {
|
|
19
|
-
const pool = new required.pg.Pool({
|
|
20
|
-
connectionString: required.connectionString,
|
|
21
|
-
max: optional.max ?? 10,
|
|
22
|
-
types: pgTypesFor(required.pg, PG_PARSERS),
|
|
23
|
-
});
|
|
24
|
-
// An idle connection dropped by the server (restart, failover, pg_terminate_backend, a pooler's
|
|
25
|
-
// idle cut) is evicted by the pool and reopened on next use; without a listener it would crash the
|
|
26
|
-
// process.
|
|
27
|
-
pool.on("error", (err) => console.warn(`[postgres] idle pool connection dropped: ${err.message}`));
|
|
28
|
-
const base = new Kysely({ dialect: new PostgresDialect({ pool }) });
|
|
29
|
-
return buildKernel({
|
|
30
|
-
dialect: "postgres",
|
|
31
|
-
transport: "node-postgres",
|
|
32
|
-
capabilities: { interactiveTransactions: true, atomicBatch: true, transactionalDdl: true, backup: false },
|
|
33
|
-
ready: Promise.resolve(),
|
|
34
|
-
base,
|
|
35
|
-
oneConnection: false,
|
|
36
|
-
begin: (body) => base.transaction().execute((tx) => body(tx)),
|
|
37
|
-
lockKey: postgresLockKey,
|
|
38
|
-
close: () => base.destroy(),
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=driver.js.map
|
package/dist/postgres/ops.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type StorageKernel, type StorageOps } from "../kernel/index.js";
|
|
2
|
-
/**
|
|
3
|
-
* The ops for a node-postgres kernel. Must be used outside a transaction.
|
|
4
|
-
*
|
|
5
|
-
* @param required.ledgerTable the migration ledger the compaction reads back (no default).
|
|
6
|
-
* @throws {Error} a kernel of another transport.
|
|
7
|
-
* @complexity `compactAndVerify` is one full `VACUUM` pass, O(size).
|
|
8
|
-
*/
|
|
9
|
-
export declare function postgresOps<DB>(kernel: StorageKernel<DB>, required: {
|
|
10
|
-
ledgerTable: string;
|
|
11
|
-
}): StorageOps;
|
|
12
|
-
//# sourceMappingURL=ops.d.ts.map
|
package/dist/postgres/ops.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { sql } from "kysely";
|
|
2
|
-
import { outsideTransactionOf, StorageOpNotSupportedError, verifyLedgerReadBack, } from "../kernel/index.js";
|
|
3
|
-
/**
|
|
4
|
-
* @file The storage ops for a node-postgres kernel: a Postgres server (`openPostgresKernel`) or a
|
|
5
|
-
* PGlite socket client (`openPgliteSocketKernel`) with no owner in this process. Neither can copy
|
|
6
|
-
* the database as a file; compacting is `VACUUM` (plus `CHECKPOINT` on PGlite) and a read-back of the
|
|
7
|
-
* consumer's migration ledger. A socket client whose owner runs in this process uses
|
|
8
|
-
* `@jini-ai/db/pglite`'s `pgliteOps` instead, which can copy through the owner.
|
|
9
|
-
*/
|
|
10
|
-
/** Why a Postgres server's database is never file-copied. */
|
|
11
|
-
const POSTGRES_COPY_REASON = "a Postgres database is backed up by its provider; use a transfer tool to copy it";
|
|
12
|
-
/** Why a PGlite socket client cannot copy on its own. */
|
|
13
|
-
const SOCKET_COPY_REASON = "a PGlite socket client copies through its owner's exclusive window (use pgliteOps with pgliteOwner)";
|
|
14
|
-
/**
|
|
15
|
-
* The ops for a node-postgres kernel. Must be used outside a transaction.
|
|
16
|
-
*
|
|
17
|
-
* @param required.ledgerTable the migration ledger the compaction reads back (no default).
|
|
18
|
-
* @throws {Error} a kernel of another transport.
|
|
19
|
-
* @complexity `compactAndVerify` is one full `VACUUM` pass, O(size).
|
|
20
|
-
*/
|
|
21
|
-
export function postgresOps(kernel, required) {
|
|
22
|
-
if (kernel.transport !== "node-postgres" && kernel.transport !== "pglite-socket") {
|
|
23
|
-
throw new Error(`postgresOps: a ${kernel.transport} kernel is not a node-postgres connection`);
|
|
24
|
-
}
|
|
25
|
-
const outsideTransaction = outsideTransactionOf(kernel);
|
|
26
|
-
return {
|
|
27
|
-
async copyTo() {
|
|
28
|
-
outsideTransaction("copyTo");
|
|
29
|
-
const reason = kernel.transport === "pglite-socket" ? SOCKET_COPY_REASON : POSTGRES_COPY_REASON;
|
|
30
|
-
throw new StorageOpNotSupportedError("copyTo", kernel.transport, reason);
|
|
31
|
-
},
|
|
32
|
-
async compactAndVerify() {
|
|
33
|
-
outsideTransaction("compactAndVerify");
|
|
34
|
-
if (kernel.transport === "pglite-socket") {
|
|
35
|
-
await kernel.execute(sql `VACUUM`);
|
|
36
|
-
await kernel.execute(sql `CHECKPOINT`);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
// A hosted server rarely lets an app's role CHECKPOINT; a commit is durable anyway.
|
|
40
|
-
await kernel.execute(sql `VACUUM (ANALYZE)`);
|
|
41
|
-
}
|
|
42
|
-
await verifyLedgerReadBack(kernel, required.ledgerTable);
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=ops.js.map
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { type StorageKernel } from "../kernel/index.js";
|
|
2
|
-
import { type PgModule } from "./types.js";
|
|
3
|
-
/**
|
|
4
|
-
* @file A storage kernel that reaches a PGlite data dir through its owner's Unix socket
|
|
5
|
-
* (`@jini-ai/db/pglite`'s `startPgliteOwner`): node-postgres with ONE connection and the kernel's turn lock, because PGlite
|
|
6
|
-
* is one backend session — a second connection gives no parallelism, and every client's transaction
|
|
7
|
-
* blocks every other client anyway. Every process sharing the data dir uses this.
|
|
8
|
-
*
|
|
9
|
-
* Kernel rules this transport makes hard: no `SET` (only `SET LOCAL`), no temp tables, no named
|
|
10
|
-
* prepared statements, no session advisory locks — all clients share one session, so any of those
|
|
11
|
-
* leaks into the other process. Backup is not offered here; it goes through the owner.
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* Opens a one-connection kernel on the PGlite owner socket at `socketPath`.
|
|
15
|
-
*
|
|
16
|
-
* @param required.pg the consumer's node-postgres module (`import pg from "pg"`).
|
|
17
|
-
* @throws {Error} a path that does not end in the Postgres socket file name.
|
|
18
|
-
*/
|
|
19
|
-
export declare function openPgliteSocketKernel<DB>(required: {
|
|
20
|
-
pg: PgModule;
|
|
21
|
-
socketPath: string;
|
|
22
|
-
}): StorageKernel<DB>;
|
|
23
|
-
//# sourceMappingURL=pglite-socket.d.ts.map
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { dirname } from "node:path";
|
|
2
|
-
import { Kysely, PostgresDialect } from "kysely";
|
|
3
|
-
import { buildKernel, PG_PARSERS, PGLITE_SOCKET_FILE, postgresLockKey } from "../kernel/index.js";
|
|
4
|
-
import { pgTypesFor } from "./types.js";
|
|
5
|
-
/**
|
|
6
|
-
* @file A storage kernel that reaches a PGlite data dir through its owner's Unix socket
|
|
7
|
-
* (`@jini-ai/db/pglite`'s `startPgliteOwner`): node-postgres with ONE connection and the kernel's turn lock, because PGlite
|
|
8
|
-
* is one backend session — a second connection gives no parallelism, and every client's transaction
|
|
9
|
-
* blocks every other client anyway. Every process sharing the data dir uses this.
|
|
10
|
-
*
|
|
11
|
-
* Kernel rules this transport makes hard: no `SET` (only `SET LOCAL`), no temp tables, no named
|
|
12
|
-
* prepared statements, no session advisory locks — all clients share one session, so any of those
|
|
13
|
-
* leaks into the other process. Backup is not offered here; it goes through the owner.
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* Opens a one-connection kernel on the PGlite owner socket at `socketPath`.
|
|
17
|
-
*
|
|
18
|
-
* @param required.pg the consumer's node-postgres module (`import pg from "pg"`).
|
|
19
|
-
* @throws {Error} a path that does not end in the Postgres socket file name.
|
|
20
|
-
*/
|
|
21
|
-
export function openPgliteSocketKernel(required) {
|
|
22
|
-
if (!required.socketPath.endsWith(`/${PGLITE_SOCKET_FILE}`)) {
|
|
23
|
-
throw new Error(`a PGlite socket path ends in /${PGLITE_SOCKET_FILE}: ${required.socketPath}`);
|
|
24
|
-
}
|
|
25
|
-
const pool = new required.pg.Pool({
|
|
26
|
-
host: dirname(required.socketPath),
|
|
27
|
-
port: 5432,
|
|
28
|
-
user: "postgres",
|
|
29
|
-
database: "postgres",
|
|
30
|
-
max: 1,
|
|
31
|
-
// Keep the one connection: every new one's startup waits behind any open transaction.
|
|
32
|
-
idleTimeoutMillis: 0,
|
|
33
|
-
types: pgTypesFor(required.pg, PG_PARSERS),
|
|
34
|
-
});
|
|
35
|
-
// An idle connection dropped by the owner (restart, idle-in-transaction timeout) is evicted by the
|
|
36
|
-
// pool and reopened on next use; without a listener it would crash the process.
|
|
37
|
-
pool.on("error", () => { });
|
|
38
|
-
const base = new Kysely({ dialect: new PostgresDialect({ pool }) });
|
|
39
|
-
return buildKernel({
|
|
40
|
-
dialect: "postgres",
|
|
41
|
-
transport: "pglite-socket",
|
|
42
|
-
capabilities: { interactiveTransactions: true, atomicBatch: true, transactionalDdl: true, backup: false },
|
|
43
|
-
ready: Promise.resolve(),
|
|
44
|
-
base,
|
|
45
|
-
oneConnection: true,
|
|
46
|
-
begin: (body) => base.transaction().execute((tx) => body(tx)),
|
|
47
|
-
lockKey: postgresLockKey,
|
|
48
|
-
close: () => base.destroy(),
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
//# sourceMappingURL=pglite-socket.js.map
|
package/dist/sqlite/driver.d.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { type StorageKernel } from "../kernel/index.js";
|
|
2
|
-
import type { SqliteClient, SqliteOpener } from "./types.js";
|
|
3
|
-
/**
|
|
4
|
-
* @file The better-sqlite3 driver: a kernel over an ALREADY OPEN SQLite connection the consumer
|
|
5
|
-
* opened (and migrated) itself. Wrapping instead of opening keeps an existing database exactly as it
|
|
6
|
-
* was: same file, same pragmas, same migrations.
|
|
7
|
-
*
|
|
8
|
-
* This package never imports better-sqlite3: the consumer passes its own client (or, for
|
|
9
|
-
* {@link openSqliteFileKernel}, its own opener). Two copies of the SQLite library in one process
|
|
10
|
-
* would share POSIX locks, a documented way to corrupt a database. Accepts an ORM handle that
|
|
11
|
-
* carries its client as `$client` (Drizzle's shape) and reads the client; the handle itself is not
|
|
12
|
-
* used. ONE kernel per connection ({@link sqliteKernel} memoizes on the
|
|
13
|
-
* client): the transaction scope only protects callers that share it. The turn lock is per FILE:
|
|
14
|
-
* every connection to one database file in this process takes turns on the same lock (see
|
|
15
|
-
* `KernelDriver.turnLock`).
|
|
16
|
-
*
|
|
17
|
-
* Queries go through Kysely's own SQLite dialect over a thin binding shim: SQLite cannot bind a
|
|
18
|
-
* boolean, so `true`/`false` are written as `1`/`0` (the usual SQLite boolean encoding). Reads
|
|
19
|
-
* of those columns come back as `0`/`1` — the generated types say `SqlBool` so a repo converts.
|
|
20
|
-
*
|
|
21
|
-
* Transactions are `BEGIN IMMEDIATE` on the connection, not Kysely's `transaction()` (which issues a
|
|
22
|
-
* deferred `BEGIN`): IMMEDIATE takes the write lock up front, which is what makes `lockKey` a no-op
|
|
23
|
-
* here and keeps read-then-write sequences from failing with SQLITE_BUSY halfway through.
|
|
24
|
-
*
|
|
25
|
-
* The connection's opener closes it; `close()` here does nothing.
|
|
26
|
-
*/
|
|
27
|
-
export type SqliteKernel<DB> = StorageKernel<DB>;
|
|
28
|
-
/** An ORM handle that carries its better-sqlite3 client as `$client`, or the client itself. */
|
|
29
|
-
export type SqliteConnectionSource = SqliteClient | {
|
|
30
|
-
readonly $client: SqliteClient;
|
|
31
|
-
};
|
|
32
|
-
/** The better-sqlite3 client under `source` (a handle's `$client`, or the client itself). */
|
|
33
|
-
export declare function sqliteClientOf(source: SqliteConnectionSource): SqliteClient;
|
|
34
|
-
export declare function sqliteKernel<DB>(source: SqliteConnectionSource): SqliteKernel<DB>;
|
|
35
|
-
/**
|
|
36
|
-
* A kernel over a new private in-memory SQLite database (foreign keys on). `close()` closes the
|
|
37
|
-
* connection. For scratch work, e.g. building a reference schema.
|
|
38
|
-
*
|
|
39
|
-
* @param required.open the consumer's better-sqlite3 opener, e.g. `(p, o) => new Database(p, o)`.
|
|
40
|
-
*/
|
|
41
|
-
export declare function openMemorySqliteKernel<DB>(required: {
|
|
42
|
-
open: SqliteOpener;
|
|
43
|
-
}): SqliteKernel<DB>;
|
|
44
|
-
/**
|
|
45
|
-
* A kernel over its OWN connection to the SQLite file at `filePath`, opened as-is: no migration, no
|
|
46
|
-
* WAL switch, foreign keys on, a 5 s busy timeout. `close()` closes the connection.
|
|
47
|
-
*
|
|
48
|
-
* For ops on a database file that is not the running app's (a duplicate being prepared, a directory
|
|
49
|
-
* being inspected). `readOnly` opens with better-sqlite3's own `readonly` mode, so SQLite itself
|
|
50
|
-
* rejects any write, and requires the file to exist.
|
|
51
|
-
*
|
|
52
|
-
* @param required.open the consumer's better-sqlite3 opener, e.g. `(p, o) => new Database(p, o)`,
|
|
53
|
-
* so the connection comes from the consumer's own copy of the library.
|
|
54
|
-
* @throws whatever the opener throws opening the file (missing file when `readOnly`, not a
|
|
55
|
-
* database, locked).
|
|
56
|
-
*/
|
|
57
|
-
export declare function openSqliteFileKernel<DB>(required: {
|
|
58
|
-
filePath: string;
|
|
59
|
-
open: SqliteOpener;
|
|
60
|
-
}, optional?: {
|
|
61
|
-
readOnly?: boolean;
|
|
62
|
-
}): SqliteKernel<DB>;
|
|
63
|
-
/**
|
|
64
|
-
* Closes the better-sqlite3 connection under `source` (a handle carrying `$client`, or the client)
|
|
65
|
-
* and forgets its kernel. For callers that opened a database themselves and must
|
|
66
|
-
* release it; the kernel's own `close()` never closes a connection it did not open.
|
|
67
|
-
*/
|
|
68
|
-
export declare function closeSqliteConnection(source: SqliteConnectionSource): void;
|
|
69
|
-
/** The connection under a kernel this driver built, for the SQLite `StorageOps` (`./ops.ts`). */
|
|
70
|
-
export declare function sqliteConnectionOf<DB>(kernel: StorageKernel<DB>): SqliteClient | undefined;
|
|
71
|
-
//# sourceMappingURL=driver.d.ts.map
|
package/dist/sqlite/driver.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { Kysely, SqliteDialect } from "kysely";
|
|
3
|
-
import { buildKernel, TurnLock } from "../kernel/index.js";
|
|
4
|
-
const kernels = new WeakMap();
|
|
5
|
-
/** The reverse of `kernels`, plus the owning kernels {@link openSqliteFileKernel} hands out. */
|
|
6
|
-
const connectionsByKernel = new WeakMap();
|
|
7
|
-
/** One turn lock per database FILE, shared by every connection to it in this process. */
|
|
8
|
-
const fileTurnLocks = new Map();
|
|
9
|
-
function turnLockFor(client) {
|
|
10
|
-
if (client.memory)
|
|
11
|
-
return undefined;
|
|
12
|
-
const file = path.resolve(client.name);
|
|
13
|
-
let lock = fileTurnLocks.get(file);
|
|
14
|
-
if (lock === undefined) {
|
|
15
|
-
lock = new TurnLock();
|
|
16
|
-
fileTurnLocks.set(file, lock);
|
|
17
|
-
}
|
|
18
|
-
return lock;
|
|
19
|
-
}
|
|
20
|
-
/** The better-sqlite3 client under `source` (a handle's `$client`, or the client itself). */
|
|
21
|
-
export function sqliteClientOf(source) {
|
|
22
|
-
return "$client" in source ? source.$client : source;
|
|
23
|
-
}
|
|
24
|
-
const bindable = (value) => (typeof value === "boolean" ? (value ? 1 : 0) : value);
|
|
25
|
-
/** better-sqlite3 as Kysely's `SqliteDatabase`, with booleans bound as integers. */
|
|
26
|
-
function bindingShim(client) {
|
|
27
|
-
return {
|
|
28
|
-
close: () => { },
|
|
29
|
-
prepare(sqlText) {
|
|
30
|
-
const statement = client.prepare(sqlText);
|
|
31
|
-
const bind = (parameters) => parameters.map(bindable);
|
|
32
|
-
return {
|
|
33
|
-
reader: statement.reader,
|
|
34
|
-
all: (parameters) => statement.all(bind(parameters)),
|
|
35
|
-
run: (parameters) => statement.run(bind(parameters)),
|
|
36
|
-
iterate: (parameters) => statement.iterate(bind(parameters)),
|
|
37
|
-
};
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
export function sqliteKernel(source) {
|
|
42
|
-
const client = sqliteClientOf(source);
|
|
43
|
-
const known = kernels.get(client);
|
|
44
|
-
if (known !== undefined)
|
|
45
|
-
return known;
|
|
46
|
-
const base = new Kysely({ dialect: new SqliteDialect({ database: bindingShim(client) }) });
|
|
47
|
-
const kernel = buildKernel({
|
|
48
|
-
dialect: "sqlite",
|
|
49
|
-
transport: "better-sqlite3",
|
|
50
|
-
capabilities: { interactiveTransactions: true, atomicBatch: true, transactionalDdl: true, backup: true },
|
|
51
|
-
ready: Promise.resolve(),
|
|
52
|
-
base,
|
|
53
|
-
oneConnection: true,
|
|
54
|
-
turnLock: turnLockFor(client),
|
|
55
|
-
async begin(body) {
|
|
56
|
-
client.exec("BEGIN IMMEDIATE");
|
|
57
|
-
try {
|
|
58
|
-
const result = await body(base);
|
|
59
|
-
client.exec("COMMIT");
|
|
60
|
-
return result;
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
if (client.inTransaction)
|
|
64
|
-
client.exec("ROLLBACK");
|
|
65
|
-
throw error;
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
// BEGIN IMMEDIATE already holds the database write lock for the whole transaction.
|
|
69
|
-
lockKey: async () => { },
|
|
70
|
-
foreignTransactionOpen: () => client.inTransaction,
|
|
71
|
-
backup: async (destPath) => {
|
|
72
|
-
await client.backup(destPath);
|
|
73
|
-
},
|
|
74
|
-
close: async () => { },
|
|
75
|
-
});
|
|
76
|
-
kernels.set(client, kernel);
|
|
77
|
-
connectionsByKernel.set(kernel, client);
|
|
78
|
-
return kernel;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* A kernel over a new private in-memory SQLite database (foreign keys on). `close()` closes the
|
|
82
|
-
* connection. For scratch work, e.g. building a reference schema.
|
|
83
|
-
*
|
|
84
|
-
* @param required.open the consumer's better-sqlite3 opener, e.g. `(p, o) => new Database(p, o)`.
|
|
85
|
-
*/
|
|
86
|
-
export function openMemorySqliteKernel(required) {
|
|
87
|
-
return openSqliteFileKernel({ filePath: ":memory:", open: required.open });
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* A kernel over its OWN connection to the SQLite file at `filePath`, opened as-is: no migration, no
|
|
91
|
-
* WAL switch, foreign keys on, a 5 s busy timeout. `close()` closes the connection.
|
|
92
|
-
*
|
|
93
|
-
* For ops on a database file that is not the running app's (a duplicate being prepared, a directory
|
|
94
|
-
* being inspected). `readOnly` opens with better-sqlite3's own `readonly` mode, so SQLite itself
|
|
95
|
-
* rejects any write, and requires the file to exist.
|
|
96
|
-
*
|
|
97
|
-
* @param required.open the consumer's better-sqlite3 opener, e.g. `(p, o) => new Database(p, o)`,
|
|
98
|
-
* so the connection comes from the consumer's own copy of the library.
|
|
99
|
-
* @throws whatever the opener throws opening the file (missing file when `readOnly`, not a
|
|
100
|
-
* database, locked).
|
|
101
|
-
*/
|
|
102
|
-
export function openSqliteFileKernel(required, optional = {}) {
|
|
103
|
-
const readOnly = optional.readOnly === true;
|
|
104
|
-
const client = required.open(required.filePath, readOnly ? { readonly: true, fileMustExist: true } : {});
|
|
105
|
-
// A second connection to a file another may be writing: wait out a transient lock rather than
|
|
106
|
-
// throwing SQLITE_BUSY at once.
|
|
107
|
-
client.pragma("busy_timeout = 5000");
|
|
108
|
-
if (!readOnly)
|
|
109
|
-
client.pragma("foreign_keys = ON");
|
|
110
|
-
const kernel = sqliteKernel(client);
|
|
111
|
-
const owned = {
|
|
112
|
-
...kernel,
|
|
113
|
-
close: async () => closeSqliteConnection(client),
|
|
114
|
-
};
|
|
115
|
-
connectionsByKernel.set(owned, client);
|
|
116
|
-
return owned;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Closes the better-sqlite3 connection under `source` (a handle carrying `$client`, or the client)
|
|
120
|
-
* and forgets its kernel. For callers that opened a database themselves and must
|
|
121
|
-
* release it; the kernel's own `close()` never closes a connection it did not open.
|
|
122
|
-
*/
|
|
123
|
-
export function closeSqliteConnection(source) {
|
|
124
|
-
const client = sqliteClientOf(source);
|
|
125
|
-
kernels.delete(client);
|
|
126
|
-
client.close();
|
|
127
|
-
}
|
|
128
|
-
/** The connection under a kernel this driver built, for the SQLite `StorageOps` (`./ops.ts`). */
|
|
129
|
-
export function sqliteConnectionOf(kernel) {
|
|
130
|
-
return connectionsByKernel.get(kernel);
|
|
131
|
-
}
|
|
132
|
-
//# sourceMappingURL=driver.js.map
|
package/dist/sqlite/ops.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type StorageKernel, type StorageOps } from "../kernel/index.js";
|
|
2
|
-
/**
|
|
3
|
-
* The storage ops (`@jini-ai/db/kernel`'s `StorageOps`) for a kernel the sqlite driver built:
|
|
4
|
-
* `VACUUM INTO` for the copy; `VACUUM` + WAL checkpoint + `integrity_check` for compacting. Must be
|
|
5
|
-
* used outside a transaction.
|
|
6
|
-
*
|
|
7
|
-
* @throws {Error} a kernel this driver did not build (no connection to act on); an op called inside
|
|
8
|
-
* a transaction.
|
|
9
|
-
* @complexity each op is one full pass over the database file, O(size).
|
|
10
|
-
*/
|
|
11
|
-
export declare function sqliteOps<DB>(kernel: StorageKernel<DB>): StorageOps;
|
|
12
|
-
//# sourceMappingURL=ops.d.ts.map
|
package/dist/sqlite/ops.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { outsideTransactionOf, StorageOpError } from "../kernel/index.js";
|
|
2
|
-
import { sqliteConnectionOf } from "./driver.js";
|
|
3
|
-
/**
|
|
4
|
-
* The storage ops (`@jini-ai/db/kernel`'s `StorageOps`) for a kernel the sqlite driver built:
|
|
5
|
-
* `VACUUM INTO` for the copy; `VACUUM` + WAL checkpoint + `integrity_check` for compacting. Must be
|
|
6
|
-
* used outside a transaction.
|
|
7
|
-
*
|
|
8
|
-
* @throws {Error} a kernel this driver did not build (no connection to act on); an op called inside
|
|
9
|
-
* a transaction.
|
|
10
|
-
* @complexity each op is one full pass over the database file, O(size).
|
|
11
|
-
*/
|
|
12
|
-
export function sqliteOps(kernel) {
|
|
13
|
-
const client = sqliteConnectionOf(kernel);
|
|
14
|
-
if (client === undefined)
|
|
15
|
-
throw new Error("sqliteOps: this SQLite kernel was not built by the sqlite driver");
|
|
16
|
-
const outsideTransaction = outsideTransactionOf(kernel);
|
|
17
|
-
return {
|
|
18
|
-
async copyTo(targetPath) {
|
|
19
|
-
outsideTransaction("copyTo");
|
|
20
|
-
// Bound parameter: VACUUM INTO takes an expression, so the path is never spliced into SQL.
|
|
21
|
-
client.prepare("VACUUM INTO ?").run(targetPath);
|
|
22
|
-
},
|
|
23
|
-
async compactAndVerify() {
|
|
24
|
-
outsideTransaction("compactAndVerify");
|
|
25
|
-
client.exec("VACUUM");
|
|
26
|
-
// WAL switch and checkpoint BEFORE integrity_check: running the check first on a just-VACUUMed
|
|
27
|
-
// connection reproducibly left a lock that failed the checkpoint with SQLITE_LOCKED.
|
|
28
|
-
client.pragma("journal_mode = WAL");
|
|
29
|
-
const [checkpoint] = client.pragma("wal_checkpoint(TRUNCATE)");
|
|
30
|
-
if (checkpoint === undefined || checkpoint.busy !== 0) {
|
|
31
|
-
throw new StorageOpError(`final WAL checkpoint on ${client.name} reported busy=${checkpoint?.busy}`);
|
|
32
|
-
}
|
|
33
|
-
const [integrity] = client.pragma("integrity_check");
|
|
34
|
-
if (integrity?.integrity_check !== "ok") {
|
|
35
|
-
throw new StorageOpError(`${client.name} failed integrity_check: ${JSON.stringify(integrity)}`);
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
//# sourceMappingURL=ops.js.map
|