A newer version of this documentation is available.

View Latest

Function: Advanced UPSERT operation

    February 16, 2025
    + 12

    Goal: Perform the Advanced UPSERT operation where Eventing interacts with the Data service.

    • This function advancedUpsertOp merely demonstrates the Advanced UPSERT operation.

    • Requires Eventing Storage (or metadata collection) and a "source" collection.

    • Needs a Binding of type "bucket alias" (as documented in the Scriptlet).

    • Will operate on any mutation where doc.type === "control_adv_insert".

    • For more information refer to Advanced UPSERT operation in the detailed documentation.

    javascript
    // To run configure the settings for this Function, advancedGetOp, as follows: // // Version 7.1+ // "Function Scope" // *.* (or try bulk.data if non-privileged) // Version 7.0+ // "Listen to Location" // bulk.data.source // "Eventing Storage" // rr100.eventing.metadata // Binding(s) // 1. "binding type", "alias name...", "bucket.scope.collection", "Access" // "bucket alias", "src_col", "bulk.data.source", "read and write" // // Version 6.6.1 // "Source Bucket" // source // "MetaData Bucket" // metadata // Binding(s) // 1. "binding type", "alias name...", "bucket", "Access" // "bucket alias", "src_col", "source", "read and write" function OnUpdate(doc, meta) { if (!meta.id.startsWith("control_adv_upsert")) return; log('input meta', meta); log('input doc ', doc); // two modes typical upsert or setting a expiration/TTL // note CAS if supplied will be ignored (use replace for this) var new_meta = {"id":"test_adv_upsert:"+doc.ins_id}; if (doc.set_expiry && doc.set_expiry === true) { new_meta = {"id":"test_adv_upsert:"+doc.ins_id, expiry_date: new Date(Date.now() + 60 * 1000)}; } var new_doc = { type: "test_adv_upsert", id:+doc.ins_id, random: Math.random()} var result = couchbase.upsert(src_col,new_meta,new_doc); if (result.success) { log('success adv. upsert: result',result); } else { log('failure adv. upsert: id',new_meta.id,'result',result); } }