How to migrate Parse DB to self-hosted MongoDB?

Author: Jose
March 8, 2016
How to migrate Parse DB to self-hosted MongoDB?

Overview

Parse, a back-end service provider for mobile applications, has officially announced its retirement and will be completely phasing out its deployment on January 28, 2017. Unfortunately, this might be one the worst nightmares for SMEs who very much rely on Parse Server. Turning out to be an open source platform, Parse Server lets you to switch the Parse API to any infrastructure or data base which can host Node.js applications.

Following is a migration guide on how to migrate Parse DB to self-hosted MongoDB.

Prerequisites

Node >=4.3
MongoDB version 2.6.X or 3.0.X
Python 2.x (For Windows users, 2.7.1 is the required version)
For deployment, an infrastructure provider like Heroku or AWS

Migrate Parse DB to Self-Hosted MongoDB
– Using your own MongoDB instance and infrastructure will help ensure that your queries will run at their highest level of performance. By migrating your database, you will be able to:
Backup your entire database periodically and on demand.
– Restore backups.
– Increase performance of queries (and thereby your app) by providing larger amounts of dedicated processing power and memory to your database.
– Eliminate risk of another app impacting the performance of your app’s queries.
– Gain raw access to your application’s data.
– Modify, add, or fine-tune indexes on your most popular or complex queries.

Use the database migration tool to transfer your data from parse.com ( App Settings → General → Migrate to external database ). Add your mongodb url in the textbox.

Make sure the url contains admin privileges.

A new collection will be added to your database specified in mongodb url. Make sure it contains all the data as in parse.com.

Now you can use this collection for further querying.

Set Up Local Parse Server (nodejs and express)

We are using an express application with parse-server module. Earlier the url contains domain name as ‘api.parse.com’, but now we can change it to our own domain name (localhost too).

Create an new express application using express generator if you didn’t have any express application. Make sure the nodejs version is >=4.3

Follow the below steps:

1. Install parse-server module using npm (version >=2.1.4)
npm install -g parse-server
2. Create a new folder named ’cloud’ in the root directory. Add a new file ‘main.js’ to this folder. This file will contain all the parse defined functions.
3. Create an instance of Parse Server in your server file (app.js)
var api = new ParseServer({
databaseURI: ‘<mongodb://your.mongo.uri>’,
cloud:’’,
appId: ”,
fileKey: ”,
masterKey: ”,
serverURL: ‘http://localhost:1337/parse’
});
4. If you are using Parse Hosting, you can migrate all these web endpoints to the same Express app that is serving Parse Server. For example, you could mount Parse Server under /parse and your website at the root, like so:
app.use(‘/parse’, api);
So that all the parse defined functions will be loaded in the url with ‘http://localhost:1337/parse’.
5. We will now migrate your existing Cloud Code to run in Parse Server. Copy your app’s Cloud Code to the application ‘cloud/main.js’

A sample function is given below:

Parse.Cloud.define(“list”, function(request, response) {
var query = new Parse.Query(‘TableName’)
query.find({
error: function(err) {
response.error(err)
},
success: function(res) {
response.success(res)
}
})
});
This will list all the data inside the collection ‘TableName’

6. Now the express app is all set to run the parse query.
Run the application ‘http://localhost:1337’ in browser and check whether its
running.
7. Run the query
curl -X POST -H “X-Parse-Application-Id: YOUR_APP_ID” \
http://localhost:1337/parse/functions/list
8. Now you will see the list of data inside the collection.

 

I hope this blog provides you an idea about migrating Parse DB to self-hosted MongoDB. If you are struggling with Parse DB migration or you want to migrate to AWS or any other data base or cloud server, shoot an inquiry right away to our most expert architect, Jerry Don, at jerry@toobler.com