Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
yieldification
Browse files Browse the repository at this point in the history
  • Loading branch information
nschloe committed Dec 25, 2015
1 parent 3901fb3 commit e50b137
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 97 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand All @@ -44,8 +47,5 @@
"data-cover-only": "src",
"data-cover-never": "node_modules"
}
},
"dependencies": {
"async": "^1.5.0"
}
}
93 changes: 37 additions & 56 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
81 changes: 44 additions & 37 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -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', () => {
(() => {
Expand Down

0 comments on commit e50b137

Please sign in to comment.