A driver for using PGlite with MikroORM.
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-pgliteConfigure 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.
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();Configure the following properties from the parameters of the driverOptions property:
The type of the connection property is:
export interface PGliteConnectionConfig {
pglite?: PGliteProvider;
}The following are the properties for configuring connection.
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.
MIT