【问题标题】:Mongoose migrate猫鼬迁移
【发布时间】:2013-06-28 21:38:30
【问题描述】:

谁有一个迁移模块,他们用 mongoose 插件迁移 mongodb 数据?

我目前正在使用“迁移”模块,它工作得很好,除了我需要在每次上/下时创建/销毁我的连接。

I.E.

// 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
    });
  });
};

这可能是一种更好的方法。想知道是否唯一的选择是创建我自己的模块,该模块启动一次连接并在所有补丁完成后关闭它。

我见过 mongoose-migrate 跟踪数据库集合中的迁移。不是真的特定于猫鼬恕我直言,我宁愿仍然使用 .migrate 文件,但只需打开一次连接。

【问题讨论】:

    标签: node.js mongodb mongoose migrate


    【解决方案1】:

    问题的原因是每次迁移时您都有连接“连接” 这就是为什么你必须断开连接。 如果将 connect 替换为 mongoose.createConnection,情况相同。你需要关闭它。

    如何解决?

    移动

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

    进入像 db 这样的模块

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

    只需要它

          var mongoose = require('./db')
    

    所以你将拥有:

    • 单连接
    • 所有模型都加载到一个地方
    • 清除代码 迁移

    【讨论】:

      【解决方案2】:

      您也可以尝试我的 migrate-mongoose 迁移框架,它提供开箱即用的 mongoose 连接。

      在您的 updown 函数中,您可以像这样访问您的模型

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

      它还将您的迁移持久化到数据库而不是文件系统。

      【讨论】:

        【解决方案3】:

        您还有非常强大的东迁移框架,它也有 mongoDB 适配器: https://github.com/okv/east

        然后你将使用命令进行 crate 迁移:

        east create my_migration_name
        

        然后您的迁移脚本将如下所示:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-20
          • 1970-01-01
          • 2019-09-16
          • 2017-10-08
          • 2020-04-24
          • 2012-02-09
          • 1970-01-01
          相关资源
          最近更新 更多