Function: Advanced INSERT Operation

    March 16, 2025
    + 12

    Perform the Advanced INSERT operation where Eventing interacts with the Data Service.]

    The advancedInsertOp function:

    • Performs the Advanced INSERT operation

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

    • Requires a binding of type bucket alias

    • Operates on any mutation where doc.type === "control_adv_insert"

    For more information about the Advanced Self-Recursion Parameter, see Advanced INSERT Operation.

    javascript
    // Configure the settings for the advancedInsertOp function 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_insert")) return; log('input meta', meta); log('input doc ', doc); // two modes: typical insert and setting an expiration/TTL var new_meta = {"id":"test_adv_insert:"+doc.ins_id}; if (doc.set_expiry && doc.set_expiry === true) { new_meta = {"id":"test_adv_insert:"+doc.ins_id, expiry_date: new Date(Date.now() + 60 * 1000)}; } var new_doc = { type: "test_adv_insert", id:+doc.ins_id, random: Math.random()} var result = couchbase.insert(src_col,new_meta,new_doc); if (result.success) { log('success adv. insert: result',result); } else { log('failure adv. insert: id',new_meta.id,'result',result); } }