ORM and Query Builder Support
The following ORMs and Query Builders are known to work properly with PGlite:
Prisma
Prisma is a modern, type-safe ORM for TypeScript and Node.js. Prisma includes built-in support for local development using PGlite via prisma dev.
Local development with prisma dev
Prisma offers a local dev database powered by PGlite. Just run:
npx prisma init
npx prisma devThis starts a local Prisma Postgres instance backed by PGlite. Copy the connection string shown in the CLI and use it as your DATABASE_URL:
DATABASE_URL="prisma+postgres://localhost:PORT/?api_key=__API_KEY__"You can then define models in your schema.prisma and use Prisma Client and migrations as usual:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}npx prisma db pushimport { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
await prisma.user.create({
data: { email: 'alice@example.com' },
})
const users = await prisma.user.findMany()
console.log(users)See the Prisma local dev docs for more details.
Drizzle
Drizzle is a TypeScript ORM with support for many databases, including PGlite. Features include:
- A declarative relational query API
- An SQL-like query builder API
- Migrations
To use PGlite with Drizzle, wrap you PGlite instance with a drizzle() call:
npm i drizzle-orm @electric-sql/pglite
npm i -D drizzle-kitimport { PGlite } from '@electric-sql/pglite';
import { drizzle } from 'drizzle-orm/pglite';
const client = new PGlite();
const db = drizzle(client);
await db.select().from(...);See the Drizzle documentation for more details.
Knex.js
Knex is a stable, reliable Query Builder for various database engines. Key features include:
- Query builder
- Schema builder
- Raw queries
- Database migration tool
To use Knex.js with PGlite, add knex and the third party knex-pglite library to your project:
npm i @electric-sql/pglite knex knex-pgliteThen you can setup a regular Knex instance:
import { knex } from 'knex'
import ClientPgLite from 'knex-pglite'
export const db = knex({
client: ClientPgLite,
dialect: 'postgres',
connection: { connectionString: 'idb://my-database' },
})Now you can check Knex documentation and knex-pglite documentation for more details.
Orange ORM
Orange ORM is a modern, TypeScript-first ORM that runs in Node.js, Bun, Deno and the browser. It follows the Active-Record pattern and ships with an expressive, LINQ-style query API. Key features include:
- Rich querying and deep filtering
- Active-Record-style change tracking
- Fully-typed models with zero code-generation
- Seamless integration with PGlite across runtimes
To use Orange ORM with PGlite, add orange-orm library to your project:
npm i @electric-sql/pglite orange-ormimport orange from 'orange-orm'
const db = map.pglite('idb://my-db')
await db.query(`
create table if not exists task (
id uuid primary key default gen_random_uuid(),
title text,
done boolean
)
`)
const map = orange.map((x) => ({
task: x.table('task').map(({ column }) => ({
id: column('id').uuid().primary(),
title: column('title').string(),
done: column('done').boolean(),
})),
}))
await db.task.insert({ title: 'Write docs', done: false })
const tasks = await db.task.getAll({
where: (x) => x.done.eq(false),
})
console.log(JSON.stringify(tasks))TypeORM
TypeORM is an ORM that can run in NodeJS, the Browser, and many other platforms. Key features include:
- Clean object-relational model
- Eager and lazy associations (relations)
- Automatic migration generation
- Elegant-syntax, flexible and powerful QueryBuilder.
To use TypeORM with PGlite, add the third party typeorm-pglite library to your project:
npm i @electric-sql/pglite typeorm-pglitetypeorm-pglite works with TypeORM's existing postgres dialect. Just provide the PGliteDriver to the driver data source option:
import { PGliteDriver, getPGliteInstance } from 'typeorm-pglite'
import { DataSource } from 'typeorm'
const PGliteDataSource = new DataSource({
type: 'postgres',
driver: new PGliteDriver().driver,
})
// You can access the internal PGlite instance using getPGliteInstance function
const pgliteDb = await getPGliteInstance()Check TypeORM documentation and typeorm-pglite documentation for more details.