Azure Cosmos DB User-defined functions
User-defined function can be used inside a query.
function tax(income) {
if (income == undefined) throw "no input";
if (income < 1000) return income * 0.1;
else if (income < 10000) return income * 0.2;
else return income * 0.4;
}
You have to register it using the SDK:
await client
.GetContainer("database", "container")
.Scripts
.CreateUserDefinedFunctionAsync(new UserDefinedFunctionProperties
{
Id = "Tax",
Body = File.ReadAllText(@"..\js\Tax.js")
});
and then you can use it:
var iterator = client
.GetContainer("database", "container")
.GetItemQueryIterator<dynamic>("SELECT * FROM Incomes t WHERE udf.Tax(t.income) > 20000");
while (iterator.HasMoreResults)
{
var results = await iterator.ReadNextAsync();
foreach (var result in results)
{
//iterate over results
}
}
Backlinks
AZ-204 - Developing Solutions for Microsoft Azure
- [[azure-cosmos-db-stored-procedures]], [[azure-cosmos-db-triggers]], [[azure-cosmos-db-user-defined-functions]]
Azure Cosmos DB
See [[azure-cosmos-db-stored-procedures]], [[azure-cosmos-db-triggers]], and [[azure-cosmos-db-user-defined-functions]].