Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
IndexOptions,
IndexStats,
DocumentsQuery,
DocumentQuery,
Document,
DocumentOptions,
EnqueuedTask,
Expand Down Expand Up @@ -343,11 +344,29 @@ class Index<T = Record<string, any>> {
* @method getDocument
* @template T
* @param {string | number} documentId Document ID
* @param {DocumentQuery<T>} [parameters={}] Parameters applied on a document
* @returns {Promise<Document<T>>} Promise containing Document response
*/
async getDocument(documentId: string | number): Promise<Document<T>> {
async getDocument<T = Record<string, any>>(
documentId: string | number,
parameters?: DocumentQuery<T>
): Promise<Document<T>> {
const url = `indexes/${this.uid}/documents/${documentId}`
return await this.httpRequest.get<Document<T>>(url)

const fields = (() => {
if (Array.isArray(parameters?.fields)) {
return parameters?.fields?.join(',')
Comment on lines +357 to +358
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you already checked for the null in the if condition, typescript understand that in the line 358 it could be just parameters.fields.join(',') ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript understands it but then it is compiled in javascript, unfortunately, typescript does not add type checking!

}
return undefined
})()

return await this.httpRequest.get<Document<T>>(
url,
removeUndefinedFromObject({
...parameters,
fields,
})
)
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,20 @@ export type FieldDistribution = {
** Documents
*/

type Fields<T = Record<string, any>> =
| Array<Extract<keyof T, string>>
| Extract<keyof T, string>

export type DocumentOptions = {
primaryKey?: string
}

export type DocumentsQuery<T = Record<string, any>> = ResourceQuery & {
fields?: Array<Extract<keyof T, string>> | Extract<keyof T, string>
fields?: Fields<T>
}

export type DocumentQuery<T = Record<string, any>> = {
fields?: Fields<T>
}

export type Document<T = Record<string, any>> = T
Expand Down
63 changes: 47 additions & 16 deletions tests/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ describe('Documents tests', () => {
}
})

test(`${permission} key: Get one document `, async () => {
const client = await getClient(permission)
const { taskUid } = await client
.index(indexNoPk.uid)
.addDocuments(dataset)
await client.index(indexNoPk.uid).waitForTask(taskUid)

const documentId = 1
const document = await client
.index(indexNoPk.uid)
.getDocument<Book>(documentId)

expect(document.title).toEqual('Alice In Wonderland')
})

test(`${permission} key: Get one document with fields parameter`, async () => {
const client = await getClient(permission)
const { taskUid } = await client
.index(indexNoPk.uid)
.addDocuments(dataset)
await client.index(indexNoPk.uid).waitForTask(taskUid)

const documentId = 1
const document = await client
.index(indexNoPk.uid)
.getDocument<Book>(documentId, { fields: ['title'] })

expect(document.title).toEqual('Alice In Wonderland')
expect(document.id).toBeUndefined()
})

test(`${permission} key: Get documents with string fields`, async () => {
const client = await getClient(permission)

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

test(`${permission} key: Replace documents from index that has NO primary key`, async () => {
const client = await getClient(permission)
const { taskUid: addDocUpdate } = await client
const { taskUid: addDocTask } = await client
.index(indexNoPk.uid)
.addDocuments(dataset)
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
await client.index(indexNoPk.uid).waitForTask(addDocTask)
const id = 2
const title = 'The Red And The Black'

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

test(`${permission} key: Add document with update documents function from index that has NO primary key`, async () => {
const client = await getClient(permission)
const { taskUid: addDocUpdate } = await client
const { taskUid: addDocTask } = await client
.index(indexNoPk.uid)
.addDocuments(dataset)
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
await client.index(indexNoPk.uid).waitForTask(addDocTask)
const id = 9
const title = '1984'

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

test(`${permission} key: Add document with update documents function from index that has a primary key`, async () => {
const client = await getClient(permission)
const { taskUid: addDocUpdate } = await client
const { taskUid: addDocTask } = await client
.index(indexPk.uid)
.addDocuments(dataset)
await client.index(indexPk.uid).waitForTask(addDocUpdate)
await client.index(indexPk.uid).waitForTask(addDocTask)
const id = 9
const title = '1984'
const task = await client
Expand All @@ -290,10 +321,10 @@ describe('Documents tests', () => {

test(`${permission} key: Delete a document from index that has NO primary key`, async () => {
const client = await getClient(permission)
const { taskUid: addDocUpdate } = await client
const { taskUid: addDocTask } = await client
.index(indexNoPk.uid)
.addDocuments(dataset)
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
await client.index(indexNoPk.uid).waitForTask(addDocTask)
const id = 9

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

test(`${permission} key: Delete a document from index that has a primary key`, async () => {
const client = await getClient(permission)
const { taskUid: addDocUpdate } = await client
const { taskUid: addDocTask } = await client
.index(indexPk.uid)
.addDocuments(dataset)
await client.index(indexPk.uid).waitForTask(addDocUpdate)
await client.index(indexPk.uid).waitForTask(addDocTask)

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

test(`${permission} key: Delete some documents from index that has NO primary key`, async () => {
const client = await getClient(permission)
const { taskUid: addDocUpdate } = await client
const { taskUid: addDocTask } = await client
.index(indexNoPk.uid)
.addDocuments(dataset)
await client.index(indexNoPk.uid).waitForTask(addDocUpdate)
await client.index(indexNoPk.uid).waitForTask(addDocTask)

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

test(`${permission} key: Delete some documents from index that has a primary key`, async () => {
const client = await getClient(permission)
const { taskUid: addDocUpdate } = await client
const { taskUid: addDocTask } = await client
.index(indexPk.uid)
.addDocuments(dataset)
await client.index(indexPk.uid).waitForTask(addDocUpdate)
await client.index(indexPk.uid).waitForTask(addDocTask)

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

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

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