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

Rule Operation

The following video conceptually shows rule operation:

Rule Operation

Update-business logic is a large part of most database applications. You specify your business logic in familiar update events, in this case, by providing server-side JavaScriptThe following page describes how rules operate.

For more information:

Declare spreadsheet-like expressions

To formalize our Check Credit solution, declare spreadsheet-like expressions for database columns. The rules look like the infamous cocktail napkin spec, to collaborate with business users:

  • A Validation, to ensure the CreditLimit is greater than the Balance, where
  • The Balance is the sum of the unShipped OrderTotals
  • Which is the Sum of the OrderDetail Amounts
  • Which is the UnitPrice * Quantity
  • Where the UnitPrice is copied from the Product
Enter these rules into Live API Creator. Live API Creator parses them to determine the rule dependencies, shown in the following image by the arrows:

You can think of these as declarative triggers, ready to Detect and Propagate Changes to dependent data. And that’s it. Our five simple rules are completely executable; no invocation logic, no persistence code.

How Rules Process an Update Request

Though we might have defined the rules for Place Order, they automatically address other transactions as well. The rules react to changing a Line Item quantity in the following ways:

  1. Live API Creator marshalls the RESTful JSON data into row objects that encapsulate the logic. These are created automatically, when you connect and create your API Project.
  2. Live API Creator performs optimistic locking checks, and obtains the oldRow, so your logic has access to all the required data.
  3. The quantity change triggers a change to the amount, which is dependent data. As a developer, this requires no explicit call. The rules execute automatically when their referenced data changes.
  4. The amount triggers an adjustment to the amountTotal. As a developer, you need not be concerned about the SQL, or performance matters like caching. Multi-table chaining is fully automatic.
  5. The Order change raises an update event. A few lines of JavaScript create a JSON document, and Post it to an external system. The logic is mostly rules with JavaScript as required.
  6. The amountTotal triggers an adjustment to the balance. Adjustments are optimized to one row updates, not expensive aggregate select sum queries.
  7. The balance triggers re-evaluation of our check-credit constraint. If it fails, the transaction is rolled back.Since there are no more rows to process, the transaction is committed.

Row Events Handled with JavaScript

The Object Model provides for row events which you can handle in JavaScript. The following is an example from the Add Payment example:


Rule operation is transparent. You can examine the log to determine all the rules and sql that fire. Chaining is shown by nesting. You can see the full row at each execution stage.

Value: Business Agility

Live API Creator's five simple rules were completely executable; no invocation logic, no persistence code. The rules, perhaps conceived for Place Order, actually address these other 12 transactions, completely. The Change Detection, Change Propagation, and SQL is automated.

Rules are bound to the data, not a specific Use Case, so they apply to all in-coming transactions. In other words, the logic above automatically processes the following transactions:
  • Order inserted - balance increased
  • Order deleted - balance decreased (if not paid)
  • Order unshipped - balance decreased
  • Order shipped - balance decreased
  • Order amountTotal changed - balance adjusted
  • Order reassigned to different customer - balance increased for new customer, decreased for old
  • OrderDetail inserted - obtain price, adjust Order and Customer (and check credit)
  • OrderDetail Deleted - reduce Order and Customer totals
  • OrderDetail Quantity increased - adjust Order and Customer (and check credit)
  • OrderDetail Product Changed - obtain price, adjust Order and Customer (and check credit)
  • OrderDetail Quantity and Product Changed - obtain price, adjust Order and Customer (and check credit)
  • Customer CreditLimit changed - check credit
This results in a meaningful improvement in quality. Reactive Programming eliminates of an entire class of programming error (e.g., balance checked when adding an order, but overlooked on unshipping an order).

If you used conventional event-based programming, these 5 lines would have required hundreds of lines of code. That’s over an order of magnitude improvement, for a significant part of your system. Maintenance is addressed too. When you change the rules, ordering and optimizations is handled automatically. And since invocation is automatic, all your existing transactions will automatically use and adapt to the new logic.