Has anyone got a migration module that they use to transfer mongodb data using the mongoose plugin?
I am currently using the "migrate" module and it works fine, except for the fact that I need to create / destroy my connection in each up / down mode.
those.
// Setup mongoose var mongoose = require('mongoose') , Role = require('../models/role') , User = require('../models/user'); exports.up = function(next) { // get a brand new connection for this patch. mongoose.connect('mongodb://localhost/sagedb'); var adminUser = { username: 'admin', password: 'admin' }; User.createUser(adminUser, function(err, user) { if (err) { mongoose.disconnect(); // Make sure to close connection return next(err); } mongoose.disconnect(next); // Make sure to close connection }); }; exports.down = function(next) { mongoose.connect('mongodb://localhost/sagedb'); // new connection for down User.getUserByUsername('admin', function(err, user) { if (err) { mongoose.disconnect(function() { // make sure to close connection return next(err); }); } if (!user) { mongoose.disconnect(); // make sure to close connection return next(); } User.deleteUser(user, function(err, user) { console.log('deleted user'); mongoose.disconnect(next); // make sure to close connection }); }); };
Probably a much better way to do this. I wonder if the only option is to create my own module, which starts the connection once and closes it when all the fixes are completed.
I saw mongoose-migrate that tracks migration in the database. Not very specific to mongoose IMHO, I would prefer to use the .migrate file, but only need to open the connection once.
source share