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!)
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.
Here an example of what I did so far:
*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();
};
This is why you need a migration tool for MongoDB. We know that mongo is a non-relational database schema-free database, but in most of the projects, we tend to use it with Mongoose, a popular ODM for MongoDB which allows us to add Schema to our collections.
Installing it in your Node.js project is as simple as running the following command: npm install mongodb --save The driver makes connecting to your database, authentication, CRUD operations, and even logging/monitoring as simple as running a command. The driver also supports JavaScript promise/async features.
Add the MongoDB Node.js Driver From the command line/terminal in your project folder, run the following command: npm install mongodb --save This will automatically download the latest version of the driver and save it to your package.json config.
In the Nodejs project, this can be achieved with different libraries like Sequelize and db-migrate, Knex … Out of the three, Knex seems to be the most mature, which best fits the migration requirements.
I just developed this one: https://github.com/eberhara/mongration - you can also find on npm.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With