Mongoose migrate

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.

+6
source share
3 answers

The cause of the problem is that you connect to the connection every time, with each transfer. That's why you need to disconnect. The same situation if you replace the connection with mongoose.createConnection. you will need to close it.

How to solve?

move

  var mongoose = require('mongoose') , Role = require('../models/role') , User = require('../models/user'); 

into a module like db

 var mongoose = require('mongoose') , Role = require('../models/role') , User = require('../models/user'); module.exports = mongoose 

and just required

  var mongoose = require('./db') 

So you will have:

  • Single connection
  • All models loaded in one place
  • Clear code in Migration
+2
source

You can also try my migrate-mongoose migration framework, which provides a mongoose connection right out of the box.

in your up or down function, you can simply access your models as follows

 this('user').findOne({ name: 'Sergey' }); 

It also transfers your migrations to the database instead of the file system.

0
source

You also have an eastbound migration structure that is very powerful, as well as mongoDB adapters: https://github.com/okv/east

Then you migrate with the command:

 east create my_migration_name 

And then your migration scripts will look like this:

 exports.migrate = function(client, done) { var db = client.db; db......... done(); }; exports.rollback = function(client, done) { var db = client.db; db......... done(); }; 
0
source

Source: https://habr.com/ru/post/948488/


All Articles