Try Bucket Operation

To help you get started with Couchbase Functions, this section provides an example use case to perform a simple bucket operation.

  1. Create three buckets, source destination and metadata buckets.
    For a detailed steps for creating source and destination buckets, refer to Creating a Source Bucket.
    For steps to create a metadata bucket, refer to Creating a Metadata Bucket.

    add buckets bucketops
  2. To add a new Function, from the Couchbase Web Console > Eventing page, click ADD FUNCTION.

  3. In the ADD FUNCTION dialog, for individual Function elements provide the below information:

    1. For the Source Bucket drop-down, select the source option. The source bucket was created in step 1 for this purpose.

    2. For the Metadata Bucket drop-down, select metadata bucket option. The metadata bucket was created in step 1 for this purpose.

    3. Enter BucketOps as the name of the Function in the Function Name text-box.

    4. Enter Sample Bucket-Ops in the function Description text-box.

    5. For the Settings option, use the default values.

    6. For the Bindings option, specify destination as the name of the bucket, and specify dst_bucket as its associated value.

      addfunctions bucketops
  4. In the ADD FUNCTION dialog, click Next: Add Code. The Bucket-Ops dialog appears. The Bucket-Ops dialog initially contains a placeholder code block. You will substitute your actual Bucket-Ops code in this block.

  5. Copy and paste the below handler code.

    function OnUpdate(doc, meta) {
        var doc_id = meta.id;
        log('creating document for : ', doc);
        dst_bucket[doc_id] = {'doc_id' : doc_id}; // SET operation
    }
    function OnDelete(meta) {
        log('deleting document', meta.id);
        delete dst_bucket[meta.id]; // DELETE operation
    }

    The above Function handler code, upon data-mutation, reads the document from the source and transfers it to the destination bucket. The handler code stores this document transfer in the Eventing-Function log file.

  6. After pasting, the screen appears as displayed below:

    bucketops handlercode
  7. Click Save.

  8. To return to the Eventing screen, click Eventing.

  9. The Function Bucket-Ops gets listed as a defined Function.

    bucketops deploy
  10. Click Deploy.

  11. From the Confirm Deploy Function dialog, in the Feed boundary drop-down, select Everything and click Deploy Function.
    From this point, the defined Function is executed on all existing documents and on subsequent mutations.

The destination bucket is empty before Function execution.

Couchbase Server waits for a mutation to triggered the Bucket-Ops Function. Upon a mutation, the Function gets executed. As a result of Function execution, you should observe that documents are created in the destination bucket.

destinationbucket bucketops

Try N1QL Operation

To help you get started with Couchbase Functions, this section provides an example use case to perform a simple N1QL operation.

  1. Create two beer-sample, destination and metadata buckets. This example uses
    the beer-sample bucket. The beer-sample bucket is available out-of-the-box.
    For a detailed steps for creating destination buckets, refer to
    Creating a Source Bucket. For steps to create a metadata bucket, refer
    to Creating a Metadata Bucket.

    add buckets bucketops
  2. To add a new Function, from the Couchbase Web Console > Eventing page, click ADD FUNCTION.

  3. In the ADD FUNCTION dialog, for individual Function elements provide the below information:

    1. For the Source Bucket drop-down, select the source option. The source bucket was created in step 1 for this purpose.

    2. For the Metadata Bucket drop-down, select metadata bucket option. The metadata bucket was created in step 1 for this purpose.

    3. Enter sample-n1ql-usecase as the name of the Function in the Function Name text-box.

    4. Enter This is an example use case to perform a simple N1QL operation in the function Description text-box.

    5. For the Settings option, use the default values.

    6. For the Bindings option, create two binding. Specify source as the name of the bucket, and specify src_bucket as its associated value, and for the second bindings, specify destination as the name of the bucket, and specify dst_bucket as its associated value.

      eventing n1ql example
  4. In the ADD FUNCTION dialog, click Next: Add Code. The sample-n1ql-usecase dialog appears. The sample-n1ql-usecase dialog initially contains a placeholder code block. You will substitute your actual sample-n1ql-usecase code in this block.

  5. Copy and paste the below handler code.

    function OnUpdate(doc, meta)
    {
        var sel=SELECT *  from `beer-sample`;
        var i=0;
        for (var item of sel)
        {
            dst_bucket[i]=item;
            i++;
        }
    }

    The above Function handler code, upon data-mutation, performs an N1QL query to read documents in the beer-sample bucket and transfers it to the destination bucket.

  6. After pasting, the screen appears as displayed below:

    eventing n1ql function handler code
  7. Click Save.

  8. To return to the Eventing screen, click Eventing.

  9. The Function sample-n1ql-usecase gets listed as a defined Function.

    eventing n1ql deploy
  10. Click Deploy.

  11. From the Confirm Deploy Function dialog, in the Feed boundary drop-down, select Everything and click Deploy Function.
    From this point, the defined Function is executed on all existing documents and on subsequent mutations.

eventing n1ql buckets before

Couchbase Server waits for a mutation to triggered the sample-n1ql-usecase Function. Upon a mutation, the Function gets executed. As a result of Function execution, you should observe that documents are created in the destination bucket.

eventing n1ql buckets after