@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/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +52 -0
- package/src/index.ts +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 unified-commerce-engine contributors
|
|
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
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @porulle/adapter-pglite
|
|
2
|
+
|
|
3
|
+
A **zero-infrastructure** `DatabaseAdapter` for [`@porulle/core`](https://porulle.asyncdot.com), backed by [PGlite](https://pglite.dev) — real PostgreSQL compiled to WASM, running **in-process**.
|
|
4
|
+
|
|
5
|
+
No database server to install. No connection string. No migration command. Construct it and the store runs — locally, in CI, in a demo, anywhere Node runs.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// commerce.config.ts
|
|
9
|
+
import { defineConfig } from "@porulle/core";
|
|
10
|
+
import { pgliteAdapter } from "@porulle/adapter-pglite";
|
|
11
|
+
|
|
12
|
+
export default defineConfig({
|
|
13
|
+
databaseAdapter: await pgliteAdapter({ path: "./.data/pgdata" }),
|
|
14
|
+
// ...rest of your config
|
|
15
|
+
});
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
That's it — `pnpm dev` and the REST API is live on a real Postgres.
|
|
19
|
+
|
|
20
|
+
## Options
|
|
21
|
+
|
|
22
|
+
| Option | Type | Default | Meaning |
|
|
23
|
+
|---|---|---|---|
|
|
24
|
+
| `path` | `string` | in-memory | Persist the DB to disk (e.g. `"./.data/pgdata"`). Omit for an ephemeral instance discarded on exit. |
|
|
25
|
+
| `migrate` | `boolean` | `true` | Push the core schema on init (create tables if missing) — no separate migration step. |
|
|
26
|
+
| `seedDefaultOrg` | `boolean` | `true` | Insert the default organization row so single-tenant / B2C stores work immediately. |
|
|
27
|
+
|
|
28
|
+
## When to use it
|
|
29
|
+
|
|
30
|
+
- **Local dev & demos** — the fastest path from `install` to a running store.
|
|
31
|
+
- **Tests & CI** — real Postgres semantics without provisioning a database.
|
|
32
|
+
- **Prototypes** — ship the 80% before you own any infrastructure.
|
|
33
|
+
|
|
34
|
+
## Going to production
|
|
35
|
+
|
|
36
|
+
PGlite is single-connection and in-process — perfect for dev, not for production scale. When you're ready, swap one line to [`@porulle/adapter-postgres`](https://porulle.asyncdot.com) — the same `DatabaseAdapter` contract, backed by a real Postgres server:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { postgresAdapter } from "@porulle/adapter-postgres";
|
|
40
|
+
|
|
41
|
+
databaseAdapter: postgresAdapter({ connectionString: process.env.DATABASE_URL! }),
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Your data model, queries, and code are unchanged — it's the same PostgreSQL.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type DatabaseAdapter } from "@porulle/core";
|
|
2
|
+
export interface PgliteAdapterOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Filesystem path to persist the database (e.g. `"./.data/pgdata"`). Omit for
|
|
5
|
+
* an ephemeral in-memory instance that is discarded on exit.
|
|
6
|
+
*/
|
|
7
|
+
path?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Push the core Drizzle schema on init (create tables if they don't exist),
|
|
10
|
+
* so there is no separate migration step. Default: `true`. Set `false` if you
|
|
11
|
+
* manage schema yourself.
|
|
12
|
+
*/
|
|
13
|
+
migrate?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Insert the default organization row after migrating, so single-tenant /
|
|
16
|
+
* B2C stores work out of the box. Default: `true`.
|
|
17
|
+
*/
|
|
18
|
+
seedDefaultOrg?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A zero-infrastructure `DatabaseAdapter` backed by PGlite — real PostgreSQL
|
|
22
|
+
* compiled to WASM, running in-process. No database server to install, no
|
|
23
|
+
* connection string, no migration command: construct it and the store runs.
|
|
24
|
+
*
|
|
25
|
+
* Ideal for local dev, demos, tests, and CI. For production, swap to
|
|
26
|
+
* `@porulle/adapter-postgres` (same `DatabaseAdapter` contract).
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* // commerce.config.ts
|
|
30
|
+
* import { defineConfig } from "@porulle/core";
|
|
31
|
+
* import { pgliteAdapter } from "@porulle/adapter-pglite";
|
|
32
|
+
*
|
|
33
|
+
* export default defineConfig({
|
|
34
|
+
* databaseAdapter: await pgliteAdapter({ path: "./.data/pgdata" }),
|
|
35
|
+
* // ...
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Requires `drizzle-kit` (used for the programmatic schema push) — it ships as a
|
|
40
|
+
* dependency of this package.
|
|
41
|
+
*/
|
|
42
|
+
export declare function pgliteAdapter(options?: PgliteAdapterOptions): Promise<DatabaseAdapter>;
|
|
43
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgC,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AAGnF,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,aAAa,CACjC,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,eAAe,CAAC,CA0B1B"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
2
|
+
import { drizzle } from "drizzle-orm/pglite";
|
|
3
|
+
import { ensureDefaultOrg, pushSchema } from "@porulle/core";
|
|
4
|
+
import * as schema from "@porulle/core/schema";
|
|
5
|
+
/**
|
|
6
|
+
* A zero-infrastructure `DatabaseAdapter` backed by PGlite — real PostgreSQL
|
|
7
|
+
* compiled to WASM, running in-process. No database server to install, no
|
|
8
|
+
* connection string, no migration command: construct it and the store runs.
|
|
9
|
+
*
|
|
10
|
+
* Ideal for local dev, demos, tests, and CI. For production, swap to
|
|
11
|
+
* `@porulle/adapter-postgres` (same `DatabaseAdapter` contract).
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* // commerce.config.ts
|
|
15
|
+
* import { defineConfig } from "@porulle/core";
|
|
16
|
+
* import { pgliteAdapter } from "@porulle/adapter-pglite";
|
|
17
|
+
*
|
|
18
|
+
* export default defineConfig({
|
|
19
|
+
* databaseAdapter: await pgliteAdapter({ path: "./.data/pgdata" }),
|
|
20
|
+
* // ...
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Requires `drizzle-kit` (used for the programmatic schema push) — it ships as a
|
|
25
|
+
* dependency of this package.
|
|
26
|
+
*/
|
|
27
|
+
export async function pgliteAdapter(options = {}) {
|
|
28
|
+
const pg = options.path ? new PGlite(options.path) : new PGlite();
|
|
29
|
+
const db = drizzle(pg, { schema });
|
|
30
|
+
if (options.migrate !== false) {
|
|
31
|
+
await pushSchema(db);
|
|
32
|
+
}
|
|
33
|
+
if (options.seedDefaultOrg !== false) {
|
|
34
|
+
await ensureDefaultOrg(db);
|
|
35
|
+
}
|
|
36
|
+
// PGlite's Drizzle `transaction()` can deadlock under some drivers; manual
|
|
37
|
+
// BEGIN/COMMIT/ROLLBACK on the raw instance is reliable.
|
|
38
|
+
async function transaction(fn) {
|
|
39
|
+
await pg.exec("BEGIN");
|
|
40
|
+
try {
|
|
41
|
+
const result = await fn(db);
|
|
42
|
+
await pg.exec("COMMIT");
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
await pg.exec("ROLLBACK");
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { provider: "postgresql", db, transaction };
|
|
51
|
+
}
|