Migrating from Parse: Moving Apps from Parse.com to Hosted Node Server

In this mobile first world, it’s BaaS (Backend as a Service) that fuels fast-paced development and deployment. Whether it is for prototyping or building a production-ready mobile application, BaaS lets you configure and set up your backend APIs and other services, like push notifications, periodic jobs, REST APIs for CRUD operations and many more, in a matter of a few hours.

Parse was one of the popular choice in this segment. With its wide set of libraries, mobile SDKs, and a lot of other options, Parse had become one of the most preferred BaaS service for mobile apps in the last couple of years.

Unfortunately, with Parse to be fully retired on Jan 28, 2017, we have to think of migrating the apps hosted on Parse.com with the least possible rewrites.

Migrating Your Mobile Apps from Parse.com

Parse.com suggests using Node module parse-server for hosting Parse applications in your own infrastructure. Parse server is an open source version of the Parse backend. It is independent of the hosted Parse backend and uses MongoDB as its datastore.

Let’s take a look at setting up Parse app in our own infrastructure (say, an AWS instance on Linux server). The main steps would be:

1. Migrating the Parse database to MongoDB
2. Setting up Parse Server

Prerequisites for setting up Parse Server

    • Node 4.1
    • MongoDB version 2.6.X or 3.0.X

Installation

npm install -g parse-server

Database Export: Migrating Data from Parse to MongoDB

As the initial step, let’s migrate the Parse database to a MongoDB instance. Parse.com provides two different options for data migration.

    • We can export data with a button click and then a download link would be sent to the registered email address.

Migration from Parse to MongoDB: Export Data Option

In this case, we will receive JSON data files for each class in Parse database, which can then be exported to MongoDB as shown below:

unzip export-data.zip                                                   (Exported data)

Each of these JSON files can be imported to the MongoDB collection as follows:

mongoimport –db <dbName> –collection <collectionName> <  <fileName.json>

    • Alternatively, we can directly migrate data from Parse to remote MongoDB instance.

Migration from Parse to MongoDB: Direct Migration

For example, if we are using MongoLab, we would have a database connection string like:

mongodb://<dbuser>:<dbpassword>@ds055525.mongolab.com:55525/migrationapp

Once the data is migrated, we need to set up a Parse server to run the Parse application.

Setting up Parse Server

Let’s use a Linux box for setting up the Parse server.

We can use parse cli option to deploy code from existing Parse application to the selected directory, as demonstrated below:

After the codebase is moved to the specified location, we need to install npm dependencies.

$ npm install parse-server

$ npm install express

With the dependencies installed, let’s create the server.js file that will bootstrap our Parse application.

Here is the required server.js file.

 

We can start the server with:

$ node server.js

parseserver-example running on port 9000.

We have now successfully set up the Parse server in our Linux box.

Parse server does not necessarily require client key, JavaScript key, .NET key, and REST API key. An application ID is sufficient to secure your app, but if you provide these keys in server.js, the same has to be passed in the REST APIs and SDKs used.

Or else, the Parse server will throw up an unauthorized error as it would in hosted Parse.com backend.

Verification of the New Parse Server

Let’s verify the newly set up Parse server and see if it is working as expected by creating a new class user (collection in MongoDB terms). Do note that our Parse server is accessible via route /parse (unlike in hosted Parse.com) as we have specified so in server.js.

Response

As the new class (collection) user has been created, let’s validate fetching of data from user class using the below REST API.

Response

If you are receiving the above response, it means the Parse server is correctly set.

Cloud Code

If your parse application has cloud functions, it should be placed in your app’s Cloud Code directory (<parseMigrationApp>/<parse-migration-app>/cloud).

Below is a sample use of cloud functions:

REST API Call

Response

 

Please note that the URL format of REST API expected for cloud functions is /parse/functions/<cloud-function-name>, instead of /1/functions/<cloud-function-name> as in hosted Parse.com backend.

Moreover, you will need to replace any relative paths like ‘cloud/…’ to <directory-path>+‘/cloud/…’, in all requires used in cloud code files.

A lot of cloud modules are also not supported. So, you will need to replace them with corresponding Node modules. Below is the list of unsupported cloud modules and their corresponding options (npm modules).

In Node, Buffer is natively supported. So, remove any require(buffer) calls.

Also, since Jobs are not supported, remove all job definitions from cloud code. These can be set up as cron jobs in server, wrapped in cli script.

Using Parse SDKs with Newly Set Up Parse Server

To use a Parse SDK with Parse Server, you will need to change the server URL to your Parse API URL. (Make sure you have the latest version of the SDKs.)

For example, if you have Parse server running locally mounted at /parse:

iOS

Android

JavaScript

This post covers the steps for migrating a Parse application to a node server and moving its database to MongoDB. If you are planning to migrate your application to PaaS services like Heroku or any other cloud service, the links below could be helpful in setting up the Parse server.

 

You May Also Like