Back to Mikro Orm

@mikro-orm/pglite

packages/pglite/README.md

7.1.02.3 KB
Original Source

@mikro-orm/pglite

MikroORM driver for PGlite — a WASM build of PostgreSQL that runs in Node.js, the browser, Bun and Deno without a separate database server.

Installation

sh
npm install @mikro-orm/core @mikro-orm/pglite

Usage

typescript
import { defineEntity, p, MikroORM } from '@mikro-orm/pglite';

const AuthorSchema = defineEntity({
  name: 'Author',
  properties: {
    id: p.integer().primary(),
    name: p.string(),
    books: () => p.oneToMany(Book).mappedBy('author'),
  },
});
export class Author extends AuthorSchema.class {}
AuthorSchema.setClass(Author);

const BookSchema = defineEntity({
  name: 'Book',
  properties: {
    id: p.integer().primary(),
    title: p.string(),
    author: () => p.manyToOne(Author).inversedBy('books'),
  },
});
export class Book extends BookSchema.class {}
BookSchema.setClass(Book);

// In-memory (default)
const orm = await MikroORM.init({
  entities: [Author, Book],
});

// Persisted to a Node.js directory
const orm = await MikroORM.init({
  entities: [Author, Book],
  dbName: './my-db',
});

// Browser, persisted in IndexedDB
const orm = await MikroORM.init({
  entities: [Author, Book],
  dbName: 'idb://my-db',
});

const author = orm.em.create(Author, { name: 'Jon Snow' });
orm.em.create(Book, { title: 'My Life on The Wall', author });
await orm.em.flush();

Features

Limitations

  • Streaming is not supported by PGlite. Use @mikro-orm/postgresql if you need cursor-based streaming.

Documentation

See the official MikroORM documentation and the PGlite usage guide.

License

Copyright © 2018-present Martin Adámek. Licensed under the MIT License.