Merge master.
This commit is contained in:
commit
725096c9d1
|
@ -23,6 +23,7 @@ jobs:
|
|||
PAYLOAD_BRANCH: ${{ github.head_ref }}
|
||||
PAYLOAD_PR_NUMBER: ${{ github.event.pull_request.number }}
|
||||
PAYLOAD_LICENSE_TYPE: "free"
|
||||
PAYLOAD_DEPLOY: "true"
|
||||
with:
|
||||
repository: budibase/budibase-deploys
|
||||
event: featurebranch-qa-deploy
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
||||
"version": "2.32.10",
|
||||
"version": "2.32.11",
|
||||
"npmClient": "yarn",
|
||||
"packages": [
|
||||
"packages/*",
|
||||
|
|
|
@ -124,9 +124,11 @@ export async function buildSqlFieldList(
|
|||
([columnName, column]) =>
|
||||
column.type !== FieldType.LINK &&
|
||||
column.type !== FieldType.FORMULA &&
|
||||
!existing.find((field: string) => field === columnName)
|
||||
!existing.find(
|
||||
(field: string) => field === `${table.name}.${columnName}`
|
||||
)
|
||||
)
|
||||
.map(column => `${table.name}.${column[0]}`)
|
||||
.map(([columnName]) => `${table.name}.${columnName}`)
|
||||
}
|
||||
|
||||
let fields: string[] = []
|
||||
|
|
|
@ -3,6 +3,8 @@ import {
|
|||
ViewV2,
|
||||
SearchRowResponse,
|
||||
SearchViewRowRequest,
|
||||
RequiredKeys,
|
||||
RowSearchParams,
|
||||
} from "@budibase/types"
|
||||
import sdk from "../../../sdk"
|
||||
import { context } from "@budibase/backend-core"
|
||||
|
@ -20,30 +22,34 @@ export async function searchView(
|
|||
ctx.throw(400, `This method only supports viewsV2`)
|
||||
}
|
||||
|
||||
const viewFields = Object.entries(view.schema || {})
|
||||
.filter(([_, value]) => value.visible)
|
||||
.map(([key]) => key)
|
||||
const { body } = ctx.request
|
||||
|
||||
await context.ensureSnippetContext(true)
|
||||
|
||||
const result = await sdk.rows.search(
|
||||
{
|
||||
viewId: view.id,
|
||||
tableId: view.tableId,
|
||||
query: body.query,
|
||||
...getSortOptions(body, view),
|
||||
limit: body.limit,
|
||||
bookmark: body.bookmark,
|
||||
paginate: body.paginate,
|
||||
countRows: body.countRows,
|
||||
},
|
||||
{
|
||||
user: sdk.users.getUserContextBindings(ctx.user),
|
||||
}
|
||||
)
|
||||
const searchOptions: RequiredKeys<SearchViewRowRequest> &
|
||||
RequiredKeys<
|
||||
Pick<RowSearchParams, "tableId" | "viewId" | "query" | "fields">
|
||||
> = {
|
||||
tableId: view.tableId,
|
||||
viewId: view.id,
|
||||
query: body.query,
|
||||
fields: viewFields,
|
||||
...getSortOptions(body, view),
|
||||
limit: body.limit,
|
||||
bookmark: body.bookmark,
|
||||
paginate: body.paginate,
|
||||
countRows: body.countRows,
|
||||
}
|
||||
|
||||
const result = await sdk.rows.search(searchOptions, {
|
||||
user: sdk.users.getUserContextBindings(ctx.user),
|
||||
})
|
||||
result.rows.forEach(r => (r._viewId = view.id))
|
||||
ctx.body = result
|
||||
}
|
||||
|
||||
function getSortOptions(request: SearchViewRowRequest, view: ViewV2) {
|
||||
if (request.sort) {
|
||||
return {
|
||||
|
|
|
@ -129,6 +129,7 @@ export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
|
|||
name: view.name,
|
||||
tableId: view.tableId,
|
||||
query: view.query,
|
||||
queryUI: view.queryUI,
|
||||
sort: view.sort,
|
||||
schema,
|
||||
primaryDisplay: view.primaryDisplay,
|
||||
|
@ -164,6 +165,7 @@ export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
|
|||
version: view.version,
|
||||
tableId: view.tableId,
|
||||
query: view.query,
|
||||
queryUI: view.queryUI,
|
||||
sort: view.sort,
|
||||
schema,
|
||||
primaryDisplay: view.primaryDisplay,
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -22,10 +22,10 @@ import {
|
|||
RelationshipType,
|
||||
TableSchema,
|
||||
RenameColumn,
|
||||
ViewFieldMetadata,
|
||||
FeatureFlag,
|
||||
BBReferenceFieldSubType,
|
||||
NumericCalculationFieldMetadata,
|
||||
ViewV2Schema,
|
||||
} from "@budibase/types"
|
||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
||||
|
@ -155,7 +155,7 @@ describe.each([
|
|||
})
|
||||
|
||||
it("can persist views with all fields", async () => {
|
||||
const newView: Required<CreateViewRequest> = {
|
||||
const newView: Required<Omit<CreateViewRequest, "queryUI">> = {
|
||||
name: generator.name(),
|
||||
tableId: table._id!,
|
||||
primaryDisplay: "id",
|
||||
|
@ -612,7 +612,7 @@ describe.each([
|
|||
it("can update all fields", async () => {
|
||||
const tableId = table._id!
|
||||
|
||||
const updatedData: Required<UpdateViewRequest> = {
|
||||
const updatedData: Required<Omit<UpdateViewRequest, "queryUI">> = {
|
||||
version: view.version,
|
||||
id: view.id,
|
||||
tableId,
|
||||
|
@ -1359,10 +1359,7 @@ describe.each([
|
|||
return table
|
||||
}
|
||||
|
||||
const createView = async (
|
||||
tableId: string,
|
||||
schema: Record<string, ViewFieldMetadata>
|
||||
) =>
|
||||
const createView = async (tableId: string, schema: ViewV2Schema) =>
|
||||
await config.api.viewV2.create({
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
|
|
|
@ -23,8 +23,8 @@ import {
|
|||
Row,
|
||||
Table,
|
||||
TableSchema,
|
||||
ViewFieldMetadata,
|
||||
ViewV2,
|
||||
ViewV2Schema,
|
||||
} from "@budibase/types"
|
||||
import sdk from "../../sdk"
|
||||
import { helpers } from "@budibase/shared-core"
|
||||
|
@ -262,7 +262,7 @@ export async function squashLinks<T = Row[] | Row>(
|
|||
FeatureFlag.ENRICHED_RELATIONSHIPS
|
||||
)
|
||||
|
||||
let viewSchema: Record<string, ViewFieldMetadata> = {}
|
||||
let viewSchema: ViewV2Schema = {}
|
||||
if (sdk.views.isView(source)) {
|
||||
if (helpers.views.isCalculationView(source)) {
|
||||
return enriched
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import {
|
||||
EmptyFilterOption,
|
||||
LegacyFilter,
|
||||
LogicalOperator,
|
||||
Row,
|
||||
RowSearchParams,
|
||||
SearchFilterKey,
|
||||
SearchFilters,
|
||||
SearchResponse,
|
||||
SortOrder,
|
||||
Table,
|
||||
|
@ -61,7 +61,6 @@ export async function search(
|
|||
if (options.viewId) {
|
||||
source = await sdk.views.get(options.viewId)
|
||||
table = await sdk.views.getTable(source)
|
||||
options = searchInputMapping(table, options)
|
||||
} else if (options.tableId) {
|
||||
source = await sdk.tables.getTable(options.tableId)
|
||||
table = source
|
||||
|
@ -74,7 +73,7 @@ export async function search(
|
|||
if (options.query) {
|
||||
const visibleFields = (
|
||||
options.fields || Object.keys(table.schema)
|
||||
).filter(field => table.schema[field].visible !== false)
|
||||
).filter(field => table.schema[field]?.visible !== false)
|
||||
|
||||
const queryableFields = await getQueryableFields(table, visibleFields)
|
||||
options.query = removeInvalidFilters(options.query, queryableFields)
|
||||
|
@ -82,39 +81,59 @@ export async function search(
|
|||
options.query = {}
|
||||
}
|
||||
|
||||
// need to make sure filters in correct shape before checking for view
|
||||
options = searchInputMapping(table, options)
|
||||
|
||||
if (options.viewId) {
|
||||
const view = await sdk.views.get(options.viewId)
|
||||
// Delete extraneous search params that cannot be overridden
|
||||
delete options.query.onEmptyFilter
|
||||
|
||||
const view = source as ViewV2
|
||||
// Enrich saved query with ephemeral query params.
|
||||
// We prevent searching on any fields that are saved as part of the query, as
|
||||
// that could let users find rows they should not be allowed to access.
|
||||
let viewQuery = dataFilters.buildQuery(view.query || [])
|
||||
let viewQuery = dataFilters.buildQueryLegacy(view.query) || {}
|
||||
delete viewQuery?.onEmptyFilter
|
||||
|
||||
if (!isExternalTable && !(await features.flags.isEnabled("SQS"))) {
|
||||
// Lucene does not accept conditional filters, so we need to keep the old logic
|
||||
const query: SearchFilters = viewQuery
|
||||
const sqsEnabled = await features.flags.isEnabled("SQS")
|
||||
const supportsLogicalOperators =
|
||||
isExternalTableID(view.tableId) || sqsEnabled
|
||||
if (!supportsLogicalOperators) {
|
||||
// In the unlikely event that a Grouped Filter is in a non-SQS environment
|
||||
// It needs to be ignored entirely
|
||||
let queryFilters: LegacyFilter[] = Array.isArray(view.query)
|
||||
? view.query
|
||||
: []
|
||||
|
||||
delete options.query.onEmptyFilter
|
||||
|
||||
// Extract existing fields
|
||||
const existingFields =
|
||||
view.query
|
||||
queryFilters
|
||||
?.filter(filter => filter.field)
|
||||
.map(filter => db.removeKeyNumbering(filter.field)) || []
|
||||
|
||||
viewQuery ??= {}
|
||||
// Carry over filters for unused fields
|
||||
Object.keys(options.query || {}).forEach(key => {
|
||||
Object.keys(options.query).forEach(key => {
|
||||
const operator = key as Exclude<SearchFilterKey, LogicalOperator>
|
||||
Object.keys(options.query[operator] || {}).forEach(field => {
|
||||
if (!existingFields.includes(db.removeKeyNumbering(field))) {
|
||||
query[operator]![field] = options.query[operator]![field]
|
||||
viewQuery![operator]![field] = options.query[operator]![field]
|
||||
}
|
||||
})
|
||||
})
|
||||
options.query = query
|
||||
options.query = viewQuery
|
||||
} else {
|
||||
const conditions = viewQuery ? [viewQuery] : []
|
||||
options.query = {
|
||||
$and: {
|
||||
conditions: [viewQuery, options.query],
|
||||
conditions: [...conditions, options.query],
|
||||
},
|
||||
}
|
||||
if (viewQuery.onEmptyFilter) {
|
||||
options.query.onEmptyFilter = viewQuery.onEmptyFilter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,8 +162,6 @@ export async function search(
|
|||
options.sortOrder = options.sortOrder.toLowerCase() as SortOrder
|
||||
}
|
||||
|
||||
options = searchInputMapping(table, options)
|
||||
|
||||
let result: SearchResponse<Row>
|
||||
if (isExternalTable) {
|
||||
span?.addTags({ searchType: "external" })
|
||||
|
|
|
@ -107,7 +107,9 @@ export function searchInputMapping(table: Table, options: RowSearchParams) {
|
|||
}
|
||||
return dataFilters.recurseLogicalOperators(filters, checkFilters)
|
||||
}
|
||||
options.query = checkFilters(options.query)
|
||||
if (options.query) {
|
||||
options.query = checkFilters(options.query)
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
|
|
|
@ -317,7 +317,7 @@ export async function enrichSchema(
|
|||
// if nothing specified in view, then it is not visible
|
||||
const ui = viewSchema[key] || { visible: false }
|
||||
schema[key] = {
|
||||
...(tableSchema[key] || {}),
|
||||
...tableSchema[key],
|
||||
...ui,
|
||||
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key]?.order,
|
||||
columns: undefined,
|
||||
|
|
|
@ -7,11 +7,11 @@ import {
|
|||
BulkImportRequest,
|
||||
BulkImportResponse,
|
||||
SearchRowResponse,
|
||||
RowSearchParams,
|
||||
DeleteRows,
|
||||
DeleteRow,
|
||||
PaginatedSearchRowResponse,
|
||||
RowExportFormat,
|
||||
SearchRowRequest,
|
||||
} from "@budibase/types"
|
||||
import { Expectations, TestAPI } from "./base"
|
||||
|
||||
|
@ -136,7 +136,7 @@ export class RowAPI extends TestAPI {
|
|||
)
|
||||
}
|
||||
|
||||
search = async <T extends RowSearchParams>(
|
||||
search = async <T extends SearchRowRequest>(
|
||||
sourceId: string,
|
||||
params?: T,
|
||||
expectations?: Expectations
|
||||
|
|
|
@ -3,7 +3,7 @@ import {
|
|||
BBReferenceFieldSubType,
|
||||
FieldType,
|
||||
FormulaType,
|
||||
SearchFilter,
|
||||
LegacyFilter,
|
||||
SearchFilters,
|
||||
SearchQueryFields,
|
||||
ArrayOperator,
|
||||
|
@ -19,9 +19,12 @@ import {
|
|||
RangeOperator,
|
||||
LogicalOperator,
|
||||
isLogicalSearchOperator,
|
||||
SearchFilterGroup,
|
||||
FilterGroupLogicalOperator,
|
||||
} from "@budibase/types"
|
||||
import dayjs from "dayjs"
|
||||
import { OperatorOptions, SqlNumberTypeRangeMap } from "./constants"
|
||||
import { processSearchFilters } from "./utils"
|
||||
import { deepGet, schema } from "./helpers"
|
||||
import { isPlainObject, isEmpty } from "lodash"
|
||||
import { decodeNonAscii } from "./helpers/schema"
|
||||
|
@ -124,7 +127,7 @@ export function recurseLogicalOperators(
|
|||
fn: (f: SearchFilters) => SearchFilters
|
||||
) {
|
||||
for (const logical of LOGICAL_OPERATORS) {
|
||||
if (filters?.[logical]) {
|
||||
if (filters[logical]) {
|
||||
filters[logical]!.conditions = filters[logical]!.conditions.map(
|
||||
condition => fn(condition)
|
||||
)
|
||||
|
@ -160,9 +163,6 @@ export function recurseSearchFilters(
|
|||
* https://github.com/Budibase/budibase/issues/10118
|
||||
*/
|
||||
export const cleanupQuery = (query: SearchFilters) => {
|
||||
if (!query) {
|
||||
return query
|
||||
}
|
||||
for (let filterField of NoEmptyFilterStrings) {
|
||||
if (!query[filterField]) {
|
||||
continue
|
||||
|
@ -304,10 +304,143 @@ export class ColumnSplitter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Builds a JSON query from the filter structure generated in the builder
|
||||
* Builds a JSON query from the filter a SearchFilter definition
|
||||
* @param filter the builder filter structure
|
||||
*/
|
||||
export const buildQuery = (filter: SearchFilter[]) => {
|
||||
|
||||
const buildCondition = (expression: LegacyFilter) => {
|
||||
// Filter body
|
||||
let query: SearchFilters = {
|
||||
string: {},
|
||||
fuzzy: {},
|
||||
range: {},
|
||||
equal: {},
|
||||
notEqual: {},
|
||||
empty: {},
|
||||
notEmpty: {},
|
||||
contains: {},
|
||||
notContains: {},
|
||||
oneOf: {},
|
||||
containsAny: {},
|
||||
}
|
||||
let { operator, field, type, value, externalType, onEmptyFilter } = expression
|
||||
|
||||
if (!operator || !field) {
|
||||
return
|
||||
}
|
||||
|
||||
const queryOperator = operator as SearchFilterOperator
|
||||
const isHbs =
|
||||
typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0
|
||||
// Parse all values into correct types
|
||||
if (operator === "allOr") {
|
||||
query.allOr = true
|
||||
return
|
||||
}
|
||||
if (onEmptyFilter) {
|
||||
query.onEmptyFilter = onEmptyFilter
|
||||
return
|
||||
}
|
||||
|
||||
// Default the value for noValue fields to ensure they are correctly added
|
||||
// to the final query
|
||||
if (queryOperator === "empty" || queryOperator === "notEmpty") {
|
||||
value = null
|
||||
}
|
||||
|
||||
if (
|
||||
type === "datetime" &&
|
||||
!isHbs &&
|
||||
queryOperator !== "empty" &&
|
||||
queryOperator !== "notEmpty"
|
||||
) {
|
||||
// Ensure date value is a valid date and parse into correct format
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
value = new Date(value).toISOString()
|
||||
} catch (error) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if (type === "number" && typeof value === "string" && !isHbs) {
|
||||
if (queryOperator === "oneOf") {
|
||||
value = value.split(",").map(item => parseFloat(item))
|
||||
} else {
|
||||
value = parseFloat(value)
|
||||
}
|
||||
}
|
||||
if (type === "boolean") {
|
||||
value = `${value}`?.toLowerCase() === "true"
|
||||
}
|
||||
if (
|
||||
["contains", "notContains", "containsAny"].includes(
|
||||
operator.toLocaleString()
|
||||
) &&
|
||||
type === "array" &&
|
||||
typeof value === "string"
|
||||
) {
|
||||
value = value.split(",")
|
||||
}
|
||||
if (operator.toLocaleString().startsWith("range") && query.range) {
|
||||
const minint =
|
||||
SqlNumberTypeRangeMap[externalType as keyof typeof SqlNumberTypeRangeMap]
|
||||
?.min || Number.MIN_SAFE_INTEGER
|
||||
const maxint =
|
||||
SqlNumberTypeRangeMap[externalType as keyof typeof SqlNumberTypeRangeMap]
|
||||
?.max || Number.MAX_SAFE_INTEGER
|
||||
if (!query.range[field]) {
|
||||
query.range[field] = {
|
||||
low: type === "number" ? minint : "0000-00-00T00:00:00.000Z",
|
||||
high: type === "number" ? maxint : "9999-00-00T00:00:00.000Z",
|
||||
}
|
||||
}
|
||||
if (operator === "rangeLow" && value != null && value !== "") {
|
||||
query.range[field] = {
|
||||
...query.range[field],
|
||||
low: value,
|
||||
}
|
||||
} else if (operator === "rangeHigh" && value != null && value !== "") {
|
||||
query.range[field] = {
|
||||
...query.range[field],
|
||||
high: value,
|
||||
}
|
||||
}
|
||||
} else if (isLogicalSearchOperator(queryOperator)) {
|
||||
// TODO
|
||||
} else if (query[queryOperator] && operator !== "onEmptyFilter") {
|
||||
if (type === "boolean") {
|
||||
// Transform boolean filters to cope with null.
|
||||
// "equals false" needs to be "not equals true"
|
||||
// "not equals false" needs to be "equals true"
|
||||
if (queryOperator === "equal" && value === false) {
|
||||
query.notEqual = query.notEqual || {}
|
||||
query.notEqual[field] = true
|
||||
} else if (queryOperator === "notEqual" && value === false) {
|
||||
query.equal = query.equal || {}
|
||||
query.equal[field] = true
|
||||
} else {
|
||||
query[queryOperator] ??= {}
|
||||
query[queryOperator]![field] = value
|
||||
}
|
||||
} else {
|
||||
query[queryOperator] ??= {}
|
||||
query[queryOperator]![field] = value
|
||||
}
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
export const buildQueryLegacy = (
|
||||
filter?: LegacyFilter[] | SearchFilters
|
||||
): SearchFilters | undefined => {
|
||||
// this is of type SearchFilters or is undefined
|
||||
if (!Array.isArray(filter)) {
|
||||
return filter
|
||||
}
|
||||
|
||||
let query: SearchFilters = {
|
||||
string: {},
|
||||
fuzzy: {},
|
||||
|
@ -368,13 +501,15 @@ export const buildQuery = (filter: SearchFilter[]) => {
|
|||
value = `${value}`?.toLowerCase() === "true"
|
||||
}
|
||||
if (
|
||||
["contains", "notContains", "containsAny"].includes(operator) &&
|
||||
["contains", "notContains", "containsAny"].includes(
|
||||
operator.toLocaleString()
|
||||
) &&
|
||||
type === "array" &&
|
||||
typeof value === "string"
|
||||
) {
|
||||
value = value.split(",")
|
||||
}
|
||||
if (operator.startsWith("range") && query.range) {
|
||||
if (operator.toLocaleString().startsWith("range") && query.range) {
|
||||
const minint =
|
||||
SqlNumberTypeRangeMap[
|
||||
externalType as keyof typeof SqlNumberTypeRangeMap
|
||||
|
@ -401,7 +536,7 @@ export const buildQuery = (filter: SearchFilter[]) => {
|
|||
}
|
||||
}
|
||||
} else if (isLogicalSearchOperator(queryOperator)) {
|
||||
// TODO
|
||||
// ignore
|
||||
} else if (query[queryOperator] && operator !== "onEmptyFilter") {
|
||||
if (type === "boolean") {
|
||||
// Transform boolean filters to cope with null.
|
||||
|
@ -423,10 +558,60 @@ export const buildQuery = (filter: SearchFilter[]) => {
|
|||
}
|
||||
}
|
||||
})
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a **SearchFilterGroup** filter definition into a grouped
|
||||
* search query of type **SearchFilters**
|
||||
*
|
||||
* Legacy support remains for the old **SearchFilter[]** format.
|
||||
* These will be migrated to an appropriate **SearchFilters** object, if encountered
|
||||
*
|
||||
* @param filter
|
||||
*
|
||||
* @returns {SearchFilters}
|
||||
*/
|
||||
|
||||
export const buildQuery = (
|
||||
filter?: SearchFilterGroup | LegacyFilter[]
|
||||
): SearchFilters | undefined => {
|
||||
const parsedFilter: SearchFilterGroup | undefined =
|
||||
processSearchFilters(filter)
|
||||
|
||||
if (!parsedFilter) {
|
||||
return
|
||||
}
|
||||
|
||||
const operatorMap: { [key in FilterGroupLogicalOperator]: LogicalOperator } =
|
||||
{
|
||||
[FilterGroupLogicalOperator.ALL]: LogicalOperator.AND,
|
||||
[FilterGroupLogicalOperator.ANY]: LogicalOperator.OR,
|
||||
}
|
||||
|
||||
const globalOnEmpty = parsedFilter.onEmptyFilter
|
||||
? parsedFilter.onEmptyFilter
|
||||
: null
|
||||
|
||||
const globalOperator: LogicalOperator =
|
||||
operatorMap[parsedFilter.logicalOperator as FilterGroupLogicalOperator]
|
||||
|
||||
return {
|
||||
...(globalOnEmpty ? { onEmptyFilter: globalOnEmpty } : {}),
|
||||
[globalOperator]: {
|
||||
conditions: parsedFilter.groups?.map((group: SearchFilterGroup) => {
|
||||
return {
|
||||
[operatorMap[group.logicalOperator]]: {
|
||||
conditions: group.filters
|
||||
?.map(x => buildCondition(x))
|
||||
.filter(filter => filter),
|
||||
},
|
||||
}
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// The frontend can send single values for array fields sometimes, so to handle
|
||||
// this we convert them to arrays at the controller level so that nothing below
|
||||
// this has to worry about the non-array values.
|
||||
|
|
|
@ -1,5 +1,17 @@
|
|||
import { ArrayOperator, BasicOperator, SearchFilters } from "@budibase/types"
|
||||
import {
|
||||
LegacyFilter,
|
||||
SearchFilterGroup,
|
||||
FilterGroupLogicalOperator,
|
||||
SearchFilters,
|
||||
BasicOperator,
|
||||
ArrayOperator,
|
||||
} from "@budibase/types"
|
||||
import * as Constants from "./constants"
|
||||
import { removeKeyNumbering } from "./filters"
|
||||
|
||||
// an array of keys from filter type to properties that are in the type
|
||||
// this can then be converted using .fromEntries to an object
|
||||
type AllowedFilters = [keyof LegacyFilter, LegacyFilter[keyof LegacyFilter]][]
|
||||
|
||||
export function unreachable(
|
||||
value: never,
|
||||
|
@ -104,3 +116,98 @@ export function isSupportedUserSearch(query: SearchFilters) {
|
|||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the filter config. Filters are migrated from
|
||||
* SearchFilter[] to SearchFilterGroup
|
||||
*
|
||||
* If filters is not an array, the migration is skipped
|
||||
*
|
||||
* @param {LegacyFilter[] | SearchFilterGroup} filters
|
||||
*/
|
||||
export const processSearchFilters = (
|
||||
filters: LegacyFilter[] | SearchFilterGroup | undefined
|
||||
): SearchFilterGroup | undefined => {
|
||||
if (!filters) {
|
||||
return
|
||||
}
|
||||
|
||||
// Base search config.
|
||||
const defaultCfg: SearchFilterGroup = {
|
||||
logicalOperator: FilterGroupLogicalOperator.ALL,
|
||||
groups: [],
|
||||
}
|
||||
|
||||
const filterAllowedKeys = [
|
||||
"field",
|
||||
"operator",
|
||||
"value",
|
||||
"type",
|
||||
"externalType",
|
||||
"valueType",
|
||||
"noValue",
|
||||
"formulaType",
|
||||
]
|
||||
|
||||
if (Array.isArray(filters)) {
|
||||
let baseGroup: SearchFilterGroup = {
|
||||
filters: [],
|
||||
logicalOperator: FilterGroupLogicalOperator.ALL,
|
||||
}
|
||||
|
||||
return filters.reduce((acc: SearchFilterGroup, filter: LegacyFilter) => {
|
||||
// Sort the properties for easier debugging
|
||||
const filterPropertyKeys = (Object.keys(filter) as (keyof LegacyFilter)[])
|
||||
.sort((a, b) => {
|
||||
return a.localeCompare(b)
|
||||
})
|
||||
.filter(key => key in filter)
|
||||
|
||||
if (filterPropertyKeys.length == 1) {
|
||||
const key = filterPropertyKeys[0],
|
||||
value = filter[key]
|
||||
// Global
|
||||
if (key === "onEmptyFilter") {
|
||||
// unset otherwise
|
||||
acc.onEmptyFilter = value
|
||||
} else if (key === "operator" && value === "allOr") {
|
||||
// Group 1 logical operator
|
||||
baseGroup.logicalOperator = FilterGroupLogicalOperator.ANY
|
||||
}
|
||||
|
||||
return acc
|
||||
}
|
||||
|
||||
const allowedFilterSettings: AllowedFilters = filterPropertyKeys.reduce(
|
||||
(acc: AllowedFilters, key) => {
|
||||
const value = filter[key]
|
||||
if (filterAllowedKeys.includes(key)) {
|
||||
if (key === "field") {
|
||||
acc.push([key, removeKeyNumbering(value)])
|
||||
} else {
|
||||
acc.push([key, value])
|
||||
}
|
||||
}
|
||||
return acc
|
||||
},
|
||||
[]
|
||||
)
|
||||
|
||||
const migratedFilter: LegacyFilter = Object.fromEntries(
|
||||
allowedFilterSettings
|
||||
) as LegacyFilter
|
||||
|
||||
baseGroup.filters!.push(migratedFilter)
|
||||
|
||||
if (!acc.groups || !acc.groups.length) {
|
||||
// init the base group
|
||||
acc.groups = [baseGroup]
|
||||
}
|
||||
|
||||
return acc
|
||||
}, defaultCfg)
|
||||
} else if (!filters?.groups) {
|
||||
return
|
||||
}
|
||||
return filters
|
||||
}
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
import { FieldType } from "../../documents"
|
||||
import { EmptyFilterOption, SearchFilters } from "../../sdk"
|
||||
import {
|
||||
EmptyFilterOption,
|
||||
FilterGroupLogicalOperator,
|
||||
SearchFilters,
|
||||
} from "../../sdk"
|
||||
|
||||
export type SearchFilter = {
|
||||
export type LegacyFilter = {
|
||||
operator: keyof SearchFilters | "rangeLow" | "rangeHigh"
|
||||
onEmptyFilter?: EmptyFilterOption
|
||||
field: string
|
||||
|
@ -9,3 +13,11 @@ export type SearchFilter = {
|
|||
value: any
|
||||
externalType?: string
|
||||
}
|
||||
|
||||
// this is a type purely used by the UI
|
||||
export type SearchFilterGroup = {
|
||||
logicalOperator: FilterGroupLogicalOperator
|
||||
onEmptyFilter?: EmptyFilterOption
|
||||
groups?: SearchFilterGroup[]
|
||||
filters?: LegacyFilter[]
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { SearchFilter, SortOrder, SortType } from "../../api"
|
||||
import { LegacyFilter, SearchFilterGroup, SortOrder, SortType } from "../../api"
|
||||
import { UIFieldMetadata } from "./table"
|
||||
import { Document } from "../document"
|
||||
import { DBView } from "../../sdk"
|
||||
import { DBView, SearchFilters } from "../../sdk"
|
||||
|
||||
export type ViewTemplateOpts = {
|
||||
field: string
|
||||
|
@ -85,15 +85,19 @@ export interface ViewV2 {
|
|||
name: string
|
||||
primaryDisplay?: string
|
||||
tableId: string
|
||||
query?: SearchFilter[]
|
||||
query?: LegacyFilter[] | SearchFilters
|
||||
// duplicate to store UI information about filters
|
||||
queryUI?: SearchFilterGroup
|
||||
sort?: {
|
||||
field: string
|
||||
order?: SortOrder
|
||||
type?: SortType
|
||||
}
|
||||
schema?: Record<string, ViewFieldMetadata>
|
||||
schema?: ViewV2Schema
|
||||
}
|
||||
|
||||
export type ViewV2Schema = Record<string, ViewFieldMetadata>
|
||||
|
||||
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema
|
||||
|
||||
export interface ViewCountOrSumSchema {
|
||||
|
|
|
@ -191,6 +191,11 @@ export enum EmptyFilterOption {
|
|||
RETURN_NONE = "none",
|
||||
}
|
||||
|
||||
export enum FilterGroupLogicalOperator {
|
||||
ALL = "all",
|
||||
ANY = "any",
|
||||
}
|
||||
|
||||
export enum SqlClient {
|
||||
MS_SQL = "mssql",
|
||||
POSTGRES = "pg",
|
||||
|
|
22
yarn.lock
22
yarn.lock
|
@ -2051,7 +2051,7 @@
|
|||
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
|
||||
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
|
||||
|
||||
"@budibase/backend-core@2.32.10":
|
||||
"@budibase/backend-core@2.32.11":
|
||||
version "0.0.0"
|
||||
dependencies:
|
||||
"@budibase/nano" "10.1.5"
|
||||
|
@ -2132,15 +2132,15 @@
|
|||
through2 "^2.0.0"
|
||||
|
||||
"@budibase/pro@npm:@budibase/pro@latest":
|
||||
version "2.32.10"
|
||||
resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-2.32.10.tgz#19fcbb3ced74791a7e96dfdc5a1270165792eea5"
|
||||
integrity sha512-TbVp2bjmA0rHK+TKi9NVW06+O23fhDm7IJ/FlpWPHIBIZW7xDkCYu6LUOhSwSWMbOTcWzaJFuMbpN1HoTc/YjQ==
|
||||
version "2.32.11"
|
||||
resolved "https://registry.yarnpkg.com/@budibase/pro/-/pro-2.32.11.tgz#c94d534f829ca0ef252677757e157a7e58b87b4d"
|
||||
integrity sha512-mOkqJpqHKWsfTWZwWcvBCYFUIluSUHltQNinc1ZRsg9rC3OKoHSDop6gzm744++H/GzGRN8V86kLhCgtNIlkpA==
|
||||
dependencies:
|
||||
"@anthropic-ai/sdk" "^0.27.3"
|
||||
"@budibase/backend-core" "2.32.10"
|
||||
"@budibase/shared-core" "2.32.10"
|
||||
"@budibase/string-templates" "2.32.10"
|
||||
"@budibase/types" "2.32.10"
|
||||
"@budibase/backend-core" "2.32.11"
|
||||
"@budibase/shared-core" "2.32.11"
|
||||
"@budibase/string-templates" "2.32.11"
|
||||
"@budibase/types" "2.32.11"
|
||||
"@koa/router" "8.0.8"
|
||||
bull "4.10.1"
|
||||
dd-trace "5.2.0"
|
||||
|
@ -2153,13 +2153,13 @@
|
|||
scim-patch "^0.8.1"
|
||||
scim2-parse-filter "^0.2.8"
|
||||
|
||||
"@budibase/shared-core@2.32.10":
|
||||
"@budibase/shared-core@2.32.11":
|
||||
version "0.0.0"
|
||||
dependencies:
|
||||
"@budibase/types" "0.0.0"
|
||||
cron-validate "1.4.5"
|
||||
|
||||
"@budibase/string-templates@2.32.10":
|
||||
"@budibase/string-templates@2.32.11":
|
||||
version "0.0.0"
|
||||
dependencies:
|
||||
"@budibase/handlebars-helpers" "^0.13.2"
|
||||
|
@ -2167,7 +2167,7 @@
|
|||
handlebars "^4.7.8"
|
||||
lodash.clonedeep "^4.5.0"
|
||||
|
||||
"@budibase/types@2.32.10":
|
||||
"@budibase/types@2.32.11":
|
||||
version "0.0.0"
|
||||
dependencies:
|
||||
scim-patch "^0.8.1"
|
||||
|
|
Loading…
Reference in New Issue