Database Cleaner for Node.js
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.
Common errors
-
Error: Cannot find module 'database-cleaner'
cause The package `database-cleaner` is not installed or not resolvable in the current Node.js environment.fixRun `npm install database-cleaner` in your project directory. -
ER_BAD_DB_ERROR: Unknown database 'database_cleaner'
cause 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.fixFor 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
cause The `databaseCleaner` object was not correctly instantiated as a new instance of `DatabaseCleaner`, or the `DatabaseCleaner` class itself was not imported correctly.fixEnsure you are using `const databaseCleaner = new DatabaseCleaner(type);` after `const DatabaseCleaner = require('database-cleaner');`.
Warnings
- breaking The `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+).
- gotcha The `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.
- gotcha For 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.
- gotcha The 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.
Install
-
npm install database-cleaner -
yarn add database-cleaner -
pnpm add database-cleaner
Imports
- DatabaseCleaner
import DatabaseCleaner from 'database-cleaner';
const DatabaseCleaner = require('database-cleaner'); - databaseCleaner instance
const databaseCleaner = DatabaseCleaner();
const databaseCleaner = new DatabaseCleaner('mongodb'); - clean method
DatabaseCleaner.clean(dbConnection, () => {});databaseCleaner.clean(dbConnection, () => console.log('Database cleaned'));
Quickstart
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);
}
});