Skip to content

Libraries adapters

Olivier Guimbal edited this page Dec 9, 2020 · 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.

Given that pg-promise does not provide any way to be hooked, I had to fork it. You must install this fork in order to use this (not necessarily use it in production):

npm i @oguimbal/pg-promise -D

Then:

// 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, node-postgres (pg).

Usage:

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

// create schema
await connection.synchronize();

// => you now can user 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.

Clone this wiki locally