How to import sails.js disk.db into mongodb

So I have a nodejs app that uses the sailsjs web framework. While I am developing, I have an adapter mounted on a sail-disc. Sample data from disk.db

is similar to

{
    "data": {
        "authentication": [
            {
                "user_id": 1,
                "username": "user1",
                "encrypted_password": "password here",
                "createdAt": "2014-05-12T07:40:24.901Z",
                "updatedAt": "2014-08-18T19:37:22.851Z",
                "id": 1,
                "email": "user1@email.com"
            }
        ],
        "user": [
            {
                "name": "user0001",
                "email": "user1@email.com",
                "role": [
                    "admin"
                ],
                "phone": [
                    {}
                ],
                "status": "Active",
                "createdAt": "2014-05-12T07:48:11.028Z",
                "updatedAt": "2014-05-24T08:12:41.646Z",
                "id": 1,
                "username": "user1"
            }
        ]
    }
}

      

This works fine on my local machine. Now I am ready to deploy it to production and use mongodb as my database. I have all the connection settings and I can successfully connect to my db.

My question is, is there a way to import my local disk database ( disk.db

) into mongodb while preserving my json data format or collections? Or do I have to manually create my collections on mongodb and import each collection?

+3


source to share


3 answers


There is no tool or official way to accomplish this kind of migration with Sails. Depending on how many models you have and how tedious it would be to write a script to carry things around for you, one option is to render your models, that is, if you have User.js

, create a new model UserNew.js

that looks like this:

module.exports = {

    connection: 'mongoDbConnection',
    tableName: 'user',
    attributes: <same as User.js attributes>

}

      

and then in config / bootstrap.js do:



User.find().exec(function(err, users) {
    UserNew.create(users).exec(...);
}

      

transfer them.

It will be a one-time thing, after which you can reset the original User.js

and rename UserNew.js

to User.js

. Quite hacky, but again depending on how much data we're talking about this might be the fastest option!

+4


source


I built the answer from sgress454 and turned it around. Instead of NewUser, I created OldUserLocation.js model.

module.exports = {
    connection: 'localDiskDb',
    tableName: 'user',
    attributes: {
    //same as in User.js
}

      

}

And in the default model I have specified mongo DB.



In the bootstrap function, I added the following:

User.find().exec(function(err, users){
    if(!err && users.length == 0){
        OldUserLocation.find().exec(function(err, oldUsers) {
            if(!err && oldUsers.length >0){
                User.create(oldUsers).exec(function (err){
                    cb();
                });
            }
            else{
                cb();
            }
        });
    }
    else{
        cb();  
    } 
});

      

Simply put, if there are no users in the user model in mongo DB, I try to import from localdisk db. If it already has users, I assume the import is already done.

0


source


Another hacky way I see it is:

  • open .tmp/localDiskDb.db

  • copy object
  • run a POST request using Postman https://www.getpostman.com/apps
  • set a POST request
  • set the endpoint url in the body tab, select raw, JSON paste the object in the box
  • click send

rince, repeat

0


source







All Articles