Exploring server-side programming
Azure Cosmos DB server-side programming consists of stored procedures, triggers, and user-defined functions (UDFs) written in JavaScript.
Implementing user-defined functions
UDFs can only be called from within a query, and they implement custom business logic like calculating tax.
Let’s define a UDF to calculate the sales tax of products:
- In the Azure Cosmos DB Emulator or Azure portal Data Explorer, create a new UDF, as shown in Figure 4.9:
Figure 4.9: Creating a new UDF
- For the User Defined Function Id, enter
salesTax
. - For the User Defined Function Body, enter JavaScript to define the
salesTax
function, as shown in the following code:function salesTax(unitPrice){ return unitPrice * 0.2; }
- In the toolbar, click Save.
- Create a new SQL query and enter SQL text to return the unit price and sales tax for products that cost more than 100, as shown in the following...