diff --git a/packages/client-sdk-web/src/internal/vector-index-data-client.ts b/packages/client-sdk-web/src/internal/vector-index-data-client.ts index 440b8af51..a96e9c37e 100644 --- a/packages/client-sdk-web/src/internal/vector-index-data-client.ts +++ b/packages/client-sdk-web/src/internal/vector-index-data-client.ts @@ -4,6 +4,7 @@ import {IVectorIndexDataClient} from '@gomomento/sdk-core/dist/src/internal/clie import { MomentoLogger, SearchOptions, + VectorCountItems, VectorDeleteItemBatch, VectorSearch, VectorSearchAndFetchVectors, @@ -53,6 +54,50 @@ export class VectorIndexDataClient implements IVectorIndexDataClient { this.client = new VectorIndexClient(vectorEndpoint, null, {}); } + public async countItems( + indexName: string + ): Promise { + try { + validateIndexName(indexName); + } catch (err) { + return this.cacheServiceErrorMapper.returnOrThrowError( + err as Error, + err => new VectorCountItems.Error(err) + ); + } + return await this.sendCountItems(indexName); + } + + private async sendCountItems( + indexName: string + ): Promise { + const request = new vectorindex._CountItemsRequest(); + request.setIndexName(indexName); + request.setAll(new vectorindex._CountItemsRequest.All()); + + return await new Promise((resolve, reject) => { + this.client.countItems( + request, + { + ...this.clientMetadataProvider.createClientMetadata(), + ...this.createVectorCallMetadata(this.deadlineMillis), + }, + (err, resp) => { + if (resp) { + resolve(new VectorCountItems.Success(resp.getItemCount())); + } else { + this.cacheServiceErrorMapper.resolveOrRejectError({ + err: err, + errorResponseFactoryFn: e => new VectorCountItems.Error(e), + resolveFn: resolve, + rejectFn: reject, + }); + } + } + ); + }); + } + public async upsertItemBatch( indexName: string, items: Array