My Companies

Uniqueness constraints in Parse Database

Parse is a great service for quickly getting an app out without getting into too much backend/server/database details. Its really easy to quickly set up your database schema and start saving and querying data. However, one of the most important things that it currently misses is the ability to provide constraints on the data. There is no way to say, for example, that two users cannot have the same email addresses.

The best way to impose constraints on data is to use Cloud CodebeforeSave hook to validate the data before it is inserted in the database. Cloud code is really easy to set up and get started with.

Let's see how we can add a simple uniqueness constraint for the database such that a user cannot register a like for a post twice. We have a PostLike that stores post, user pairs for each like. To ensure that a user cannot like the same post twice, before saving each like, we check if the post, user pair already exists and discard the like if it does.