Skip to content
NewMulti-tenancy and row-level security (RLS) in TypeScript6 min read

PGlite

PGlite is Postgres compiled to WebAssembly, running inside your process with no server, container or port. UQL treats it as Postgres. Use it for tests, where each file gets a fresh database in milliseconds, and for local development on your production dialect.

Terminal window
npm install uql-orm @electric-sql/pglite

UQL needs PGlite 0.5.15 or later.

import { PgliteQuerierPool } from 'uql-orm/pglite';
export const pool = new PgliteQuerierPool(); // in memory

Pass a directory to keep the data, and PGlite’s own options second. On disk, relaxedDurability stops PGlite from waiting on a flush after every statement inside a transaction; the data is still written:

const pool = new PgliteQuerierPool('file://./pgdata', {
relaxedDurability: true,
});

Pass a function instead of a directory, and the pool runs on the handle it returns. Configure PGlite where you open it; the pool’s second argument is then UQL’s own options. This is how a PGliteWorker shares one database across browser tabs:

pglite-worker.ts
import { PGlite } from '@electric-sql/pglite';
import { worker } from '@electric-sql/pglite/worker';
worker({ init: () => PGlite.create('idb://app') });
import { PGliteWorker } from '@electric-sql/pglite/worker';
import { PgliteQuerierPool } from 'uql-orm/pglite';
const pool = new PgliteQuerierPool(() =>
PGliteWorker.create(
new Worker(new URL('./pglite-worker.ts', import.meta.url), {
type: 'module',
}),
),
);

The pool decodes rows the way every Postgres pool does, wide integers exact and zoneless timestamps as UTC, without changing the handle’s own parsers, so your own queries on it read as before. pool.end() closes the handle.

pgvector is a separate bundle that PGlite loads at startup:

Terminal window
npm install @electric-sql/pglite-pgvector
import { vector } from '@electric-sql/pglite-pgvector';
const pool = new PgliteQuerierPool('memory://', { extensions: { vector } });

Vector search then works as on a server, halfvec and sparsevec included. The extension pins an exact @electric-sql/pglite version, so upgrade both together.

PGlite has a single connection that every querier shares. A transaction holds PGlite’s own lock until it ends, so everything else on the handle waits for it: other queriers, your own queries on a handle you opened, live queries, and other tabs through a worker. Give work that must run concurrently its own pool, and so its own database; a test suite or a single-user dev session is unaffected.

$lock emits SELECT ... FOR UPDATE, but no second transaction can contend with it, so test row-locking behaviour on a Postgres server. findManyStream reads its rows in one statement rather than through a cursor, whose transaction would hold the shared connection until the loop ends; for a large result, use findManyPage.

A BYTEA column reads back as a Uint8Array, not a Buffer, so instanceof Buffer fails. Migration runs hold their lock as a uql/lock record, since queriers share a session.

Otherwise the two are interchangeable: both report the postgres dialect, so migrations and drift:check carry across.