简体   繁体   English

使用node.js运行数据库迁移(mongodb)

[英]Run database migration (mongodb) with node.js

I'm looking for a node module to make mongo database migrations. 我正在寻找一个进行mongo数据库迁移的节点模块。 So far I found mongo-migrate , but not really powerful enough. 到目前为止,我发现mongo-migrate ,但功能还不够强大。 (Better than nothing but I need more, I'm used to use the Ruby migration which was really powerful!) (总比没有好,但是我需要更多,我习惯于使用真正强大的Ruby迁移!)

I found another one few weeks ago, powerful but doesn't deal with mongoDb, only with MySQL, PostGre and so on. 几周前,我又发现了一个强大的功能,但不处理mongoDb,仅处理MySQL,PostGre等。

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. 我的项目很大,我需要控制。

Here an example of what I did so far: 这里是我到目前为止所做的一个例子:

*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();
};

I just developed this one: https://github.com/eberhara/mongration - you can also find on npm. 我刚刚开发了一个: https : //github.com/eberhara/mongration-您也可以在npm上找到它。

We needed a good node migration framework for mongodb, but could not find any - so we built one. 我们需要一个适用于mongodb的良好节点迁移框架,但找不到任何框架-因此我们构建了一个框架。

It has lots of better features than the regular migration frameworks: 它具有比常规迁移框架更好的功能:

  • Checksum (issues an error when a previosuly ran migration does not match its old version) 校验和(如果先前运行的迁移与其旧版本不匹配,则会发出错误)
  • Persists migration state to mongo (there is no regular state file) 保持迁移到mongo的状态(没有常规状态文件)
  • Full support to replica sets 完全支持副本集
  • Automatic handle rollbacks (developers must specify the rollback procedures) 自动处理回滚(开发人员必须指定回滚过程)
  • Ability to run multiple migrations (sync or async) at the same time 能够同时运行多个迁移(同步或异步)
  • Ability to run migrations against different databases at the same time 能够同时针对不同的数据库运行迁移

看一下https://github.com/emirotin/mongodb-migrations,它似乎更具有功能丰富,成熟和维护的特点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM