Migrating from Parse.com to MongoDB

You probably read the announcement that Parse.com is closing its hosting service. This is probably a good time to look at firebase, but if you are using my dbabstraction library, you can change to some other backend with a simple change to how you open the database. All the other query and other code remains exactly the same , regardless of the backend. 

MongoLab provide a free tier if if you want to have a go at MongoDB. 

What changes are needed.


You are probably getting the database handle using some code like this. Here I'm picking up my parse api keys from a property store, and getting a handle for my table called 'ColorTable'. 
  // get a parse handle
  var parse = new cDbAbstraction.DbAbstraction(cDriverParse, {
    dbid:"mp",
    siloid:"ColorTable",
    driverob:parseKeys,
  });
  if (!parse.isHappy()) throw 'could not open parse database';

If you want to change to use mongo instead, all you have to do is this - include a different driver library.
  // get a mongo handle
  var mongo = new cDbAbstraction.DbAbstraction(cDriverMongoLab, {
    dbid:"mp",
    siloid:"ColorTable",
    driverob:mongoKeys,
  });
  if (!mongo.isHappy()) throw 'could not open mongo database';

The rest of your app is probably doing some queries, saves etc, but none of that needs to change. 

Migrating the data over

I've just migrated one of my tables - about 6000 rows over. Once you've got your credentials from both Parse and Mongolab, save them to your property store.
/**
* one off setting of my database credentials
*/
function OneTimeSetMyCredentials() {
  
  var store = PropertiesService.getUserProperties();
  
  // from mongolab dashboard
  store.setProperty("mongoLabKeys", JSON.stringify({
    "restAPIKey":"h3UExxxxxuY0Dcz"
  }));
  
  // from parse.com dashboard
  store.setProperty("parseKeys", JSON.stringify({
    "restAPIKey":"uHgHV7xxxxxxxMxxjMT", 
    "applicationID":"axxn8"
  }));

}



And here's all you have to do, after creating a database using the mongolab dashboard.
function copyFromParseToMongo() {

  // key the db keys
  var store = PropertiesService.getUserProperties();
  var parseKeys = JSON.parse(store.getProperty('parseKeys'));
  var mongoKeys = JSON.parse(store.getProperty('mongoLabKeys'));

  // get a parse handle
  var parse = new cDbAbstraction.DbAbstraction(cDriverParse, {
    dbid:"mp",
    siloid:"ColorTable",
    driverob:parseKeys,
    disablecache:1
  });
  if (!parse.isHappy()) throw 'could not open parse database';

  var mongo = new cDbAbstraction.DbAbstraction(cDriverMongoLab, {
    dbid:'xliberation',
    siloid:"ColorTable",
    driverob:mongoKeys,
    disablecache:1
  });
  if (!mongo.isHappy()) throw 'could not open mongo database';

  
  // get the data from parse
  var parseResult = parse.query();
  if (parseResult.handleCode < 0) {
    throw JSON.stringify(parseResult);
  }

  // write it to mongo
  var mongoResult = mongo.save(parseResult.data);
  if (mongoResult.handleCode < 0) {
    throw JSON.stringify(mongoResult);
  }
  
  // do a count
  Logger.log (parseResult.data.length + ' read from parse');
  Logger.log (mongo.count().data[0].count + ' written to mongo');

}

The result

17 seconds later I've switched over to Mongolab!

[16-02-07 02:39:35:937 PST] 6719 read from parse
[16-02-07 02:39:36:062 PST] 6719 written to mongo

[16-02-07 02:39:36:192 PST] Execution succeeded [17.094 seconds total runtime]






More info


You can get the keys of all my libraries here

https://script.google.com/macros/s/AKfycbwZ2Hht93wTNzvRmYINYF7obaOHciBXWcP_wAiEtyGq70_x3cI/exec

For this example you'll need


Here's a few slides on copying between databases from the Do something useful with GAS in 5 minutes series

do something in 5 with gas-8-copy between databases


For more like this, see Google Apps Scripts snippets and Database abstraction with google apps script. Why not join our forumfollow the blog or follow me on twitter to ensure you get updates when they are available. 




Comments