Merge branch 'default-app-design' of github.com:Budibase/budibase into default-app-design

This commit is contained in:
Andrew Kingston 2025-03-13 16:15:21 +00:00
commit f16e6d5300
No known key found for this signature in database
17 changed files with 342 additions and 301 deletions

View File

@ -1,6 +1,6 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "3.5.2",
"version": "3.5.3",
"npmClient": "yarn",
"concurrency": 20,
"command": {

View File

@ -147,9 +147,7 @@ export class FlagSet<T extends { [name: string]: boolean }> {
for (const [name, value] of Object.entries(posthogFlags)) {
if (!this.isFlagName(name)) {
// We don't want an unexpected PostHog flag to break the app, so we
// just log it and continue.
console.warn(`Unexpected posthog flag "${name}": ${value}`)
// We don't want an unexpected PostHog flag to break the app
continue
}

View File

@ -8,6 +8,7 @@ import {
Row,
SearchFilters,
SortOrder,
SortType,
TableSchema,
} from "@budibase/types"
import { APIClient } from "../api/types"
@ -71,6 +72,8 @@ export default abstract class BaseDataFetch<
options: DataFetchOptions<TQuery> & {
datasource: TDatasource
sortType: SortType | null
// Client side feature customisation
clientSideSearching: boolean
clientSideSorting: boolean
@ -103,6 +106,7 @@ export default abstract class BaseDataFetch<
// Sorting config
sortColumn: null,
sortOrder: SortOrder.ASCENDING,
sortType: null,
// Pagination config
paginate: true,
@ -223,12 +227,31 @@ export default abstract class BaseDataFetch<
this.options.sortColumn = this.getDefaultSortColumn(definition, schema)
}
// If we don't have a sort column specified then just ensure we don't set
// any sorting params
if (!this.options.sortColumn) {
this.options.sortOrder = SortOrder.ASCENDING
this.options.sortType = null
} else {
// Otherwise determine what sort type to use base on sort column
this.options.sortType = SortType.STRING
const fieldSchema = schema?.[this.options.sortColumn]
if (
fieldSchema?.type === FieldType.NUMBER ||
fieldSchema?.type === FieldType.BIGINT ||
("calculationType" in fieldSchema && fieldSchema?.calculationType)
) {
this.options.sortType = SortType.NUMBER
}
// If no sort order, default to ascending
if (!this.options.sortOrder) {
this.options.sortOrder = SortOrder.ASCENDING
} else {
// Ensure sortOrder matches the enum
this.options.sortOrder = this.options.sortOrder.toLowerCase() as SortOrder
this.options.sortOrder =
this.options.sortOrder.toLowerCase() as SortOrder
}
}
// Build the query
@ -271,6 +294,7 @@ export default abstract class BaseDataFetch<
const {
sortColumn,
sortOrder,
sortType,
limit,
clientSideSearching,
clientSideSorting,
@ -287,8 +311,8 @@ export default abstract class BaseDataFetch<
}
// If we don't support sorting, do a client-side sort
if (!this.features.supportsSort && clientSideSorting && sortColumn) {
rows = sort(rows, sortColumn, sortOrder)
if (!this.features.supportsSort && clientSideSorting && sortType) {
rows = sort(rows, sortColumn as any, sortOrder, sortType)
}
// If we don't support pagination, do a client-side limit

View File

@ -29,7 +29,8 @@ export default class TableFetch extends BaseDataFetch<TableDatasource, Table> {
}
async getData() {
const { datasource, limit, sortColumn, sortOrder, paginate } = this.options
const { datasource, limit, sortColumn, sortOrder, sortType, paginate } =
this.options
const { tableId } = datasource
const { cursor, query } = get(this.store)
@ -40,6 +41,7 @@ export default class TableFetch extends BaseDataFetch<TableDatasource, Table> {
limit,
sort: sortColumn,
sortOrder: sortOrder ?? SortOrder.ASCENDING,
sortType,
paginate,
bookmark: cursor,
})

View File

@ -1,5 +1,4 @@
import {
SearchViewRowRequest,
SortOrder,
ViewDatasource,
ViewV2Enriched,
@ -41,7 +40,8 @@ export default class ViewV2Fetch extends BaseDataFetch<
}
async getData() {
const { datasource, limit, sortColumn, sortOrder, paginate } = this.options
const { datasource, limit, sortColumn, sortOrder, sortType, paginate } =
this.options
const { cursor, query, definition } = get(this.store)
// If this is a calculation view and we have no calculations, return nothing
@ -68,13 +68,14 @@ export default class ViewV2Fetch extends BaseDataFetch<
}
try {
const request: SearchViewRowRequest = {
const request = {
query,
paginate,
limit,
bookmark: cursor,
sort: sortColumn,
sortOrder: sortOrder,
sortType,
}
if (paginate) {
const res = await this.API.viewV2.fetch(datasource.id, {

View File

@ -263,6 +263,7 @@ export async function search(ctx: Ctx<SearchRowRequest, SearchRowResponse>) {
limit: searchRequest.limit,
sort: searchRequest.sort ?? undefined,
sortOrder: searchRequest.sortOrder,
sortType: searchRequest.sortType ?? undefined,
countRows: searchRequest.countRows,
version: searchRequest.version,
disableEscaping: searchRequest.disableEscaping,

View File

@ -63,12 +63,14 @@ function getSortOptions(request: SearchViewRowRequest, view: ViewV2) {
return {
sort: request.sort,
sortOrder: request.sortOrder,
sortType: request.sortType ?? undefined,
}
}
if (view.sort) {
return {
sort: view.sort.field,
sortOrder: view.sort.order,
sortType: view.sort.type,
}
}

View File

@ -38,7 +38,7 @@ import {
import _ from "lodash"
import tk from "timekeeper"
import { encodeJSBinding } from "@budibase/string-templates"
import { dataFilters, InMemorySearchQuery } from "@budibase/shared-core"
import { dataFilters } from "@budibase/shared-core"
import { Knex } from "knex"
import { generator, structures, mocks } from "@budibase/backend-core/tests"
import { DEFAULT_EMPLOYEE_TABLE_SCHEMA } from "../../../db/defaultData/datasource_bb_default"
@ -200,26 +200,31 @@ if (descriptions.length) {
const isView = sourceType === "view"
class SearchAssertion {
constructor(
private readonly query: SearchRowRequest & {
sortType?: SortType
}
) {}
constructor(private readonly query: SearchRowRequest) {}
private async performSearch(): Promise<SearchResponse<Row>> {
if (isInMemory) {
const inMemoryQuery: RequiredKeys<InMemorySearchQuery> = {
const inMemoryQuery: RequiredKeys<
Omit<RowSearchParams, "tableId">
> = {
sort: this.query.sort ?? undefined,
query: { ...this.query.query },
paginate: this.query.paginate,
bookmark: this.query.bookmark ?? undefined,
limit: this.query.limit,
sortOrder: this.query.sortOrder,
sortType: this.query.sortType ?? undefined,
version: this.query.version,
disableEscaping: this.query.disableEscaping,
countRows: this.query.countRows,
viewId: undefined,
fields: undefined,
indexer: undefined,
rows: undefined,
}
return dataFilters.search(_.cloneDeep(rows), inMemoryQuery)
} else {
const { sortType, ...query } = this.query
return config.api.row.search(tableOrViewId, query)
return config.api.row.search(tableOrViewId, this.query)
}
}
@ -395,9 +400,7 @@ if (descriptions.length) {
}
}
function expectSearch(
query: SearchRowRequest & { sortType?: SortType }
) {
function expectSearch(query: SearchRowRequest) {
return new SearchAssertion(query)
}
@ -1116,7 +1119,6 @@ if (descriptions.length) {
}).toMatchExactly([{ name: "foo" }, { name: "bar" }])
})
isInMemory &&
describe("sortType STRING", () => {
it("sorts ascending", async () => {
await expectSearch({
@ -1317,7 +1319,6 @@ if (descriptions.length) {
})
})
isInMemory &&
describe("sortType NUMBER", () => {
it("sorts ascending", async () => {
await expectSearch({
@ -1472,7 +1473,6 @@ if (descriptions.length) {
}).toMatchExactly([{ dob: JAN_10TH }, { dob: JAN_1ST }])
})
isInMemory &&
describe("sortType STRING", () => {
it("sorts ascending", async () => {
await expectSearch({
@ -1639,7 +1639,6 @@ if (descriptions.length) {
])
})
isInMemory &&
describe("sortType STRING", () => {
it("sorts ascending", async () => {
await expectSearch({
@ -1676,7 +1675,6 @@ if (descriptions.length) {
})
})
!isInMemory &&
describe("datetime - date only", () => {
describe.each([true, false])(
"saved with timestamp: %s",
@ -1849,7 +1847,6 @@ if (descriptions.length) {
])
})
isInMemory &&
describe("sortType STRING", () => {
it("sorts ascending", async () => {
await expectSearch({

View File

@ -24,6 +24,7 @@ import {
SearchResponse,
SearchViewRowRequest,
SortOrder,
SortType,
StaticQuotaName,
Table,
TableSchema,
@ -153,6 +154,7 @@ if (descriptions.length) {
sort: {
field: "fieldToSort",
order: SortOrder.DESCENDING,
type: SortType.STRING,
},
schema: {
id: { visible: true },
@ -215,6 +217,7 @@ if (descriptions.length) {
sort: {
field: "fieldToSort",
order: SortOrder.DESCENDING,
type: SortType.STRING,
},
schema: {
id: { visible: true },
@ -1144,6 +1147,7 @@ if (descriptions.length) {
sort: {
field: generator.word(),
order: SortOrder.DESCENDING,
type: SortType.STRING,
},
schema: {
id: { visible: true },
@ -3149,6 +3153,7 @@ if (descriptions.length) {
{
field: string
order?: SortOrder
type?: SortType
},
string[]
][] = [
@ -3156,6 +3161,7 @@ if (descriptions.length) {
{
field: "name",
order: SortOrder.ASCENDING,
type: SortType.STRING,
},
["Alice", "Bob", "Charly", "Danny"],
],
@ -3172,6 +3178,22 @@ if (descriptions.length) {
},
["Danny", "Charly", "Bob", "Alice"],
],
[
{
field: "name",
order: SortOrder.DESCENDING,
type: SortType.STRING,
},
["Danny", "Charly", "Bob", "Alice"],
],
[
{
field: "age",
order: SortOrder.ASCENDING,
type: SortType.NUMBER,
},
["Danny", "Alice", "Charly", "Bob"],
],
[
{
field: "age",
@ -3182,13 +3204,15 @@ if (descriptions.length) {
[
{
field: "age",
order: SortOrder.DESCENDING,
},
["Danny", "Alice", "Charly", "Bob"],
["Bob", "Charly", "Alice", "Danny"],
],
[
{
field: "age",
order: SortOrder.DESCENDING,
type: SortType.NUMBER,
},
["Bob", "Charly", "Alice", "Danny"],
],
@ -3275,6 +3299,7 @@ if (descriptions.length) {
sort: {
field: "name",
order: SortOrder.ASCENDING,
type: SortType.STRING,
},
schema: viewSchema,
})
@ -3282,6 +3307,7 @@ if (descriptions.length) {
const response = await config.api.viewV2.search(view.id, {
sort: sortParams.field,
sortOrder: sortParams.order,
sortType: sortParams.type,
query: {},
})

View File

@ -46,6 +46,7 @@ export async function search(
query: options.query,
sort: options.sort,
sortOrder: options.sortOrder,
sortType: options.sortType,
limit: options.limit,
bookmark: options.bookmark,
paginate: options.paginate,

View File

@ -1,6 +1,5 @@
import {
Aggregation,
AutoFieldSubType,
CalculationType,
DocumentType,
EnrichedQueryJson,
@ -424,11 +423,7 @@ export async function search(
}
} else if (sortField) {
const sortType =
sortField.type === FieldType.NUMBER ||
(sortField.type === FieldType.AUTO &&
sortField.subtype === AutoFieldSubType.AUTO_ID)
? SortType.NUMBER
: SortType.STRING
sortField.type === FieldType.NUMBER ? SortType.NUMBER : SortType.STRING
request.sort = {
[mapToUserColumn(sortField.name)]: {
direction: params.sortOrder || SortOrder.ASCENDING,

View File

@ -11,6 +11,7 @@ import {
SortType,
FieldConstraints,
SortOrder,
RowSearchParams,
EmptyFilterOption,
SearchResponse,
Table,
@ -24,8 +25,6 @@ import {
isArraySearchOperator,
isRangeSearchOperator,
SearchFilter,
WithRequired,
SearchParams,
} from "@budibase/types"
import dayjs from "dayjs"
import { OperatorOptions, SqlNumberTypeRangeMap } from "./constants"
@ -522,19 +521,9 @@ export function fixupFilterArrays(filters: SearchFilters) {
return filters
}
type SearchQuery = WithRequired<
Pick<
SearchParams,
"query" | "sort" | "sortOrder" | "sortType" | "limit" | "countRows"
>,
"query"
>
export type InMemorySearchQuery = SearchQuery
export function search<T extends Record<string, any>>(
docs: T[],
query: SearchQuery
query: Omit<RowSearchParams, "tableId">
): SearchResponse<T> {
let result = runQuery(docs, query.query)
if (query.sort) {

View File

@ -1,6 +1,5 @@
export * from "./constants"
export * as dataFilters from "./filters"
export type * from "./filters"
export * as helpers from "./helpers"
export * as utils from "./utils"
export * as sdk from "./sdk"

View File

@ -8,7 +8,11 @@ import {
SearchFilterKey,
} from "../../../../sdk"
import { Row } from "../../../../documents"
import { PaginationResponse, SortOrder } from "../../../../api/web/pagination"
import {
PaginationResponse,
SortOrder,
SortType,
} from "../../../../api/web/pagination"
import { z } from "zod"
const fieldKey = z
@ -66,6 +70,7 @@ const searchRowRequest = z.object({
limit: z.number().optional(),
sort: z.string().nullish(),
sortOrder: z.nativeEnum(SortOrder).optional(),
sortType: z.nativeEnum(SortType).nullish(),
version: z.string().optional(),
disableEscaping: z.boolean().optional(),
countRows: z.boolean().optional(),
@ -78,6 +83,7 @@ export type SearchViewRowRequest = Pick<
SearchRowRequest,
| "sort"
| "sortOrder"
| "sortType"
| "limit"
| "bookmark"
| "paginate"

View File

@ -50,7 +50,7 @@ export interface SearchParams {
// when searching for rows we want a more extensive search type that requires certain properties
export interface RowSearchParams
extends WithRequired<Omit<SearchParams, "sortType">, "tableId" | "query"> {}
extends WithRequired<SearchParams, "tableId" | "query"> {}
export interface SearchResponse<T> {
rows: T[]