compute method

Future<APIResponse> compute(
  1. dynamic computations
)

Runs the specified computation(s) on the model objects and returns the computation results. This method is typically chained with group and filter methods. See table below for applicable modifiers that can be used with this method.

Modifier Chained with compute?
filter
group
limit
lookup
omit
page
sort

For example, you might have an orders model where you keep track of your sales of particular products. Using this method you can calculate the total order revenues, average order size, total number of orders and revenues on a weekly or monthly basis etc. The group method helps you to group your orders. If you would like to group your orders by the week or the month of the year, you can specify a grouping expression which calculates the week or the month of your order creation date. You can also specify the name of the field in the group method, such as the productId, which will group your orders by product.

The computations parameter defines the calculations that you will be running on the filtered and/or grouped objects. You can either specify a single computation or an array of computations. Altogic will perform the specified calculations for each group and return their results. You can specify multiple calculations at the same time, such as, you can calculate the total number of orders, total sales amount, and average order size on a weekly basis, etc.

If you do not specify any group or filter methods in your query builder chain, it performs the computations on all objects of the model, namely groups all objects stored in the database into a single group and runs the calculations on this group.

If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

computations An object or list of objects that contains the fields and their values to append to an object-list. computations can be GroupComputation or List of GroupComputation.

Returns the computation results

Implementation

Future<APIResponse<dynamic>> compute(dynamic computations) {
  if (!(computations is GroupComputation ||
      computations is List<GroupComputation>)) {
    throw ArgumentError('[computations] can be [List<GroupComputation>]'
        ' or [GroupComputation]');
  }

  return _call('/_api/rest/v1/db/compute', {
    'computations': (computations is List<GroupComputation>
            ? computations
            : [computations as GroupComputation])
        .map((e) => e.toJson())
        .toList()
  });
}