Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

database-cleaner

emerleite8.8k1.3.0

Database Cleaner for node.js

database, cleaner, mongodb, redis, couchdb, tests, package.json, elasticsearch

readme

Build Status

Node.js Database Cleaner

The simplest way to clean your database.

Say you're doing test cases. How can you clean up your data after each test run? With Database Cleaner you can simply do it with one line of code.

Supported Databases

  • MongoDB
  • Redis
  • CouchDB
  • MySQL
  • Postgres
  • Elasticsearch

Dependencies

Runtime

  • Node 0.8.x+
  • node-mongodb-native (a.k.a mongodb)
  • redis
  • cradle for couchdb (http://cloudhead.io/cradle)
  • mysql server
  • postgres server
  • elasticsearch

Development/Tests

  • mocha
  • should
  • hredis
  • redis
  • cradle
  • mysql
  • pg
  • elasticsearch

Installation

$ npm install database-cleaner

Usage

var DatabaseCleaner = require('database-cleaner');
var databaseCleaner = new DatabaseCleaner(type); //type = 'mongodb|redis|couchdb'

databaseCleaner.clean(database, callback);

Config

By default it attempts to load the config/cleaner-config.js file and will fall back to the default config.

Both MySQL and Postgres support truncation and deletion strategies. To specify this strategy specify the configuration as follows:

{
  postgresql: {
    strategy: 'truncation',
    skipTables: []
  }
}

Examples

Take look at test folder to see how it works.

Look at examples dir.

Running tests

There is some ways to run tests:

$ mocha test # you need mocha globaly installed. `npm install -g mocha`
$ npm test

For tests you need each database running (mongodb, redis, couchdb, mysql) Or run once. Ex: mocha test/redis.test.js

You can also run tests using local mocha. ./node_modules/mocha/bin/mocha

MySQL

To run mysql tests you need to have a database_cleaner database.

mysql -u root -e 'create database database_cleaner;'

Postgres

To run postgres tests you need to have a database_cleaner database.

createdb database_cleaner

To-Do

Author

License:

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

changelog

1.3.0 / 2018-11-20

  • Upgrade deps to newest versions
  • Fixing Node deps security warning - Node 8, LTS and Stable
  • fix: postgres -> error: cannot truncate a table referenced in a foreign key constraint (Sergei Khoma - @srghma)

1.2.0 / 2016-11-15

  • Support pooled MySQL connections (Rob Wells)
  • Fix: Do not drop VIEWS (Matteo Ferrando)

1.1.0 / 2016-08-16

  • Support for specifying postgres schema (Ian Zabel)

1.0.0 / 2016-06-25

  • Sequence reset for postgres truncation (Matheus Pedroso)
  • Skip system.indexes when delete MongoDB collections (Kostas Bariotis)

0.13.0 / 2016-03-25

  • Cleanup now have two strategy options. 'delete' (default) or 'truncate'. (yads)

0.12.0 / 2016-02-25

  • Elastic Search support (luizgpsantos)
  • Removed version 0.8.x support (emerleite)
  • Handle errors and pass it to the callback (gabceb)

0.11.0 / 2016-01-13

  • Allow to pass the config data on initialize (gabceb)
  • Fix: mysql/postgesql stuck if no table to clean. (Amoki)

0.10.1 / 2015-10-09

  • Fix: Config can be relative to executing dir. Closes #31 (knappe)

0.10.0 / 2015-07-20

  • Allow skip tables during tests. Supports MySQL and Postgres

0.9.2 / 2015-07-15

  • MySQL cleanner. Issue #25
  • Postgres: quote database table name when delete data, fixed an issue when table name is capitalized. Pull Request #23

0.9.1 / 2014-01-24

  • Moved all dependencies to devDependencies because you only need than on test environment. Closes issues #20 and #21.

0.9.0 / 2014-10-25

  • Postgres support
  • Added POSTGRES_HOST, MYSQL_HOST and COUCHDB_HOST to allow use another ip than localhost when testing.

0.8.0 / 2014-10-03

  • DatabaseCleanner global leak fixed.
  • Supports only node >= 0.8.0

0.7.0 / 2011-12-18

  • Remove system.indexes during cleanner - Issue #13 (mongodb)
  • Invoke callback even if empty collection - Bug #14 and #15 (mongodb)
  • Mongodb usage example at examples dir
  • Updated mongodb, cradle, mysql and redis pkg versions

0.6.1 / 2011-12-18

  • rewrited all tests using mocha
  • removed nodeunit dependency (not used anymore)

0.6.0 / 2011-11-05

  • Node.JS 0.6.0 compatibility - Issue #10
  • Fixed nodeunit dependency version
  • Using database 1 for redis tests to not screw up user database
  • Added a test script to not require user install nodeunit globaly

0.5.0 / 2011-09-28

  • Removed mongoose dependency and using only mongodb native driver
  • Fixed mongodb driver update - Issue #9

0.4.0 / 2011-08-08

0.3.3 / 2011-05-10

  • Added dependencies and dev dependencies

0.3.2 / 2011-03-09

  • Using index.js as main in package.json. This is a standard in npm

0.3.1 / 2011-03-09

0.3.0 / 2011-03-04

0.2.0 / 2011-03-02

  • Added redis support
  • Changed the API
  • Updated documentation with the new API sintax

0.1.1 / 2011-02-09

  • Added node-mongodb-native as a project dependency

0.1.0 / 2011-02-09

  • Support MongoDB
  • Clean all databases but system.indexes
  • Basic test structure
  • Initial documentation