Perform the Advanced INSERT operation where Eventing interacts with the Data Service.]
The advancedInsertOp
function:
-
Performs the Advanced INSERT operation
-
Requires Eventing Storage (or a metadata collection) and a source collection
-
Requires a binding of type
bucket alias
-
Operates on any mutation where
doc.type === "control_adv_insert"
For more information about the Advanced Self-Recursion Parameter, see Advanced INSERT Operation.
-
advancedInsertOp
-
Input data
-
Output data
-
Output log
// Configure the settings for the advancedInsertOp function 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 and setting an 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
}
The output data inserts 3 documents.
The first 2 insertions are successful.
The test_adv_insert: 2
has an expiration of 60 seconds.
The third insertion attempt fails because 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
}