prisma-pglite-bridge 0.3.1 → 0.3.2
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 +36 -26
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ Requires **Prisma 7+** and **Node.js 20+**.
|
|
|
12
12
|
pnpm add -D prisma-pglite-bridge @electric-sql/pglite @prisma/adapter-pg pg
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
The last three are peer dependencies you may already have.
|
|
15
16
|
TypeScript users also need `@types/pg`.
|
|
16
17
|
|
|
17
18
|
## Quickstart
|
|
@@ -28,7 +29,8 @@ beforeEach(() => resetDb());
|
|
|
28
29
|
```
|
|
29
30
|
|
|
30
31
|
That's it. Schema is auto-discovered from `prisma.config.ts`
|
|
31
|
-
and migration files
|
|
32
|
+
and migration files (run `prisma migrate dev` first if you
|
|
33
|
+
haven't already). No Docker, no database server — works
|
|
32
34
|
in GitHub Actions, GitLab CI, and any environment where
|
|
33
35
|
Node.js runs.
|
|
34
36
|
|
|
@@ -56,8 +58,8 @@ Creates a Prisma adapter backed by an in-process PGlite instance.
|
|
|
56
58
|
```typescript
|
|
57
59
|
const { adapter, pglite, resetDb, close } = await createPgliteAdapter({
|
|
58
60
|
// All optional — migrations auto-discovered from prisma.config.ts
|
|
59
|
-
migrationsPath: './prisma/migrations',
|
|
60
|
-
sql: 'CREATE TABLE ...',
|
|
61
|
+
migrationsPath: './prisma/migrations', // or:
|
|
62
|
+
sql: 'CREATE TABLE ...', // (first match wins, see Schema Resolution)
|
|
61
63
|
configRoot: '../..', // monorepo: where to find prisma.config.ts
|
|
62
64
|
dataDir: './data/pglite', // omit for in-memory
|
|
63
65
|
extensions: {}, // PGlite extensions
|
|
@@ -81,7 +83,8 @@ Returns:
|
|
|
81
83
|
### `createPool(options?)`
|
|
82
84
|
|
|
83
85
|
Lower-level escape hatch. Creates a `pg.Pool` backed by PGlite
|
|
84
|
-
without Prisma
|
|
86
|
+
without automatic schema resolution — useful for custom Prisma
|
|
87
|
+
setups, other ORMs, or raw SQL.
|
|
85
88
|
|
|
86
89
|
```typescript
|
|
87
90
|
import { createPool } from 'prisma-pglite-bridge';
|
|
@@ -104,14 +107,16 @@ same PGlite instance, pass a shared `SessionLock` to prevent
|
|
|
104
107
|
transaction interleaving.
|
|
105
108
|
|
|
106
109
|
```typescript
|
|
107
|
-
import { PGliteBridge } from 'prisma-pglite-bridge';
|
|
110
|
+
import { PGliteBridge, SessionLock } from 'prisma-pglite-bridge';
|
|
108
111
|
import { PGlite } from '@electric-sql/pglite';
|
|
109
112
|
import pg from 'pg';
|
|
110
113
|
|
|
111
114
|
const pglite = new PGlite();
|
|
112
115
|
await pglite.waitReady;
|
|
116
|
+
|
|
117
|
+
const lock = new SessionLock();
|
|
113
118
|
const client = new pg.Client({
|
|
114
|
-
stream: () => new PGliteBridge(pglite),
|
|
119
|
+
stream: () => new PGliteBridge(pglite, lock),
|
|
115
120
|
});
|
|
116
121
|
```
|
|
117
122
|
|
|
@@ -161,7 +166,9 @@ Now every test file that imports `prisma` from `lib/prisma`
|
|
|
161
166
|
gets the PGlite-backed instance. No Docker, no test database,
|
|
162
167
|
no cleanup scripts.
|
|
163
168
|
|
|
164
|
-
For Jest, the same pattern works with `jest.mock
|
|
169
|
+
For Jest, the same pattern works with `jest.mock`. Note that
|
|
170
|
+
`jest.mock` is hoisted to the top of the file — place it at
|
|
171
|
+
the top level, not inside `beforeAll`:
|
|
165
172
|
|
|
166
173
|
```typescript
|
|
167
174
|
// jest.setup.ts
|
|
@@ -171,7 +178,6 @@ const { PrismaClient } = require('@prisma/client');
|
|
|
171
178
|
let testPrisma;
|
|
172
179
|
let resetDb;
|
|
173
180
|
|
|
174
|
-
// jest.mock is hoisted — must be at top level, not inside beforeAll
|
|
175
181
|
jest.mock('./lib/prisma', () => ({
|
|
176
182
|
get prisma() { return testPrisma; },
|
|
177
183
|
}));
|
|
@@ -190,17 +196,17 @@ beforeEach(() => resetDb());
|
|
|
190
196
|
If your code accepts `PrismaClient` as a parameter:
|
|
191
197
|
|
|
192
198
|
```typescript
|
|
193
|
-
import { createPgliteAdapter } from 'prisma-pglite-bridge';
|
|
199
|
+
import { createPgliteAdapter, type ResetDbFn } from 'prisma-pglite-bridge';
|
|
194
200
|
import { PrismaClient } from '@prisma/client';
|
|
195
201
|
import { beforeAll, beforeEach, it, expect } from 'vitest';
|
|
196
202
|
|
|
197
203
|
let prisma: PrismaClient;
|
|
198
|
-
let resetDb:
|
|
204
|
+
let resetDb: ResetDbFn;
|
|
199
205
|
|
|
200
206
|
beforeAll(async () => {
|
|
201
|
-
const
|
|
202
|
-
prisma = new PrismaClient({ adapter });
|
|
203
|
-
resetDb =
|
|
207
|
+
const result = await createPgliteAdapter();
|
|
208
|
+
prisma = new PrismaClient({ adapter: result.adapter });
|
|
209
|
+
resetDb = result.resetDb;
|
|
204
210
|
});
|
|
205
211
|
|
|
206
212
|
beforeEach(() => resetDb());
|
|
@@ -227,25 +233,27 @@ export const seed = async (prisma: PrismaClient) => {
|
|
|
227
233
|
await prisma.user.create({ data: { name: 'Bob', role: 'MEMBER' } });
|
|
228
234
|
};
|
|
229
235
|
|
|
230
|
-
//
|
|
231
|
-
|
|
232
|
-
|
|
236
|
+
// Script entry point for `prisma db seed`
|
|
237
|
+
if (import.meta.url === new URL(process.argv[1]!, 'file:').href) {
|
|
238
|
+
const prisma = new PrismaClient();
|
|
239
|
+
seed(prisma).then(() => prisma.$disconnect());
|
|
240
|
+
}
|
|
233
241
|
```
|
|
234
242
|
|
|
235
243
|
Then reuse it in tests:
|
|
236
244
|
|
|
237
245
|
```typescript
|
|
238
|
-
import { createPgliteAdapter } from 'prisma-pglite-bridge';
|
|
246
|
+
import { createPgliteAdapter, type ResetDbFn } from 'prisma-pglite-bridge';
|
|
239
247
|
import { PrismaClient } from '@prisma/client';
|
|
240
248
|
import { seed } from '../prisma/seed';
|
|
241
249
|
|
|
242
250
|
let prisma: PrismaClient;
|
|
243
|
-
let resetDb:
|
|
251
|
+
let resetDb: ResetDbFn;
|
|
244
252
|
|
|
245
253
|
beforeAll(async () => {
|
|
246
|
-
const
|
|
247
|
-
prisma = new PrismaClient({ adapter });
|
|
248
|
-
resetDb =
|
|
254
|
+
const result = await createPgliteAdapter();
|
|
255
|
+
prisma = new PrismaClient({ adapter: result.adapter });
|
|
256
|
+
resetDb = result.resetDb;
|
|
249
257
|
await seed(prisma);
|
|
250
258
|
});
|
|
251
259
|
|
|
@@ -271,8 +279,9 @@ const { adapter } = await createPgliteAdapter({
|
|
|
271
279
|
});
|
|
272
280
|
```
|
|
273
281
|
|
|
274
|
-
|
|
275
|
-
|
|
282
|
+
Extensions are included in the `@electric-sql/pglite` package —
|
|
283
|
+
no extra install needed. See [PGlite extensions](https://pglite.dev/extensions/)
|
|
284
|
+
for the full list.
|
|
276
285
|
|
|
277
286
|
### Pre-generated SQL (fastest)
|
|
278
287
|
|
|
@@ -302,11 +311,12 @@ const { adapter, close } = await createPgliteAdapter({
|
|
|
302
311
|
});
|
|
303
312
|
const prisma = new PrismaClient({ adapter });
|
|
304
313
|
|
|
305
|
-
// Data persists across restarts. Schema is only applied
|
|
306
|
-
//
|
|
314
|
+
// Data persists across restarts. Schema is only applied on first run
|
|
315
|
+
// (PGlite detects an existing PGDATA directory). Delete the data
|
|
316
|
+
// directory after schema changes to pick up new migrations.
|
|
307
317
|
```
|
|
308
318
|
|
|
309
|
-
Add `data/pglite/` to `.gitignore
|
|
319
|
+
**Add `data/pglite/` to `.gitignore`.** This gives you a local
|
|
310
320
|
PostgreSQL without Docker — useful for offline development or
|
|
311
321
|
environments where installing PostgreSQL is impractical.
|
|
312
322
|
|
package/dist/index.cjs
CHANGED
package/dist/index.d.cts
CHANGED
|
@@ -203,5 +203,5 @@ declare class PGliteBridge extends Duplex {
|
|
|
203
203
|
private trackSessionStatus;
|
|
204
204
|
}
|
|
205
205
|
//#endregion
|
|
206
|
-
export { type CreatePgliteAdapterOptions, type CreatePoolOptions, PGliteBridge, type PgliteAdapter, type PoolResult, type ResetDbFn, type ResetSnapshotFn, type SnapshotDbFn, createPgliteAdapter, createPool };
|
|
206
|
+
export { type CreatePgliteAdapterOptions, type CreatePoolOptions, PGliteBridge, type PgliteAdapter, type PoolResult, type ResetDbFn, type ResetSnapshotFn, SessionLock, type SnapshotDbFn, createPgliteAdapter, createPool };
|
|
207
207
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -203,5 +203,5 @@ declare class PGliteBridge extends Duplex {
|
|
|
203
203
|
private trackSessionStatus;
|
|
204
204
|
}
|
|
205
205
|
//#endregion
|
|
206
|
-
export { type CreatePgliteAdapterOptions, type CreatePoolOptions, PGliteBridge, type PgliteAdapter, type PoolResult, type ResetDbFn, type ResetSnapshotFn, type SnapshotDbFn, createPgliteAdapter, createPool };
|
|
206
|
+
export { type CreatePgliteAdapterOptions, type CreatePoolOptions, PGliteBridge, type PgliteAdapter, type PoolResult, type ResetDbFn, type ResetSnapshotFn, SessionLock, type SnapshotDbFn, createPgliteAdapter, createPool };
|
|
207
207
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -648,6 +648,6 @@ const createPgliteAdapter = async (options = {}) => {
|
|
|
648
648
|
};
|
|
649
649
|
};
|
|
650
650
|
//#endregion
|
|
651
|
-
export { PGliteBridge, createPgliteAdapter, createPool };
|
|
651
|
+
export { PGliteBridge, SessionLock, createPgliteAdapter, createPool };
|
|
652
652
|
|
|
653
653
|
//# sourceMappingURL=index.mjs.map
|