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 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( let operators = LuceneUtils.getValidOperatorsForType(
filter.type, type,
filter.field, filter.field,
datasource 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 return operators
} }
</script> </script>

View File

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