pglite-electric-client
v0.2.1
Published
Client library for PGlite-Electric sync - PostgreSQL in your browser, because why not?
Readme
@pglite-electric/client
Offline-first Electric SQL client for browsers. Companion to @pglite-electric/server.
Install
pnpm add @pglite-electric/client
# or
npm install @pglite-electric/clientQuick start
import { setup, registerPwa } from '@pglite-electric/client';
const client = await setup('myapp-user-sveltelectric', process.env.POSTGRES_DSN!, {
electricUrl: 'http://localhost:3000',
baseShapes: ['todos']
});
registerPwa();Required backend setup
- Enable logical replication in PostgreSQL and create a publication that Electric SQL can subscribe to.
- Run the migrations exported by
@pglite-electric/server(or copy the SQL from this repo) so tables like__auth_sessions,__magic_links, etc. exist. - Ensure application tables include
__modified_at,__modified_by, and the triggers shown inscripts/migrate.js. Our server package exposesaddTimestampTracking()for this. - Deploy the Electric SQL sync service and point the client at its
/v1/shapeendpoint.
Per-user databases
Call setup() with a unique name per identity (e.g. ${userSlug}-sveltelectric). Use destroyDB({ clearLocal: true, identifier }) on logout to wipe the IndexedDB store so another user can log in cleanly.
PWA helpers
registerPwa&unregisterPwaregister the default service worker.pwa/manifest.webmanifest,pwa/service-worker.ts, and icons are bundled for consumers to copy into their app.
Storage limits
Use estimateRemainingDBSpace() to check navigator.storage.estimate() and watch for QuotaExceededError when writing to the queue. Surface those errors so the UI can prompt the user to free space or grant persistent storage.
