Skip to content

Commit b73df30

Browse files
committed
Add batch operations
1 parent 5099b5d commit b73df30

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/lib/indexes.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,31 @@ class Index<T = Record<string, any>> {
317317
return await this.httpRequest.post(url, documents, options)
318318
}
319319

320+
/**
321+
* Add or replace multiples documents to an index in batches
322+
* @memberof Index
323+
* @method addDocumentsInBatch
324+
*/
325+
async addDocumentsInBatch(
326+
documents: Array<Document<T>>,
327+
batchSize: number = 1000,
328+
options?: AddDocumentParams
329+
): Promise<EnqueuedUpdate[]> {
330+
const resultArray = []
331+
let batchDocuments = [];
332+
for(let i = 0, n = documents.length; i < n; ++i) {
333+
if(batchDocuments.length == batchSize) {
334+
resultArray.push(await this.addDocuments(batchDocuments, options));
335+
batchDocuments = []
336+
}
337+
batchDocuments.push(documents[i]);
338+
}
339+
if(batchDocuments.length != 0) {
340+
resultArray.push(await this.addDocuments(batchDocuments, options));
341+
}
342+
return resultArray
343+
}
344+
320345
/**
321346
* Add or update multiples documents to an index
322347
* @memberof Index
@@ -330,6 +355,31 @@ class Index<T = Record<string, any>> {
330355
return await this.httpRequest.put(url, documents, options)
331356
}
332357

358+
/**
359+
* Add or update multiples documents to an index in batches
360+
* @memberof Index
361+
* @method updateDocuments
362+
*/
363+
async updateDocumentsInBatch(
364+
documents: Array<Document<T>>,
365+
batchSize: number = 1000,
366+
options?: AddDocumentParams
367+
): Promise<EnqueuedUpdate[]> {
368+
const resultArray = []
369+
let batchDocuments = [];
370+
for(let i = 0, n = documents.length; i < n; ++i) {
371+
if(batchDocuments.length == batchSize) {
372+
resultArray.push(await this.updateDocuments(batchDocuments, options));
373+
batchDocuments = []
374+
}
375+
batchDocuments.push(documents[i]);
376+
}
377+
if(batchDocuments.length != 0) {
378+
resultArray.push(await this.updateDocuments(batchDocuments, options));
379+
}
380+
return resultArray
381+
}
382+
333383
/**
334384
* Delete one document
335385
* @memberof Index

tests/documents_tests.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ describe.each([
7171
await client.index(indexPk.uid).waitForPendingUpdate(updateId)
7272
})
7373

74+
75+
test(`${permission} key: Add documents to uid with primary key in batch`, async () => {
76+
const updateIds:number[] = await client
77+
.index(indexPk.uid)
78+
.addDocumentsInBatch(dataset, 4)
79+
.then((response: EnqueuedUpdate[]) => {
80+
expect(response).toBeInstanceOf(Array);
81+
expect(response).toHaveLength(2);
82+
expect(response[0]).toHaveProperty('updateId', expect.any(Number))
83+
const tempIds:number[] = [];
84+
response.forEach((entry) => tempIds.push(entry.updateId))
85+
return tempIds
86+
})
87+
for(let updateId of updateIds) {
88+
await client.index(indexPk.uid).waitForPendingUpdate(updateId)
89+
}
90+
})
91+
7492
test(`${permission} key: Get documents with string attributesToRetrieve`, async () => {
7593
await client
7694
.index(indexNoPk.uid)

0 commit comments

Comments
 (0)