A newer version of this documentation is available.

View Latest

Function: cascadeKvDeleteWithDoc

    March 16, 2025
    + 12

    Goal: Perform a cascade delete operation using just the Data Service (or KV).

    • This function cascadeKvDeleteWithDoc merely demonstrates two different Eventing solutions

      • A cascade delete using just KV or the Data Service.

      • A work around for the fact that the OnDelete() entry point does not supply the actual document being deleted.

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

    • Needs a Binding of type Bucket Alias (as documented in the Scriptlet).

    • Will utilize a special doc.type === "proxy" with a prefix KEY of "proxy::" where the suffix after "proxy::" is the KEY to the actual document.

    • Will operate on any mutation where 1) doc.type exists and 2) doc.type !== "proxy".

    • When a "proxy" document is deleted the corresponding "real" document is read from KV. This allows actions such as cURL calls to be taken based on the data in the "real document.

    • After the real document is read it is then deleted.

    • This Scriptlet uses just KV unlike the similar example Cascade Delete which uses SQL++ (or N1QL).

    javascript
    // To run configure the settings for this Function, cascadeKvDeleteWithDoc, as follows: // // 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.X // "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) { // filter out any proxy:: docs, ignore all others if ((meta.id).startsWith("proxy::") === true) return; log('OnUpdate notified of insert/update to key', meta.id); } function OnDelete(meta, options) { // only process proxy:: docs, ignore all others if ((meta.id).startsWith("proxy::") === false) return; // optional filter for just type == real or apply logic to all non 'proxy' types // if ((meta.id).startsWith("real::") !== true) return; log('A. OnDelete notified of '+options.expired ? 'delete' : 'expiry'+' of proxy', meta.id); var real_key = (meta.id).substr(7); var real_doc = src_col[real_key]; if (real_doc) { delete src_col[real_key]; log('B. OnDelete removed the real doc via key',real_key); log('C. OnDelete do what you want curl, etc. with the real doc',real_doc) } else { log('D. OnDelete unexpected no real doc present for key', real_key); } }

    We want a small "proxy" doc as a placeholder that we delete instead of the "real" document