[英]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: 它具有比常规迁移框架更好的功能:
看一下https://github.com/emirotin/mongodb-migrations,它似乎更具有功能丰富,成熟和维护的特点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.