Skip to content

Commit cf24316

Browse files
Merge #1209
1209: Make updateDocuments accept partial documents in typing r=bidoubiwa a=bidoubiwa # Pull Request ## What does this PR do? Fixes #1187 In a typed environment, when using the `updateDocuments` method, you should be able to provide only some parts of the document that you want to update. Unfortunately, the typing was asking for the whole document. This PR changes the type to `partial` ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Charlotte Vermandel <[email protected]> Co-authored-by: cvermand <[email protected]>
2 parents 6970aff + 11c2703 commit cf24316

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,11 @@ If you want to know more about the development workflow or want to contribute, p
385385

386386
- [Add or update multiple documents](https://docs.meilisearch.com/reference/api/documents.html#add-or-update-documents):
387387

388-
`index.updateDocuments(documents: Document<T>[]): Promise<EnqueuedTask>`
388+
`index.updateDocuments(documents: Array<Document<Partial<T>>>): Promise<EnqueuedTask>`
389389

390390
- [Add or update multiple documents in batches](https://docs.meilisearch.com/reference/api/documents.html#add-or-update-documents):
391391

392-
`index.updateDocumentsInBatches(documents: Document<T>[], batchSize = 1000): Promise<EnqueuedTask[]>`
392+
`index.updateDocumentsInBatches(documents: Array<Document<Partial<T>>>, batchSize = 1000): Promise<EnqueuedTask[]>`
393393

394394
- [Get Documents](https://docs.meilisearch.com/reference/api/documents.html#get-documents):
395395

src/lib/indexes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,12 @@ class Index<T = Record<string, any>> {
394394
* Add or update multiples documents to an index
395395
* @memberof Index
396396
* @method updateDocuments
397-
* @param {Array<Document<T>>} documents Array of Document objects to add/update
397+
* @param {Array<Document<Partial<T>>>} documents Array of Document objects to add/update
398398
* @param {AddDocumentParams} options? Query parameters
399399
* @returns {Promise<EnqueuedTask>} Promise containing object of the enqueued update
400400
*/
401401
async updateDocuments(
402-
documents: Array<Document<T>>,
402+
documents: Array<Document<Partial<T>>>,
403403
options?: AddDocumentParams
404404
): Promise<EnqueuedTask> {
405405
const url = `indexes/${this.uid}/documents`
@@ -417,7 +417,7 @@ class Index<T = Record<string, any>> {
417417
* @returns {Promise<EnqueuedTasks>} Promise containing array of enqueued update objects for each batch
418418
*/
419419
async updateDocumentsInBatches(
420-
documents: Array<Document<T>>,
420+
documents: Array<Document<Partial<T>>>,
421421
batchSize = 1000,
422422
options?: AddDocumentParams
423423
): Promise<EnqueuedTask[]> {

tests/documents_tests.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
MeiliSearch,
1212
getClient,
1313
dataset,
14+
Book,
1415
} from './meilisearch-test-utils'
1516

1617
const indexNoPk = {
@@ -183,6 +184,20 @@ describe('Documents tests', () => {
183184
expect(response).toHaveProperty('title', title)
184185
})
185186

187+
test(`${permission} key: Partial update of a document`, async () => {
188+
const client = await getClient(permission)
189+
const id = 456
190+
const documents: EnqueuedTask = await client
191+
.index<Book>(indexPk.uid)
192+
.updateDocuments([{ id }])
193+
await client.index(indexPk.uid).waitForTask(documents.uid)
194+
195+
const response = await client.index(indexPk.uid).getDocument(id)
196+
197+
expect(response).toHaveProperty('id', id)
198+
expect(response).not.toHaveProperty('title')
199+
})
200+
186201
test(`${permission} key: Update document from index that has a primary key in batch`, async () => {
187202
const client = await getClient(permission)
188203
const tasks = await client
@@ -200,6 +215,28 @@ describe('Documents tests', () => {
200215
}
201216
})
202217

218+
test(`${permission} key: Partial update of a document in batch`, async () => {
219+
const client = await getClient(permission)
220+
const partialDocument = { id: 1 }
221+
222+
const tasks = await client
223+
.index<Book>(indexPk.uid)
224+
.updateDocumentsInBatches([partialDocument], 2)
225+
226+
expect(tasks).toBeInstanceOf(Array)
227+
expect(tasks).toHaveLength(1)
228+
expect(tasks[0]).toHaveProperty('uid', expect.any(Number))
229+
230+
for (const EnqueuedTask of tasks) {
231+
const task = await client
232+
.index(indexPk.uid)
233+
.waitForTask(EnqueuedTask.uid)
234+
235+
expect(task.status).toBe(TaskStatus.TASK_SUCCEEDED)
236+
expect(task.type).toBe('documentPartial')
237+
}
238+
})
239+
203240
test(`${permission} key: Add document with update documents function from index that has NO primary key`, async () => {
204241
const client = await getClient(permission)
205242
const { uid: addDocUpdate } = await client

tests/meilisearch-test-utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ const dataset = [
127127
{ id: 42, title: "The Hitchhiker's Guide to the Galaxy" },
128128
]
129129

130+
export type Book = {
131+
id: number
132+
title: string
133+
comment: string
134+
}
135+
130136
export {
131137
clearAllIndexes,
132138
config,

0 commit comments

Comments
 (0)