{"id":16001,"library":"database-cleaner","title":"Database Cleaner for Node.js","description":"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.","status":"abandoned","version":"1.3.0","language":"javascript","source_language":"en","source_url":"git://github.com/emerleite/node-database-cleaner","tags":["javascript","database","cleaner","mongodb","redis","couchdb","tests","package.json","elasticsearch"],"install":[{"cmd":"npm install database-cleaner","lang":"bash","label":"npm"},{"cmd":"yarn add database-cleaner","lang":"bash","label":"yarn"},{"cmd":"pnpm add database-cleaner","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for MongoDB database cleaning functionality.","package":"mongodb","optional":true},{"reason":"Required for Redis database cleaning functionality.","package":"redis","optional":true},{"reason":"Required for CouchDB database cleaning functionality. Note: `cradle` is an extremely old CouchDB driver.","package":"cradle","optional":true},{"reason":"Required for MySQL database cleaning functionality.","package":"mysql","optional":true},{"reason":"Required for PostgreSQL database cleaning functionality.","package":"pg","optional":true},{"reason":"Required for Elasticsearch database cleaning functionality.","package":"elasticsearch","optional":true}],"imports":[{"note":"This package primarily uses CommonJS `require` syntax and does not provide an official ESM entry point. Direct ESM import is not supported.","wrong":"import DatabaseCleaner from 'database-cleaner';","symbol":"DatabaseCleaner","correct":"const DatabaseCleaner = require('database-cleaner');"},{"note":"The `DatabaseCleaner` class must be instantiated with `new` and requires a database type string ('mongodb', 'redis', 'couchdb', 'mysql', 'postgres', 'elasticsearch').","wrong":"const databaseCleaner = DatabaseCleaner();","symbol":"databaseCleaner instance","correct":"const databaseCleaner = new DatabaseCleaner('mongodb');"},{"note":"The `clean` method is an instance method, not a static method of the `DatabaseCleaner` class.","wrong":"DatabaseCleaner.clean(dbConnection, () => {});","symbol":"clean method","correct":"databaseCleaner.clean(dbConnection, () => console.log('Database cleaned'));"}],"quickstart":{"code":"const DatabaseCleaner = require('database-cleaner');\n\n// Example for MongoDB. In a real application, 'db' would be a connected database object.\n// The exact 'database' object type depends on the specific driver (e.g., mongoose connection, node-mongodb-native Db object).\nconst type = 'mongodb'; // Can be 'mongodb', 'redis', 'couchdb', 'mysql', 'postgres', 'elasticsearch'\n\n// For demonstration, we'll use a placeholder for the database connection object.\n// In a real scenario, this would be an active connection instance.\nconst dummyDbConnection = { name: 'myTestDb', close: () => console.log('Dummy DB closed.') };\n\nconst databaseCleaner = new DatabaseCleaner(type);\n\nconsole.log(`Attempting to clean database of type: ${type}...`);\n\ndatabaseCleaner.clean(dummyDbConnection, (err) => {\n  if (err) {\n    console.error('Error cleaning database:', err.message);\n    process.exit(1);\n  } else {\n    console.log(`Successfully simulated cleaning ${type} database.`);\n    // In a real test suite, you might close the connection here or let the test runner handle it.\n    // dummyDbConnection.close();\n    process.exit(0);\n  }\n});\n","lang":"javascript","description":"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."},"warnings":[{"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.","message":"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+).","severity":"breaking","affected_versions":">=1.3.0 (due to project abandonment)"},{"fix":"Avoid using the CouchDB cleaner functionality within this package. If CouchDB testing is required, implement custom cleaning logic using a modern CouchDB client.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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: [] } }`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Run `npm install database-cleaner` in your project directory.","cause":"The package `database-cleaner` is not installed or not resolvable in the current Node.js environment.","error":"Error: Cannot find module 'database-cleaner'"},{"fix":"For MySQL, run `mysql -u root -e 'create database database_cleaner;'`. For PostgreSQL, run `createdb 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.","error":"ER_BAD_DB_ERROR: Unknown database 'database_cleaner'"},{"fix":"Ensure you are using `const databaseCleaner = new DatabaseCleaner(type);` after `const DatabaseCleaner = require('database-cleaner');`.","cause":"The `databaseCleaner` object was not correctly instantiated as a new instance of `DatabaseCleaner`, or the `DatabaseCleaner` class itself was not imported correctly.","error":"TypeError: Cannot read properties of undefined (reading 'clean') or TypeError: databaseCleaner.clean is not a function"}],"ecosystem":"npm"}