Skip to content

Commit 4b5a1fe

Browse files
committed
Review comments
1 parent 0b3a4dd commit 4b5a1fe

File tree

2 files changed

+46
-71
lines changed

2 files changed

+46
-71
lines changed

src/lib/indexes.ts

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

320-
/**
320+
/**
321321
* Add or replace multiples documents to an index in batches
322322
* @memberof Index
323323
* @method addDocumentsInBatch
324324
*/
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
325+
async addDocumentsInBatch(
326+
documents: Array<Document<T>>,
327+
batchSize = 1000,
328+
options?: AddDocumentParams
329+
): Promise<EnqueuedUpdate[]> {
330+
const resultArray = []
331+
for (let i = 0, n = documents.length; i < n; i += batchSize) {
332+
resultArray.push(
333+
await this.addDocuments(documents.slice(i, i + batchSize), options)
334+
)
343335
}
336+
return resultArray
337+
}
344338

345339
/**
346340
* Add or update multiples documents to an index
@@ -355,30 +349,24 @@ class Index<T = Record<string, any>> {
355349
return await this.httpRequest.put(url, documents, options)
356350
}
357351

358-
/**
352+
/**
359353
* Add or update multiples documents to an index in batches
360354
* @memberof Index
361355
* @method updateDocuments
362356
*/
363-
async updateDocumentsInBatch(
357+
async updateDocumentsInBatch(
364358
documents: Array<Document<T>>,
365-
batchSize: number = 1000,
359+
batchSize = 1000,
366360
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
361+
): Promise<EnqueuedUpdate[]> {
362+
const resultArray = []
363+
for (let i = 0, n = documents.length; i < n; i += batchSize) {
364+
resultArray.push(
365+
await this.updateDocuments(documents.slice(i, i + batchSize), options)
366+
)
381367
}
368+
return resultArray
369+
}
382370

383371
/**
384372
* Delete one document

tests/documents_tests.ts

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

74-
7574
test(`${permission} key: Add documents to uid with primary key in batch`, async () => {
76-
const updateIds:number[] = await client
75+
const response: EnqueuedUpdate[] = await client
7776
.index(indexPk.uid)
7877
.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)
78+
expect(response).toBeInstanceOf(Array)
79+
expect(response).toHaveLength(2)
80+
expect(response[0]).toHaveProperty('updateId', expect.any(Number))
81+
for (const enqueuedUpdate of response) {
82+
const addResponse = await client
83+
.index(indexPk.uid)
84+
.waitForPendingUpdate(enqueuedUpdate.updateId)
85+
expect(addResponse.status).toBe('processed')
86+
expect(addResponse.type.name).toBe('DocumentsAddition')
8987
}
9088
})
9189

@@ -227,30 +225,19 @@ describe.each([
227225
})
228226

229227
test(`${permission} key: Update document from index that has a primary key in batch`, async () => {
230-
const id = 456
231-
const title = 'The Little Prince'
232-
const comment = 'Updated comment'
233-
234-
const updateIds = await client
235-
.index(indexPk.uid)
236-
.updateDocumentsInBatch([{ id, title }, {id, comment}], 1)
237-
.then((response: EnqueuedUpdate[]) => {
238-
expect(response).toHaveLength(2)
239-
expect(response[0]).toHaveProperty('updateId', expect.any(Number))
240-
const tempIds:number[] = [];
241-
response.forEach((entry) => tempIds.push(entry.updateId))
242-
return tempIds
243-
})
244-
await client.index(indexPk.uid).waitForPendingUpdate(updateIds[0])
245-
await client.index(indexPk.uid).waitForPendingUpdate(updateIds[1])
246-
await client
228+
const response: EnqueuedUpdate[] = await client
247229
.index(indexPk.uid)
248-
.getDocument(id)
249-
.then((response) => {
250-
expect(response).toHaveProperty('id', id)
251-
expect(response).toHaveProperty('title', title)
252-
expect(response).toHaveProperty('comment', comment)
253-
})
230+
.updateDocumentsInBatch(dataset, 2)
231+
expect(response).toBeInstanceOf(Array)
232+
expect(response).toHaveLength(4)
233+
expect(response[0]).toHaveProperty('updateId', expect.any(Number))
234+
for (const enqueuedUpdate of response) {
235+
const addResponse = await client
236+
.index(indexPk.uid)
237+
.waitForPendingUpdate(enqueuedUpdate.updateId)
238+
expect(addResponse.status).toBe('processed')
239+
expect(addResponse.type.name).toBe('DocumentsPartial')
240+
}
254241
})
255242

256243
test(`${permission} key: Add document with update documents function from index that has NO primary key`, async () => {

0 commit comments

Comments
 (0)