Registry / testing / pg-mem

pg-mem

JSON →
library3.0.14jsnpmunverified

pg-mem v3.0.14 is an experimental in-memory emulation of a PostgreSQL database for Node.js and browser environments, released under active development with weekly commits. It supports SQL syntax parsing via a custom parser (pgsql-ast-parser), provides adapters for popular ORMs/query builders such as TypeORM, MikroORM, Knex, Kysely, pg-promise, Slonik, and postgres.js, and offers unique features like immutable restore points for zero-cost rollback in unit tests. Key differentiators include browser compatibility, custom function registration, and no native dependencies. However, it is not a full PostgreSQL replacement; many advanced features are missing (e.g., some data types, triggers, window functions) and the SQL parser may reject valid PostgreSQL syntax. It ships TypeScript types and is primarily used for testing and development.

npm install pg-mem
INSTALL
IMPORT
SIG · PG-MEM
P
pg-mem
testingjavascriptv3.0.14
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

newDb
import { newDb } from 'pg-mem'
const pgmem = require('pg-mem'); const db = pgmem.newDb()
ESM-only since v3; named import required.
DataType
import { DataType } from 'pg-mem'
import DataType from 'pg-mem'
Named export, not default.
IDb
import type { IDb } from 'pg-mem'
Type import for TypeScript; only available since v3.

Creates an in-memory database, runs SQL schema and inserts, queries data, then demonstrates restore point rollback.

import { newDb } from 'pg-mem'; const db = newDb(); db.public.none(`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL)`); db.public.none(`INSERT INTO users (name) VALUES ('Alice'), ('Bob')`); const rows = db.public.many(`SELECT * FROM users`); console.log(rows); // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }] // Create restore point and rollback const backup = db.backup(); db.public.none(`UPDATE users SET name = 'Charlie' WHERE id = 1`); backup.restore(); const afterRestore = db.public.many(`SELECT * FROM users`); console.log(afterRestore); // [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
Debug
Known issues
gotchapg-mem does not implement all PostgreSQL features; some SQL syntax or functions may fail silently or throw unexpected errors.
fix
Before relying on pg-mem, test your SQL against it. For unsupported features, consider filing an issue or using alternative mocking strategies.
affects: >=0.0.0
breakingIn v3, the package is ESM-only. Using require() will fail.
fix
Switch to ES module imports: import { newDb } from 'pg-mem'.
affects: >=3.0.0
breakingThe default export was removed in v3; only named exports are available.
fix
Use named imports: import { newDb, DataType, IDb } from 'pg-mem'.
affects: >=3.0.0
gotchaRestore points are only valid within the same db session; creating a second db instance will not share restore points.
fix
Always use the same db instance for restore points.
affects: >=0.0.0
gotchaCustom function return values are not type-checked; returning an incompatible type can cause runtime errors.
fix
Ensure the implementation returns a value compatible with the declared DataType.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: pg_mem_1.newDb is not a function
Using default import on ESM-only package v3
fix
Change to named import: import { newDb } from 'pg-mem'
SyntaxError: Unexpected token 'export'
Using require() on ESM-only package
fix
Set type: 'module' in package.json or use dynamic import: const { newDb } = await import('pg-mem')
Error: unsupported statement type: <some specific SQL>
pg-mem's SQL parser does not recognize that statement
fix
Simplify the SQL or split into multiple statements; consider using pgsql-ast-parser separately.
Upgrade
Version history
3.0.14latest on npm
Audit
Dependencies
typeormoptionalOptional adapter for TypeORM integration
@mikro-orm/coreoptionalOptional adapter for MikroORM integration
@mikro-orm/postgresqloptionalOptional adapter for MikroORM PostgreSQL driver
knexoptionalOptional adapter for Knex query builder
kyselyoptionalOptional adapter for Kysely query builder
pg-promiseoptionalOptional adapter for pg-promise
slonikoptionalOptional adapter for Slonik
postgresoptionalOptional adapter for postgres.js
pg-serveroptionalOptional adapter for pg-server
Agent activity
15 hits · last 30 days
node
12
Resources
packagepg-mem
pg-mem — npm install pg-mem · libregistry