-
-
Notifications
You must be signed in to change notification settings - Fork 99
Libraries adapters
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();
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();
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();
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();
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.
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.
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.
Since postgres.js does not have any kind of easily hookable mechanism, pg-mem uses pg-server to communicate with it... so:
- you will have to install peer dependencies:
postgres
ANDpg-server
- 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", }]
- concurrency, obviously (do not run concurrent queries)
- named prepared statements
- savepoints
- other ?
see unit tests here
a good example is worth a thousand words.
See also this issue, where migrations are discussed