First, as always when changing the schema of your database, you must activate "Developer Mode" as shown.
Then, we click on the field that we want to be calculated. We enter our formula (using Handlebars template) like this:
{{hour-price}}*{{hour-estimate}}
. When the record is saved, the value of this field will be calculated.We can now see that the value is calculated according to the expression. Also note that it is read only.
Aggregation functions
Now that we've finished adding a calculated field, we want to add a sum to our total. If you are a developer or admin of a database, you will find a small symbol in the left corner of the record list.
Clicking this, enables the aggregation functions on number columns. We'll add a function to the "total-value" calculated field.
We'll just select the "Sum" function.
We can now see the sum of the "total-value" field.
We can now see the sum of the "total-value" field.
Aggregation of child record fields
RestDB can also show aggregations of child fields. For example if we have an Activity collection with Hours children, you can show the sum of the Hours on the Activity. We'll come back in another article with a more detailed example.
We have just shown you how to add calculated fields and aggregations to RestDB collections. Read more about aggregation in the docs here: https://restdb.io/docs/aggregate-data