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.
You need a proper configuration to deploy the cloud code. You must follow the following steps.
curl -s https:/www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash
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”.
The folder structure will be right in this order –
Test
cloud
main.js
public
index.html
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.
The most important part in cloud code is webhook.In parse have four types 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.
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.
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.