使用node.js运行数据库迁移(mongodb) [英] Run database migration (mongodb) with node.js

查看:224
本文介绍了使用node.js运行数据库迁移(mongodb)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个进行mongo数据库迁移的节点模块.到目前为止,我发现了mongo-migrate,但还不够强大. (总比没有好,但是我需要更多,我习惯了使用真正强大的Ruby迁移!)

I'm looking for a node module to make mongo database migrations. So far I found mongo-migrate, but not really powerful enough. (Better than nothing but I need more, I'm used to use the Ruby migration which was really powerful!)

几周前,我又发现了一个强大的功能,但不处理mongoDb,仅处理MySQL,PostGre等.

I found another one few weeks ago, powerful but doesn't deal with mongoDb, only with MySQL, PostGre and so on.

您知道一个模块或可以帮助我的东西吗?我的意思是,我不是第一个要处理数据库迁移的人,您如何管理它?我的项目很大,我需要控制.

Do you know a module or something that could help me? I mean, I'm not the first person to want to deal with DB migrations, how do you manage that? My project will be big and I need control.

以下是我到目前为止所做的一个示例:

* 0010-init_category_table.js *

*0010-init_category_table.js*

var mongodb = require('mongodb');

exports.up = function(db, next){

    var documentName = 'category';
    var collection = mongodb.Collection(db, documentName);
    var index;
    var indexOptions;

    /**
     * Create indexes.
     */
    index = { "code": 1 };
    indexOptions = { unique: true };
    collection.ensureIndex( index, {unique: true, w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [ensureIndex] ' + JSON.stringify(index) + JSON.stringify(indexOptions));
    });

    index = { "name": 1 };
    indexOptions = { unique: true };
    collection.ensureIndex( index, {unique: true, w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [ensureIndex] ' + JSON.stringify(index) + JSON.stringify(indexOptions));
    });

    /**
     * Create basic data.
     */
    collection.insert({
        code: 'a',
        name: 'languageStatus'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });
    collection.insert({
        code: 'b',
        name: 'accessName'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });
    collection.insert({
        code: 'c',
        name: 'roleName'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });
    collection.insert({
        code: 'd',
        name: 'translationStatus'
    }, {w: 1}, function(error, data){
        console.log(error ? error : documentName + ': [insert] ' + JSON.stringify(data));
    });

    /**
     * Display index information.
     */
    collection.indexInformation(function(error, data){
        console.log(error ? error : documentName + ': [indexes] ' + JSON.stringify(data));
    });

    next();
};

exports.down = function(db, next){
    var documentName = 'category';
    var document = mongodb.Collection(db, documentName);

    var query = {
        $or: [
            {name: 'languageStatus'},
            {name: 'accessName'},
            {name: 'roleName'},
            {name: 'translationStatus'}
        ]
    };
    document.find(query, function(error, data){
        data.each(function(error, data){
            document.remove(data, {w: 1}, function(error, number){
                console.log(error ? error : documentName + ': [remove] (' + number + ') ' + JSON.stringify(data));
            })
        });
    });

    next();
};

推荐答案

我刚刚开发了这个: https://github.com/eberhara/mongration -您也可以在npm上找到.

I just developed this one: https://github.com/eberhara/mongration - you can also find on npm.

我们需要一个用于mongodb的好的节点迁移框架,但找不到任何框架-因此我们构建了一个框架.

We needed a good node migration framework for mongodb, but could not find any - so we built one.

它具有比常规迁移框架更好的功能:

It has lots of better features than the regular migration frameworks:

  • 校验和(如果以前运行的迁移与其旧版本不匹配,则会发出错误消息
  • 坚持迁移到mongo的状态(没有常规状态文件)
  • 完全支持副本集
  • 自动处理回滚(开发人员必须指定回滚过程)
  • 能够同时运行多个迁移(同步或异步)
  • 能够同时对不同的数据库运行迁移

这篇关于使用node.js运行数据库迁移(mongodb)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆