@porulle/adapter-pglite 0.9.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 ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@porulle/adapter-pglite",
3
+ "version": "0.9.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "bun": "./src/index.ts",
9
+ "import": "./dist/index.js",
10
+ "types": "./src/index.ts"
11
+ }
12
+ },
13
+ "dependencies": {
14
+ "@electric-sql/pglite": "^0.3.15",
15
+ "drizzle-kit": "^0.31.9",
16
+ "drizzle-orm": "^0.45.1",
17
+ "@porulle/core": "0.9.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^24.5.2",
21
+ "eslint": "^9.39.1",
22
+ "typescript": "5.9.2",
23
+ "vitest": "^3.2.4",
24
+ "@porulle/eslint-config": "0.1.0",
25
+ "@porulle/typescript-config": "0.1.0"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "files": [
31
+ "src",
32
+ "dist",
33
+ "README.md"
34
+ ],
35
+ "description": "Zero-infrastructure PGlite (embedded WASM PostgreSQL) DatabaseAdapter for @porulle/core. No server, no connection string, no migration step — construct it and the store runs. Ideal for dev, demos, tests, and CI.",
36
+ "homepage": "https://porulle.asyncdot.com",
37
+ "bugs": {
38
+ "url": "https://github.com/asyncdotengineering/porulle/issues"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/asyncdotengineering/porulle.git",
43
+ "directory": "packages/adapters/adapter-pglite"
44
+ },
45
+ "author": "Porulle contributors",
46
+ "scripts": {
47
+ "build": "rm -rf dist tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json",
48
+ "check-types": "tsc --noEmit",
49
+ "lint": "eslint . --max-warnings 1000",
50
+ "test": "vitest run"
51
+ }
52
+ }
package/src/index.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { PGlite } from "@electric-sql/pglite";
2
+ import { drizzle } from "drizzle-orm/pglite";
3
+ import { ensureDefaultOrg, pushSchema, type DatabaseAdapter } from "@porulle/core";
4
+ import * as schema from "@porulle/core/schema";
5
+
6
+ export interface PgliteAdapterOptions {
7
+ /**
8
+ * Filesystem path to persist the database (e.g. `"./.data/pgdata"`). Omit for
9
+ * an ephemeral in-memory instance that is discarded on exit.
10
+ */
11
+ path?: string;
12
+ /**
13
+ * Push the core Drizzle schema on init (create tables if they don't exist),
14
+ * so there is no separate migration step. Default: `true`. Set `false` if you
15
+ * manage schema yourself.
16
+ */
17
+ migrate?: boolean;
18
+ /**
19
+ * Insert the default organization row after migrating, so single-tenant /
20
+ * B2C stores work out of the box. Default: `true`.
21
+ */
22
+ seedDefaultOrg?: boolean;
23
+ }
24
+
25
+ /**
26
+ * A zero-infrastructure `DatabaseAdapter` backed by PGlite — real PostgreSQL
27
+ * compiled to WASM, running in-process. No database server to install, no
28
+ * connection string, no migration command: construct it and the store runs.
29
+ *
30
+ * Ideal for local dev, demos, tests, and CI. For production, swap to
31
+ * `@porulle/adapter-postgres` (same `DatabaseAdapter` contract).
32
+ *
33
+ * ```ts
34
+ * // commerce.config.ts
35
+ * import { defineConfig } from "@porulle/core";
36
+ * import { pgliteAdapter } from "@porulle/adapter-pglite";
37
+ *
38
+ * export default defineConfig({
39
+ * databaseAdapter: await pgliteAdapter({ path: "./.data/pgdata" }),
40
+ * // ...
41
+ * });
42
+ * ```
43
+ *
44
+ * Requires `drizzle-kit` (used for the programmatic schema push) — it ships as a
45
+ * dependency of this package.
46
+ */
47
+ export async function pgliteAdapter(
48
+ options: PgliteAdapterOptions = {},
49
+ ): Promise<DatabaseAdapter> {
50
+ const pg = options.path ? new PGlite(options.path) : new PGlite();
51
+ const db = drizzle(pg, { schema });
52
+
53
+ if (options.migrate !== false) {
54
+ await pushSchema(db);
55
+ }
56
+ if (options.seedDefaultOrg !== false) {
57
+ await ensureDefaultOrg(db);
58
+ }
59
+
60
+ // PGlite's Drizzle `transaction()` can deadlock under some drivers; manual
61
+ // BEGIN/COMMIT/ROLLBACK on the raw instance is reliable.
62
+ async function transaction<T>(fn: (tx: unknown) => Promise<T>): Promise<T> {
63
+ await pg.exec("BEGIN");
64
+ try {
65
+ const result = await fn(db);
66
+ await pg.exec("COMMIT");
67
+ return result;
68
+ } catch (error) {
69
+ await pg.exec("ROLLBACK");
70
+ throw error;
71
+ }
72
+ }
73
+
74
+ return { provider: "postgresql", db, transaction };
75
+ }