LunaQL - short functions

Back

About an hour ago, I thought having functions in LunaQL would be cool. So I started working on the feature.

At the moment, this new feature only works with the select statement. Its currently a proof of concept and I might not actually ship it when LunaQL goes live.

Let's take a look at an example:

query({
from({
tasks: {
select: [
/**
* Get the total number of hours.
*/
'SUM => hours => total_hours'
];
};
});
});

In the example above, we're using the SUM function to get the total number of all hours then returning the results as total_hours. We can also use other functions such as MAX, MIN, AVG, and ABS.

At the moment, this feature doesn't allow you to select anything else besides selecting anything with numeric functions.

We can also compute some data. For example, let's say in the users collection we have a first_name and last_name field. We can compute the full name of the user by using the computed function:

query({
from({
users: {
select: [
'computed => {$.first_name $.last_name} => full_name'
];
};
});
});

The computed function allows us to transform data before it is returned. For example, in the example above, we are joining users first_name and last_name and returning the results as full_name. We can also use the computed function to return a value as upper case or lower case:

query({
from({
users: {
select: [
'computed => {upperCase($.email)} => email'
];
};
});
});

In the example above, we're using the upperCase helper to return the email address in upper case letters.

Closing comments

While this is cool and all, I'm not so sure If I'll actually include this feature in the first release. This was just a proof of concept.

© Donald Pakkies.
github