Skip to content

ORM and Query Builder Support

The following ORMs and Query Builders are known to work properly with PGlite:

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:

sh
npm i drizzle-orm @electric-sql/pglite
npm i -D drizzle-kit
ts
import { 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:

bash
npm i @electric-sql/pglite knex knex-pglite

Then you can setup a regular Knex instance:

javascript
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:

bash
npm i @electric-sql/pglite orange-orm
javascript
import 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:

bash
npm i @electric-sql/pglite typeorm-pglite

typeorm-pglite works with TypeORM's existing postgres dialect. Just provide the PGliteDriver to the driver data source option:

javascript
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.