Function: cascadeKvDeleteWithDoc


    Goal: Perform a cascade delete operation with just the KV service.

    • 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 a metadata bucket and a source bucket.

    • 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 N1QL.

    • cascadeKvDeleteWithDoc

    • Input Data/Mutation(s)

    • Output Data/Logged

    // To run need a Binding in this Function's Settings as follows:
    // 1. Bucket Alias
    // 2. src_bkt
    // 3. source
    // 4. read+write
    function OnUpdate(doc, meta) {
        // filter out any proxy:: docs, ignore all others
        if (("proxy::") === true) return;
        log('OnUpdate notified of insert/update to key',;
    function OnDelete(meta, options) {
        // only process proxy:: docs, ignore all others
        if (("proxy::") === false) return;
        // optional filter for just type == real or apply logic to all non 'proxy' types
        //if (("real::") !== true) return;
        log('A. OnDelete notified of '+options.expired ? 'expiry' : 'delete'+' of proxy',;
        var real_key = (;
        var real_doc = src_bkt[real_key];
        if (real_doc) {
            delete src_bkt[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

    INPUT: KEY: proxy:real::1
        "id": "real::1",
        "type": "proxy"
    INPUT: KEY: real::1
    KEY real::1
      "id": 1,
      "type": "real",
      "f1": "yes",
      "f2": 1100,
      "fn": "n"

    Steps to run:

    • Deploy the function

    • Then add the two test documents via the UI’s doc editor.

    • Then delete the proxy:real::1 doc via the UI’s doc editor.

    • The OnDelete() function will:

      • read and store the "real" doc for processing based on the "real" doc’s internal data.

      • Perform a cascade delete of the "real" doc.

    2020-12-20T09:24:58.786-08:00 [INFO] "OnUpdate notified of insert/update to key" "real::1"
    2020-12-20T09:25:17.420-08:00 [INFO] "A. OnDelete notified of delete of proxy" "proxy::real::1"
    2020-12-20T09:25:17.422-08:00 [INFO] "B. OnDelete removed the real doc via key" "real::1"
    2020-12-20T09:25:17.422-08:00 [INFO] "C. OnDelete do what you want curl, etc. with the real doc" {"id":1,"type":"real","f1":"yes","f2":1100,"fn":"n"}