@playfast/reform-db-pglite
v1.0.1
Published
PGlite (Postgres-in-WASM) engine for @playfast/reform-db — a Db driver with native reactive live queries, dump/restore snapshots, and a browser-capable persistence path.
Maintainers
Readme
@playfast/reform-db-pglite
The PGlite (Postgres-in-WASM) engine for @playfast/reform-db.
Backs the engine-neutral Db driver from @playfast/reform-db with
PGlite. You get native reactive live queries (db.live.query re-runs and calls
back on every write), dump/restore snapshots for host-fs persistence, and the
idb:// browser-persistence path — while your tables, queries, and migrations stay
defined in the engine-neutral core.
Install
npm install @playfast/reform-db-pglite @playfast/reform-db @electric-sql/pglite kyselyeffect, kysely, @playfast/reform, and @playfast/reform-db are peer
dependencies; @electric-sql/pglite is an optional peer, only needed at runtime
(its heavy WASM is imported lazily on open, so importing this package stays light).
Quick Start
import { DbSchema, DbTable, DbColumn, Migration } from '@playfast/reform-db'
import { Pglite } from '@playfast/reform-db-pglite'
import { Layer } from 'effect'
const AppDb = DbSchema.make('app', { tables: [/* … */] })
// Persistent (browser IndexedDB, or a filesystem path on the host):
const DataLayer = QueriesAndProcedures.pipe(
Layer.provideMerge(
Pglite.layer({ dataDir: 'idb://app', migrations: Migration.fromSchema(AppDb) }),
),
)
// Ephemeral in-memory — the default for tests / proofs:
const TestLayer = QueriesAndProcedures.pipe(
Layer.provideMerge(Pglite.memory({ migrations: Migration.fromSchema(AppDb) })),
)Both layers build the Db service synchronously — PGlite opens on a background
fiber inside makeDriver — so they slot straight under reform's runSync scene /
proof mount without a "Fiber cannot be resolved synchronously" error.
API
Pglite.layer(options?)— a persistent (or in-memory, ifdataDiris omitted) PGlite-backedDb. Options:dataDir?,migrations?,loadDataDir?(seed from a priordumptarballBlob).Pglite.memory(options?)— an ephemeral in-memoryDb. Options:migrations?,loadDataDir?.makePgliteEngine(options)— the rawDbEngine, for wiringmakeDriveryourself.
Snapshotting: driver.dump() returns an uncompressed tarball Blob
(dumpDataDir('none')); feed it back via loadDataDir to boot a database already
populated. See @playfast/reform-db for the schema/query/migration
primitives.
License
MIT
