@statewalker/db-pglite 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/README.md +12 -4
- package/dist/index.d.mts +5 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +11 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/src/pglite-db.ts +16 -20
package/README.md
CHANGED
|
@@ -50,10 +50,18 @@ const rows = await db.query("SELECT $1::int + 1 AS n", [41]);
|
|
|
50
50
|
`CREATE EXTENSION`; do that after opening.
|
|
51
51
|
- **No transactions helper.** `inTransaction` from `@statewalker/db-sqlite` is SQLite-only and
|
|
52
52
|
rejects this Db. Postgres runs `BEGIN ... COMMIT` as ordinary SQL inside one `exec`.
|
|
53
|
-
- **
|
|
54
|
-
`close()`
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
- **One call at a time.** The `Db` is built by `newSerialisedDb` from `@statewalker/db-sqlite`
|
|
54
|
+
(the lifecycle every driver shares): calls run in call order; `close()` waits for the calls
|
|
55
|
+
made before it, then closes PGlite; a call made once `close()` has started rejects with
|
|
56
|
+
`db-pglite: the database is closed`; a second `close()` is a no-op.
|
|
57
|
+
- **Values are PGlite's**, with integers as the `Db` contract asks (`number` within
|
|
58
|
+
±(2^53 - 1), `bigint` beyond): BYTEA is a `Uint8Array`, DATE and TIMESTAMP(TZ) a `Date`, and
|
|
59
|
+
NUMERIC/DECIMAL its exact decimal string (`"1.25"`), which PGlite returns to keep every digit;
|
|
60
|
+
convert it with `Number(value)` where a double is enough. The conformance suite declares its
|
|
61
|
+
BLOB column as `BYTEA` here (`blobType`).
|
|
62
|
+
- **Dependencies:** `@statewalker/db-api` (types), `@statewalker/db-sqlite` (`newSerialisedDb`,
|
|
63
|
+
the shared lifecycle wrapper; no SQLite engine comes with it); peer `@electric-sql/pglite`
|
|
64
|
+
(the engine, provided by the caller).
|
|
57
65
|
|
|
58
66
|
## License
|
|
59
67
|
|
package/dist/index.d.mts
CHANGED
|
@@ -6,7 +6,11 @@ type PgliteDbOptions = {
|
|
|
6
6
|
/** PGlite extensions to load, e.g. `{ vector }` from `@electric-sql/pglite-pgvector`. */
|
|
7
7
|
extensions?: Record<string, unknown>;
|
|
8
8
|
};
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. Calls run one at a time, in
|
|
11
|
+
* call order; `close()` waits for them, then closes PGlite; later calls reject with
|
|
12
|
+
* `db-pglite: the database is closed`.
|
|
13
|
+
*/
|
|
10
14
|
export declare function newPgliteDb(options?: PgliteDbOptions): Promise<Db$1>;
|
|
11
15
|
//#endregion
|
|
12
16
|
export type { Db, DbEntry, PgliteDbOptions };
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":["Db"],"sources":["../src/pglite-db.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":["Db"],"sources":["../src/pglite-db.ts"],"mappings":";;KAIY;;EAEV;;EAEA,aAAa;;;;;;;wBAQO,YAAY,UAAU,kBAAkB,QAAQA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
1
|
import { PGlite } from "@electric-sql/pglite";
|
|
2
|
+
import { newSerialisedDb } from "@statewalker/db-sqlite";
|
|
2
3
|
//#region src/pglite-db.ts
|
|
3
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. Calls run one at a time, in
|
|
6
|
+
* call order; `close()` waits for them, then closes PGlite; later calls reject with
|
|
7
|
+
* `db-pglite: the database is closed`.
|
|
8
|
+
*/
|
|
4
9
|
async function newPgliteDb(options) {
|
|
5
10
|
const pg = await PGlite.create({
|
|
6
11
|
dataDir: options?.path,
|
|
7
12
|
extensions: options?.extensions
|
|
8
13
|
});
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
return {
|
|
14
|
-
async query(sql, params) {
|
|
15
|
-
live();
|
|
16
|
-
return (await pg.query(sql, params ?? [])).rows;
|
|
17
|
-
},
|
|
18
|
-
async exec(sql) {
|
|
19
|
-
live();
|
|
14
|
+
return newSerialisedDb({
|
|
15
|
+
query: async (sql, params) => (await pg.query(sql, params)).rows,
|
|
16
|
+
exec: async (sql) => {
|
|
20
17
|
await pg.exec(sql);
|
|
21
18
|
},
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
closed = true;
|
|
25
|
-
await pg.close();
|
|
26
|
-
}
|
|
27
|
-
};
|
|
19
|
+
close: () => pg.close()
|
|
20
|
+
}, { name: "db-pglite" });
|
|
28
21
|
}
|
|
29
22
|
//#endregion
|
|
30
23
|
export { newPgliteDb };
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/pglite-db.ts"],"sourcesContent":["import { type Extensions, PGlite } from \"@electric-sql/pglite\";\nimport type { Db
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/pglite-db.ts"],"sourcesContent":["import { type Extensions, PGlite } from \"@electric-sql/pglite\";\nimport type { Db } from \"@statewalker/db-api\";\nimport { newSerialisedDb } from \"@statewalker/db-sqlite\";\n\nexport type PgliteDbOptions = {\n /** PGlite data directory: a folder on Node.js, `idb://<name>` in a browser. Omit for in-memory. */\n path?: string;\n /** PGlite extensions to load, e.g. `{ vector }` from `@electric-sql/pglite-pgvector`. */\n extensions?: Record<string, unknown>;\n};\n\n/**\n * Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. Calls run one at a time, in\n * call order; `close()` waits for them, then closes PGlite; later calls reject with\n * `db-pglite: the database is closed`.\n */\nexport async function newPgliteDb(options?: PgliteDbOptions): Promise<Db> {\n const pg = await PGlite.create({\n dataDir: options?.path,\n extensions: options?.extensions as Extensions | undefined,\n });\n return newSerialisedDb(\n {\n query: async (sql, params) => (await pg.query<Record<string, unknown>>(sql, params)).rows,\n exec: async (sql) => {\n await pg.exec(sql);\n },\n close: () => pg.close(),\n },\n { name: \"db-pglite\" },\n );\n}\n"],"mappings":";;;;;;;;AAgBA,eAAsB,YAAY,SAAwC;CACxE,MAAM,KAAK,MAAM,OAAO,OAAO;EAC7B,SAAS,SAAS;EAClB,YAAY,SAAS;CACvB,CAAC;CACD,OAAO,gBACL;EACE,OAAO,OAAO,KAAK,YAAY,MAAM,GAAG,MAA+B,KAAK,MAAM,EAAA,CAAG;EACrF,MAAM,OAAO,QAAQ;GACnB,MAAM,GAAG,KAAK,GAAG;EACnB;EACA,aAAa,GAAG,MAAM;CACxB,GACA,EAAE,MAAM,YAAY,CACtB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/db-pglite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "PGlite (Postgres in WASM) driver for @statewalker/db-api, for Node.js and the browser.",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"!src/**/*.test.ts"
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@statewalker/db-api": "^0.1.
|
|
35
|
+
"@statewalker/db-api": "^0.1.7",
|
|
36
|
+
"@statewalker/db-sqlite": "^0.1.4"
|
|
36
37
|
},
|
|
37
38
|
"peerDependencies": {
|
|
38
39
|
"@electric-sql/pglite": "^0.5.15"
|
package/src/pglite-db.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Extensions, PGlite } from "@electric-sql/pglite";
|
|
2
|
-
import type { Db
|
|
2
|
+
import type { Db } from "@statewalker/db-api";
|
|
3
|
+
import { newSerialisedDb } from "@statewalker/db-sqlite";
|
|
3
4
|
|
|
4
5
|
export type PgliteDbOptions = {
|
|
5
6
|
/** PGlite data directory: a folder on Node.js, `idb://<name>` in a browser. Omit for in-memory. */
|
|
@@ -8,29 +9,24 @@ export type PgliteDbOptions = {
|
|
|
8
9
|
extensions?: Record<string, unknown>;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. Calls run one at a time, in
|
|
14
|
+
* call order; `close()` waits for them, then closes PGlite; later calls reject with
|
|
15
|
+
* `db-pglite: the database is closed`.
|
|
16
|
+
*/
|
|
12
17
|
export async function newPgliteDb(options?: PgliteDbOptions): Promise<Db> {
|
|
13
18
|
const pg = await PGlite.create({
|
|
14
19
|
dataDir: options?.path,
|
|
15
20
|
extensions: options?.extensions as Extensions | undefined,
|
|
16
21
|
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return (await pg.query<T>(sql, params ?? [])).rows;
|
|
22
|
+
return newSerialisedDb(
|
|
23
|
+
{
|
|
24
|
+
query: async (sql, params) => (await pg.query<Record<string, unknown>>(sql, params)).rows,
|
|
25
|
+
exec: async (sql) => {
|
|
26
|
+
await pg.exec(sql);
|
|
27
|
+
},
|
|
28
|
+
close: () => pg.close(),
|
|
25
29
|
},
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
await pg.exec(sql);
|
|
29
|
-
},
|
|
30
|
-
async close(): Promise<void> {
|
|
31
|
-
if (closed) return;
|
|
32
|
-
closed = true;
|
|
33
|
-
await pg.close();
|
|
34
|
-
},
|
|
35
|
-
};
|
|
30
|
+
{ name: "db-pglite" },
|
|
31
|
+
);
|
|
36
32
|
}
|