@statewalker/db-pglite 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mikhail Kotelnikov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,55 @@
1
- # Temporary Holding Version
1
+ # @statewalker/db-pglite
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ## What it is
4
+
5
+ A driver that implements `Db` from `@statewalker/db-api` on PGlite, Postgres compiled to
6
+ WebAssembly, in Node.js or in the browser. Extensions such as pgvector are passed in by the
7
+ caller.
8
+
9
+ ## Why it exists
10
+
11
+ Postgres-backed code (full-text search with `tsvector`, pgvector indexes) can then take a `Db`
12
+ like every other engine here, instead of a `PGlite` instance, and be tested with the same
13
+ conformance suite.
14
+
15
+ ## How to use
16
+
17
+ ```sh
18
+ pnpm add @statewalker/db-pglite @electric-sql/pglite
19
+ pnpm add @electric-sql/pglite-pgvector # only for the pgvector example below
20
+ ```
21
+
22
+ | Import | Gives | Environment |
23
+ | --- | --- | --- |
24
+ | `@statewalker/db-pglite` | `newPgliteDb`; types `PgliteDbOptions`, `Db`, `DbEntry` | Node.js, browser |
25
+
26
+ `newPgliteDb({ path?, extensions? })` returns `Promise<Db>`. Use `$1`, `$2`, ... placeholders.
27
+ `exec` runs several statements; `query` runs one, with parameters.
28
+
29
+ ## Examples
30
+
31
+ ```ts
32
+ import { vector } from "@electric-sql/pglite-pgvector";
33
+ import { newPgliteDb } from "@statewalker/db-pglite";
34
+
35
+ const db = await newPgliteDb({ path: "./pgdata", extensions: { vector } });
36
+ await db.exec("CREATE EXTENSION IF NOT EXISTS vector");
37
+ const rows = await db.query("SELECT $1::int + 1 AS n", [41]);
38
+ ```
39
+
40
+ ## Internals
41
+
42
+ - **`path` is a PGlite data directory**, not a file: a folder on Node.js, `idb://<name>`
43
+ (IndexedDB) in a browser. Omit it for an in-memory database. This package's tests run on
44
+ Node.js only.
45
+ - **The caller owns extensions.** The driver loads what it is given and never runs
46
+ `CREATE EXTENSION`; do that after opening.
47
+ - **No transactions helper.** `inTransaction` from `@statewalker/db-sqlite` is SQLite-only and
48
+ rejects this Db. Postgres runs `BEGIN ... COMMIT` as ordinary SQL inside one `exec`.
49
+ - **After `close()`** every call rejects with `db-pglite: the database is closed`; a second
50
+ `close()` does nothing.
51
+ - **Dependencies:** `@electric-sql/pglite` (the engine), `@statewalker/db-api` (types).
52
+
53
+ ## License
54
+
55
+ MIT
@@ -0,0 +1,13 @@
1
+ import { Db, Db as Db$1, DbEntry } from "@statewalker/db-api";
2
+ //#region src/pglite-db.d.ts
3
+ type PgliteDbOptions = {
4
+ /** PGlite data directory: a folder on Node.js, `idb://<name>` in a browser. Omit for in-memory. */
5
+ path?: string;
6
+ /** PGlite extensions to load, e.g. `{ vector }` from `@electric-sql/pglite-pgvector`. */
7
+ extensions?: Record<string, unknown>;
8
+ };
9
+ /** Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. */
10
+ export declare function newPgliteDb(options?: PgliteDbOptions): Promise<Db$1>;
11
+ //#endregion
12
+ export type { Db, DbEntry, PgliteDbOptions };
13
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":["Db"],"sources":["../src/pglite-db.ts"],"mappings":";;KAGY;;EAEV;;EAEA,aAAa;;;wBAIO,YAAY,UAAU,kBAAkB,QAAQA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,32 @@
1
+ import { PGlite } from "@electric-sql/pglite";
2
+ //#region src/pglite-db.ts
3
+ /** Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. */
4
+ async function newPgliteDb(options) {
5
+ const pg = await PGlite.create({
6
+ dataDir: options?.path,
7
+ extensions: options?.extensions
8
+ });
9
+ let closed = false;
10
+ const live = () => {
11
+ if (closed) throw new Error("db-pglite: the database is closed");
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();
20
+ await pg.exec(sql);
21
+ },
22
+ async close() {
23
+ if (closed) return;
24
+ closed = true;
25
+ await pg.close();
26
+ }
27
+ };
28
+ }
29
+ //#endregion
30
+ export { newPgliteDb };
31
+
32
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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, DbEntry } from \"@statewalker/db-api\";\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/** Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. */\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 let closed = false;\n const live = () => {\n if (closed) throw new Error(\"db-pglite: the database is closed\");\n };\n return {\n async query<T = DbEntry>(sql: string, params?: unknown[]): Promise<T[]> {\n live();\n return (await pg.query<T>(sql, params ?? [])).rows;\n },\n async exec(sql: string): Promise<void> {\n live();\n await pg.exec(sql);\n },\n async close(): Promise<void> {\n if (closed) return;\n closed = true;\n await pg.close();\n },\n };\n}\n"],"mappings":";;;AAWA,eAAsB,YAAY,SAAwC;CACxE,MAAM,KAAK,MAAM,OAAO,OAAO;EAC7B,SAAS,SAAS;EAClB,YAAY,SAAS;CACvB,CAAC;CACD,IAAI,SAAS;CACb,MAAM,aAAa;EACjB,IAAI,QAAQ,MAAM,IAAI,MAAM,mCAAmC;CACjE;CACA,OAAO;EACL,MAAM,MAAmB,KAAa,QAAkC;GACtE,KAAK;GACL,QAAQ,MAAM,GAAG,MAAS,KAAK,UAAU,CAAC,CAAC,EAAA,CAAG;EAChD;EACA,MAAM,KAAK,KAA4B;GACrC,KAAK;GACL,MAAM,GAAG,KAAK,GAAG;EACnB;EACA,MAAM,QAAuB;GAC3B,IAAI,QAAQ;GACZ,SAAS;GACT,MAAM,GAAG,MAAM;EACjB;CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "@statewalker/db-pglite",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "private": false,
5
+ "type": "module",
6
+ "description": "PGlite (Postgres in WASM) driver for @statewalker/db-api, for Node.js and the browser.",
7
+ "bugs": {
8
+ "url": "https://github.com/statewalker/statewalker-db/issues"
9
+ },
10
+ "homepage": "https://github.com/statewalker/statewalker-db/tree/main/packages/db-pglite#readme",
11
+ "author": {
12
+ "name": "Mikhail Kotelnikov",
13
+ "email": "mikhail.kotelnikov@gmail.com"
14
+ },
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/statewalker/statewalker-db.git",
19
+ "directory": "packages/db-pglite"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "source": "./src/index.ts",
24
+ "types": "./dist/index.d.mts",
25
+ "import": "./dist/index.mjs"
26
+ }
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "src"
31
+ ],
32
+ "dependencies": {
33
+ "@electric-sql/pglite": "^0.5.8",
34
+ "@statewalker/db-api": "^0.1.5"
35
+ },
36
+ "devDependencies": {
37
+ "@electric-sql/pglite-pgvector": "^0.0.16",
38
+ "@types/node": "^26.6.4",
39
+ "rimraf": "^6.1.3",
40
+ "tsdown": "^0.23.0",
41
+ "typescript": "^7.0.2",
42
+ "vitest": "^4.1.10 || ^5.0.3",
43
+ "@statewalker/db-tests": "^0.1.1"
44
+ },
45
+ "sideEffects": false,
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "scripts": {
50
+ "build": "tsdown",
51
+ "dev": "tsdown --watch",
52
+ "test": "vitest run",
53
+ "typecheck": "tsc --noEmit",
54
+ "clean": "rimraf dist",
55
+ "lint": "biome check --write .",
56
+ "format": "biome format --write ."
57
+ }
6
58
  }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type { Db, DbEntry } from "@statewalker/db-api";
2
+ export { newPgliteDb, type PgliteDbOptions } from "./pglite-db.js";
@@ -0,0 +1,20 @@
1
+ import { vector } from "@electric-sql/pglite-pgvector";
2
+ import { runDbConformance } from "@statewalker/db-tests";
3
+ import { describe, expect, it } from "vitest";
4
+ import { newPgliteDb } from "./pglite-db.js";
5
+
6
+ runDbConformance((options) => newPgliteDb(options), { placeholder: "$1" });
7
+
8
+ describe("newPgliteDb", () => {
9
+ it("loads the extensions it is given (pgvector, as indexer-pglite needs)", async () => {
10
+ const db = await newPgliteDb({ extensions: { vector } });
11
+ try {
12
+ await db.exec("CREATE EXTENSION IF NOT EXISTS vector; CREATE TABLE v (e vector(3))");
13
+ await db.query("INSERT INTO v VALUES ($1::vector)", ["[1,2,3]"]);
14
+ const rows = await db.query<{ d: number }>("SELECT e <-> '[1,2,4]'::vector AS d FROM v");
15
+ expect(rows[0]?.d).toBeCloseTo(1);
16
+ } finally {
17
+ await db.close();
18
+ }
19
+ });
20
+ });
@@ -0,0 +1,36 @@
1
+ import { type Extensions, PGlite } from "@electric-sql/pglite";
2
+ import type { Db, DbEntry } from "@statewalker/db-api";
3
+
4
+ export type PgliteDbOptions = {
5
+ /** PGlite data directory: a folder on Node.js, `idb://<name>` in a browser. Omit for in-memory. */
6
+ path?: string;
7
+ /** PGlite extensions to load, e.g. `{ vector }` from `@electric-sql/pglite-pgvector`. */
8
+ extensions?: Record<string, unknown>;
9
+ };
10
+
11
+ /** Create a {@link Db} on PGlite. Use `$1`, `$2`, ... placeholders. */
12
+ export async function newPgliteDb(options?: PgliteDbOptions): Promise<Db> {
13
+ const pg = await PGlite.create({
14
+ dataDir: options?.path,
15
+ extensions: options?.extensions as Extensions | undefined,
16
+ });
17
+ let closed = false;
18
+ const live = () => {
19
+ if (closed) throw new Error("db-pglite: the database is closed");
20
+ };
21
+ return {
22
+ async query<T = DbEntry>(sql: string, params?: unknown[]): Promise<T[]> {
23
+ live();
24
+ return (await pg.query<T>(sql, params ?? [])).rows;
25
+ },
26
+ async exec(sql: string): Promise<void> {
27
+ live();
28
+ await pg.exec(sql);
29
+ },
30
+ async close(): Promise<void> {
31
+ if (closed) return;
32
+ closed = true;
33
+ await pg.close();
34
+ },
35
+ };
36
+ }