Database Cleaner for Node.js

1.3.0 · abandoned · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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);
  }
});

view raw JSON →