Skip to content

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

mikro-orm-postgrejs

NPM Version NPM Downloads CI Tests Test Coverage

A MikroORM 7 driver for PostgreJS, a PostgreSQL client written from the wire protocol up in TypeScript.

Your entities, repositories, migrations and queries stay as they are. Only the connection under them changes.

Install

npm install mikro-orm-postgrejs @mikro-orm/core @mikro-orm/postgresql postgrejs

@mikro-orm/core and @mikro-orm/postgresql (>=7.2.13 <7.3), postgrejs (>=3.15 <4) and kysely (>=0.29.9 <0.31, which @mikro-orm/postgresql already installs) are peer dependencies. Node

=22.17, as for MikroORM 7.

Quick start

import { MikroORM } from 'mikro-orm-postgrejs';

const orm = await MikroORM.init({
  clientUrl: 'postgresql://app@localhost:5432/mydb',
  entities: [User, Article],
});

The package exports everything @mikro-orm/postgresql does, with its own MikroORM, defineConfig and EntityManager in their place, so an import can switch from one to the other. The driver class on its own is PostgrejsDriver:

import { defineConfig, PostgrejsDriver } from 'mikro-orm-postgrejs';

export default defineConfig({
  driver: PostgrejsDriver, // already the default here
  dbName: 'mydb',
  entities: [User, Article],
});

How it works

MikroORM 7 runs every statement through Kysely, and its PostgreSQL connection builds a Kysely dialect over a pg pool. This driver extends that connection and builds kysely-postgrejs's dialect over a PostgreJS pool instead. The platform, the query builder, the schema generator, the migrator and the entity manager are @mikro-orm/postgresql's own and keep tracking its releases.

Values

Entity properties come out the same as on @mikro-orm/postgresql. MikroORM's PostgreSQL platform reads a few types from strings and parses them itself - date, time, timestamp, timestamptz, interval and numeric - so the driver asks the server to send those as text. Every other type is decoded by PostgreJS.

That shows in raw results. em.execute('select count(*) as n ...') answers n as a number, where pg answers a string; an int8 past 2^53 is a BigInt, ranges are Range objects and geometric types are classes. Code that reads raw rows the way pg shapes them can ask for that:

MikroORM.init({
  // ...
  driverOptions: { decoding: 'pg' },
});

Types PostgreJS has no binary decoder for - citext, hstore, native enums, regclass and other extension types - come back as the text PostgreSQL prints, as they do on pg.

Options

driverOptions takes a PostgreJS pool configuration, which wins over what the driver derives from MikroORM's own settings, plus:

Option Default What it does
decoding 'native' 'pg' makes raw rows carry the values @mikro-orm/postgresql gives
onPoolCreated Called with the PostgreJS pool once it exists
fetchAsString More OIDs to receive as text, on top of the ones the driver asks for

MikroORM's host, port, user, password, dbName, clientUrl (including sslmode and application_name in its query string), pool.min, pool.max, pool.idleTimeoutMillis, onCreateConnection, onReserveConnection and sessionContext all work as they do on @mikro-orm/postgresql, and so does the { max: Infinity, maxUses: 1 } pool setting from MikroORM's Cloudflare Workers guide (PostgreJS 3.16 or later). em.getConnection().getNativeClient() answers the PostgreJS Pool.

Tests

pnpm test runs the unit tests, the live tests and a differential suite that sends the same calls through @mikro-orm/postgresql's pg driver and through this one and compares the answers, against PostgreSQL on 127.0.0.1:5432.

pnpm run test:mikro-orm-suite runs MikroORM's own PostgreSQL tests on this driver, with a control run on pg in the same invocation. It needs Docker: the tests run in containers of their own, so a PostgreSQL on the host is never touched.

License

BSD-3-Clause

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages