Skip to content

Commit

Permalink
Add delete task api for v0.30.0 (meilisearch#1382)
Browse files Browse the repository at this point in the history
Add delete task api for v0.30.0 (meilisearch#1382)
  • Loading branch information
bidoubiwa authored Nov 11, 2022
1 parent b98549d commit ea102e6
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ client.getTasks(parameters: TasksQuery): Promise<TasksResults>
client.getTask(uid: number): Promise<Task>
```

#### [Delete tasks](https://docs.meilisearch.com/reference/api/tasks.html#delete-task)

```ts
client.deleteTasks(parameters: DeleteTasksQuery = {}): Promise<EnqueuedTask>
```

#### [Cancel tasks](https://docs.meilisearch.com/reference/api/tasks.html#cancel-task)

```ts
Expand Down
14 changes: 14 additions & 0 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TasksResults,
EnqueuedTaskObject,
CancelTasksQuery,
DeleteTasksQuery,
} from '../types'
import { HttpRequests } from '../http-requests'
import { TaskClient, Task } from '../task'
Expand Down Expand Up @@ -264,6 +265,19 @@ class Client {
async cancelTasks(parameters: CancelTasksQuery = {}): Promise<EnqueuedTask> {
return await this.tasks.cancelTasks(parameters)
}

/**
* Delete a list of tasks.
* @memberof MeiliSearch
* @method deleteTasks
* @param {DeleteTasksQuery} [parameters={}] - Parameters to filter the tasks.
*
* @returns {Promise<EnqueuedTask>} Promise containing an EnqueuedTask
*/
async deleteTasks(parameters: DeleteTasksQuery = {}): Promise<EnqueuedTask> {
return await this.tasks.deleteTasks(parameters)
}

///
/// KEYS
///
Expand Down
20 changes: 20 additions & 0 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TaskObject,
CancelTasksQuery,
TasksResultsObject,
DeleteTasksQuery,
} from './types'
import { HttpRequests, toQueryParams } from './http-requests'
import { sleep } from './utils'
Expand Down Expand Up @@ -153,6 +154,25 @@ class TaskClient {

return new EnqueuedTask(task)
}

/**
* Delete a list tasks.
* @memberof Tasks
* @method deleteTasks
* @param {DeleteTasksQuery} [parameters={}] - Parameters to filter the tasks.
*
* @returns {Promise<EnqueuedTask>} Promise containing an EnqueuedTask
*/
async deleteTasks(parameters: DeleteTasksQuery = {}): Promise<EnqueuedTask> {
const url = `tasks`

const task = await this.httpRequest.delete(
url,
{},
toQueryParams<DeleteTasksQuery>(parameters)
)
return new EnqueuedTask(task)
}
}

export { TaskClient, Task }
5 changes: 5 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export const enum TaskTypes {
DOCUMENTS_ADDITION_OR_UPDATE = 'documentAdditionOrUpdate',
DOCUMENT_DELETION = 'documentDeletion',
SETTINGS_UPDATE = 'settingsUpdate',
TASK_DELETION = 'taskDeletion',
SNAPSHOT_CREATION = 'snapshotCreation',
TASK_CANCELATION = 'taskCancelation',
}
Expand All @@ -251,6 +252,7 @@ export type TasksQuery = {
from?: number
}
export type CancelTasksQuery = Omit<TasksQuery, 'limit' | 'from'> & {}
export type DeleteTasksQuery = Omit<TasksQuery, 'limit' | 'from'> & {}

export type EnqueuedTaskObject = {
taskUid: number
Expand Down Expand Up @@ -309,6 +311,9 @@ export type TaskObject = Omit<EnqueuedTaskObject, 'taskUid'> & {
// Number of tasks that were canceled
canceledTasks?: number

// Number of tasks that were deleted
deletedTasks?: number

// Query parameters used to filter the tasks
originalQuery?: string
}
Expand Down
168 changes: 165 additions & 3 deletions tests/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
await client.waitForTask(task1.taskUid)
await client.waitForTask(task2.taskUid)

const tasks = await client.getTasks({ from: 1, limit: 1 })
const tasks = await client.getTasks({ from: task1.taskUid, limit: 1 })

expect(tasks.results.length).toEqual(1)
expect(tasks.from).toEqual(1)
expect(tasks.from).toEqual(task1.taskUid)
expect(tasks.limit).toEqual(1)
expect(tasks.next).toEqual(0)
expect(tasks.next).toEqual(task1.taskUid - 1)
})

// get tasks: status
Expand Down Expand Up @@ -522,6 +522,168 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
expect(task.details?.originalQuery).toContain('afterFinishedAt')
})

// delete: uid
test(`${permission} key: Delete a task using the uid filter`, async () => {
const client = await getClient(permission)
const addDocuments = await client
.index(index.uid)
.addDocuments([{ id: 1 }])

const deleteTask = await client.deleteTasks({
uid: [addDocuments.taskUid],
})
const task = await client.waitForTask(deleteTask.taskUid)

expect(deleteTask.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.deletedTasks).toBeDefined()
await expect(client.getTask(addDocuments.taskUid)).rejects.toHaveProperty(
'code',
ErrorStatusCode.TASK_NOT_FOUND
)
})

// delete: indexUid
test(`${permission} key: Delete a task using the indexUid filter`, async () => {
const client = await getClient(permission)
const addDocuments = await client
.index(index.uid)
.addDocuments([{ id: 1 }])

const enqueuedTask = await client.deleteTasks({
indexUid: [index.uid],
})
const deleteTask = await client.waitForTask(enqueuedTask.taskUid)

expect(deleteTask.type).toEqual(TaskTypes.TASK_DELETION)
await expect(client.getTask(addDocuments.taskUid)).rejects.toHaveProperty(
'code',
ErrorStatusCode.TASK_NOT_FOUND
)
})

// delete: type
test(`${permission} key: Delete a task using the type filter`, async () => {
const client = await getClient(permission)

const enqueuedTask = await client.deleteTasks({
type: [
TaskTypes.DOCUMENTS_ADDITION_OR_UPDATE,
TaskTypes.DOCUMENT_DELETION,
],
})
const deleteTask = await client.waitForTask(enqueuedTask.taskUid)

expect(deleteTask.type).toEqual(TaskTypes.TASK_DELETION)
expect(deleteTask.details?.originalQuery).toEqual(
'type=documentAdditionOrUpdate%2CdocumentDeletion'
)
})

// delete: status
test(`${permission} key: Delete a task using the status filter`, async () => {
const client = await getClient(permission)

const enqueuedTask = await client.deleteTasks({
status: [TaskStatus.TASK_ENQUEUED, TaskStatus.TASK_PROCESSING],
})
const task = await client.waitForTask(enqueuedTask.taskUid)

expect(task.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.originalQuery).toEqual(
'status=enqueued%2Cprocessing'
)
})

// delete: beforeEnqueuedAt
test(`${permission} key: Delete a task using beforeEnqueuedAt filter`, async () => {
const client = await getClient(permission)

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
const enqueuedTask = await client.deleteTasks({
beforeEnqueuedAt: currentTime,
})
const task = await client.waitForTask(enqueuedTask.taskUid)

expect(task.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.originalQuery).toContain('beforeEnqueuedAt')
})

// delete: afterEnqueuedAt
test(`${permission} key: Delete a task using afterEnqueuedAt filter`, async () => {
const client = await getClient(permission)

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
const enqueuedTask = await client.deleteTasks({
afterEnqueuedAt: currentTime,
})
const task = await client.waitForTask(enqueuedTask.taskUid)

expect(task.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.originalQuery).toContain('afterEnqueuedAt')
})

// delete: beforeStartedAt
test(`${permission} key: Delete a task using beforeStartedAt filter`, async () => {
const client = await getClient(permission)

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
const enqueuedTask = await client.deleteTasks({
beforeStartedAt: currentTime,
})
const task = await client.waitForTask(enqueuedTask.taskUid)

expect(task.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.originalQuery).toContain('beforeStartedAt')
})

// delete: afterStartedAt
test(`${permission} key: Delete a task using afterStartedAt filter`, async () => {
const client = await getClient(permission)

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
const enqueuedTask = await client.deleteTasks({
afterStartedAt: currentTime,
})
const task = await client.waitForTask(enqueuedTask.taskUid)

expect(task.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.originalQuery).toContain('afterStartedAt')
})

// delete: beforeFinishedAt
test(`${permission} key: Delete a task using beforeFinishedAt filter`, async () => {
const client = await getClient(permission)

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
const enqueuedTask = await client.deleteTasks({
beforeFinishedAt: currentTime,
})
const task = await client.waitForTask(enqueuedTask.taskUid)

expect(task.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.originalQuery).toContain('beforeFinishedAt')
})

// delete: afterFinishedAt
test(`${permission} key: Delete a task using afterFinishedAt filter`, async () => {
const client = await getClient(permission)

const currentTimeStamp = Date.now()
const currentTime = new Date(currentTimeStamp)
const enqueuedTask = await client.deleteTasks({
afterFinishedAt: currentTime,
})
const task = await client.waitForTask(enqueuedTask.taskUid)

expect(task.type).toEqual(TaskTypes.TASK_DELETION)
expect(task.details?.originalQuery).toContain('afterFinishedAt')
})

test(`${permission} key: Get all indexes tasks with index instance`, async () => {
const client = await getClient(permission)
await client.index(index.uid).addDocuments([{ id: 1 }])
Expand Down

0 comments on commit ea102e6

Please sign in to comment.