Function: Multi Collection Eventing

    March 23, 2025
    + 12

    Access the Data Service when Eventing is listening to multiple collections.

    The multiCollectionEventing function:

    • Demonstrates how to access the Data Service when using * wildcard bindings

    • Requires 2 bindings of type bucket alias

    • Requires the following 4 keyspaces in two buckets, rr100 and source:

      • rr100.eventing.metadata

      • source._default._default

      • source.myscope.mycol1

      • source.myscope.mycol2

    • Operates on 3 test documents

    • Highlights the use of meta.keyspace

    For more information about the Advanced Self-Recursion Parameter, see Eventing Functions that Listen to Multiple Collections.

    javascript
    // Configure the settings for the multiCollectionEventing function as follows: // // Set up 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 from 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 document 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 the insert can be tested) 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 // Remove the added items 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); } } }