Sunday, October 21, 2018

Using N1QL with Couchbase Eventing Functions.

Now, this is not the end. It is not even the beginning of the end. But it is, perhaps, the end of the beginning. — Winston Churchill
Updating data is usually not the end, but usually a progress of a workflow. Shipping follows ordering; inventory update follows shipping; adjusting credit follows returning;  The next step in the process is required to act to keep the workflow going. The workflows can be simple with few steps or complex with hundreds of steps.  Business Process Management (BPM) is an industry by itself.
Couchbase 5.5 introduced Evening service. Developers can write a Javascript function to execute upon a change to the data. We refer inserts, Updates, Merges, and Deletions together as mutations. Multiple specific use cases have been documented for developing these eventing functions.
JSON data model in Couchbase came from JavaScript.  N1QL is SQL for JSON. The eventing functions are written in Javascript and has integrated N1QL. Using the Eventing functions, writing procedural business logic with instant access to data easy.
Here’s the lifecycle of the functions from a developer perspective. For every mutation, you can define any number of these functions to be executed. It’s the developer responsibility to size the execution times of the functions, depending on the number of mutations.
Here’s the lifecycle of the Eventing functions:
For every insert, update or delete (direct or expiry), you can execute one or more javascript functions.  These functions can read the new data and type of action and then execute the subsequent action. This functionality is well described in Couchbase blogs, and articles.
Statement TypeEventing Function Invoked
INSERTOnUpdate().  The function is invoked once per document inserted.Simple insert inserts a single document. Inserts can have multiple documents using multiple documents in the VALUES clause or can insert multiple documents via INSERT INTO…SELECT statement.
UPDATEOnUpdate() is invoked once per document updated, except when multiple updates on the same document is deduped into a single update. Update statement can update multiple documents.
UPSERTOnUpdate().  The behavior is similar to INSERT.
DELETEOnDelete(). Invoked once per documented deleted.
MERGEOnUpdate() and/or OnDelete() depending on the insert, update, and delete actions.
EXECUTEDepends on the type of the statement executed.
These functions can also execute N1QL statements.  Rest of the article looks at all aspects of N1QL executed in Eventing Functions.
N1QL Statements in Eventing functions.
StatementUse cases for N1QL statements in Eventing Functions
CREATE INDEXSince the schema is flexible, you could potentially inspect the data often/periodically to detect new fields and then create indexes on it.
DELETECascade deletes.
DROP INDEXA corollary to the CREATE INDEX use case.
INFERPeriodic introspection of the bucket for the structure.  Then take action (validate, create index, update the data model) if necessary.
INSERTMaintaining referential data (eventually).
Updating other documents with references to this data.  E.g Data from a new zip, state, etc.
Data copy (fully or partially) to secondary/tertiary documents. Similar to post-trigger action.
MERGEKeep the secondary data in sync.
SELECTFetch any data, run any reports periodically, etc.
Check for various things like data quality, data validity,
When you do know the target document key, use the built-in direct document references. See examples at:
UPSERTKeeping secondary/tertiary data in sync.
Similar to post-trigger action.
UPDATEKeeping secondary/tertiary data in sync.
Similar to post-trigger action.
Examples:  Let’s try some eventing functions with N1QL.
  1. Simple N1QL in Functions for logging and cascade delete.
Save away every deleted document in a separate bucket.
  1. OnUpdate() function to keep the aggregate information ready periodically.
  1. Couchbase documentation:
  2. Couchbase blogs on eventing:

Searching JSON: comparing text search in Couchbase and MongoDB.

“Apps without search is like Google homepage without the search bar.” "Apps without search is like Google homepage withou...