Function: Advanced UPSERT Operation

      +

      Perform the Advanced UPSERT operation where Eventing interacts with the Data Service.

      The advancedUpsertOp function:

      • Performs the Advanced UPSERT 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 UPSERT Operation.

      • advancedUpsertOp

      • Input data

      • Output data

      • Output log

      // Configure the settings for the advancedUpsertOp 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_upsert")) return;
          log('input meta', meta);
          log('input doc ', doc);
          // two modes: typical upsert and setting an expiration/TTL
          // note that CAS, if supplied, is ignored. You can use REPLACE instead.
          var new_meta = {"id":"test_adv_upsert:"+doc.ins_id};
          if (doc.set_expiry && doc.set_expiry === true) {
              new_meta = {"id":"test_adv_upsert:"+doc.ins_id, expiry_date: new Date(Date.now() + 60 * 1000)};
          }
          var new_doc = { type: "test_adv_upsert", id:+doc.ins_id, random: Math.random()}
          var result = couchbase.upsert(src_col,new_meta,new_doc);
          if (result.success) {
              log('success adv. upsert: result',result);
          } else {
              log('failure adv. upsert: id',new_meta.id,'result',result);
          }
      }
      Mutation #1
      
      INPUT: KEY control_adv_upsert::1
      
      {
          "id": 1,
          "type": "control_adv_upsert",
          "ins_id": 1,
          "set_expiry": false
      }
      
      Mutation #2
      
      INPUT: KEY control_adv_upsert::2
      
      {
          "id": 2,
          "type": "control_adv_upsert",
          "ins_id": 2,
          "set_expiry": true
      }
      
      Mutation #3
      
      INPUT: KEY control_adv_upsert::3
      
      {
          "id": 3,
          "type": "control_adv_upsert",
          "ins_id": 1,
          "set_expiry": false
      }

      The output data upserts 3 documents. The first 2 upsertions are successful. The test_adv_insert: 2 has an expiration of 60 seconds. The third upsertion attempt fails because test_adv_insert: 1 already exists.

      KEY: test_adv_upsert:1
      
      {
        "type": "test_adv_upsert",
        "id": 1,
        "random": 0.24687491488383362
      }
      
      KEY: test_adv_upsert:2
      
      {
        "type": "test_adv_upsert",
        "id": 2,
        "random": 0.08984103133112087
      }
      Logs from Mutation #1
      
      2021-01-07T17:43:52.527-08:00 [INFO] "input meta"
      {
          "cas": "1610070232443518976",
          "id": "control_adv_upsert::1",
          "expiration": 0,
          "flags": 33554438,
          "vb": 334,
          "seq": 1
      }
      2021-01-07T17:43:52.527-08:00 [INFO] "input doc "
      {
          "id": 1,
          "type": "control_adv_upsert",
          "ins_id": 1,
          "set_expiry": false
      }
      2021-01-07T17:43:52.529-08:00 [INFO] "success adv. upsert: result"
      {
          "meta": {
              "id": "test_adv_upsert:1",
              "cas": "1610070232527667200"
          },
          "success": true
      }
      
      Logs from Mutation #2
      
      2021-01-07T17:44:21.926-08:00 [INFO] "input meta"
      {
          "cas": "1610070261867741184",
          "id": "control_adv_upsert::2",
          "expiration": 0,
          "flags": 33554438,
          "vb": 71,
          "seq": 1
      }
      2021-01-07T17:44:21.926-08:00 [INFO] "input doc "
      {
          "id": 2,
          "type": "control_adv_upsert",
          "ins_id": 2,
          "set_expiry": true
      }
      2021-01-07T17:44:21.929-08:00 [INFO] "success adv. upsert: result"
      {
          "meta": {
              "id": "test_adv_upsert:2",
              "cas": "1610070261927641088",
              "expiry_date": "2021-01-08T01:45:21.000Z"
          },
          "success": true
      }
      
      Logs from Mutation #3
      
      2021-01-07T17:44:58.063-08:00 [INFO] "input meta"
      {
          "cas": "1610070298010845184",
          "id": "control_adv_upsert::3",
          "expiration": 0,
          "flags": 33554438,
          "vb": 832,
          "seq": 1
      }
      2021-01-07T17:44:58.063-08:00 [INFO] "input doc "
      {
          "id": 3,
          "type": "control_adv_upsert",
          "ins_id": 1,
          "set_expiry": false
      }
      2021-01-07T17:44:58.065-08:00 [INFO] "success adv. upsert: result"
      {
          "meta": {
              "id": "test_adv_upsert:1",
              "cas": "1610070298064257024"
          },
          "success": true
      }