Announcing Parse Server 4.4
Parse Platform nurtures a vibrant open source community reaching +33K stars on Github’s official repo. According to npm, the number of Parse Server installations grew up over 50% in the last 12 months and reached over 30K per week. Parse Javascript SDK installations are already +100K per week, growing up over 50% in the previous 12 months.
Today we are pleased to announce that the most recent Parse Server version, the 4.4, is available on Back4App (How to upgrade Parse Server). This is a big release with a new cloud code validation feature, a new afterLiveQueryEvent
trigger, and fixes/improvements on GraphQL API, security, auth, and more. For a detailed change list, please visit the repo release page.
On the next topics, I’m going to deep dive into some of those changes showing practical examples.
Contents
Cloud Code Validators
Now it’s possible to add validators to your Cloud Code Functions. The validator can be specified as an object or another function and is called before the cloud code function. It can be used to ensure your cloud function will receive all the necessary parameters, and they’ll come on the correct datatype.
Suppose you need to retrieve all Dogs older than a certain age
. The attribute age
is mandatory because you need a limited age to retrieve records above that given value. If no value is provided, you cannot determine which dogs are older. So, to ensure the age parameter is sent in, use this example:
The same logic can be used if you need to ensure more than one parameter is sent. In this example, we will retrieve dogs between two ages, so, logically, you will need to have two values passed the minimumAge
and the maximumAge
. Parse Cloud Code Validators support multiple parameters to ensure all are met, or the code will not run.
Sometimes you need more complex validation. For those cases, validators allow you to write business logic that will validate (or not) any given parameter. If your validation code returns true, it will consider the parameter as valid. If false, it will consider your parameter invalid. In this example, we will try to find all dogs belong to a specific owner. We need to ensure a name
parameter is given (required), and also, it has a certain length
, as “A” is just not a valid name.
Let’s consider 3 as the minimum length for a name (as Joe, for instance).
The parameter “owner” will be a String, it is required (or the code won’t run), the options are the javascript code that will validate the parameter, so let’s return TRUE if its length is 3 or more.
If you want more details about the Validation please check the official Parse Platform documentation.
After Live Query Event
Additionally to BeforeConnect
, BeforeSubscribe
and OnLiveQueryEvent
, we have now available the AfterLiveQuery trigger. This new event trigger allows manipulating the results of a live query before sending it to the user.
For instance, let’s say you have a Dog class and when creating a new Dog, that object must have its name property set to Fido
. Also, when updating an existing Dog, that object must have its name
property set to Rex
. You could use the Cloud Code below to achieve that functionality:
You must remember that Live Queries are meant to deliver real-time data to the clients, so, you have to make sure all Cloud Code inside that Event is very efficient and as fast as it can be, otherwise clients will experience delays in data delivery, as the Live Query events will NOT be triggered until the afterLiveQueryEvent
has finished running.
Other Cloud Code Improvements
SkipWithMasterkey
It’s also available on this release other Cloud Code improvements like the parameter skipWithMasterKey
that can be used on Parse triggers. This change allows for triggers to be skipped if a master key is provided, and if the parameter: skipWithMasterKey
is true.
Improvements on Logs
Duplicated functions
If you create two cloud functions with the same name, the first will be ignored, which is expected behavior. But it can lead to headaches if your main.js is large and you accidentally name a function the same or redefine a cloud trigger. This just adds a simple console.log on server start if there are duplicated cloud functions, so developers can understand more what’s happening:
Warning: Duplicate cloud functions exist for trigger beforeSave on class TestObject. The first will be ignored.
Detailed Error Stack
This improvement makes it easier to trace your Cloud Code errors. Now you’ll see the specific file, line, and column that threw a particular error.
For the following code:
You’ll see the Log Error below:
error: Parse error: yolo is not defined {"code":141,"stack":"ReferenceError: yolo is not defined
at /cloud/main.js:3:5
at /node_modules/parse-server/lib/Routers/FunctionsRouter.js:199:16
at processTicksAndRejections (internal/process/task_queues.js:93:5)"}
If you don’t know how to use the latest version please check this guide.
Other article you may be interested are CLI for Parse Server, Parse Analytics, and Costs to run Parse on AWS.