diff --git a/package.json b/package.json index 87c5886..e4ccdb5 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "src/index.js", "scripts": { "lint": "eslint test src", - "test": "npm run lint && mocha test/", + "test": "mocha test/", "coveralls": "mocha --require blanket -R mocha-lcov-reporter | coveralls" }, "repository": { @@ -30,7 +30,10 @@ "homepage": "https://github.com/paperhive/mongoose-erase", "devDependencies": { "babel-core": "^6.3.26", + "babel-preset-es2015": "^6.3.13", "blanket": "^1.2.1", + "bluebird": "^3.1.1", + "co": "^4.6.0", "coveralls": "^2.11.6", "eslint": "^1.9.0", "eslint-config-airbnb": "^2.0.0", @@ -44,8 +47,5 @@ "data-cover-only": "src", "data-cover-never": "node_modules" } - }, - "dependencies": { - "async": "^1.5.0" } } diff --git a/src/index.js b/src/index.js index 50a9b5a..469f087 100644 --- a/src/index.js +++ b/src/index.js @@ -1,62 +1,43 @@ -const async = require('async'); +import { promisify } from 'bluebird'; +import co from 'co'; // requires mongoose to be connected to the database -function erase(mongoose, done) { - async.waterfall([ - // ensure a database connection is established - (cb) => { - if (!mongoose.connection.db) { - return cb(new Error('no established database connection')); - } - cb(); - }, - // get collections - (cb) => { - mongoose.connection.db.collections(cb); - }, - // drop collections - (collections, cb) => { - async.each(collections, (collection, eachCb) => { - if (collection.collectionName.match(/^system\./)) {return eachCb();} - // drop collection - collection.drop(eachCb); - }, cb); - }, - // reset mongoose models - (cb) => { - mongoose.connection.models = {}; - mongoose.models = {}; - mongoose.modelSchemas = {}; - cb(); - }, - // // ensureIndexes - // _.map(models, function (model) { - // return model.ensureIndexes.bind(model); - // }) - ], done); -} +export const erase = co.wrap(function* erase(mongoose) { + // ensure a database connection is established + if (!mongoose.connection.db) { + throw new Error('no established database connection'); + } -function connect(mongoose, dbURI, options, cb) { - if (mongoose.connection.db) {return cb();} - mongoose.connect(dbURI, options, cb); -} + // get collections + const collections = yield promisify( + mongoose.connection.db.collections, + {context: mongoose.connection.db} + ); -function connectAndErase(mongoose, dbURI, options) { + // drop collections + yield collections.map(co.wrap((collection) => { + // Older MongoDB installations featur system.* collections; leave those + // untouched. + if (!collection.collectionName.match(/^system\./)) { + // drop collection + collection.drop(); + } + })); + + // reset mongoose models + mongoose.connection.models = {}; + mongoose.models = {}; + mongoose.modelSchemas = {}; +}); + +export const connect = co.wrap(function* connect(mongoose, dbURI, options) { + if (mongoose.connection.db) {return;} const newOptions = options || {}; - return (done) => { - async.series([ - (cb) => { - connect(mongoose, dbURI, newOptions, cb); - }, - (cb) => { - erase(mongoose, cb); - }, - ], done); - }; -} + yield promisify(mongoose.connect, {context: mongoose})(dbURI, options); +}); -module.exports = { - connect, - erase, - connectAndErase, -}; +export const connectAndErase = co.wrap(function* connectAndErase(mongoose, dbURI, options) { + const newOptions = options || {}; + yield connect(mongoose, dbURI, newOptions); + yield erase(mongoose); +}); diff --git a/test/index.js b/test/index.js index cb44e50..bf2d227 100644 --- a/test/index.js +++ b/test/index.js @@ -1,42 +1,49 @@ -const dbURI = 'mongodb://localhost/mongoose-erase'; -const mongoose = require('mongoose'); -const async = require('async'); - -const erase = require('../'); - -describe('erase()', () => { - before((done) => { - async.series([ - // make sure that database is connected and erase it - erase.connectAndErase(mongoose, dbURI), - // add collection users - (cb) => { - const User = mongoose.model('User', new mongoose.Schema({ - name: {type: String, index: true}, - })); - User.create({name: 'Darth'}, cb); - }, - // erase - (cb) => { - erase.erase(mongoose, cb); - }, - ], done); - }); +import { promisify } from 'bluebird'; +import co from 'co'; +import mongoose from 'mongoose'; + +import { connect, connectAndErase, erase } from '../src'; + +const dbURI = 'mongodb://localhost/test'; + +// generator/yield-version of the async/await-based mochaAsync +function mochaAsync(fn) { + return function (done) { + const ctx = this; + co(function* () { + yield fn(ctx); + }).then(done, done); + }; +} + +describe('erase()', function lol() { + before(mochaAsync(function* () { + yield connectAndErase(mongoose, dbURI); - it('should remove collections', function assertDeletion(done) { - mongoose.connection.db.collections((err, collections) => { - if (err) {return done(err);} - // On older MongoDB installations, a collection "system.indexes" is - // always present. - if (collections.length > 0 && + const User = mongoose.model('User', new mongoose.Schema({ + name: {type: String, index: true}, + })); + yield User.create({name: 'Darth'}); + + yield erase(mongoose); + })); + + it('should remove collections', mochaAsync(function* () { + // get collections + const collections = yield promisify( + mongoose.connection.db.collections, + {context: mongoose.connection.db} + ); + + // On older MongoDB installations, a collection "system.indexes" is + // always present. + if (collections.length > 0 && collections[0].collectionName.match(/^system\./)) { - collections.should.have.lengthOf(1); - } else { - collections.should.have.lengthOf(0); - } - done(); - }); - }); + collections.should.have.lengthOf(1); + } else { + collections.should.have.lengthOf(0); + } + })); it('should remove models', () => { (() => {