Function: cascadeKvDeleteWithDoc

    +

    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++.

    • cascadeKvDeleteWithDoc

    • Input Data/Mutation(s)

    • Output Data/Logged

    // To run configure the settings for this Function, cascadeKvDeleteWithDoc, 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.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

    INPUT: KEY: proxy::real::1
    
    {
        "id": "real::1",
        "type": "proxy"
    }
    INPUT: 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.

    2021-07-18T20:08:04.459-07:00 [INFO] "C. OnDelete do what you want curl, etc. with the real doc" {"id":1,"type":"real","f1":"yes","f2":1100,"fn":"n"}
    
    2021-07-18T20:08:04.459-07:00 [INFO] "B. OnDelete removed the real doc via key" "real::1"
    
    2021-07-18T20:08:04.457-07:00 [INFO] "A. OnDelete notified of expiry of proxy" "proxy::real::1"
    
    2021-07-18T20:08:00.757-07:00 [INFO] "OnUpdate notified of insert/update to key" "real::1"