Skip to content

Commit c4fc383

Browse files
authored
Add fields parameter to getDocument route (#1291)
1 parent ed23994 commit c4fc383

File tree

3 files changed

+77
-19
lines changed

3 files changed

+77
-19
lines changed

src/indexes.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
IndexOptions,
2121
IndexStats,
2222
DocumentsQuery,
23+
DocumentQuery,
2324
Document,
2425
DocumentOptions,
2526
EnqueuedTask,
@@ -343,11 +344,29 @@ class Index<T = Record<string, any>> {
343344
* @method getDocument
344345
* @template T
345346
* @param {string | number} documentId Document ID
347+
* @param {DocumentQuery<T>} [parameters={}] Parameters applied on a document
346348
* @returns {Promise<Document<T>>} Promise containing Document response
347349
*/
348-
async getDocument(documentId: string | number): Promise<Document<T>> {
350+
async getDocument<T = Record<string, any>>(
351+
documentId: string | number,
352+
parameters?: DocumentQuery<T>
353+
): Promise<Document<T>> {
349354
const url = `indexes/${this.uid}/documents/${documentId}`
350-
return await this.httpRequest.get<Document<T>>(url)
355+
356+
const fields = (() => {
357+
if (Array.isArray(parameters?.fields)) {
358+
return parameters?.fields?.join(',')
359+
}
360+
return undefined
361+
})()
362+
363+
return await this.httpRequest.get<Document<T>>(
364+
url,
365+
removeUndefinedFromObject({
366+
...parameters,
367+
fields,
368+
})
369+
)
351370
}
352371

353372
/**

src/types/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,20 @@ export type FieldDistribution = {
129129
** Documents
130130
*/
131131

132+
type Fields<T = Record<string, any>> =
133+
| Array<Extract<keyof T, string>>
134+
| Extract<keyof T, string>
135+
132136
export type DocumentOptions = {
133137
primaryKey?: string
134138
}
135139

136140
export type DocumentsQuery<T = Record<string, any>> = ResourceQuery & {
137-
fields?: Array<Extract<keyof T, string>> | Extract<keyof T, string>
141+
fields?: Fields<T>
142+
}
143+
144+
export type DocumentQuery<T = Record<string, any>> = {
145+
fields?: Fields<T>
138146
}
139147

140148
export type Document<T = Record<string, any>> = T

tests/documents.test.ts

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,37 @@ describe('Documents tests', () => {
5757
}
5858
})
5959

60+
test(`${permission} key: Get one document `, async () => {
61+
const client = await getClient(permission)
62+
const { taskUid } = await client
63+
.index(indexNoPk.uid)
64+
.addDocuments(dataset)
65+
await client.index(indexNoPk.uid).waitForTask(taskUid)
66+
67+
const documentId = 1
68+
const document = await client
69+
.index(indexNoPk.uid)
70+
.getDocument<Book>(documentId)
71+
72+
expect(document.title).toEqual('Alice In Wonderland')
73+
})
74+
75+
test(`${permission} key: Get one document with fields parameter`, async () => {
76+
const client = await getClient(permission)
77+
const { taskUid } = await client
78+
.index(indexNoPk.uid)
79+
.addDocuments(dataset)
80+
await client.index(indexNoPk.uid).waitForTask(taskUid)
81+
82+
const documentId = 1
83+
const document = await client
84+
.index(indexNoPk.uid)
85+
.getDocument<Book>(documentId, { fields: ['title'] })
86+
87+
expect(document.title).toEqual('Alice In Wonderland')
88+
expect(document.id).toBeUndefined()
89+
})
90+
6091
test(`${permission} key: Get documents with string fields`, async () => {
6192
const client = await getClient(permission)
6293

@@ -136,10 +167,10 @@ describe('Documents tests', () => {
136167

137168
test(`${permission} key: Replace documents from index that has NO primary key`, async () => {
138169
const client = await getClient(permission)
139-
const { taskUid: addDocUpdate } = await client
170+
const { taskUid: addDocTask } = await client
140171
.index(indexNoPk.uid)
141172
.addDocuments(dataset)
142-
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
173+
await client.index(indexNoPk.uid).waitForTask(addDocTask)
143174
const id = 2
144175
const title = 'The Red And The Black'
145176

@@ -248,10 +279,10 @@ describe('Documents tests', () => {
248279

249280
test(`${permission} key: Add document with update documents function from index that has NO primary key`, async () => {
250281
const client = await getClient(permission)
251-
const { taskUid: addDocUpdate } = await client
282+
const { taskUid: addDocTask } = await client
252283
.index(indexNoPk.uid)
253284
.addDocuments(dataset)
254-
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
285+
await client.index(indexNoPk.uid).waitForTask(addDocTask)
255286
const id = 9
256287
const title = '1984'
257288

@@ -269,10 +300,10 @@ describe('Documents tests', () => {
269300

270301
test(`${permission} key: Add document with update documents function from index that has a primary key`, async () => {
271302
const client = await getClient(permission)
272-
const { taskUid: addDocUpdate } = await client
303+
const { taskUid: addDocTask } = await client
273304
.index(indexPk.uid)
274305
.addDocuments(dataset)
275-
await client.index(indexPk.uid).waitForTask(addDocUpdate)
306+
await client.index(indexPk.uid).waitForTask(addDocTask)
276307
const id = 9
277308
const title = '1984'
278309
const task = await client
@@ -290,10 +321,10 @@ describe('Documents tests', () => {
290321

291322
test(`${permission} key: Delete a document from index that has NO primary key`, async () => {
292323
const client = await getClient(permission)
293-
const { taskUid: addDocUpdate } = await client
324+
const { taskUid: addDocTask } = await client
294325
.index(indexNoPk.uid)
295326
.addDocuments(dataset)
296-
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
327+
await client.index(indexNoPk.uid).waitForTask(addDocTask)
297328
const id = 9
298329

299330
const task = await client.index(indexNoPk.uid).deleteDocument(id)
@@ -305,10 +336,10 @@ describe('Documents tests', () => {
305336

306337
test(`${permission} key: Delete a document from index that has a primary key`, async () => {
307338
const client = await getClient(permission)
308-
const { taskUid: addDocUpdate } = await client
339+
const { taskUid: addDocTask } = await client
309340
.index(indexPk.uid)
310341
.addDocuments(dataset)
311-
await client.index(indexPk.uid).waitForTask(addDocUpdate)
342+
await client.index(indexPk.uid).waitForTask(addDocTask)
312343

313344
const id = 9
314345
const task = await client.index(indexPk.uid).deleteDocument(id)
@@ -320,10 +351,10 @@ describe('Documents tests', () => {
320351

321352
test(`${permission} key: Delete some documents from index that has NO primary key`, async () => {
322353
const client = await getClient(permission)
323-
const { taskUid: addDocUpdate } = await client
354+
const { taskUid: addDocTask } = await client
324355
.index(indexNoPk.uid)
325356
.addDocuments(dataset)
326-
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
357+
await client.index(indexNoPk.uid).waitForTask(addDocTask)
327358

328359
const ids = [1, 2]
329360
const task = await client.index(indexNoPk.uid).deleteDocuments(ids)
@@ -339,10 +370,10 @@ describe('Documents tests', () => {
339370

340371
test(`${permission} key: Delete some documents from index that has a primary key`, async () => {
341372
const client = await getClient(permission)
342-
const { taskUid: addDocUpdate } = await client
373+
const { taskUid: addDocTask } = await client
343374
.index(indexPk.uid)
344375
.addDocuments(dataset)
345-
await client.index(indexPk.uid).waitForTask(addDocUpdate)
376+
await client.index(indexPk.uid).waitForTask(addDocTask)
346377

347378
const ids = [1, 2]
348379
const task = await client.index(indexPk.uid).deleteDocuments(ids)
@@ -410,7 +441,7 @@ describe('Documents tests', () => {
410441
expect(response).toHaveProperty('primaryKey', 'unique')
411442
})
412443

413-
test(`${permission} key: Add a document without a primary key and check response in update status`, async () => {
444+
test(`${permission} key: Add a document without a primary key and check response in task status`, async () => {
414445
const client = await getClient(permission)
415446
const docs = [
416447
{
@@ -427,7 +458,7 @@ describe('Documents tests', () => {
427458
expect(error).toHaveProperty('type')
428459
})
429460

430-
test(`${permission} key: Try to add documents from index with no primary key with NO valid primary key, update should fail`, async () => {
461+
test(`${permission} key: Try to add documents from index with no primary key with NO valid primary key, task should fail`, async () => {
431462
const client = await getClient(permission)
432463
const { taskUid } = await client.index(indexNoPk.uid).addDocuments([
433464
{

0 commit comments

Comments
 (0)