Goal: Perform the Advanced INSERT operation where Eventing interacts with the Data service.]
-
This function advancedInsertOp merely demonstrates the Advanced INSERT operation.
-
Requires Eventing Storage (or metadata collection) and a "source" collection.
-
Needs a Binding of type "bucket alias" (as documented in the Scriptlet).
-
Will operate on any mutation where doc.type === "control_adv_insert".
-
For more information refer to Advanced INSERT operation in the detailed documentation.
-
advancedInsertOp
-
Input Data/Mutation
-
Output Data
-
Output Log
// To run configure the settings for this Function, advancedInsertOp, 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.6.1
// "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) {
if (!meta.id.startsWith("control_adv_insert")) return;
log('input meta', meta);
log('input doc ', doc);
// two modes typical insert or setting a expiration/TTL
var new_meta = {"id":"test_adv_insert:"+doc.ins_id};
if (doc.set_expiry && doc.set_expiry === true) {
new_meta = {"id":"test_adv_insert:"+doc.ins_id, expiry_date: new Date(Date.now() + 60 * 1000)};
}
var new_doc = { type: "test_adv_insert", id:+doc.ins_id, random: Math.random()}
var result = couchbase.insert(src_col,new_meta,new_doc);
if (result.success) {
log('success adv. insert: result',result);
} else {
log('failure adv. insert: id',new_meta.id,'result',result);
}
}
Mutation #1
INPUT: KEY control_adv_insert::1
{
"id": 1,
"type": "control_adv_insert",
"ins_id": 1,
"set_expiry": false
}
Mutation #2
INPUT: KEY control_adv_insert::2
{
"id": 2,
"type": "control_adv_insert",
"ins_id": 2,
"set_expiry": true
}
Mutation #3
INPUT: KEY control_adv_insert::3
{
"id": 3,
"type": "control_adv_insert",
"ins_id": 1,
"set_expiry": false
}
We try to insert three (3) documents the first two (2) inserts succeed but the second, test_adv_insert:2, will expire in 60 seconds because we set an expiration. The third insert attempt will fail since test_adv_insert:1 already exists.
KEY: test_adv_insert:1
{
"type": "test_adv_insert",
"id": 1,
"random": 0.6213609884848352
}
KEY: test_adv_insert:2
{
"type": "test_adv_insert",
"id": 2,
"random": 0.005665509244788591
}
Logs from Mutation #1
2021-01-07T16:52:24.525-08:00 [INFO] "input meta"
{
"cas": "1610067144497299456",
"id": "control_adv_insert::1",
"expiration": 0,
"flags": 33554438,
"vb": 651,
"seq": 6
}
2021-01-07T16:52:24.525-08:00 [INFO] "input doc "
{
"id": 1,
"type": "control_adv_insert",
"ins_id": 1,
"set_expiry": false
}
2021-01-07T16:52:24.527-08:00 [INFO] "success adv. insert: result"
{
"meta": {
"id": "test_adv_insert:1",
"cas": "1610067144526397440"
},
"success": true
}
Logs from Mutation #2
2021-01-07T16:52:48.243-08:00 [INFO] "input meta"
{
"cas": "1610067168218185728",
"id": "control_adv_insert::2",
"expiration": 0,
"flags": 33554438,
"vb": 898,
"seq": 3
}
2021-01-07T16:52:48.243-08:00 [INFO] "input doc "
{
"id": 2,
"type": "control_adv_insert",
"ins_id": 2,
"set_expiry": true
}
2021-01-07T16:52:48.245-08:00 [INFO] "success adv. insert: result"
{
"meta": {
"id": "test_adv_insert:2",
"cas": "1610067168243810304",
"expiry_date": "2021-01-08T00:53:48.000Z"
},
"success": true
}
Logs from Mutation #3
2021-01-07T16:53:20.498-08:00 [INFO] "input meta"
{
"cas": "1610067200451018752",
"id": "control_adv_insert::3",
"expiration": 0,
"flags": 33554438,
"vb": 133,
"seq": 1
}
2021-01-07T16:53:20.498-08:00 [INFO] "input doc "
{
"id": 3,
"type": "control_adv_insert",
"ins_id": 1,
"set_expiry": false
}
2021-01-07T16:53:20.500-08:00 [INFO] "failure adv. insert: id" "test_adv_insert:1" "result"
{
"error": {
"code": 272,
"name": "LCB_KEY_EEXISTS",
"desc": "The document key already exists in the server.",
"key_already_exists": true
},
"success": false
}