Registry / database / knex-db-manager

knex-db-manager

JSON →
library0.7.0jsnpmunverified

Collection of administrative database operations for knex, supporting PostgreSQL, MySQL, and SQLite3. Version 0.7.0 (latest) provides APIs for creating, dropping, copying, truncating databases, and managing database owners via a privileged superuser connection. Helps with test setup, initializing fresh databases, and truncating between tests. Requires knex 0.x and the appropriate database driver. Differentiates from raw knex by offering high-level admin functions like copyDb and truncateDb.

npm install knex-db-manager
INSTALL
IMPORT
SIG · KNEX-DB-MANAGER
K
knex-db-manager
databasejavascriptv0.7.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

databaseManagerFactory
import { databaseManagerFactory } from 'knex-db-manager'
const knexDbManager = require('knex-db-manager')
CJS require is fine; ESM named import also works.
default
import knexDbManager from 'knex-db-manager'
const { default: knexDbManager } = require('knex-db-manager')
Module has a default export that is the factory function itself. CJS users can use require directly.
DatabaseManager
import type { DatabaseManager } from 'knex-db-manager'
import { DatabaseManager } from 'knex-db-manager'
DatabaseManager is only exported as a type, not a value.

Shows initialization and basic usage: create db owner, create database, truncate tables except migrations.

const { databaseManagerFactory } = require('knex-db-manager'); const config = { knex: { client: 'postgres', connection: { host: 'localhost', user: 'dbowneruser', password: process.env.DB_PASSWORD || '', database: 'myapp_dev', }, pool: { min: 0, max: 10 }, migrations: { directory: './migrations' }, }, dbManager: { superUser: 'postgres', superPassword: process.env.SUPER_PASSWORD || '', collate: ['en_US.UTF-8'], populatePathPattern: 'data/**/*.js', }, }; const dbManager = databaseManagerFactory(config); dbManager.createDbOwnerIfNotExist() .then(() => dbManager.createDb()) .then(() => dbManager.truncateDb(['migrations'])) .catch(err => console.error(err));
Debug
Known issues
gotchaDatabaseManager may not close all connections; superuser connection can remain open. Call dbManager.close() to close the knex instance and superuser connection.
fix
Add dbManager.close() after all operations to ensure clean exit.
affects: >=0.5.0
gotchaWhen using copyDb with large databases, the operation copies on the server side and can cause high disk usage; consider using pg_dump/pg_restore instead.
fix
Avoid copyDb for very large databases; use native tools.
affects: *
breakingThe superUser and superPassword fields are required; omitting them results in an error. This was not always enforced in early versions.
fix
Always provide dbManager.superUser and dbManager.superPassword.
affects: >=0.6.0
deprecatedThe populatePathPattern option is deprecated; use knex seed functionality instead.
fix
Remove populatePathPattern and run seeds via knex commands.
affects: >=0.6.0
gotchaThe copyDb method does not work with MySQL; it throws an error if used.
fix
Do not use copyDb with MySQL; implement your own copy logic.
affects: *
gotchatruncateDb may fail if there are foreign key constraints; it disables triggers temporarily but some databases may require CASCADE.
fix
Ensure all tables are listed; use ignoreTables for tables with dependencies.
affects: *
gotchaThe collate option is database-specific; using wrong collation can cause errors on createDb.
fix
Set collate to an array of collations that the database supports; can be empty array if unsure.
affects: *
Errors
Common errors & fixes
Error: knex: Required configuration option 'client' is missing.
knex config object not passed correctly; missing client property.
fix
Ensure config.knex includes client: 'postgres' (or mysql/sqlite3).
Error: Cannot find module 'pg-escape'
The package requires pg-escape for PostgreSQL, but it is not installed.
fix
Run: npm install pg-escape
DBManager: superUser and superPassword must be specified
dbManager config missing superUser or superPassword fields.
fix
Add superUser and superPassword to dbManager configuration object.
dbManager.createDb is not a function
The module was imported incorrectly; databaseManagerFactory returns an object with methods.
fix
Use: const dbManager = databaseManagerFactory(config); then call dbManager.createDb().
error: role "dbowneruser" does not exist
The db owner user has not been created yet; use createDbOwnerIfNotExist() first.
fix
Call await dbManager.createDbOwnerIfNotExist() before createDb().
Upgrade
Version history
0.7.0latest on npm
Audit
Dependencies
knexrequiredpeer dependency for database query building and connection management
pg-escapeoptionalstring escaping for PostgreSQL, automatically installed but may be needed explicitly
Agent activity
5 hits · last 30 days
node
4
Resources