Count Rule Type

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 rules are useful as chid existence checks. For example, you can ensure that orders have at least one line item entry using the 
 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.
Layer7 Live 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, 
Layer7 Live API Creator
 assumes count values 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:
Create Count Rules 
 Your API is connected to an active database that contains at least one table or view.
  1. In API Creator, in the Manage section, click 
    If your API does not have existing rules, the Welcome to Rules page appears. If your API has existing rules, they are listed on the By entity tab by default.
  2. Click 
    Create a Rule
  3. Select the 
     rule type, the entity to which it applies, and then click 
    Create Rule
  4. Define the parameters of the rule:
    The title for the count rule. The title for rules does not affect logic processing. To apply a default value, leave this field blank.
    The name of the parent entity and column representing the count result.
    The topic associated with this rule.
    Defines the child role and the attribute that 
    Layer7 Live API Creator
     is counting.
    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 "==."
    Select this checkbox to activate the rule.
    The API 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 that 
    Layer7 Live API Creator
     recompute the data values.
    If the value in the database is null, 
    Layer7 Live API Creator
     executes the rule on the first read, stores the value for that column in the database, and returns the value. To prevent 
    Layer7 Live API Creator
     from executing rules with null return values on every read, the rule should not return a null value.
    Specifying count rules as Just in Time (JIT) can affect performance whenever new values are computed on the fly.
    For more information about JIT, including how to specify to have 
    Layer7 Live API Creator
     recompute your rules when/as it detects null vales, see Synchronize Data with Logic.
    The name of the count rule. To apply a default value, leave this field blank.
  5. Click 
    Activate and Close
     to return to the list of rules.
The count rule is created.