diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 3e9a3d232b..3169e4f77c 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -156,7 +156,14 @@ export async function searchView(ctx: Ctx) { ctx.status = 200 ctx.body = await quotas.addQuery( - () => sdk.rows.search({ tableId, query: view.query || {} }), + () => + sdk.rows.search({ + tableId, + query: view.query || {}, + sort: view.sort?.field, + sortOrder: view.sort?.order, + sortType: view.sort?.type, + }), { datasourceId: tableId, } diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 98ec40c9ae..e6da42ef8c 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -14,6 +14,8 @@ import { Row, Table, FieldType, + SortType, + SortOrder, } from "@budibase/types" import { generator, structures } from "@budibase/backend-core/tests" @@ -694,12 +696,12 @@ describe("/rows", () => { schema: { name: { type: FieldType.STRING, - name: "Name", + name: "name", constraints: { type: "string" }, }, age: { type: FieldType.NUMBER, - name: "Age", + name: "age", constraints: {}, }, }, @@ -757,5 +759,66 @@ describe("/rows", () => { rows: expect.arrayContaining(expectedRows.map(expect.objectContaining)), }) }) + + it.each([ + [ + { + field: "name", + order: SortOrder.ASCENDING, + type: SortType.STRING, + }, + ["Alice", "Bob", "Charly", "Danny"], + ], + [ + { + field: "name", + }, + ["Alice", "Bob", "Charly", "Danny"], + ], + [ + { + field: "name", + order: SortOrder.DESCENDING, + }, + ["Danny", "Charly", "Bob", "Alice"], + ], + [ + { + field: "name", + order: SortOrder.DESCENDING, + type: SortType.STRING, + }, + ["Danny", "Charly", "Bob", "Alice"], + ], + ])("allow sorting", async (sortParams, expected) => { + await config.createTable(userTable()) + const users = [ + { name: "Alice", age: 25 }, + { name: "Bob", age: 30 }, + { name: "Charly", age: 27 }, + { name: "Danny", age: 15 }, + ] + for (const user of users) { + await config.createRow({ + tableId: config.table!._id, + ...user, + }) + } + + const createViewResponse = await config.api.viewV2.create({ + sort: sortParams, + }) + + const response = await request + .get(`/api/views/v2/${createViewResponse._id}/search`) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + + expect(response.body.rows).toHaveLength(4) + expect(response.body).toEqual({ + rows: expected.map(name => expect.objectContaining({ name })), + }) + }) }) }) diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 0a4886278a..c6d0e013fa 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -1,4 +1,4 @@ -import { SearchFilters } from "@budibase/types" +import { SearchFilters, SortType } from "@budibase/types" import { isExternalTable } from "../../../integrations/utils" import * as internal from "./search/internal" import * as external from "./search/external" @@ -11,8 +11,8 @@ export interface SearchParams { bookmark?: string limit?: number sort?: string - sortOrder?: string - sortType?: string + sortOrder?: "ascending" | "descending" + sortType?: SortType version?: string disableEscaping?: boolean } diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index 7826cc06fd..c03bc3fe7f 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -1,5 +1,5 @@ -import { SortType } from "../../api" -import { SearchFilters, SortDirection } from "../../sdk" +import { SortOrder, SortType } from "../../api" +import { SearchFilters } from "../../sdk" import { Document } from "../document" export interface View { @@ -20,7 +20,7 @@ export interface ViewV2 extends Document { query?: SearchFilters sort?: { field: string - order?: SortDirection + order?: SortOrder type?: SortType } }