A newer version of this documentation is available.

View Latest

Function: Multi Collection Eventing

    March 16, 2025
    + 12

    Goal: Show how to access the Data Service when Eventing is listening to multiple collections. Couchbase Server 7.1.1

    • This function multiCollectionEventing demonstrates how to access the Data Service when using wildcard bindings.

    • Requires four (4) keyspaces in two buckets "rr100" and "source"

      • rr100.eventing.metadata

      • source._default._default

      • source.myscope.mycol1

      • source.myscope.mycol2

    • Needs two Bindings of type "bucket alias" (as documented in the Scriptlet).

    • Will operate on three test documents. Add them one at a time after the function is deployed.

    • Highlights the use of meta.keyspace

    javascript
    // To run configure the settings for this Function, multiCollectionEventing, as follows: // // Setup four (4) required keyspaces in two buckets "rr100" and "source" // rr100.eventing.metadata // source._default._default // source.myscope.mycol1 // source.myscope.mycol2 // // Version 7.1.1+ // "Function Scope" // *.* (or try source.* if non-privileged) // "Listen to Location" // source.*.* // "Eventing Storage" // rr100.eventing.metadata // Create four (4) Bindings // "binding type", "alias name...", "bucket.scope.collection", "Access" // --------------------------------------------------------------------------- // "bucket alias", "alias_ro", "source.myscope.*", "read only" // "bucket alias", "alias_rw", "source.myscope.*", "read and write" // Deploy the Function // Create the following three (3) documents one at a time and inspect the Application log each time // "bucket.scope.collection" KEY DATA // --------------------------------------------------------------------------- // source._default._default doc0 {"data": "doc0"} // source.myscope.mycol1 doc1 {"data": "doc1"} // source.myscope.mycol2 doc2 {"data": "doc2"} function OnUpdate(doc, meta) { log('>>>A IN',doc, meta); // TEST GET with hardcode keyspace var res1 = couchbase.get(alias_ro,{"id": "doc2", "keyspace": {"bucket_name": "source","scope_name": "myscope","collection_name": "mycol2"}}); log('>>>B fixed read',"res1", res1); // protect against reading from something outside the alias if (meta.keyspace.scope_name == "myscope") { // TEST GET with keyspace form meta var res2 = couchbase.get(alias_ro,meta); log('>>>C read using passed meta (must be myscope)',"res2", res2); if (meta.keyspace.collection_name == "mycol2") { // TEST UPSERT with hardcode keyspace // add a field to the doc we read in res1 res1.doc.random1 = Math.random(); var res3 = couchbase.upsert(alias_rw,{"id": "doc2", "keyspace": {"bucket_name": "source","scope_name": "myscope","collection_name": "mycol2"}}, res1.doc) log('>>>D upsert',"res3", res3); // TEST REPLACE with hardcode keyspace res1.doc.random2 = Math.random(); var res4 = couchbase.replace(alias_rw,{"id": "doc2", "keyspace": {"bucket_name": "source","scope_name": "myscope","collection_name": "mycol2"}}, res1.doc) log('>>>E replace',"res4", res4); // TEST GET with hardcode keyspace var res5 = couchbase.get(alias_rw,meta); log('>>>F get (show added fields)',"res5", res5); // TEST DELETE with hardcode keyspace (so we can TEST the insert) var res6 = couchbase.delete(alias_rw,{"id": "doc2", "keyspace": {"bucket_name": "source","scope_name": "myscope","collection_name": "mycol2"}}) log('>>>G delete',"res6", res6); // TEST INSERT with hardcode keyspace // now remove the added items put thnigs back delete res1.doc.random1; delete res1.doc.random2; var res7 = couchbase.insert(alias_rw,{"id": "doc2", "keyspace": {"bucket_name": "source","scope_name": "myscope","collection_name": "mycol2"}}, res1.doc) log('>>>H upsert',"res7", res7); } } }