1- import { IStore , HealthEvents , IElasticStoreConfig } from "./interfaces" ;
2- import { Client } from "elasticsearch" ;
3- import { logger as log } from "./logger" ;
41import { inspect } from "util" ;
52import { EventEmitter } from "events" ;
3+ import { Client } from "elasticsearch" ;
4+ import { logger as log } from "./logger" ;
5+ import { IStore , HealthEvents , IElasticStoreConfig } from "./interfaces" ;
6+ import { validate } from "./validator" ;
67
78export class ElasticStore extends EventEmitter implements IStore {
89 public client : Client ;
@@ -29,7 +30,9 @@ export class ElasticStore extends EventEmitter implements IStore {
2930 }
3031
3132 public async disconnect ( ) {
32- if ( ! this . client ) { return ; }
33+ if ( ! this . client ) {
34+ throw new Error ( "Client is not initialized" ) ;
35+ }
3336 this . emit ( HealthEvents . Ready , false ) ;
3437 return this . client . close ( ) ;
3538 }
@@ -38,8 +41,8 @@ export class ElasticStore extends EventEmitter implements IStore {
3841 if ( ! this . client ) {
3942 throw new Error ( "Client is not initialized" ) ;
4043 }
41- if ( ! keys || keys . length === 0 ) {
42- return "nothing to query" ;
44+ if ( ! keys || keys . length === 0 || keys . includes ( "" ) ) {
45+ throw new Error ( "invalid keys" ) ;
4346 }
4447 return this . client . mget ( {
4548 index : this . options . index ,
@@ -64,8 +67,9 @@ export class ElasticStore extends EventEmitter implements IStore {
6467 public async remove ( key : string ) {
6568 if ( ! this . client ) {
6669 throw new Error ( "Client is not initialized" ) ;
70+ } else if ( ! key ) {
71+ throw new Error ( "not a valid key" ) ;
6772 }
68-
6973 await this . client . delete ( {
7074 index : this . options . index ,
7175 type : this . options . type ,
@@ -78,15 +82,10 @@ export class ElasticStore extends EventEmitter implements IStore {
7882 if ( ! this . client ) {
7983 throw new Error ( "Client is not initialized" ) ;
8084 }
81-
82- await this . client . deleteByQuery ( {
85+ // delete the index
86+ await this . client . indices . delete ( {
8387 index : this . options . index ,
8488 ignoreUnavailable : true ,
85- body : {
86- query : {
87- match_all : { } ,
88- } ,
89- } ,
9089 } ) ;
9190
9291 }
@@ -95,11 +94,10 @@ export class ElasticStore extends EventEmitter implements IStore {
9594 if ( ! this . client ) {
9695 throw new Error ( "Client is not initialized" ) ;
9796 }
98-
99- const keys = Object . keys ( mappings ) ;
100- if ( keys . length === 0 ) {
101- return ;
97+ if ( ! validate ( mappings ) ) {
98+ throw new Error ( "not a valid object to save" ) ;
10299 }
100+ const keys = Object . keys ( mappings ) ;
103101 let body = "" ;
104102 const indexName = this . options . index ;
105103 const typeName = this . options . type ;
0 commit comments