Add a key object to metadata

Hi @vsr1

Is there a way I can add a custom key-value pair to this metadata object?

Thanks

This was answered over on Stack Overflow by @vsr1 (and edited a little myself) - https://stackoverflow.com/questions/65351982/add-a-key-object-to-metadata-couchbase

1 Like

Thanks for the reply,
Let me tell you about our use case, if there is any other soln available.

when a document gets deleted we have written a eventing on delete and want to make a api call. But I need to pass a value which is inside the document to that rest call. Now in on delete function of eventing, we have only access to meta object.

So that’s the reason I am planning to save that custom value in meta and read it in on delete function. Is there any other way to implement this functionality.?

@matthew.groves @vsr1

@jon.strabala has expertise on eventing.

Hi @jon.strabala can you help me with this ??

@Ashish_Mishra WRT the Eventing service there are several ideas the come to mind some are trivial and some are complex - the right choice depends on your cluster, the performance you need, and possible other factors.

The most basic is to have a small “proxy” doc as a placeholder for the delete

KEY: proxy:real::1
{
  "id": "real::1",
  "type": "proxy"
}

which points to the actual doc you want to delete

KEY real::1 
{
  "id": 1,
  "type": "real",
  "f1": "yes",
  "f2": 1100,
  "fn": "n"
}

Then follow the design pattern without N1QL in https://docs.couchbase.com/server/current/eventing/eventing-examples-cascade-delete.html and create an Eventing Function that will operate via pure KV:

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;
    log('A. OnDelete notified of delete of proxy', meta.id);
    var real_key = (meta.id).substr(7);
    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);
    }
}

To run this function “cascadeKvDelete” (works on 6.5.1+):

  • Set up the above function with an bucket alias of src_bkt to you eventing source in mode read+write.
  • Deploy the function
  • Then add the two test documents via the UI’s doc editor.
  • Then delete the the proxy:real::1 doc it will do a cascade delete and you will be able to process the data form deleting that document.

You should see something like the following in the cascadeKvDelete.log (the UI’s log will be reverse sorted):

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"}