Table of Contents
Parse.com is a Baas service. Baas enables us to create smartphone application without building complicated server. And Parse.com can be used for free if our request and auto jobs are not so many.
I created one Cloud Code there. Here’s what I did.
環境
- Ubuntu 15.04
What I Wanted to Do
Set another column value on receiving request from iPhone application.
Procedure
Parse.com Cloud Code can be deployed to 2 environment. Whichever will do.
- Parse.com
- Heroku
I chose Parse.com.
Setup
First, I needed to setup developing environment. I installed parse
command, according to Parse.com document.
1 2 3 4 5 6 7 8 |
$ curl -s https://www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash [sudo] password for the_user: Fetching latest version ... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 20 100 20 0 0 24 0 --:--:-- --:--:-- --:--:-- 24 ######################################################################## 100.0% Installing ... |
The way how to use parse
command is on Parse.com document, or you can check it by executing parse --help
.
Next, prepare the working directory by parse new
. And account information, application for deployment, deployment destination (Heroku or Parse) and working directory in your local machine were required. At last, you should choose initial directory structure, blank or skeleton. I chose latter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
$ parse new We've changed the way the CLI works. To save time logging in, you should create an account key. Type "parse configure accountkey" to create a new account key. Read more at: https://parse.com/docs/cloudcode/guide#command-line-account-keys Email: your@email.com Password (will be hidden): Would you like to create a new app, or add Cloud Code to an existing app? Type "(n)ew" or "(e)xisting": e 1: ABC 2: DEF 3: GHI 4: JKL Select an App to add to config: 4 Which of these providers would you like 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 "JKL" Directory Name: trigger_test 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: Your Cloud Code has been created at /home/the_user/current_directory/trigger_test This includes a "Hello world" cloud function, so once you deploy, you can test that it works, with the printed curl command. Next, you might want to deploy this code with: cd /home/the_user/current_directory/trigger_test parse deploy Once deployed you can test that it works by running: curl -X POST -H "X-Parse-Application-Id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "X-Parse-REST-API-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" -d '{}' https://api.parse.com/1/functions/hello |
Then, the working directory was created at your current directory. It is similar to rails new
command.
Development
In my case, before save trigger was required and I used beforeSave
. There are other methods for creating trigger, beforeSave
, afterSave
, beforeDelete
, afterDelete
.
The cloud code should be written in JavaScript. And the first argument for beforeSave
is the class name, which is we want to modify.
Function defined by beforeSave
is, of course, executed before saving. It is executed when we change record on web browser and when we insert or update from other application.
The code should be written in main.js
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Parse.Cloud.beforeSave("Example", function(request, response) { var list = { "X":{ "a":"1", "b":"1", }, "Y":{ "a":"2", "b":"2", }, }; try { var valueString = request.object.get('value'); if (valueString in list) { var dataSet = list[valueString]; request.object.set("a", dataSet["a"]); request.object.set("b", dataSet["b"]); } } catch (e) { console.error(e.message); } response.success(); }); |
Explanation for the Function
Change data on saving Example
data.
If the value of value
field is in predefined list
, it changes the values of a
and b
fields.
For preventing stop for exception, I added try and catch.
console
is a utility for writing log. console.log
, console.error
, console.warn
methods are available. The log they output can be seen on log page in Parse.com. And, console.error
and console.warn
write error log, too. The log can be also seen be parse logs
command. Apart from that, trigger execution log is written automatically.
Deploy
Deploy with parse deploy
command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ parse deploy We've changed the way the CLI works. To save time logging in, you should create an account key. Type "parse configure accountkey" to create a new account key. Read more at: https://parse.com/docs/cloudcode/guide#command-line-account-keys Email: your@mail.com Password (will be hidden): Uploading source files Uploading recent changes to scripts... The following files will be uploaded: /home/the_user/current_directory/trigger_test/cloud/main.js Finished uploading files New release is named v5 (using Parse JavaScript SDK v1.6.14) |
After deployment, the trigger automatically start working.
Log
Here’s log and error log example.
1 2 3 4 5 6 7 8 9 10 11 |
I2016-01-22T08:56:05.763Z]Deployed v3 with triggers: test: before_save Cloud Functions: Example E2016-01-22T08:57:02.850Z]v3 before_save triggered for test as master: Input: {"original":null,"update":{"value":"X"}} Result: TypeError: "Error Message" I2016-01-22T09:02:38.107Z]v4 before_save triggered for test as master: Input: {"original":{"createdAt":"2016-01-22T09:02:36.452Z","objectId":"OQ3dAPSrtr","updatedAt":"2016-01-22T09:02:36.452Z","value":"Y"},"update":{"value":"X"}} Result: Update changed to {"a":"1","b":"1"} |
At Parse.com, we can setup job like cron, after defining the method as I did like above.