Skip to content

Commit

Permalink
Merge #1603
Browse files Browse the repository at this point in the history
1603: Feat/snapshots r=brunoocasali a=brunoocasali

Add the ability to create snapshots according to the v1.5 spec.

Co-authored-by: Bruno Casali <[email protected]>
  • Loading branch information
meili-bors[bot] and brunoocasali authored Nov 20, 2023
2 parents d37d50a + d9b93ad commit 2140aa3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ faceted_search_1: |-
client.index('books').search('classic', { facets: ['genres', 'rating', 'language'] })
post_dump_1: |-
client.createDump()
create_snapshot_1: |-
client.createSnapshot()
phrase_search_1: |-
client.index('movies')
.search('"african american" horror')
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,13 @@ client.getVersion(): Promise<Version>
client.createDump(): Promise<EnqueuedTask>
```

### Snapshots <!-- omit in toc -->

#### [Trigger a snapshot on-demand process](https://www.meilisearch.com/docs/reference/api/snapshots#create-a-snapshot)

```ts
client.createSnapshot(): Promise<EnqueuedTask>
```
---

Meilisearch provides and maintains many SDKs and integration tools like this one. We want to provide everyone with an **amazing search experience for any kind of project**. For a full overview of everything we create and maintain, take a look at the [integration-guides](https:/meilisearch/integration-guides) repository.
16 changes: 16 additions & 0 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ class Client {
return new EnqueuedTask(task)
}

///
/// SNAPSHOTS
///

/**
* Creates a snapshot
*
* @returns Promise returning object of the enqueued task
*/
async createSnapshot(): Promise<EnqueuedTask> {
const url = `snapshots`
const task = await this.httpRequest.post<undefined, EnqueuedTaskObject>(url)

return new EnqueuedTask(task)
}

///
/// TOKENS
///
Expand Down
70 changes: 70 additions & 0 deletions tests/snapshots.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ErrorStatusCode } from '../src/types'
import {
clearAllIndexes,
config,
MeiliSearch,
BAD_HOST,
getClient,
} from './utils/meilisearch-test-utils'

beforeEach(async () => {
await clearAllIndexes(config)
})

describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
'Test on snapshot',
({ permission }) => {
test(`${permission} key: create a new snapshot`, async () => {
const client = await getClient(permission)
const { taskUid } = await client.createSnapshot()

await client.waitForTask(taskUid)
})
}
)

describe.each([{ permission: 'Search' }])(
'Test on snapshot with search api key should not have access',
({ permission }) => {
test(`${permission} key: try to create snapshot with search key and be denied`, async () => {
const client = await getClient(permission)
await expect(client.createSnapshot()).rejects.toHaveProperty(
'code',
ErrorStatusCode.INVALID_API_KEY
)
})
}
)

describe.each([{ permission: 'No' }])(
'Test on snapshot without api key should not have access',
({ permission }) => {
test(`${permission} key: try to create snapshot with no key and be denied`, async () => {
const client = await getClient(permission)
await expect(client.createSnapshot()).rejects.toHaveProperty(
'code',
ErrorStatusCode.MISSING_AUTHORIZATION_HEADER
)
})
}
)

describe.each([
{ host: BAD_HOST, trailing: false },
{ host: `${BAD_HOST}/api`, trailing: false },
{ host: `${BAD_HOST}/trailing/`, trailing: true },
])('Tests on url construction', ({ host, trailing }) => {
test(`Test createSnapshot route`, async () => {
const route = `snapshots`
const client = new MeiliSearch({ host })
const strippedHost = trailing ? host.slice(0, -1) : host

await expect(client.createSnapshot()).rejects.toHaveProperty(
'message',
`request to ${strippedHost}/${route} failed, reason: connect ECONNREFUSED ${BAD_HOST.replace(
'http://',
''
)}`
)
})
})

0 comments on commit 2140aa3

Please sign in to comment.