Skip to content

Commit 902002c

Browse files
meili-bors[bot]susamongusacedemybidoubiwa
authored
Merge #1220
1220: Updated created at in index r=bidoubiwa a=bidoubiwa # Pull Request ## What does this PR do? Fixes #1040 Since index creation is asynchronous it is never initialized with the knowledge of `createdAt` or `updatedAt` that information can only be obtained through `getRawInfo` Co-authored-by: 0xrang3 <[email protected]> Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 32f1771 + 5abbf4c commit 902002c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/indexes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ import { TaskClient } from './task'
4343
class Index<T = Record<string, any>> {
4444
uid: string
4545
primaryKey: string | undefined
46+
createdAt: Date | undefined
47+
updatedAt: Date | undefined
4648
httpRequest: HttpRequests
4749
tasks: TaskClient
4850

4951
/**
5052
* @param {Config} config Request configuration options
5153
* @param {string} uid UID of the index
52-
* @param {string} primaryKey? Primary Key of the index
54+
* @param {string} [primaryKey] Primary Key of the index
5355
*/
5456
constructor(config: Config, uid: string, primaryKey?: string) {
5557
this.uid = uid
@@ -145,6 +147,8 @@ class Index<T = Record<string, any>> {
145147
const url = `indexes/${this.uid}`
146148
const res = await this.httpRequest.get<IndexResponse>(url)
147149
this.primaryKey = res.primaryKey
150+
this.updatedAt = new Date(res.updatedAt)
151+
this.createdAt = new Date(res.createdAt)
148152
return res
149153
}
150154

tests/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,33 @@ describe.each([{ permission: 'Master' }, { permission: 'Private' }])(
326326
expect(response).toHaveProperty('isIndexing', false)
327327
expect(response).toHaveProperty('fieldDistribution', {})
328328
})
329+
330+
test(`${permission} key: Get updatedAt and createdAt through fetch info`, async () => {
331+
const client = await getClient(permission)
332+
const { uid } = await client.createIndex(indexPk.uid)
333+
await client.waitForTask(uid)
334+
335+
const index = await client.index(indexPk.uid).fetchInfo()
336+
337+
expect(index.createdAt).toBeInstanceOf(Date)
338+
expect(index.updatedAt).toBeInstanceOf(Date)
339+
})
340+
341+
test(`${permission} key: Get updatedAt and createdAt index through getRawInfo`, async () => {
342+
const client = await getClient(permission)
343+
const { uid } = await client.createIndex(indexPk.uid)
344+
await client.waitForTask(uid)
345+
346+
const index = client.index(indexPk.uid)
347+
348+
expect(index.createdAt).toBe(undefined)
349+
expect(index.updatedAt).toBe(undefined)
350+
351+
await index.getRawInfo()
352+
353+
expect(index.createdAt).toBeInstanceOf(Date)
354+
expect(index.updatedAt).toBeInstanceOf(Date)
355+
})
329356
}
330357
)
331358

0 commit comments

Comments
 (0)