@ultimat3/db 31.26.0 → 31.28.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/package.json +2 -2
- package/src/pglite-snapshot.ts +75 -1
- package/src/pglite.ts +47 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/db",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.28.0",
|
|
4
4
|
"description": "Postgres access, transactions, migrations and drift detection",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"test": "bun test"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ultimat3/core": "31.
|
|
48
|
+
"@ultimat3/core": "31.28.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@electric-sql/pglite": ">=0.5.0"
|
package/src/pglite-snapshot.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { rename, rm } from 'node:fs/promises';
|
|
|
8
8
|
// why: Bun ships no path joiner.
|
|
9
9
|
import { basename, dirname, join } from 'node:path';
|
|
10
10
|
import { evictCacheFiles } from '@ultimat3/core';
|
|
11
|
+
import type { PgliteDriver } from './pglite';
|
|
11
12
|
import { PGLITE_PACKAGE } from './pglite-package';
|
|
12
13
|
|
|
13
14
|
/** Bumped when the file layout below changes, so an old file is a miss rather than a misread. */
|
|
@@ -108,7 +109,12 @@ export async function readSnapshot(file: string, key: string): Promise<Blob | un
|
|
|
108
109
|
* Best effort: a cache that cannot be written (a read-only checkout, a full disk) costs the next
|
|
109
110
|
* boot an `initdb`, which is what it cost before there was a cache.
|
|
110
111
|
*/
|
|
111
|
-
export async function writeSnapshot(
|
|
112
|
+
export async function writeSnapshot(
|
|
113
|
+
file: string,
|
|
114
|
+
key: string,
|
|
115
|
+
blob: Blob,
|
|
116
|
+
keepPrevious = 1,
|
|
117
|
+
): Promise<void> {
|
|
112
118
|
memo.set(key, blob);
|
|
113
119
|
const body = new Uint8Array(await blob.arrayBuffer());
|
|
114
120
|
const temp = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
|
@@ -121,12 +127,14 @@ export async function writeSnapshot(file: string, key: string, blob: Blob): Prom
|
|
|
121
127
|
}
|
|
122
128
|
// One file per PGlite version, and nothing else ever removes an old version's (#738): keep this
|
|
123
129
|
// one and the newest previous one, which a second checkout on the older PGlite may be reading.
|
|
130
|
+
// A caller-keyed cache (`dataSnapshot`, below) keeps its own count in a directory of its own.
|
|
124
131
|
// A reader loads the whole file in one read and takes a missing one as a miss, so no lock.
|
|
125
132
|
await evictCacheFiles({
|
|
126
133
|
dir: dirname(file),
|
|
127
134
|
current: basename(file),
|
|
128
135
|
matches: isSnapshot,
|
|
129
136
|
temporary: isSnapshotTemp,
|
|
137
|
+
keepPrevious,
|
|
130
138
|
});
|
|
131
139
|
}
|
|
132
140
|
|
|
@@ -134,3 +142,69 @@ export async function discardSnapshot(file: string, key: string): Promise<void>
|
|
|
134
142
|
memo.delete(key);
|
|
135
143
|
await rm(file, { force: true }).catch(() => undefined);
|
|
136
144
|
}
|
|
145
|
+
|
|
146
|
+
// A CALLER-keyed snapshot of an in-memory database's whole data directory — a scratch database the
|
|
147
|
+
// caller migrated, restored on the next boot naming the same key. In this file, not its own: a
|
|
148
|
+
// lazy `import()` of a second module made Bun wrap every shared module in `__esm` init stubs, +740 B
|
|
149
|
+
// in the browser rpc chunk that reaches this barrel (`client-bundle.test.ts`).
|
|
150
|
+
|
|
151
|
+
/** `PgliteOptions.dataSnapshot`: where the caller's snapshots live, and which one this boot is. */
|
|
152
|
+
export interface PgliteDataSnapshot {
|
|
153
|
+
/** A directory of this cache's own: its files are evicted newest-`keep` first. */
|
|
154
|
+
readonly dir: string;
|
|
155
|
+
/** What produced the state, hashed by the caller (schema, migrations). Never a TTL. */
|
|
156
|
+
readonly key: string;
|
|
157
|
+
/** Snapshots besides the one just written to keep. Default 2. */
|
|
158
|
+
readonly keep?: number | undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The file and memo key of `snapshot` under the installed PGlite `version`. */
|
|
162
|
+
export function dataSnapshotAt(
|
|
163
|
+
snapshot: PgliteDataSnapshot,
|
|
164
|
+
version: string,
|
|
165
|
+
): { readonly file: string; readonly key: string } {
|
|
166
|
+
const key = `${snapshotKey(version)}-${snapshot.key}`;
|
|
167
|
+
return { file: snapshotFile(snapshot.dir, key), key };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Where `snapshot` lives, or `undefined`: none asked, a boot on disk (a directory is its own
|
|
172
|
+
* snapshot), or a PGlite that cannot name its version — a cache that must not outlive an upgrade.
|
|
173
|
+
*/
|
|
174
|
+
export async function dataSnapshotFor(
|
|
175
|
+
snapshot: PgliteDataSnapshot | undefined,
|
|
176
|
+
inMemory: boolean,
|
|
177
|
+
version: () => Promise<string | undefined>,
|
|
178
|
+
): Promise<{ readonly file: string; readonly key: string } | undefined> {
|
|
179
|
+
if (snapshot === undefined || !inMemory) return undefined;
|
|
180
|
+
const named = await version();
|
|
181
|
+
return named === undefined ? undefined : dataSnapshotAt(snapshot, named);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The drivers a boot restored from a caller's snapshot: they hold its state already. */
|
|
185
|
+
const restored = new WeakSet<PgliteDriver>();
|
|
186
|
+
|
|
187
|
+
export const markRestored = (driver: PgliteDriver): void => {
|
|
188
|
+
restored.add(driver);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export const wasRestored = (driver: PgliteDriver): boolean => restored.has(driver);
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The driver's data directory, written under `at` — unless it was restored from that very file, or
|
|
195
|
+
* the driver cannot dump. Best effort, as the post-`initdb` cache is: a write that fails costs the
|
|
196
|
+
* next boot a migration, which is what it cost before.
|
|
197
|
+
*/
|
|
198
|
+
export async function saveDataSnapshot(
|
|
199
|
+
driver: PgliteDriver,
|
|
200
|
+
at: { readonly file: string; readonly key: string } | undefined,
|
|
201
|
+
keep: number | undefined,
|
|
202
|
+
): Promise<boolean> {
|
|
203
|
+
if (at === undefined || wasRestored(driver) || driver.dumpDataDir === undefined) return false;
|
|
204
|
+
try {
|
|
205
|
+
await writeSnapshot(at.file, at.key, await driver.dumpDataDir('none'), keep ?? 2);
|
|
206
|
+
return true;
|
|
207
|
+
} catch {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
}
|
package/src/pglite.ts
CHANGED
|
@@ -18,11 +18,16 @@ import { PGLITE_INSTANT_PARSERS } from './pg-instant';
|
|
|
18
18
|
import { linkPgliteExtensions, type PgliteExtensionLoader } from './pglite-extensions';
|
|
19
19
|
import { PGLITE_PACKAGE } from './pglite-package';
|
|
20
20
|
import {
|
|
21
|
+
dataSnapshotFor,
|
|
21
22
|
discardSnapshot,
|
|
23
|
+
markRestored,
|
|
24
|
+
type PgliteDataSnapshot,
|
|
22
25
|
pgliteVersion,
|
|
23
26
|
readSnapshot,
|
|
27
|
+
saveDataSnapshot,
|
|
24
28
|
snapshotFile,
|
|
25
29
|
snapshotKey,
|
|
30
|
+
wasRestored,
|
|
26
31
|
writeSnapshot,
|
|
27
32
|
} from './pglite-snapshot';
|
|
28
33
|
import { createTurnQueue } from './pglite-turns';
|
|
@@ -93,6 +98,11 @@ export interface PgliteOptions {
|
|
|
93
98
|
readonly snapshotDir?: string | undefined;
|
|
94
99
|
/** Swap how the installed PGlite version is read. Tests use it; `undefined` disables caching. */
|
|
95
100
|
readonly version?: (() => Promise<string | undefined>) | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* A caller-keyed snapshot of the WHOLE data directory (`pglite-snapshot.ts`): restored at
|
|
103
|
+
* boot when its file holds that key, written by `saveDataSnapshot()`. Read only for `memory://`.
|
|
104
|
+
*/
|
|
105
|
+
readonly dataSnapshot?: PgliteDataSnapshot | undefined;
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
export const PGLITE_FIX =
|
|
@@ -173,6 +183,16 @@ async function restore(
|
|
|
173
183
|
}
|
|
174
184
|
}
|
|
175
185
|
|
|
186
|
+
/** `options.dataSnapshot` under the installed PGlite, read only for an in-memory boot. */
|
|
187
|
+
const dataSnapshotOf = async (options: PgliteOptions) =>
|
|
188
|
+
options.dataSnapshot === undefined
|
|
189
|
+
? undefined
|
|
190
|
+
: dataSnapshotFor(
|
|
191
|
+
options.dataSnapshot,
|
|
192
|
+
(options.dataDir ?? PGLITE_MEMORY) === PGLITE_MEMORY,
|
|
193
|
+
() => (options.version ?? pgliteVersion)(),
|
|
194
|
+
);
|
|
195
|
+
|
|
176
196
|
/** Boots one embedded Postgres. Costs seconds — `pgliteClient` calls it exactly once. */
|
|
177
197
|
export async function loadPgliteDriver(options: PgliteOptions = {}): Promise<PgliteDriver> {
|
|
178
198
|
if (options.driver !== undefined) return options.driver;
|
|
@@ -198,6 +218,15 @@ export async function loadPgliteDriver(options: PgliteOptions = {}): Promise<Pgl
|
|
|
198
218
|
key === undefined || options.snapshotDir === undefined
|
|
199
219
|
? undefined
|
|
200
220
|
: snapshotFile(options.snapshotDir, key);
|
|
221
|
+
// The caller's own state first: a hit is a migrated database, a miss boots as below.
|
|
222
|
+
const data = await dataSnapshotOf(options);
|
|
223
|
+
if (data !== undefined) {
|
|
224
|
+
const restored = await restore(PGlite, base, data.file, data.key);
|
|
225
|
+
if (restored !== undefined) {
|
|
226
|
+
markRestored(restored);
|
|
227
|
+
return restored;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
201
230
|
if (file !== undefined && key !== undefined) {
|
|
202
231
|
const restored = await restore(PGlite, base, file, key);
|
|
203
232
|
if (restored !== undefined) return restored;
|
|
@@ -241,6 +270,13 @@ export async function loadPgliteDriver(options: PgliteOptions = {}): Promise<Pgl
|
|
|
241
270
|
export interface PgliteClient extends ReservableClient, ListeningClient {
|
|
242
271
|
/** Pay the boot up front. `x dev` calls it so the first request is not the slow one. */
|
|
243
272
|
ping(): Promise<void>;
|
|
273
|
+
/**
|
|
274
|
+
* Whether this boot restored `options.dataSnapshot`: its state is the caller's already. Optional
|
|
275
|
+
* so a client written against 31.26 still implements the interface; `pglite()` always has it.
|
|
276
|
+
*/
|
|
277
|
+
restoredDataSnapshot?(): Promise<boolean>;
|
|
278
|
+
/** The data directory, written under `options.dataSnapshot`'s key; `false` when not written. */
|
|
279
|
+
saveDataSnapshot?(): Promise<boolean>;
|
|
244
280
|
close(): Promise<void>;
|
|
245
281
|
}
|
|
246
282
|
|
|
@@ -400,6 +436,17 @@ export function pgliteClient(options: PgliteOptions = {}): PgliteClient {
|
|
|
400
436
|
async ping(): Promise<void> {
|
|
401
437
|
await connect();
|
|
402
438
|
},
|
|
439
|
+
async restoredDataSnapshot(): Promise<boolean> {
|
|
440
|
+
const driver = await connect();
|
|
441
|
+
return options.dataSnapshot !== undefined && wasRestored(driver);
|
|
442
|
+
},
|
|
443
|
+
async saveDataSnapshot(): Promise<boolean> {
|
|
444
|
+
const driver = await connect();
|
|
445
|
+
// A turn of its own: the dump is the session's whole state, never one mid-transaction.
|
|
446
|
+
return turns.run(async () =>
|
|
447
|
+
saveDataSnapshot(driver, await dataSnapshotOf(options), options.dataSnapshot?.keep),
|
|
448
|
+
);
|
|
449
|
+
},
|
|
403
450
|
async close(): Promise<void> {
|
|
404
451
|
const pending = booting;
|
|
405
452
|
booting = undefined;
|