A newer version of this documentation is available.

View Latest

Function: Advanced DELETE operation

    February 16, 2025
    + 12

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

    • This function advancedDeleteOp merely demonstrates the Advanced DELETE 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_delete".

    • Always tries to insert the test document, any insert error will be ignored.

    • There are 4 modes of operation: no_cas, bad_cas, no_key, and good_cas.

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

    javascript
    // To run configure the settings for this Function, advancedDeleteOp, 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_delete")) return; log('input meta', meta); log('input doc ', doc); // Setup, make sure we have our doc to "delete", ignore any errors couchbase.insert(src_col,{"id":"test_adv_delete:" + doc.ins_id},{"a:": 1}); var new_meta; if (doc.mode && doc.mode === "no_cas") { // Here we pass no CAS it will always succeed new_meta = {"id":"test_adv_delete:" + doc.ins_id}; } if (doc.mode && doc.mode === "bad_cas") { // Here we pass a non-matching CAS it will always fail new_meta = {"id":"test_adv_delete:" + doc.ins_id, "cas":"1111111111111111111"}; } if (doc.mode && doc.mode === "good_cas") { // Here we will pass the matching or current CAS it will succeed var tmp_r = couchbase.get(src_col,{"id":"test_adv_delete:" + doc.ins_id}); if (tmp_r.success) { // Here we use the current CAS just read via couchbase.get(...) new_meta = {"id":"test_adv_delete:" + doc.ins_id, "cas": tmp_r.meta.cas}; } else { log('Cannot delete due to no such key',"test_adv_delete:" + doc.ins_id); return; } } if (doc.mode && doc.mode === "no_key") { // Remove (delete with a basic bucket op) so that we have: no such key delete src_col["test_adv_delete:" + doc.ins_id] new_meta = {"id":"test_adv_delete:" + doc.ins_id}; } var result = couchbase.delete(src_col,new_meta); if (result.success) { log('success adv. delete: result',result); } else { log('failure adv. delete: id',new_meta.id,'result',result); } }