Type views

This commit is contained in:
Adria Navarro 2025-01-02 15:48:04 +01:00
parent 65fa3e0434
commit 1f51489368
3 changed files with 39 additions and 13 deletions

View File

@ -1,6 +1,7 @@
import { import {
CreateViewRequest, CreateViewRequest,
CreateViewResponse, CreateViewResponse,
PaginatedSearchRowResponse,
SearchRowResponse, SearchRowResponse,
SearchViewRowRequest, SearchViewRowRequest,
UpdateViewRequest, UpdateViewRequest,
@ -13,10 +14,14 @@ export interface ViewV2Endpoints {
fetchDefinition: (viewId: string) => Promise<ViewResponseEnriched> fetchDefinition: (viewId: string) => Promise<ViewResponseEnriched>
create: (view: CreateViewRequest) => Promise<CreateViewResponse> create: (view: CreateViewRequest) => Promise<CreateViewResponse>
update: (view: UpdateViewRequest) => Promise<UpdateViewResponse> update: (view: UpdateViewRequest) => Promise<UpdateViewResponse>
fetch: ( fetch: <T extends SearchViewRowRequest>(
viewId: string, viewId: string,
opts: SearchViewRowRequest opts: T
) => Promise<SearchRowResponse> ) => Promise<
T extends { paginate: true }
? PaginatedSearchRowResponse
: SearchRowResponse
>
delete: (viewId: string) => Promise<void> delete: (viewId: string) => Promise<void>
} }
@ -59,7 +64,7 @@ export const buildViewV2Endpoints = (API: BaseAPIClient): ViewV2Endpoints => ({
* @param viewId the id of the view * @param viewId the id of the view
* @param opts the search options * @param opts the search options
*/ */
fetch: async (viewId, opts) => { fetch: async (viewId, opts: SearchViewRowRequest) => {
return await API.post({ return await API.post({
url: `/api/v2/views/${encodeURIComponent(viewId)}/search`, url: `/api/v2/views/${encodeURIComponent(viewId)}/search`,
body: opts, body: opts,

View File

@ -68,7 +68,7 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> {
} }
try { try {
const res = await this.API.viewV2.fetch(datasource.id, { const request = {
...(query ? { query } : {}), ...(query ? { query } : {}),
paginate, paginate,
limit, limit,
@ -76,11 +76,27 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> {
sort: sortColumn, sort: sortColumn,
sortOrder: sortOrder, sortOrder: sortOrder,
sortType, sortType,
}) }
return { if (paginate) {
rows: res?.rows || [], const res = await this.API.viewV2.fetch(datasource.id, {
hasNextPage: res?.hasNextPage || false, ...request,
cursor: res?.bookmark || null, paginate,
})
return {
rows: res?.rows || [],
hasNextPage: res?.hasNextPage || false,
cursor: res?.bookmark || null,
}
} else {
const res = await this.API.viewV2.fetch(datasource.id, {
...request,
paginate,
})
return {
rows: res?.rows || [],
hasNextPage: false,
cursor: null,
}
} }
} catch (error) { } catch (error) {
return { return {

View File

@ -1,16 +1,16 @@
import { import {
UserCtx, UserCtx,
ViewV2, ViewV2,
SearchRowResponse,
SearchViewRowRequest, SearchViewRowRequest,
RequiredKeys, RequiredKeys,
RowSearchParams, RowSearchParams,
PaginatedSearchRowResponse,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
export async function searchView( export async function searchView(
ctx: UserCtx<SearchViewRowRequest, SearchRowResponse> ctx: UserCtx<SearchViewRowRequest, PaginatedSearchRowResponse>
) { ) {
const { viewId } = ctx.params const { viewId } = ctx.params
@ -49,7 +49,12 @@ export async function searchView(
user: sdk.users.getUserContextBindings(ctx.user), user: sdk.users.getUserContextBindings(ctx.user),
}) })
result.rows.forEach(r => (r._viewId = view.id)) result.rows.forEach(r => (r._viewId = view.id))
ctx.body = result
ctx.body = {
rows: result.rows,
bookmark: result.bookmark,
hasNextPage: result.hasNextPage,
}
} }
function getSortOptions(request: SearchViewRowRequest, view: ViewV2) { function getSortOptions(request: SearchViewRowRequest, view: ViewV2) {