Skip to content

harryplusplus/mikro-orm-pglite

Repository files navigation

MikroORM PGlite Driver

test npm version npm downloads

A driver for using PGlite with MikroORM.

Table of Contents

Language

Installation

Install with the following commands.

# using npm
npm install @electric-sql/pglite @mikro-orm/postgresql mikro-orm-pglite
# using pnpm
pnpm add @electric-sql/pglite @mikro-orm/postgresql mikro-orm-pglite

Warning

Yarn and Bun environments have not been tested yet for compatibility.

Usage

Easy initialization

Configure the driver property of MikroORM.init parameters to PGliteDriver.

The example below shows an easy initialization method.

import { MikroORM } from "@mikro-orm/core";
import { PGliteDriver } from "mikro-orm-pglite";

const orm = await MikroORM.init({
  driver: PGliteDriver,
  dbName: "postgres",
});

await orm.close();

Warning

The dbName property must be configured.

Note

Please read the documentation below for more information on how to configure MikroORM.

Injecting a PGlite instance

When using the Easy initialization method, the PGlite instance is initialized using the In-memory ephemeral storage method. Additionally, the PGlite instance becomes owned by the MikroORM instance. In other words, the PGlite instance shares its lifecycle with the MikroORM instance.

For PGlite instances with In-memory ephemeral storage, sharing the lifecycle with a MikroORM instance may not meet your needs depending on your use case. You may also want to initialize it in a File system storage method or take advantage of the various parameters of the PGlite constructor.

Configure the driverOptions.connection.pglite property to a synchronous or asynchronous function that returns a PGlite instance. In this case, MikroORM considers the PGlite instance as borrowed. In other words, MikroORM does not manage the creation and destruction of PGlite instances, since it delegates the lifecycle of PGlite instances to an external entity.

The example below shows how to inject a PGlite instance.

import { PGlite } from "@electric-sql/pglite";
import { MikroORM } from "@mikro-orm/core";
import { PGliteConnectionConfig, PGliteDriver } from "mikro-orm-pglite";

const pglite = new PGlite();

const orm = await MikroORM.init({
  driver: PGliteDriver,
  dbName: "postgres",
  driverOptions: {
    connection: {
      pglite: () => pglite,
    } satisfies PGliteConnectionConfig,
  },
});

await orm.close();

await pglite.close();

API

Configure the following properties from the parameters of the driverOptions property:

connection

The type of the connection property is:

export interface PGliteConnectionConfig {
  pglite?: PGliteProvider;
}

The following are the properties for configuring connection.

pglite

The type of the pglite property is:

export interface PGliteProvider {
  (): MaybePromise<PGlite>; // (): PGlite | Promise<PGlite>
}

Note

To learn more about PGlite's various features, please read the PGlite API documentation.

License

MIT

About

A driver for using PGlite with MikroORM.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •