LunaQL docs are incomplete.
How To
Short Functions

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:

queries/users/all.lunaql
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:

queries/posts/index.lunaql
query({
	from({
		posts: {
			select([
				"mutate => $.title.capitalize! => title"
			]);
		};
	});
});

.lower!

Transform's a value to lower case:

queries/posts/index.lunaql
query({
	from({
		posts: {
			select([
				"mutate => $.title.lower! => title"
			]);
		};
	});
});

.slug!

Changes a value into a slug:

queries/posts/index.lunaql
query({
	from({
		posts: {
			select([
				"mutate => $.title.slug! => slug"
			]);
		};
	});
});

.upper!

Transform's a value to upper case:

queries/users/index.lunaql
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.

queries/tasks/index.lunaql
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.

queries/tasks/index.lunaql
query({
	from({
		tasks: {
			select([
				"MAX => hours => average_hours"
			]);
		};
	});
});

MIN

Return the minimum of given values.

queries/tasks/index.lunaql
query({
	from({
		tasks: {
			select([
				"MIN => hours => average_hours"
			]);
		};
	});
});

SUM

Return the sum of a given value.

queries/tasks/index.lunaql
query({
	from({
		tasks: {
			select([
				"SUM => hours => total_hours"
			]);
		};
	});
});