Skip to content

Libraries adapters

Olivier Guimbal edited this page Aug 6, 2024 · 8 revisions

πŸ‘‰ pg-native

You can ask pg-mem to get you an object wich implements the same behaviour as pg-native.

// instead of
import Client from 'pg-native';

// use:
import {newDb} from 'pg-mem';
const Client = newDb().adapters.createPgNative();

πŸ‘‰ node-postgres (pg)

You can use pg-mem to get a memory version of the node-postgres (pg) module.

// instead of
import {Client} from 'pg';

// use:
import {newDb} from 'pg-mem';
const {Client} = newDb().adapters.createPg();

πŸ‘‰ pg-promise (pgp)

You can ask pg-mem to get you a pg-promise instance bound to this db.

⚠ This requires [email protected] or newer.

// instead of
import pgp from 'pg-promise';
const pg = pgp(opts)

// use:
import {newDb} from 'pg-mem';
const pg = await newDb().adapters.createPgPromise();

// then use it like you would with pg-promise
await pg.connect();

πŸ‘‰ slonik

You can use pg-mem to get a memory version of a slonik pool.

// instead of
import {createPool} from 'slonik';
const pool = createPool(/* args */);

// use:
import {newDb} from 'pg-mem';
const pool = newDb().adapters.createSlonik();

πŸ‘‰ Typeorm

You can use pg-mem as a backend database for Typeorm Usage:

const db = newDb();
const connection = await db.adapters.createTypeormConnection({
    type: 'postgres',
    entities: [/* your entities here ! */]
})

// create schema
await connection.synchronize();

// => you now can use your typeorm connection !

See detailed examples here and here.

See restore points to avoid running schema creation (.synchronize()) on each test.

NB: Restore points only work if the schema has not been changed after the restore point has been created

note: You must install typeorm module first.

πŸ‘‰ Knex

You can use pg-mem as a backend database for Knex

Usage:

const db = newDb();

// use this instead of require('knex')({ ... })
const knex = await db.adapters.createKnex() as import('knex');

// => use 'knex' as usual
knex.schema.createTable(...)

See detailed example here

See restore points to avoid running schema creation (your .createTable()s calls) on each test.

NB: Restore points only work if the schema has not been changed after the restore point has been created

note: You must install knex module first.

πŸ‘‰ Mikro-orm

You can use pg-mem as a backend database for mikro-orm

Usage:

const db = newDb();
const orm: MikroORM = await db.adapters.createMikroOrm({
    entities: [/* your entities here ! */],
})

// create schema
await orm.getSchemaGenerator().synchronize();

// => you now can use mikro-orm as you are used to !

See detailed examples here

See restore points to avoid running schema creation (.synchronize()) on each test.

NB: Restore points only work if the schema has not been changed after the restore point has been created

note: You must install @mikro-orm/core and @mikro-orm/postgresql modules first.

πŸ‘‰ postgres.js

Since postgres.js does not have any kind of easily hookable mechanism, pg-mem uses pg-server to communicate with it... so:

  1. you will have to install peer dependencies: postgres AND pg-server
  2. connect like this:
import { newDb } from 'pg-mem';

// init db
const db = newDb();
await sql`create table test(name text)`;
await sql`insert into test values ('Alice'), ('Bob')`;

// create postgres.js tag (use this instead of require('postgres').connect())
const sql = db.adapters.createPostgresJsTag() as import('postgres').Sql;

const pattern = 'A%';
const results = [...await sql`select * from test where name like ${pattern}`];
console.log(results);
// prints [{    name: "Alice",   }]

⚠️ Given that this binding is very low level (down to the PG protocol itself), you can expect hangs or things that are not well implemented. Particularily related to:

  • concurrency, obviously (do not run concurrent queries)
  • named prepared statements
  • savepoints
  • other ?

see unit tests here

Kysely

a good example is worth a thousand words.

See also this issue, where migrations are discussed