Getting Started with Parse

I am a big fan of Parse, a technology that makes developing web and mobile apps faster. It lets you store, sync and push data in the cloud without writing a line of server-side code. Read on to see how you can integrate Parse in your next project.


A Bit of Background

Interacting with the database is extremely painless!

Let's take a step back and evaluate where web development was five years ago. We were using languages such as PHP/Ruby on the back-end that interacted with relational databases (such as MySQL), and our front-end was littered with various JavaScript files. I don't know about you, but most of my projects were on some server with a small amount of disk space, RAM and bandwidth.

A lot of new technologies leverage the cloud, and it's not uncommon to see developers taking advantage of these cost-effective services, such as AWS, Amazon S3, Heroku, and more. The introduction of these services essentially altered the web development landscape; more of what we do resides in the cloud.

Today, we're going to look at Parse, a startup that offers a cloud-based data management system. By leveraging their service, I was able to rapidly create new mobile and web apps. We're going to use Parse to create the obligatory To-do web application, and I hope it gives you a taste of what the service can offer.

Envato Todo List App

What's Parse?

Simply put, Parse is a cloud-based data management system that allows you to quickly develop web and mobile apps.

It makes developing apps really easy by taking the back-end out of your hands.

So, how's this work? Well, let's assume you're a web developer. You can add the Parse JavaScript file on your page, get an API key, and start saving "objects" in the cloud with only a few lines of code. Parse frees you from setting up a server-side stack.

Think about this for a minute. Traditionally, you set up a server-side stack (LAMP, or RoR, ASP.NET, or something else), set up a database, and then interact with it via Ajax on the client. Parse just reduced all that work to a few lines of code.

In this tutorial, we'll use Parse's JavaScript SDK. You're not limited to using only JavaScript, however; there are Parse libraries in many different languages, including PHP, NodeJS, Java, C# and more. You can find all the available libraries here.


Using Parse for Your Web-based Projects

Before we start, let's take a minute and think how a traditional To-do list app could be created using a LAMP stack.

  • You would create a MySQL database.
  • You may have a PHP class that is responsible for performing CRUD operations. Optionally, you could just have a bunch of PHP functions.
  • You may use JavaScript and Ajax on the client-side to call the respective PHP scripts and pass in query strings.
  • You would need to sanitize the input to protect against XSS attacks, as well as worry about database security in general.
  • If a collaborative app, you would need to track different users and manage their lists. More code, more tables, and more schemas.
  • You would need to make sure your database stays performant.

You get the idea. There's a lot to think about and many areas to make mistakes. Parse handles these issues for us.

Create a Parse Account

Before you do anything, create a free Parse account. Then create a new app called EnvatoTodo.

Create a new Parse App

Download the Empty Project

Parse provides a great QuickStart guide to help you get started. Select JavaScript and choose EnvatoTodo from the drop down menu in step 2 (shown in the image below). Parse will generate a zip file that contains the Parse SDK and index.html.

Parse Quickstart Guide

The Client-Side

Before we start interacting with Parse, let's set up a basic client-side structure for our application. Since the UI is not the focus of this application, I'll just show you the code that I used. It's nothing fancy, but I'm using YUI3. You could optionally use jQuery. Everything is in index.html.

The important thing to note in the above code is the inclusion of the Parse JavaScript file, <script src="http://www.parsecdn.com/js/parse-1.0.19.min.js"></script>. This file contains the Parse object that we will interact with.

Add Classes in the Data Browser

Parse frees you from setting up a server-side stack.

Let's get back to Parse. View the Data Browser for EnvatoTodo (it's currently empty). The Data Browser is similar to a MySQL database server, but the Parse DB is schema-less. While you can specify columns in the Data Browser, you can actually save any object with simple JSON (not unlike many NoSQL/Document databases). This is an extremely important and useful feature of Parse.

We're going to go ahead and add a "class" to the database. Think of a class as a table. The reason it's called a class is because you'll generally create objects from a certain class and then save that object to the database.

Disclaimer: JavaScript does not officially have classes (yet), but it does have the logical equivalent. For the sake of simplicity, I will use the terminology "class" in this tutorial.

Parse has five types of classes.

  • User classes store user-specific information, and Parse provides sugar methods such as signUp(), login(), and more to help with user administration.
  • Installation classes are typically used to send push notifications for mobile apps. Yes, Parse supports push notifications to all clients.
  • Role classes help segregate users into specific roles, controlling access to read/write to other classes. This is called ACL (access control list) within Parse.
  • Product classes store in-app product data.
  • Custom classes are for anything else.

Let's create a custom class called ListItem. Once it's created, you'll notice it already has four properties. Parse automatically updates the properties of every class that you create. Some classes, such as user classes, may have their own specific properties.

Our ListItem objects will contain a content property to show what the Todo contains, and an isComplete property to indicate whether the Todo was completed. We could add columns for these properties, but Parse will figure it out from our JSON.

Initializing Parse

Let's go back to index.html to add some code. The first thing we want to do is initialize Parse with our App ID and JavaScript key. These can be found under the Overview tab of your application inside the Data Browser. Add the following line to your JavaScript, replacing APP_ID and JS_KEY with the appropriate values:

Saving Todos

Let's start adding functionality to our application. We'll first save a To-do list item when the user clicks the add button. The following code is all we need for this functionality:

Interacting with the database is extremely painless! Every custom class in Parse inherits from Parse.Object; therefore, ListItem is a subclass. The string argument "ListItem" tells Parse that this class correlates to the ListItem table that we created. Then we create a new ListItem object, set its properties, and call save().

Showing Items

In order to display a list of To-do items, we'll use the Parse JavaScript API to query the ten most recently saved items, and list those below the app. This will give you an idea as to how queries work in Parse.

Once again, it's fairly simple and easy to read. The Parse.Query() method is quite powerful. Here, we perform a fairly basic query, but Parse queries can be complex as well. You can query for specific regular expressions, perform relational queries, and many more. Be sure to visit the Query Documentation for more examples and code snippets.

Putting it all together

The next thing we need to add is the ability to mark a to-do item as complete when checking the item's checkbox. Each checkbox has a unique id associated with the Parse object that it represents. Therefore, when a checkbox is clicked, we need to:

  • Get the id.
  • Query for a Parse object with that id.
  • Get the returned Parse object, and update it's isComplete property as true.
  • Save the updated object.
  • Remove it from the displayed list.

Here's what that code looks like:

In this snippet, we query for an individual element with a specific id, update it, and save it. Then we remove it from the list with self.remove().

In Parse, updating objects is very similar to saving objects--we call save() in both cases. Parse figures out whether it's an existing (dirty) object, or an entirely new object and performs the appropriate action.

Complete Source Code

With that, we're good to go! Pretty easy, eh? Download the attached ZIP file and open index.html to view the entire source code.


Other Features

In this tutorial, we primarily looked at Parse from a web development point-of-view. However, the service was originally started with mobile developers in mind. Parse has a very thorough SDK for Android and iOS, and it supports features such as push notifications to all users of your mobile app.

Some other big features that we didn't cover in this tutorial involve users, roles, and storing relational information.

Parse makes it relatively simple to create users, and assign different roles to them. For example, in a learning management system, you may have "Teachers", "Students", and "Administrators". By assigning roles, you can specify which type of user has access to certain Parse objects.


What about Security?

A lot of new technologies leverage the cloud...

As a developer, I like that Parse takes care of a lot of the security concerns that I have. As it states on the website, the Parse SDKs are designed "so that you typically don't need to worry about how data is saved." There's still the issue of users having access to information that they shouldn't be able to. To address this concern, Parse offers Object-level and Class-level permissions.

Object-level permissions allow a developer to specify an array of object IDs that have access to read and/or write the object. Class-level permissions allow a developer to specify what aspects of a Parse class can be manipulated from the API. The following operations can be allowed or denied on a per-class basis: Get, Find, Update, Create, Delete, and Add Fields.


Pros and Cons

I want to stress is that Parse is not suitable for every type of project. Although the free plan is very generous, Parse is a paid service. It can get expensive if you go above certain limits. Luckily, the pricing model is very transparent, and you should be able to figure out how much your app can cost. Generally speaking, I use it for smaller projects where I can anticipate a certain cap when it comes to the number of API requests that I make. I have yet to try Parse for a large project.

One common issue with a service such as Parse is the lock-in effect.

If you use Parse for a small project that suddenly takes off, it can be difficult to move to a difference service or platform. As you can imagine, replacing Parse with your own back-end would entail a significant amount of refactoring. This doesn't mean you shouldn't use Parse, but it's something to keep in mind.


Concluding Remarks

In this tutorial, we looked at how we can use Parse to create a relatively simple web application. I've used this service for a number of projects for various clients, and I find the Parse team to be very helpful when issues arise. I encourage you give the service a try, and make your own decision!

Disclosure: I have no relationship with Parse or any of its employees. I'm just a developer who uses their service, and I find it to be a helpful addition to my development workflow.

Tags:

Comments

Related Articles