Creating APIs‎ > ‎Logic‎ > ‎Rule Types‎ > ‎

Count Rule Type

Count rules are derivation rules that calculate the number of child objects, with an optional child qualification condition (where clause). API Creator derives the parent attribute value by counting designated child rows. You can 'trigger' an event action using the state change (increase or decrease) in a child row. For example, if the count of the child transactions is greater than 100, you could archive the older rows to MongoDB.

The count rule declares an attribute to be maintained as the count of a designated child role, with an optional qualification to filter child objects. For example:

Derive orders.item_count as count(lineitemsList)

Count rules are useful as chid existence checks. For example, you can ensure that orders have at least one line item entry using the item_count in a (commit!) validation. When necessary, API Creator updates the value of the attribute to reflect the count of the specified attribute in the child objects. This includes the child objects you add to and remove from the parent object, as well as modifications to the child objects that change their qualification in the count. Changes to the child triggers count processing. You can view the changes in the log.

API Creator makes adjustments to recalculate the count. For instance, when you add a new (qualifying) child to the parent, the count attribute is incremented by one. The performance impact is small.

Best Practice: To maintain high performance, count values are assumed to be correct on disk. For proper operation, if you define new counts on existing data, bring your current schema into conformance by initializing those new counts using SQL tools.

For more information:

  1. In API Creator, select Manage, Rules, and click the Create New Rule.
  2. Select the Count rule type, the table to which it applies, and click Create Rule.
  3. Define the parameters of the rule:

    Count name

    The name of the count rule.
    Tip: To apply a default value, leave this field blank.


    The name of the parent table and column representing the count result.


    The topic associates with this rule.


    Defines the child role and the attribute that is being counted.

    For more information about roles and foreign key-based relationships, see Data Sources.


    Boolean JavaScript expression designating the child rows that contribute to the count.
    Syntax notes: 
    Use logical operators such as && for "and", || for "or." You can specify equals either as "=", or "==."
    Optional: Yes


    Select this checkbox to activate the rule.
    Note: The definition must be complete.


    Indicates whether you can optimize the rule by executing it in SQL instead of in JavaScript.

    Just In Time

    Select to specify this formulas and aggregates as Just in Time (JIT) rules. You can define JIT rules on persistent attributes and on rules of type formula, sum, count, min, and max.

    If the value in the database is null, API Creator executes the rule on the first read, stores the value for that column in the database, and returns the value. To prevent API Creator from executing rules with null return values on every read, the rule should never return a null value.

  4. Click Activate and Close to return to the list of rules.

The count rule is created.