Skip to content

Libraries adapters

Olivier Guimbal edited this page Feb 4, 2021 · 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 pg-promise@10.8.7 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.

Clone this wiki locally