Skip to main content

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
SELECTNone
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.
CREATE INDEX, DROP INDEX, EXPLAIN, PREPARE, GRANT, REVOKENo eventing function is invoked.
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: https://docs.couchbase.com/server/5.5/eventing/eventing-examples.html
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.
References:
  1. Couchbase documentation: https://docs.couchbase.com/server/5.5/eventing/eventing-overview.html
  2. Couchbase blogs on eventing: https://blog.couchbase.com

Comments

Popular posts from this blog

Swami Vivekananda: The Monk That Nobody Sent to Chicago

  There’s a saying in Chicago: “We don’t want nobody that nobody sent.” This was the cold reception Swami Vivekananda faced when he arrived in the windy city in July 1893, determined to attend the World Parliament of Religions that September. He belonged to no organization, carried no letter of recommendation, his countrymen were nobody, and represented an alien religion to the Western world. As the days passed, his hope of attending the parliament dwindled. With money running out and the odds stacked against him, he left the Windy City and went to Boston, praying for a glimmer of opportunity.  Swamiji came to America to share India’s most profound gift: the wisdom of the Hindu sages, preserved through centuries of oral tradition and embodied by its monks. This was 1893, not 1993—India was under the British grip, its resources drained, and its spirit subdued. Swamiji’s mission was not just a cultural exchange; it was a bold step toward envisioning a future where India could re...

Why Should Databases Go Natural?

From search to CRM, applications are adopting natural language and intuitive interactions. Should databases follow? This article provides a strategic perspective. Amid the many technological evolutions in software and hardware (CISC/RISC, Internet, Cloud, and AI), one technology has endured:  Relational Database Systems   (RDBMS), aka SQL databases. For over 50 years, RDBMS has survived and thrived, overcoming many challenges. It has evolved and adopted beneficial features from emerging technologies like object-relational databases and now competes robustly with   NoSQL databases .  Today, RDBMS dominates the market, with four of the top five databases and seven of the top ten being relational. RDBMS has smartly borrowed ideas, like JSON support, from NoSQL, while NoSQL has also borrowed from RDBMS. NoSQL no longer rejects SQL. From a user perspective, all modern databases have SQL-inspired query language and a set of APIs. All applications manage the respective data...

iQ Interactive: Cool Things for Developers on Couchbase Capella iQ

  The landscape of software development is ever-evolving with the advent of new technologies. As we venture into 2023, natural language processing ( NLP ) is rapidly emerging as a pivotal aspect of programming. Unlike previous generations of tools that primarily aimed at enhancing coding productivity and code quality, the new generation of Artificial Intelligence ( GenAI ) tools, like iQ, is set to revolutionize every facet of a developer's workflow. This encompasses a wide range of activities: Reading, writing, and rewriting specifications Designing, prototyping, and coding Reviewing, refactoring, and verifying software Going through the iterative cycle of deploying, debugging, and improving the software Create a draft schema and sample data for any use case Natural language queries. Generate sample queries on a given dataset Fix the syntax error for a query Don't stop here. Let your imagination fly. Although the insights garnered from iQ are preliminary and should be treated ...