Code Deployment in Parse

Swarnendu De February 10, 2016

Parse.com provides backend services to developers. If your mobile or desktop app requires a backend on the internet, then Parse is one of the options you can choose.

I first came across this service a few months ago and noted that they provide SDKs that let your apps run on various devices connected to the backend or server. They provide SDKs for devices running iOS, Android, Windows (Phone) 8, OS X and Javascript.

In this blog, I will explore  the basic steps to deploy code in Parse and how developers gain an advantage with this cloud platform.

How to Deploy the Cloud Code

 

You need a proper configuration to deploy the cloud code. You must follow the following steps.

  1. At first you need to open an account in Parse.The link to the account is https://www.parse.com/
  2. After login,in the Dashboard page you will get an option “Create a new App”.Click on it.
  3. You must give the app a name and create the app.
  4. Then you need to install parse in the local machine.
  5. Open the terminal and paste the following code for installation.
curl -s https:/www.parse.com/downloads/cloud_code/installer.sh | sudo   /bin/bash
  1.  After installing the parse you will receive a comment like ‘parse new’. Then it will show a message such as –

Would you like to create a new app, or add Cloud Code to an existing app?
Type “(n)ew” or “(e)xisting”: e

1: Test
Select an App to add to config:1

Which of these providers would you like to use for running your server code?:
1) Heroku (https://www.heroku.com)
2) Parse  (https://parse.com/docs/cloudcode/guide)
Type 1 or 2 to make a selection: 2

Please enter the name of the folder where we can download the latest deployed Cloud Code for your app “**App name**”
Directory Name:

You can either set up a blank project or download the current deployed Cloud Code.
Please type “(b)lank” if you wish to setup a blank project, otherwise press ENTER:
Successfully downloaded Cloud Code to    “/home/*****/Test”.
Successfully configured email for current project to: “Your email address”.

  1. Now go to the parse project where it is downloaded. If you see two folder in the parse project.

The folder structure will be right in this order –

Test
cloud
main.js
public
index.html

  1. Now write some cloud code inside the main.js.
  2. Then again go into the parse project and inside the terminal command ‘parse deploy’.

First Cloud code

In the main.js file  you will find the first code.

Parse.Cloud.define(‘hello’,function(request,response){
     response.success(‘hello world’)
});

You can run this code in two way.

1. Using curl

 curl -X POST 
      -H "X-Parse-Application-Id: ${APPLICATION_ID}" 
      -H "X-Parse-REST-API-Key: ${REST_API_KEY}" 
      -H "Content-Type: application/json" 
      -d '{}' 
      https://api.parse.com/1/functions/hello

2. Using  javascript function

 Parse.Cloud.run(‘hello’,{},{
                success:function(result){
                     
                 },error:function(error){
                
                 }
 });

Output of the code is ‘Hello World’.

If you want some modification in the code you need to deploy the parse code again.When you deploy  new code,the previous code will be overwritten by the new one.

The ‘APPLICATION_ID’ and ‘REST_API_KEY’ is already provided in the parse website (inside your parse application).

Application->settings->keys.

Here you will get all the keys that you want.

Parse Webhook Application

The most important part in cloud code is webhook.In parse have four types webhook.

  1. Beforesave Triggers.
  2. Aftersave  Triggers.
  3. BeforeDelete Triggers.
  4. AfterDelete Triggers.

Here some example about webhook

1. beforesave webhook

Parse.Cloud.beforeSave("Review", function(request, response) {
                if (request.object.get("stars") < 1) {
                            response.error("you cannot give less than one star");
                } else if (request.object.get("stars") > 5) {
                            response.error("you cannot give more than five stars");
                } else {
                            response.success();
               }
 });

Here is an example of beforesave webhook. In this example you can see that this function executes before any value is saved in the ‘Review’ table. In the review table has ‘stars’ field. User can’t give the star less than 1 and greater than 5.

2. aftersave webhook

Parse.Cloud.afterSave("Review", function(request, response) {
                if(**some logic**) {
                          // some operation
                } else if (**some logic**) {
                             // some operation
                } else {
                            // some operation
                }
});

Here is example of aftersave webhook. In this example you can see that this function executes after any value is already saved in the ‘Review’ table. In this webhook you can use any operation after the ‘Review’ table’s save operation.

3. beforeDelete webhook

Parse.Cloud.beforeDelete(“**table name**”, function(request, response) {
                if(**some logic**) {
                          // some operation
                } else if (**some logic**) {
                             // some operation
                } else {
                            // some operation
               }
});

Here is an example of beforeDelete webhook. In this example you can see that this function executes before any value is deleted from the existing table.In this webhook you can use any operation before the following table’s delete operation.

4. afterDelete webhook

 Parse.Cloud.afterDelete(Parse.User, function(request) {
           var collection = ["userProfileInfo", "loanDetails"];
                    for (var classnumber = 0; classnumber < collection.length; classnumber++) {
                    var query = getQueryObject(collection[classnumber]);
                    query.equalTo("userId", request.object);
                  query.find({
                           success: function(data) {
                            var len = data.length;
                             while (len--) {
                                 data[len].destroy({});
                             }
                    },
                      error: function(error) {
                         console.log(error);
                   }
                });
         }
 });

Here is the example of afterDelete webhook.In this example you see that when any value is deleted from the ‘Parse.User’ table, after the delete of any value corresponding to ‘Parse.User’ table then the other value is also deleted from the ‘userProfileInfo’ and ‘loanDetails’ table.

Parse Job Scheduler

Another important topic is parse job scheduling. We can do lot of job using parse. Some example are as followed.

Parse.Cloud.job("my_job", function(request, response) { 
response.success("Ran scheduled job."); 
});

In the previous code, you can see that a background job is initialized. In the parse website you can see an option “Job”.The previous code is written inside the main.js file and deployed. In job scheduler you can set your job when it will be run.This is very simple that you don’t bother about the operation. Parse is automatically doing this job based on whatever logic is written  inside the ‘my_job’ function.

Parse Advantage

  1. It is cloud database. You can access it from any platform (iOS and Android).
  2. Parse already provides ParseUser class that can be used to store the data related to the user. Also this class has some inbuilt methods like signUpInBackground , loginInBackground, which you can use to provide signup and login functionality in your app without writing much code.
  3. You can easily store your audio, video and image files on parse server using ParseFile class.
  4. The best feature of this platform is that it is free till your app becomes a super hit. Parse starts charging its users when their app hits the Parse server more than 30 times in one second. This means students and freelancers can use this platform to start without bothering about server side coding and deployment charges.

Parse Disadvantage

  1. Parse is a fully cloud based. If you do not have a good net connection it is a huge problem.
  2. If the parse request take more than 10 second time, then you can also give an error like ‘Request time out’.

As per the latest news Parse is going to be shut down the very next year. But they provide   database migration tools, that let you migrate data from your Parse app to any MongoDB database. During this migration, the Parse API will continue to operate as usual based on your new database, so this can happen without downtime.

Second, parse released the open source Parse Server, which let you run most of the Parse API from your own Node.js server. Once you have your data in your own database, Parse Server let your application running without major changes in the client-side code.