Short Functions
Introduction
LunaQL provides out of the box short functions for performing things such as calculations or changing your properties by mutating the data of your collections on query runs.
Mutating Data
You may find yourself wanting to merge 2 or more properties that are related to each other. For example, you can merge a user's first_name and last_name:
query({
from({
users: {
select([
"mutate => $.first_name $.last_name => full_name"
]);
};
});
});This will turn the user's first_name and last_name into one value. So if we had "Donald" for the first_name and "Pakkies" for the last_name then full_name would be "Donald Pakkies".
.capitalize!
Capitalizes a value:
query({
from({
posts: {
select([
"mutate => $.title.capitalize! => title"
]);
};
});
});.lower!
Transform's a value to lower case:
query({
from({
posts: {
select([
"mutate => $.title.lower! => title"
]);
};
});
});.slug!
Changes a value into a slug:
query({
from({
posts: {
select([
"mutate => $.title.slug! => slug"
]);
};
});
});.upper!
Transform's a value to upper case:
query({
from({
users: {
select([
"mutate => $.first_name $.last_name.upper! => full_name"
]);
};
});
});Numeric Functions
You can write queries that do calculations before returning the results to you. For example, you may calculate the total sum of all hours logged by a specific user, or the average hours logged by all users.
AVG
Return the average of a given value.
query({
from({
tasks: {
select([
"AVG => hours => average_hours"
]);
};
});
});hours in this case is a property that exists in the documents of the tasks collections.
MAX
Return the maximum of given values.
query({
from({
tasks: {
select([
"MAX => hours => average_hours"
]);
};
});
});MIN
Return the minimum of given values.
query({
from({
tasks: {
select([
"MIN => hours => average_hours"
]);
};
});
});SUM
Return the sum of a given value.
query({
from({
tasks: {
select([
"SUM => hours => total_hours"
]);
};
});
});