Docs‎ > ‎API Creator‎ > ‎Specify your Business Rules‎ > ‎

Reactive Logic

Reactive programming addresses the common issue of Change Management, by providing a mechanism for watching for changes and communicating them, and include the following classes:
  • Procedural. A variation of the Observer pattern, where register listener routines to observers, who invoke the Listeners when changes are made. Reactive programming extends this to order Listener invocation.

  • Declarative. You provide expressions for variables, where Live API Creator watches for changes to referenced data and reacts by re-evaluating the referencing variable.
For more information about reactive programming, see the Reactive Programming wikipedia page.

Declarative

Logic is declarative, providing active enforcement, re-use over transactions, optimizations, and dependency management/ordering. The last rule in the following diagram illustrates that you can also use server-side JavaScript for complete control. You define business logic using a combination of events and declarative logic:

The declarative approach is simpler and more powerful than procedural code by virtue of specialization to domains such as spreadsheets and database-transaction logic. Live API Creator employs this paradigm for derivation rules (for example, formulas, sums) and validations:
  1. You bind spreadsheet-like expressions (or, more generally, JavaScript functions) to database tables and columns.
  2. On RESTful updates (POST, PUT, DELETE), Live API Creator:
    1. Watches for changes to referenced data,
    2. Reacts to adjust referencing data, which can
    3. Chain to other rules. Chaining includes references in other tables. SQL and transaction bracketing are automated.

Reactive logic works in conjunction with server-side JavaScript events. This provides complete flexibility, including access to extensible JavaScript libraries.

For more information:

Reactive Logic has the following key differences to conventional procedural code: 

Key Difference What it means Why it matters
Automatic Invocation The API Server calls the logic without the need to explicitly invoke such logic. Eliminates errors where certain paths might fail to invoke required logic.
Automatic Chaining Changes trigger other changes and that chains to adjust the Order total. Simplifies maintenance, since ordering is dictated by dependencies and is recomputed on each change.
Automated SQL Watch/React includes multi-table logic. For example, you do not need to issue the SQL to retrieve the order. SQL is a chore to code, and to optimize (pruning, caching).

Logic Events

You can supply JavaScript table event handlers. The logic engine invokes these on every update, providing key contextual information such as the row, oldRow, and LogicContext.

For more information about logic events, including event firing and how to supply JavaScript table event handlers, see Logic Events.

How to Establish Watches

When you declare rules, Live API Creator processes them to determine their references and establish watches on the referenced attributes. You specify your business logic in the following ways:
  • By filling out a form. You use this for parent copies, sum, count, min, max.
  • By providing server-side JavaScript. For formulas and validations, you provide an expression, or more generally a function

This determines how references are detected:

Rule Type Watch
parent copies This takes effect when a child is first associated to a parent, either on on insert, or a change to the Foreign Key.

Note: Changes to the copied attribute are not watched; to propagate these values, make parent.attribute references in formulas or validations
aggregates: sum, count, min, max Watches are established on:
  • Foreign Key: These are all based on relationships, so all the elements of the Foreign Key are watched.
  • Sum / Min / Max attribute - the value being summed, min'd etc.
  • Qualification Condition - the child condition that identifies which child rows are included in the aggregation.
For example:
balance = sum(orders.amountTotal where paid = false)
places watches on order attributes customerId (the foreign key), amountTotal, and paid.
formula, validations Your code is scanned, and a watch is established for every row.attribute reference.  For example:
amount = row.price * row.quantity

places watches on the price and quantity.

How React Operates

In the case of our customer.balance rule, the statement obligates Live API Creator to respond to all of the following:

  • Order inserted - balance increased
  • Order deleted - balance decreased (if not paid)
  • Order paid - balance decreased
  • Order unpaid - balance decreased
  • Order amountTotal changed - balance adjusted
  • Order reassigned to different customer - balance increased for new customer, decreased for old

How Chain Operates

Logic can chain, since one rule can reference the value of another rule. There are dependencies between rules. In our previous example, the Customer.balance refers to the Purchaseorder.amount_total, itself defined by a rule. A change to the Lineitem.amount affects the Customer.balance.

The following table explains how Live API Creator implements chaining:

Rule Type React
parent copies There is no chaining for parent copies.

For child parent.attribute references, the system visits each child and evaluates the rules that are dependent on the parent attribute.
aggregates: sum, count, min, max The system issues a 1-row update to the parent object, which already be in cache.  Select sum queries are expensive, so are not employed.


 formula, validations The JavaScript code is executed.

Changes are affected instantly, without the need for code generation or deployment. The logic is fully executable. Update logic is complementary to other forms of business logic, such as process (workflow) logic, application integration, and decision logic.

For more information:

  • About each rule type, including tutorials and training, see Quick Reference.
  • About the architecture, which includes basic background on business logic, see Architecture.

Contrast to Traditional

Without such declarative automation, your procedural code must perform all of this watch/react dependency management, ordering, change detection, and optimization - over all the Use Cases. This is how five rules can represent hundreds of lines of code.

Complexity Handling

Declarative logic is extensible, in particular with server-side JavaScript logic events that can use loadable libraries. This enables you to solve very complex problems with remarkably little effort.

For more information: