Function: removeObjectStubs

    +

    Goal: Recursively remove all empty object stubs from a document.

    • This function removeObjectStubs will recursively prune empty objects from a given JSON document.

    • Requires a metadata bucket and a source bucket aliased to "src_bkt" in mode read+write.

    • Will operate on all documents where doc.type === "my_data_to_prune".

    • The removeEmptyParts function returns two (2) values in an array the doc and a flag indicating if a change has occurred. This flag ary_obj_upd[1] will increase the performance of subsequent runs where the Feed boundary is set to Everything on large document sets.

    • This function is a subset of the cleanup functionality found in the Scriptlet removeNullsAndEmptys.

    • removeObjectStubs

    • Input Data/Mutation

    • Output Data/Mutation

    function removeEmptyParts(ary_obj_upd) {
        if (ary_obj_upd[0] !== null && typeof ary_obj_upd[0] == "object") {
            Object.entries(ary_obj_upd[0]).forEach(([k, v]) => {
                if (ary_obj_upd[0][k] && typeof ary_obj_upd[0][k] === 'object') {
                    // recurse
                    if (removeEmptyParts(
                        [ary_obj_upd[0][k],ary_obj_upd[1]])[1] == true) {
                        ary_obj_upd[1] = true;
                    }
                    // remove stub {} object items
                    if (ary_obj_upd[0][k] && !Array.isArray(ary_obj_upd[0][k]) &&
                        Object.keys(ary_obj_upd[0][k]).length === 0
                    ) {
                        delete(ary_obj_upd[0][k]) // 6.6+ can use "delete obj"
                        ary_obj_upd[1] = true;
                    }
                }
            });
        } else {
            // obj is a number or a string
        }
        // return [ary_obj_upd[0],ary_obj_upd[1]];
        return ary_obj_upd;
    }
    
    function OnUpdate(doc, meta) {
        // optional filter to specific types
        if (doc.type !== "my_data_to_prune") return;
    
        // try make a new doc without {} stubs
        var altered = false;
        var ary_obj_upd = removeEmptyParts([doc,altered]);
        if (ary_obj_upd[1]) {
            // We actualy did an update so we need to write to KV
            // Note, the alias src_bkt (mode r+w) references the
            // handlers source bucket (allowed version 6.5+).
            src_bkt[meta.id] = ary_obj_upd[0];
        }
    }
    INPUT: KEY my_data_to_prune::1001
    
    {
      "type": "my_data_to_prune",
      "root": {
        "field1a": {},
        "field1b": {
          "field2a": {
            "value": 2
          }
        },
        "field1c": {
          "field2c": {
            "value": 3,
            "field3c": {
                "value": 4,
                "field4c": {}
            }
          }
        }
      }
    }
    UPDATED/OUTPUT: KEY my_data_to_prune::1001
    
     {
      "type": "my_data_to_prune",
      "root": {
        "field1b": {
          "field2a": {
            "value": 2
          }
        },
        "field1c": {
          "field2c": {
            "value": 3,
            "field3c": {
              "value": 4
            }
          }
        }
      }
    }