Appery.io Server Code enables mobile developers to write any custom app logic that is automatically exposed via a REST API. You develop the script using JavaScript which is nice because you don’t have to learn a new language. The Server Code provides built-in APIs to make it simple to integrate with other Appery.io services. For example, you can query the Appery.io Database, send a push notification, and invoke any 3rd party REST API service.
For example, you want to develop a script that will query a products database. If an inventory for a particular product falls below a set number, you want to send a push message to the manager notifying him or her that inventory is low.
This is not something you want to check manually, this is something you want to automate. Running the script periodically is actually very simple once you have a finished script. I’m going to show you how to do that starting with the script first.
var databaseId = "5625aaf....."; var collectionName = "Products"; var result = {}; var params = {}; params.criteria = { 'quantity': { "$lt": 5 } }; result.query = Collection.query(databaseId, collectionName, params); var msgText = []; var productCount = result.query.length; console.log(result.query); if (productCount !== 0) { for (var i = 0; i < productCount; i++) { msgText.push(result.query[i].name + ": " + result.query[i].quantity); } PN.send("bb26439d.....", { "message": msgText.toString(); }); } response.success(result.query, "application/json");
Let’s review the script, it’s very simple:
- Line 1 & 2: you set the database API key and the collection name.
- Line 5: you set the query. This query will check if quantity for any product is less then 5.
- Line 12: is where you query the database using the built-in Server Code API.
- Because a number of products can have a low inventory, you iterate over the result and create the message to send.
- Line 23: you send a push message. The id is Push API configured for an app that the manager installed.
- At the end the script returns a response.
You can test the script but first let’s look at the database so you know which product has low inventory. This is the database:

Based on the query parameters on line 8, Coffee has low inventory (less than 5). When you test the script, this is the result you will see:

The result contains Coffee product which has low inventory.
You want this script to run periodically and automatically, say once a day without any user action. You can very easily schedule this script to run once a day via Jobs.
Switch to Jobs tab where you can schedule the script to run periodically:

At the top of the page you configure the schedule. In the table below you will see all the scripts that have been scheduled. Use the toggle to set the schedule to run (or not). You can click on the link in Last result to see the the log trace for the last script execution.
Server Code enables you quickly to expose any app logic via a REST API. With Server Code Jobs, you can quickly schedule the script to run periodically.
Leave a Reply