angular-akita

how to avoid duplicate entries in akita store?


Before sending API request to insert I want to check the item is already exist in store or not, if not then only send the request to insert.

add(){
   const item = {id: 4,name:'test1',description:'description'}

   // here i want to check whether test1 already exist or not

   this.store.add(item);
   this.httpService.insert(item).subscribe(
   result => {
    this.messageService.handleSuccess('inserted');
    this.store.updateId(id, result.id);
  },
  error => this.messageService.handleError('error on insert:', error)
);

can someone please help?


Solution

  • You can use the query:

    add(){
       const item = {id: 4,name:'test1',description:'description'}
    
       if(query.hasEntity(4)) {
         // exists
       }
    
       this.store.add(item);
       this.httpService.insert(item).subscribe(
       result => {
        this.messageService.handleSuccess('inserted');
        this.store.updateId(id, result.id);
      },
      error => this.messageService.handleError('error on insert:', error)
    );