1
0
Fork 0
mirror of synced 2024-10-02 18:16:29 +13:00

Smarter getValidOperatorsForType

This commit is contained in:
Adria Navarro 2023-10-03 14:26:12 +02:00
parent b2429e1fe0
commit c84d2449f2
2 changed files with 28 additions and 26 deletions

View file

@ -184,26 +184,22 @@
}
const getValidOperatorsForType = filter => {
const fieldSchema = getSchema(filter)
const type =
fieldSchema.type !== FieldType.BB_REFERENCE
? field.type
: {
type: fieldSchema.type,
multiple:
fieldSchema.relationshipType === RelationshipType.MANY_TO_MANY,
}
let operators = LuceneUtils.getValidOperatorsForType(
filter.type,
type,
filter.field,
datasource
)
const fieldSchema = getSchema(filter)
if (
fieldSchema.type === FieldType.BB_REFERENCE &&
fieldSchema.relationshipType !== RelationshipType.MANY_TO_MANY
) {
operators = operators.filter(
o =>
![
OperatorOptions.Contains.value,
OperatorOptions.NotContains.value,
].includes(o.value)
)
}
return operators
}
</script>

View file

@ -12,12 +12,22 @@ import { deepGet } from "./helpers"
const HBS_REGEX = /{{([^{].*?)}}/g
type RequestedFieldType =
| Exclude<FieldType, FieldType.BB_REFERENCE>
| { type: FieldType.BB_REFERENCE; multiple: boolean }
export function isFieldType(
r: RequestedFieldType
): r is Exclude<FieldType, FieldType.BB_REFERENCE> {
return typeof r === "string" && Object.values(FieldType).includes(r)
}
/**
* Returns the valid operator options for a certain data type
* @param type the data type
*/
export const getValidOperatorsForType = (
type: FieldType,
type: RequestedFieldType,
field: string,
datasource: Datasource & { tableId: any } // TODO: is this table id ever populated?
) => {
@ -60,16 +70,12 @@ export const getValidOperatorsForType = (
ops = numOps
} else if (type === FieldType.FORMULA) {
ops = stringOps.concat([Op.MoreThan, Op.LessThan])
} else if (type === FieldType.BB_REFERENCE) {
ops = [
Op.Equals,
Op.NotEquals,
Op.Empty,
Op.NotEmpty,
Op.Contains,
Op.NotContains,
Op.ContainsAny,
]
} else if (!isFieldType(type) && type.type === FieldType.BB_REFERENCE) {
if (type.multiple) {
ops = [Op.Contains, Op.NotContains, Op.ContainsAny, Op.Empty, Op.NotEmpty]
} else {
ops = [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty, Op.ContainsAny]
}
}
// Only allow equal/not equal for _id in SQL tables