Fun with Parse’s Cloud Code

Turkish Things utilizes Parse as its backend. I heard about Parse during my iOS course at Flatiron School and thought that it was a super cool idea to get started with an app, with a really short learning curve to start sorting and retrieving data.

I have been building web and PC apps based on relational databases for years. One of the biggest challenges I had with Parse was wrapping my head around NoSQL which Parse works with. I am used to creating relationships using Foreign Keys — which you really don’t do when you’re building apps on a NoSQL database. You instead use “Relations” — where you embed pointers to related objects on other tables.

Everything starts making more sense after writing some test code to see how all this works.

One of the other challenges that this switch to NoSQL brought was the lack of “GROUP BY” queries which I heavily rely on traditional relational databases. On Turkish Things, we have venues, and each venue has multiple reviews. Each review has a star rating, and a venue has an “average rating” which is calculated by:

Average Rating for Venue: Total Ratings / Review Count.

With the lack of AVERAGE, SUM and GROUP BY commands that we use with SQL, I had to come up with an alternative solution. While it’s debatable whether or not this makes the most sense, but I decided to keep two columns on my Venues table named: ratingCount and ratingTotal to be able to calculate the average rating.

There was one problem of updating these fields. When should I increment the ratingCount and calculate a new sum for ratingTotal? Should I do that on the device, every time a new review is inserted to my database? Sounded too costly.

This is where I started learning how to hook Parse’s Cloud Code. Cloud Code is set of JavaScript functions which get triggered based on an action. In my case, I wanted a function to run every time a new review was created. 

This is what the code looks like:

Cloud Code

 

The algorithm first finds the “Venue” that the Venue Review is associated with. Then it increments the ratingCount by one, and then adds the new rating to the ratingTotal variable. This way I can accurately calculate the average rating for each venue on Turkish Things.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Fun with Parse’s Cloud Code

  1. Rather than adding 1 to the ratingCount you received, you should use the increment method. object.increment(‘ratingCount’); This will insure it is atomic and multiple people adding reviews at the same time won’t overwrite the object with the wrong count.

  2. Also, I would move the sum logic for ratingTotal into an afterSave handler on the venue class, for the same reason. Sorry for the double post, didn’t realize that part until after.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s