@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/tools/timeline.js
CHANGED
|
@@ -18,8 +18,9 @@
|
|
|
18
18
|
* See docs/decisions/DR-001-bounded-operational-timeline.md.
|
|
19
19
|
*/
|
|
20
20
|
/** The server-side row cap — no raw/unbounded query surface is ever offered. See docs/decisions/DR-001-bounded-operational-timeline.md. */
|
|
21
|
-
const MAX_TIMELINE_PAGE_SIZE = 200;
|
|
22
|
-
|
|
21
|
+
export const MAX_TIMELINE_PAGE_SIZE = 200;
|
|
22
|
+
/** Page size when a caller names none. */
|
|
23
|
+
export const DEFAULT_TIMELINE_PAGE_SIZE = 50;
|
|
23
24
|
/** Thrown when a caller requests a page size above the server's bound. */
|
|
24
25
|
class TimelineValidationError extends Error {
|
|
25
26
|
constructor(message) {
|
|
@@ -37,6 +38,11 @@ class TimelineValidationError extends Error {
|
|
|
37
38
|
export async function getTimeline(required, _optional = {}) {
|
|
38
39
|
const { ledger, filter = {} } = required;
|
|
39
40
|
const limit = filter.limit ?? DEFAULT_TIMELINE_PAGE_SIZE;
|
|
41
|
+
if (!Number.isInteger(limit) || limit < 1) {
|
|
42
|
+
// A 0 or fractional limit reached the ledger as an empty page or a fractional SQL LIMIT; an HTTP
|
|
43
|
+
// caller's `Number("abc")` arrived as NaN.
|
|
44
|
+
throw new TimelineValidationError(`'limit' must be an integer between 1 and ${MAX_TIMELINE_PAGE_SIZE}`);
|
|
45
|
+
}
|
|
40
46
|
if (limit > MAX_TIMELINE_PAGE_SIZE) {
|
|
41
47
|
// See docs/decisions/DR-001-bounded-operational-timeline.md for the invariant behind this refusal; keep external identifiers out of caller-facing messages.
|
|
42
48
|
throw new TimelineValidationError(`requested limit ${limit} exceeds the server cap of ${MAX_TIMELINE_PAGE_SIZE}`);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readFile, unlink } from "node:fs/promises";
|
|
2
|
-
import { ToolInputError } from "@jini-ai/core";
|
|
2
|
+
import { createApprovalHandler, ToolInputError } from "@jini-ai/core";
|
|
3
3
|
import { countSourceRows, inspectTarget, runCopy, planTransfer, SourceSchemaMismatchError, IncompleteTransferPlanError, InvalidConnectionStringError, LEFT_OUT_REASON } from "../transfer/index.js";
|
|
4
4
|
import { DOMAIN, PLAN_TOOL_ID, STATUS_TOOL_ID, RUN_PERMISSION, SNAPSHOT_SCOPE_ID, MIN_SERVER_VERSION_NUM, getDatabaseTransferAgentToolCatalog } from "./transfer-catalog.js";
|
|
5
5
|
import { defaultDbMessages } from "../core/messages.js";
|
|
@@ -181,23 +181,6 @@ async function handlePlan(deps, ctx) {
|
|
|
181
181
|
});
|
|
182
182
|
return planResult(plan);
|
|
183
183
|
}
|
|
184
|
-
async function askToConfirm(ctx, surfaces, plan, emitSurface) {
|
|
185
|
-
const exchange = surfaces.open({ toolId: DATABASE_TRANSFER_RUN_TOOL_ID, principalId: ctx.principal.id }, emitSurface);
|
|
186
|
-
const emission = surfaces.confirmation(plan, exchange.id);
|
|
187
|
-
const closeOnAbort = () => exchange.close();
|
|
188
|
-
ctx.signal.addEventListener("abort", closeOnAbort, { once: true });
|
|
189
|
-
try {
|
|
190
|
-
const outcome = await surfaces.resolveDecision(exchange, emission);
|
|
191
|
-
if (outcome.confirmed)
|
|
192
|
-
return { confirmed: true };
|
|
193
|
-
if (outcome.reason === "declined")
|
|
194
|
-
return { confirmed: false, result: { copied: false, cancelled: true } };
|
|
195
|
-
return { confirmed: false, result: { copied: false, cancelled: false, reason: outcome.reason } };
|
|
196
|
-
}
|
|
197
|
-
finally {
|
|
198
|
-
ctx.signal.removeEventListener("abort", closeOnAbort);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
184
|
/** @complexity O(total rows), streamed. */
|
|
202
185
|
async function copyPlanned(deps, plan) {
|
|
203
186
|
const sources = snapshotSources(deps, plan.snapshot, plan.chatSnapshot);
|
|
@@ -238,23 +221,39 @@ const PLAN_TAKE_MESSAGES = {
|
|
|
238
221
|
PLAN_NOT_FOUND: "no copy plan with that planId is waiting (a planId works once, for this principal, and a server restart drops plans). Call database_transfer_plan again.",
|
|
239
222
|
PLAN_EXPIRED: "that copy plan expired (plans last 10 minutes). Call database_transfer_plan again.",
|
|
240
223
|
};
|
|
224
|
+
/** The plan store retains actor/workspace binding and one-shot consumption. Core owns consent;
|
|
225
|
+
* the host supplies only transport, and permission is freshly evaluated after the human wait. */
|
|
241
226
|
async function handleRun(deps, surfaces, ctx, optional = {}) {
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
227
|
+
const handler = createApprovalHandler({
|
|
228
|
+
prepare: async ({ ctx: snapshot }) => {
|
|
229
|
+
const planId = deps.ports.readers.string(deps.ports.readers.inputRecord(snapshot.input), "planId");
|
|
230
|
+
await deps.ports.requirePermission({ principalId: snapshot.principal.id, permission: RUN_PERMISSION, entityType: DOMAIN });
|
|
231
|
+
const taken = deps.ports.plans.take({ planId, principalId: snapshot.principal.id, workspaceId: deps.ports.workspaceId });
|
|
232
|
+
if (!taken.ok)
|
|
233
|
+
return { refusal: { copied: false, cancelled: false, code: taken.code, message: PLAN_TAKE_MESSAGES[taken.code] } };
|
|
234
|
+
return { plan: taken.plan };
|
|
235
|
+
},
|
|
236
|
+
describe: ({ prepared }) => "refusal" in prepared
|
|
237
|
+
? { ask: false, description: undefined }
|
|
238
|
+
: { ask: prepared.plan.replaces !== null, description: prepared.plan },
|
|
239
|
+
askHuman: ({ ctx: snapshot, description }, options) => {
|
|
240
|
+
if (!options.emitSurface)
|
|
241
|
+
throw new ToolInputError({ message: `${DATABASE_TRANSFER_RUN_TOOL_ID}: this execution context has no interactive confirmation channel (no emitSurface), so replacement cannot be confirmed here. Nothing was copied.` });
|
|
242
|
+
if (!description)
|
|
243
|
+
throw new ToolInputError({ message: "No transfer plan can be approved. Nothing was copied." });
|
|
244
|
+
return surfaces.confirmApproval({ ctx: snapshot, plan: description }, options);
|
|
245
|
+
},
|
|
246
|
+
notConfirmed: ({ reason }) => reason === "declined" ? { copied: false, cancelled: true } : { copied: false, cancelled: false, reason },
|
|
247
|
+
run: async ({ ctx: snapshot, prepared }) => {
|
|
248
|
+
if ("refusal" in prepared)
|
|
249
|
+
return prepared.refusal;
|
|
250
|
+
await deps.ports.requirePermission({ principalId: snapshot.principal.id, permission: RUN_PERMISSION, entityType: DOMAIN });
|
|
251
|
+
if (snapshot.signal.aborted)
|
|
252
|
+
return { copied: false, cancelled: false, reason: "abandoned" };
|
|
253
|
+
return copyPlanned(deps, prepared.plan);
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
return handler(ctx, optional);
|
|
258
257
|
}
|
|
259
258
|
function failedDestination(surfaces, exchangeId, code, message) {
|
|
260
259
|
return { result: { saved: false, code, message }, outcome: surfaces.destinationOutcome({ exchangeId, state: "failure", message: `Not saved: ${message}.` }) };
|
|
@@ -298,11 +297,11 @@ async function handleSetDestination(deps, surfaces, ctx, optional = {}) {
|
|
|
298
297
|
}
|
|
299
298
|
if (ctx.signal.aborted)
|
|
300
299
|
return { saved: false, reason: "abandoned" };
|
|
301
|
-
const exchange = surfaces.open({ toolId: SET_DESTINATION_TOOL_ID, principalId: ctx.principal.id }, optional.emitSurface);
|
|
302
|
-
const closeOnAbort = () => exchange.close();
|
|
300
|
+
const exchange = surfaces.open({ binding: { toolId: SET_DESTINATION_TOOL_ID, principalId: ctx.principal.id }, emit: optional.emitSurface });
|
|
301
|
+
const closeOnAbort = () => exchange.close({});
|
|
303
302
|
ctx.signal.addEventListener("abort", closeOnAbort, { once: true });
|
|
304
303
|
try {
|
|
305
|
-
return await surfaces.askThenReport(exchange, surfaces.destinationForm(exchange.id), (answer) => handleDestinationAnswer(deps, surfaces, exchange.id, answer));
|
|
304
|
+
return await surfaces.askThenReport({ exchange, confirmationEmission: surfaces.destinationForm(exchange.id), handle: (answer) => handleDestinationAnswer(deps, surfaces, exchange.id, answer) });
|
|
306
305
|
}
|
|
307
306
|
finally {
|
|
308
307
|
ctx.signal.removeEventListener("abort", closeOnAbort);
|
|
@@ -12,6 +12,9 @@ export declare function fitIdentifier(name: string): string;
|
|
|
12
12
|
export declare function createTableSql(schema: string, table: TransferTable): string;
|
|
13
13
|
/** Same rule as `manifest.ts`'s `reseedSequenceSql`, schema-qualified: empty -> next is 1, else max + 1. */
|
|
14
14
|
export declare function reseedIdentitySql(schema: string, table: TransferTable, sqlTag?: string): string;
|
|
15
|
+
/** Writes declared indexes, retaining trusted host predicates and their uniqueness scope.
|
|
16
|
+
* @complexity O(index columns plus predicate bytes); emits SQL without executing it.
|
|
17
|
+
*/
|
|
15
18
|
export declare function indexSql(schema: string, table: TransferTable): string;
|
|
16
19
|
/**
|
|
17
20
|
* Every CHECK and foreign key, added NOT VALID and then validated; a failed validation is recorded
|
|
@@ -63,9 +63,12 @@ export function reseedIdentitySql(schema, table, sqlTag = "transfer") {
|
|
|
63
63
|
})
|
|
64
64
|
.join("");
|
|
65
65
|
}
|
|
66
|
+
/** Writes declared indexes, retaining trusted host predicates and their uniqueness scope.
|
|
67
|
+
* @complexity O(index columns plus predicate bytes); emits SQL without executing it.
|
|
68
|
+
*/
|
|
66
69
|
export function indexSql(schema, table) {
|
|
67
70
|
return table.indexes
|
|
68
|
-
.map((index) => `CREATE ${index.unique ? "UNIQUE " : ""}INDEX ${quoteIdent(fitIdentifier(index.name))} ON ${qualified(schema, table.name)} (${index.columns.map(quoteIdent).join(", ")});\n`)
|
|
71
|
+
.map((index) => `CREATE ${index.unique ? "UNIQUE " : ""}INDEX ${quoteIdent(fitIdentifier(index.name))} ON ${qualified(schema, table.name)} (${index.columns.map(quoteIdent).join(", ")})${index.where === undefined ? "" : ` WHERE ${index.where}`};\n`)
|
|
69
72
|
.join("");
|
|
70
73
|
}
|
|
71
74
|
const REFERENTIAL_ACTIONS = new Set(["NO ACTION", "RESTRICT", "CASCADE", "SET NULL", "SET DEFAULT"]);
|
|
@@ -84,7 +87,8 @@ function parentColumns(fk, byName) {
|
|
|
84
87
|
const columns = fk.foreignColumns ?? parent.primaryKey;
|
|
85
88
|
if (columns.length !== fk.columns.length)
|
|
86
89
|
return null;
|
|
87
|
-
|
|
90
|
+
// A partial unique index covers only some rows; Postgres cannot use it as an FK target.
|
|
91
|
+
const unique = sameColumns(columns, parent.primaryKey) || parent.indexes.some((index) => index.unique && index.where === undefined && sameColumns(index.columns, columns));
|
|
88
92
|
return unique ? columns : null;
|
|
89
93
|
}
|
|
90
94
|
function constraintsOf(schema, table, byName) {
|
package/dist/transfer/types.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export interface TransferIndex {
|
|
|
11
11
|
readonly name: string;
|
|
12
12
|
readonly columns: readonly string[];
|
|
13
13
|
readonly unique: boolean;
|
|
14
|
+
/** Trusted, host-declared Postgres predicate SQL; never operator input or SQLite plugin SQL. */
|
|
15
|
+
readonly where?: string;
|
|
14
16
|
}
|
|
15
17
|
export interface TransferForeignKey {
|
|
16
18
|
readonly name: string;
|
|
@@ -35,13 +37,5 @@ export interface TransferTable {
|
|
|
35
37
|
readonly foreignKeys: readonly TransferForeignKey[];
|
|
36
38
|
readonly checks: readonly TransferCheck[];
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
export interface TransferNaming {
|
|
40
|
-
readonly defaultSchema: string;
|
|
41
|
-
readonly markerTable: string;
|
|
42
|
-
readonly unvalidatedTable: string;
|
|
43
|
-
readonly schemaPrefix: string;
|
|
44
|
-
/** Optional SQL dollar-quote tag for compatibility with existing script consumers. */
|
|
45
|
-
readonly sqlTag?: string;
|
|
46
|
-
}
|
|
40
|
+
export type { TransferNaming } from '../core/transfer-naming.js';
|
|
47
41
|
//# sourceMappingURL=types.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Database ports, connection helpers, ownership, isolated Kysely kernels and migrations. All peers are optional; drivers are injected.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -14,7 +14,25 @@
|
|
|
14
14
|
"runtime": "node",
|
|
15
15
|
"neutralEntries": [
|
|
16
16
|
"src/core"
|
|
17
|
-
]
|
|
17
|
+
],
|
|
18
|
+
"isolation": {
|
|
19
|
+
"entries": {
|
|
20
|
+
"./tools": "transfer"
|
|
21
|
+
},
|
|
22
|
+
"shared": [
|
|
23
|
+
"src/kernel/dialect.ts",
|
|
24
|
+
"src/kernel/index.ts",
|
|
25
|
+
"src/kernel/kernel-core.ts",
|
|
26
|
+
"src/kernel/ops.ts",
|
|
27
|
+
"src/kernel/port.ts",
|
|
28
|
+
"src/kernel/postgres-lock.ts",
|
|
29
|
+
"src/kernel/schema-scope.ts",
|
|
30
|
+
"src/kernel/schema-shape.ts",
|
|
31
|
+
"src/kernel/store-copy.ts",
|
|
32
|
+
"src/kernel/turn-lock.ts",
|
|
33
|
+
"src/kernel/typegen.ts"
|
|
34
|
+
]
|
|
35
|
+
}
|
|
18
36
|
},
|
|
19
37
|
"type": "module",
|
|
20
38
|
"sideEffects": false,
|
|
@@ -64,6 +82,11 @@
|
|
|
64
82
|
"import": "./dist/migrate/index.js",
|
|
65
83
|
"default": "./dist/migrate/index.js"
|
|
66
84
|
},
|
|
85
|
+
"./testing/pg-fixture": {
|
|
86
|
+
"types": "./dist/pg-fixture.d.ts",
|
|
87
|
+
"import": "./dist/pg-fixture.js",
|
|
88
|
+
"default": "./dist/pg-fixture.js"
|
|
89
|
+
},
|
|
67
90
|
"./package.json": "./package.json",
|
|
68
91
|
"./transfer": {
|
|
69
92
|
"types": "./dist/transfer/index.d.ts",
|
|
@@ -79,6 +102,16 @@
|
|
|
79
102
|
"types": "./dist/kernel/store-copy.d.ts",
|
|
80
103
|
"import": "./dist/kernel/store-copy.js",
|
|
81
104
|
"default": "./dist/kernel/store-copy.js"
|
|
105
|
+
},
|
|
106
|
+
"./recovery/*": {
|
|
107
|
+
"types": "./dist/recovery/*.d.ts",
|
|
108
|
+
"import": "./dist/recovery/*.js",
|
|
109
|
+
"default": "./dist/recovery/*.js"
|
|
110
|
+
},
|
|
111
|
+
"./recovery": {
|
|
112
|
+
"types": "./dist/recovery/index.d.ts",
|
|
113
|
+
"import": "./dist/recovery/index.js",
|
|
114
|
+
"default": "./dist/recovery/index.js"
|
|
82
115
|
}
|
|
83
116
|
},
|
|
84
117
|
"typesVersions": {
|
|
@@ -110,6 +143,9 @@
|
|
|
110
143
|
"migrate": [
|
|
111
144
|
"./dist/migrate/index.d.ts"
|
|
112
145
|
],
|
|
146
|
+
"testing/pg-fixture": [
|
|
147
|
+
"./dist/pg-fixture.d.ts"
|
|
148
|
+
],
|
|
113
149
|
"package.json": [
|
|
114
150
|
"./package.json"
|
|
115
151
|
],
|
|
@@ -121,6 +157,12 @@
|
|
|
121
157
|
],
|
|
122
158
|
"kernel/store-copy": [
|
|
123
159
|
"./dist/kernel/store-copy.d.ts"
|
|
160
|
+
],
|
|
161
|
+
"recovery/*": [
|
|
162
|
+
"./dist/recovery/*.d.ts"
|
|
163
|
+
],
|
|
164
|
+
"recovery": [
|
|
165
|
+
"./dist/recovery/index.d.ts"
|
|
124
166
|
]
|
|
125
167
|
}
|
|
126
168
|
},
|
|
@@ -141,6 +183,9 @@
|
|
|
141
183
|
"access": "public",
|
|
142
184
|
"registry": "https://registry.npmjs.org"
|
|
143
185
|
},
|
|
186
|
+
"dependencies": {
|
|
187
|
+
"@jini-ai/core": "^0.4.2"
|
|
188
|
+
},
|
|
144
189
|
"peerDependencies": {
|
|
145
190
|
"kysely": "^0.29.6",
|
|
146
191
|
"better-sqlite3": "^13.0.0",
|
|
@@ -169,10 +214,7 @@
|
|
|
169
214
|
"better-sqlite3": "13.0.3",
|
|
170
215
|
"kysely": "0.29.6",
|
|
171
216
|
"pg": "8.23.0",
|
|
172
|
-
"@jini-ai/core": "0.4.
|
|
173
|
-
},
|
|
174
|
-
"dependencies": {
|
|
175
|
-
"@jini-ai/core": "0.4.0"
|
|
217
|
+
"@jini-ai/core": "0.4.2"
|
|
176
218
|
},
|
|
177
219
|
"scripts": {
|
|
178
220
|
"build": "tsc -p tsconfig.build.json",
|
|
@@ -1,22 +0,0 @@
|
|
|
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 declare const PG_OID: {
|
|
15
|
-
readonly int8: 20;
|
|
16
|
-
readonly json: 114;
|
|
17
|
-
readonly jsonb: 3802;
|
|
18
|
-
};
|
|
19
|
-
export declare function parseInt8(text: string): number | bigint;
|
|
20
|
-
/** Parser overrides by type oid. */
|
|
21
|
-
export declare const PG_PARSERS: Readonly<Record<number, (text: string) => unknown>>;
|
|
22
|
-
//# sourceMappingURL=pg-types.d.ts.map
|
package/dist/kernel/pg-types.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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
|
package/dist/kernel/socket.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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
|
package/dist/kernel/socket.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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
|
package/dist/pglite/dialect.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { PGlite } from "@electric-sql/pglite";
|
|
2
|
-
import { type Dialect, type Driver, type Kysely, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler } from "kysely";
|
|
3
|
-
export declare class PgliteDialect implements Dialect {
|
|
4
|
-
private readonly client;
|
|
5
|
-
constructor(client: PGlite);
|
|
6
|
-
createAdapter(): PostgresAdapter;
|
|
7
|
-
createDriver(): Driver;
|
|
8
|
-
createQueryCompiler(): PostgresQueryCompiler;
|
|
9
|
-
createIntrospector(db: Kysely<unknown>): PostgresIntrospector;
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=dialect.d.ts.map
|
package/dist/pglite/dialect.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { CompiledQuery, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler, } from "kysely";
|
|
2
|
-
/**
|
|
3
|
-
* @file A Kysely dialect over one PGlite instance: Kysely's own Postgres adapter, compiler and
|
|
4
|
-
* introspector, plus a driver that runs compiled queries through `PGlite#query`.
|
|
5
|
-
*
|
|
6
|
-
* Written here rather than taken from npm (2026-09-28): `kysely-pglite-dialect` 1.3.1 declares
|
|
7
|
-
* `@electric-sql/pglite` peer `^0.2 || ^0.3 || ^0.4` (this package pins 0.5.8), is one maintainer, and is
|
|
8
|
-
* ~11 KB of the same few lines; `kysely-pglite` was last published 2024. This file is that driver,
|
|
9
|
-
* sized to what the kernel needs and nothing else.
|
|
10
|
-
*
|
|
11
|
-
* PGlite is ONE connection. The driver hands it out one holder at a time (a transaction holds it
|
|
12
|
-
* from `BEGIN` to `COMMIT`), so a statement from outside can never land inside someone else's
|
|
13
|
-
* transaction. The kernel's turn lock already orders callers; this keeps the dialect correct on its
|
|
14
|
-
* own too.
|
|
15
|
-
*/
|
|
16
|
-
class PgliteConnection {
|
|
17
|
-
client;
|
|
18
|
-
constructor(client) {
|
|
19
|
-
this.client = client;
|
|
20
|
-
}
|
|
21
|
-
async executeQuery(compiled) {
|
|
22
|
-
const result = await this.client.query(compiled.sql, [...compiled.parameters]);
|
|
23
|
-
return {
|
|
24
|
-
rows: result.rows,
|
|
25
|
-
...(result.affectedRows !== undefined ? { numAffectedRows: BigInt(result.affectedRows) } : {}),
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
// eslint-disable-next-line require-yield -- the interface requires a generator; PGlite has no cursor API here
|
|
29
|
-
async *streamQuery() {
|
|
30
|
-
throw new Error("streaming queries are not supported on PGlite");
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
class PgliteDriver {
|
|
34
|
-
client;
|
|
35
|
-
connection;
|
|
36
|
-
holder = Promise.resolve();
|
|
37
|
-
releaseHolder;
|
|
38
|
-
constructor(client) {
|
|
39
|
-
this.client = client;
|
|
40
|
-
this.connection = new PgliteConnection(client);
|
|
41
|
-
}
|
|
42
|
-
async init() {
|
|
43
|
-
await this.client.waitReady;
|
|
44
|
-
}
|
|
45
|
-
async acquireConnection() {
|
|
46
|
-
const previous = this.holder;
|
|
47
|
-
let release;
|
|
48
|
-
this.holder = new Promise((resolve) => (release = resolve));
|
|
49
|
-
await previous;
|
|
50
|
-
this.releaseHolder = release;
|
|
51
|
-
return this.connection;
|
|
52
|
-
}
|
|
53
|
-
async releaseConnection() {
|
|
54
|
-
const release = this.releaseHolder;
|
|
55
|
-
this.releaseHolder = undefined;
|
|
56
|
-
release?.();
|
|
57
|
-
}
|
|
58
|
-
async beginTransaction(connection) {
|
|
59
|
-
await connection.executeQuery(CompiledQuery.raw("BEGIN"));
|
|
60
|
-
}
|
|
61
|
-
async commitTransaction(connection) {
|
|
62
|
-
await connection.executeQuery(CompiledQuery.raw("COMMIT"));
|
|
63
|
-
}
|
|
64
|
-
async rollbackTransaction(connection) {
|
|
65
|
-
await connection.executeQuery(CompiledQuery.raw("ROLLBACK"));
|
|
66
|
-
}
|
|
67
|
-
/** The PGlite instance belongs to whoever opened it (the kernel closes it). */
|
|
68
|
-
async destroy() { }
|
|
69
|
-
}
|
|
70
|
-
export class PgliteDialect {
|
|
71
|
-
client;
|
|
72
|
-
constructor(client) {
|
|
73
|
-
this.client = client;
|
|
74
|
-
}
|
|
75
|
-
createAdapter() {
|
|
76
|
-
return new PostgresAdapter();
|
|
77
|
-
}
|
|
78
|
-
createDriver() {
|
|
79
|
-
return new PgliteDriver(this.client);
|
|
80
|
-
}
|
|
81
|
-
createQueryCompiler() {
|
|
82
|
-
return new PostgresQueryCompiler();
|
|
83
|
-
}
|
|
84
|
-
createIntrospector(db) {
|
|
85
|
-
return new PostgresIntrospector(db);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
//# sourceMappingURL=dialect.js.map
|
package/dist/pglite/driver.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { PGlite } from "@electric-sql/pglite";
|
|
2
|
-
import { type StorageKernel } from "../kernel/index.js";
|
|
3
|
-
import type { PgliteClass } from "./types.js";
|
|
4
|
-
/**
|
|
5
|
-
* @file The PGlite driver: one in-process Postgres (WASM) per data directory, behind the same
|
|
6
|
-
* Kysely query bodies as every other driver.
|
|
7
|
-
*
|
|
8
|
-
* PGlite holds one connection: a repo that kept using the base handle inside a transaction would
|
|
9
|
-
* wait on itself forever. The kernel's transaction scope hands the open transaction to every
|
|
10
|
-
* `run()` inside it, which is what makes that safe.
|
|
11
|
-
*
|
|
12
|
-
* Only ONE process may open a data dir; to share one, run an owner (`startPgliteOwner`) and connect
|
|
13
|
-
* through its socket.
|
|
14
|
-
*/
|
|
15
|
-
export type PgKernel<DB> = StorageKernel<DB>;
|
|
16
|
-
/**
|
|
17
|
-
* Opens (and on first use creates) a PGlite database. Synchronous so it fits the synchronous
|
|
18
|
-
* composition root; the async setup runs behind `ready`, which every kernel call awaits.
|
|
19
|
-
*
|
|
20
|
-
* @param required.PGlite the consumer's `PGlite` class (this package never imports it).
|
|
21
|
-
* @param optional.dataDir absent = in-memory (tests).
|
|
22
|
-
* @param optional.prepare runs once after open, before any call (e.g. schema creation).
|
|
23
|
-
*/
|
|
24
|
-
export declare function openPgliteKernel<DB>(required: {
|
|
25
|
-
PGlite: PgliteClass;
|
|
26
|
-
}, optional?: {
|
|
27
|
-
dataDir?: string;
|
|
28
|
-
prepare?: (client: PGlite) => Promise<void>;
|
|
29
|
-
}): PgKernel<DB>;
|
|
30
|
-
//# sourceMappingURL=driver.d.ts.map
|
package/dist/pglite/driver.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { mkdirSync } from "node:fs";
|
|
2
|
-
import { writeFile } from "node:fs/promises";
|
|
3
|
-
import { Kysely } from "kysely";
|
|
4
|
-
import { buildKernel, PG_PARSERS, postgresLockKey } from "../kernel/index.js";
|
|
5
|
-
import { PgliteDialect } from "./dialect.js";
|
|
6
|
-
/**
|
|
7
|
-
* Opens (and on first use creates) a PGlite database. Synchronous so it fits the synchronous
|
|
8
|
-
* composition root; the async setup runs behind `ready`, which every kernel call awaits.
|
|
9
|
-
*
|
|
10
|
-
* @param required.PGlite the consumer's `PGlite` class (this package never imports it).
|
|
11
|
-
* @param optional.dataDir absent = in-memory (tests).
|
|
12
|
-
* @param optional.prepare runs once after open, before any call (e.g. schema creation).
|
|
13
|
-
*/
|
|
14
|
-
export function openPgliteKernel(required, optional = {}) {
|
|
15
|
-
if (optional.dataDir !== undefined)
|
|
16
|
-
mkdirSync(optional.dataDir, { recursive: true });
|
|
17
|
-
// One options object: PGlite drops the second argument when the first (dataDir) is undefined.
|
|
18
|
-
const client = new required.PGlite({ ...(optional.dataDir === undefined ? {} : { dataDir: optional.dataDir }), parsers: PG_PARSERS });
|
|
19
|
-
const base = new Kysely({ dialect: new PgliteDialect(client) });
|
|
20
|
-
const ready = (async () => {
|
|
21
|
-
await client.waitReady;
|
|
22
|
-
await optional.prepare?.(client);
|
|
23
|
-
})();
|
|
24
|
-
// Surfaced by every call that awaits `ready`; this only stops an unhandled-rejection crash when
|
|
25
|
-
// nothing has called in yet.
|
|
26
|
-
ready.catch(() => { });
|
|
27
|
-
return buildKernel({
|
|
28
|
-
dialect: "postgres",
|
|
29
|
-
transport: "pglite",
|
|
30
|
-
capabilities: { interactiveTransactions: true, atomicBatch: true, transactionalDdl: true, backup: true },
|
|
31
|
-
ready,
|
|
32
|
-
base,
|
|
33
|
-
oneConnection: true,
|
|
34
|
-
begin: (body) => base.transaction().execute((tx) => body(tx)),
|
|
35
|
-
lockKey: postgresLockKey,
|
|
36
|
-
async backup(destPath) {
|
|
37
|
-
const dump = await client.dumpDataDir("none");
|
|
38
|
-
await writeFile(destPath, Buffer.from(await dump.arrayBuffer()));
|
|
39
|
-
},
|
|
40
|
-
async close() {
|
|
41
|
-
await ready.catch(() => { });
|
|
42
|
-
await client.close();
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=driver.js.map
|
package/dist/pglite/ops.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { PGlite } from "@electric-sql/pglite";
|
|
2
|
-
import { type StorageKernel, type StorageOps } from "../kernel/index.js";
|
|
3
|
-
import type { PgliteClass } from "./types.js";
|
|
4
|
-
/**
|
|
5
|
-
* @file The storage ops for PGlite: an in-process kernel (`openPgliteKernel`), or a socket client
|
|
6
|
-
* whose owner runs in this process. The copy is a data-dir dump restored into a new data dir;
|
|
7
|
-
* compacting is `VACUUM` + `CHECKPOINT`, then a read-back of the consumer's migration ledger. A
|
|
8
|
-
* socket client reaches the dump only through its owner's exclusive window ({@link PgliteExclusive});
|
|
9
|
-
* without one, use `@jini-ai/db/postgres`'s `postgresOps`, which refuses the copy.
|
|
10
|
-
*/
|
|
11
|
-
/** A PGlite owner's exclusive window (`PgliteOwner.runExclusive`): the one way to dump a served data dir. */
|
|
12
|
-
export interface PgliteExclusive {
|
|
13
|
-
runExclusive<T>(fn: (db: PGlite) => Promise<T>): Promise<T>;
|
|
14
|
-
}
|
|
15
|
-
/** What restoring a dump needs: the consumer's `PGlite` class and the owner lock file to drop from the copy. */
|
|
16
|
-
export interface PgliteRestore {
|
|
17
|
-
readonly PGlite: PgliteClass;
|
|
18
|
-
/** The owner's pid lock file name; the dump carries the source owner's lock, the copy has no owner yet. */
|
|
19
|
-
readonly ownerLockFileName?: string;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* The ops for a PGlite kernel. Must be used outside a transaction (Postgres cannot `VACUUM` inside one).
|
|
23
|
-
*
|
|
24
|
-
* @param required.ledgerTable the migration ledger the compaction reads back (no default).
|
|
25
|
-
* @param optional.pgliteOwner the owner serving the data dir `kernel` is a socket client of.
|
|
26
|
-
* @throws {Error} a kernel that is neither in-process PGlite nor a socket client given its owner.
|
|
27
|
-
* @complexity each op is one full pass over the data dir, O(size).
|
|
28
|
-
*/
|
|
29
|
-
export declare function pgliteOps<DB>(kernel: StorageKernel<DB>, required: PgliteRestore & {
|
|
30
|
-
ledgerTable: string;
|
|
31
|
-
}, optional?: {
|
|
32
|
-
pgliteOwner?: PgliteExclusive;
|
|
33
|
-
}): StorageOps;
|
|
34
|
-
/**
|
|
35
|
-
* `copyTo` for a PGlite data dir served by `owner` (in this process): the dump is taken inside the
|
|
36
|
-
* owner's exclusive window, then restored into `targetPath` (which must not exist).
|
|
37
|
-
*/
|
|
38
|
-
export declare function copyServedPgliteTo(owner: PgliteExclusive, targetPath: string, required: PgliteRestore): Promise<void>;
|
|
39
|
-
//# sourceMappingURL=ops.d.ts.map
|