Monday, July 26, 2021

TRIGGER in Couchbase using Eventing concept

 Eventing is the concept in the couchbase which is used as a triggering concept in RDBMS.

for using eventing concept we need to create three new bucket

1- Source bucket :- The bucket on which the operation is going to be done. i.e. during update (insert and update) and delete call.
2- metadata bucket :- This is the bucket used by the couchbase server for calculation and please dont mess with it.
3- Output or destination bucker :- this is the bucket where we want the data to be stored after the fillter is applied on the data of source bucket.

Lets take an example where in when we try to update or insert new record in source bucket if the location in the record is “Pune” we will send the data to new bucket siddhuPune using eventing function concept.

Let say we have siddhu bucket

Create a new bucket name as siddhuMetadata

Create the output bucket siddhuPune

Now lets create a fucntion in our Eventing menu option that will apply logic on the source bucket and transfer the data to destination bucket.

Add following logic inside the JAVA script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function OnUpdate(doc, meta) {
    log("Doc created/updated", meta.id);
    if(doc.address[1]=="Pune")
    {
        log("<<<<<<<<<<<<<<<<Inside IF>>>>>>>>>>>>>>>>>>>>");
        siddhupunealias[meta.id]=doc;
    }
    log("doc.name:"+doc.name);
    log("doc.address[0]:"+doc.address[0]);
    log("doc.address[1]:"+doc.address[1]);
    log("doc:------------------------------------------"+doc);
}
 
function OnDelete(meta, options) {
    log("Doc deleted/expired", meta.id);
    log(doc);
}

click on the deploy button

Now lets try to insert the recourd in the siddhu bucket with these data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.siddhu;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
 
import com.siddhu.model.Employee;
import com.siddhu.repository.CustomerRepository;
 
@SpringBootApplication
@RestController
public class SpringbootCouchbaseApplication {
     
    @Autowired
    private CustomerRepository repository;
 
     
    @GetMapping("/fetchAllCustomers")
    public Iterable<Employee> getAll() {
        return repository.findAll();
    }
     
 
    @GetMapping("/insertEmployee")
    public void addData() {
         
        Employee objEmployeeOne =  new Employee(1, "Siddhu1", new String[] { "Siddhu1 Address 1", "Mumbai" });
        Employee objEmployeeTwo =  new Employee(2, "Siddhu2", new String[] { "Siddhu2 Address 2", "Pune" });
        Employee objEmployeeThree =  new Employee(3, "Siddhu3", new String[] { "Siddhu3 Address 3", "Pune" });
        Employee objEmployeeFour =  new Employee(4, "Siddhu4", new String[] { "Siddhu4 Address 4", "Gujarat" });
        repository.save(objEmployeeOne);
        repository.save(objEmployeeTwo);   
        repository.save(objEmployeeThree);
        repository.save(objEmployeeFour);
         
    }
 
    @GetMapping("/updateEmployee/{id}/{name}/{address}")
    public void updateData(@PathVariable("id") String id ,@PathVariable("name") String name,@PathVariable String[] address ) {
         
        Employee objEmployeeOne =  new Employee(new Integer(id), name, address);       
        repository.save(objEmployeeOne);           
         
    }
     
    @GetMapping("/deleteEmployee/{id}")
    public void deleteData(@PathVariable("id") String id) {
         
        repository.deleteById(new Integer(id));        
         
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringbootCouchbaseApplication.class, args);
    }
 
}

Note :- you can find this code from below github location

https://github.com/shdhumale/simple-springboot-couchbase

Now start spring boot application and hit the url

You will be able to see out of 4 record given below

1
2
3
4
Employee objEmployeeOne =  new Employee(1, "Siddhu1", new String[] { "Siddhu1 Address 1", "Mumbai" });
    Employee objEmployeeTwo =  new Employee(2, "Siddhu2", new String[] { "Siddhu2 Address 2", "Pune" });
    Employee objEmployeeThree =  new Employee(3, "Siddhu3", new String[] { "Siddhu3 Address 3", "Pune" });
    Employee objEmployeeFour =  new Employee(4, "Siddhu4", new String[] { "Siddhu4 Address 4", "Gujarat" });

two record with location as “Pune” are inserted into our siddhupune bucket as per the logic we had written in eventing functions.