Registry / database / database-cleaner

database-cleaner

JSON →
library1.3.0jsnpmunverified

This package, `database-cleaner` (version 1.3.0), provides a simple, unified API for cleaning various databases in Node.js applications, primarily intended for testing scenarios. It supports MongoDB, Redis, CouchDB, MySQL, PostgreSQL, and Elasticsearch. Its main purpose is to facilitate test setup and teardown by quickly resetting database states between test runs, ensuring a clean slate for each test. The package allows configuration of specific cleaning strategies, such as truncation for SQL databases, and the ability to skip certain tables during the cleaning process. However, a significant consideration is that the project appears to be abandoned, with no updates or maintenance for several years. This implies potential compatibility issues with modern Node.js runtimes (e.g., Node.js 14, 16, 18, 20+) and contemporary versions of the supported database drivers (e.g., `mongodb` v4+, `redis` v4+, `pg` v8+). Developers should be cautious when integrating this into newer projects due to the lack of ongoing support and potential security vulnerabilities arising from outdated dependencies.

npm install database-cleaner
INSTALL
IMPORT
SIG · DATABASE-CLEANER
D
database-cleaner
databasejavascriptv1.3.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.

DatabaseCleaner
const DatabaseCleaner = require('database-cleaner');
import DatabaseCleaner from 'database-cleaner';
This package primarily uses CommonJS `require` syntax and does not provide an official ESM entry point. Direct ESM import is not supported.
databaseCleaner instance
const databaseCleaner = new DatabaseCleaner('mongodb');
const databaseCleaner = DatabaseCleaner();
The `DatabaseCleaner` class must be instantiated with `new` and requires a database type string ('mongodb', 'redis', 'couchdb', 'mysql', 'postgres', 'elasticsearch').
clean method
databaseCleaner.clean(dbConnection, () => console.log('Database cleaned'));
DatabaseCleaner.clean(dbConnection, () => {});
The `clean` method is an instance method, not a static method of the `DatabaseCleaner` class.

Demonstrates how to import the `database-cleaner` module, instantiate it for a specific database type, and call the `clean` method with a placeholder database connection and callback.

const DatabaseCleaner = require('database-cleaner'); // Example for MongoDB. In a real application, 'db' would be a connected database object. // The exact 'database' object type depends on the specific driver (e.g., mongoose connection, node-mongodb-native Db object). const type = 'mongodb'; // Can be 'mongodb', 'redis', 'couchdb', 'mysql', 'postgres', 'elasticsearch' // For demonstration, we'll use a placeholder for the database connection object. // In a real scenario, this would be an active connection instance. const dummyDbConnection = { name: 'myTestDb', close: () => console.log('Dummy DB closed.') }; const databaseCleaner = new DatabaseCleaner(type); console.log(`Attempting to clean database of type: ${type}...`); databaseCleaner.clean(dummyDbConnection, (err) => { if (err) { console.error('Error cleaning database:', err.message); process.exit(1); } else { console.log(`Successfully simulated cleaning ${type} database.`); // In a real test suite, you might close the connection here or let the test runner handle it. // dummyDbConnection.close(); process.exit(0); } });
Debug
Known issues
breakingThe `database-cleaner` package is effectively abandoned, with its last update approximately nine years ago. It is unlikely to be compatible with modern Node.js versions (e.g., Node.js 14+ or current LTS releases) or contemporary versions of database drivers (e.g., `mongodb` v4+, `redis` v4+, `pg` v8+).
fix
Consider using actively maintained alternatives such as `jest-mongodb` for MongoDB, custom database setup/teardown scripts, or frameworks with built-in test utilities for database resets.
affects: >=1.3.0 (due to project abandonment)
gotchaThe `cradle` dependency for CouchDB support is an extremely outdated driver and is no longer maintained. Using it can lead to severe compatibility issues with modern CouchDB versions and Node.js, and may pose security risks.
fix
Avoid using the CouchDB cleaner functionality within this package. If CouchDB testing is required, implement custom cleaning logic using a modern CouchDB client.
affects: >=1.0.0
gotchaFor MySQL and PostgreSQL, specific cleaning strategies ('truncation' or 'deletion') must be explicitly configured in a `config/cleaner-config.js` file, otherwise default behavior might not be as expected. Missing this configuration can lead to incomplete data resets.
fix
Ensure a `config/cleaner-config.js` file is present at the root of your project with the desired strategy, e.g., `{ postgresql: { strategy: 'truncation', skipTables: [] } }`.
affects: >=1.0.0
gotchaThe package's transitive dependencies (e.g., specific versions of `mongodb`, `redis`, `mysql`) are likely very old. Installing this package might pull in insecure or incompatible versions of these drivers, leading to runtime errors or security vulnerabilities in your application.
fix
Thoroughly inspect `npm ls` output for `database-cleaner` to identify outdated dependencies. Pin modern versions of database drivers if possible, but be aware this may cause conflicts with `database-cleaner`'s internal requirements.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'database-cleaner'
The package `database-cleaner` is not installed or not resolvable in the current Node.js environment.
fix
Run `npm install database-cleaner` in your project directory.
ER_BAD_DB_ERROR: Unknown database 'database_cleaner'
When using MySQL or PostgreSQL, the `database_cleaner` database, which the tests and potentially the cleaner expect, has not been created on the database server.
fix
For MySQL, run `mysql -u root -e 'create database database_cleaner;'`. For PostgreSQL, run `createdb database_cleaner`.
TypeError: Cannot read properties of undefined (reading 'clean') or TypeError: databaseCleaner.clean is not a function
The `databaseCleaner` object was not correctly instantiated as a new instance of `DatabaseCleaner`, or the `DatabaseCleaner` class itself was not imported correctly.
fix
Ensure you are using `const databaseCleaner = new DatabaseCleaner(type);` after `const DatabaseCleaner = require('database-cleaner');`.
Upgrade
Version history
1.3.0latest on npm
Audit
Dependencies
mongodboptionalRequired for MongoDB database cleaning functionality.
redisoptionalRequired for Redis database cleaning functionality.
cradleoptionalRequired for CouchDB database cleaning functionality. Note: `cradle` is an extremely old CouchDB driver.
mysqloptionalRequired for MySQL database cleaning functionality.
pgoptionalRequired for PostgreSQL database cleaning functionality.
elasticsearchoptionalRequired for Elasticsearch database cleaning functionality.
Agent activity
8 hits · last 30 days
node
8
Resources
database-cleaner — npm install database-cleaner · libregistry