Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

feat: filter removal

Pranav C7ea028933f73
diff --git a/packages/nocodb-sdk/src/lib/filter/filterUtils.ts b/packages/nocodb-sdk/src/lib/filter/filterUtils.ts
index 21e8b49ed6..08fc2c00b4 100644
--- a/packages/nocodb-sdk/src/lib/filter/filterUtils.ts
+++ b/packages/nocodb-sdk/src/lib/filter/filterUtils.ts
@@ -1,29 +1,6 @@
-import {
- ClientType,
- getEquivalentUIType,
- isDateMonthFormat,
- isNumericCol,
- isSystemColumn,
- isVirtualCol,
- numericUITypes,
- SqlUiFactory,
- UITypes,
-} from '~/lib/UITypes';
-import type {
- Api,
- type ColumnType,
- type FilterType,
- type LinkToAnotherRecordType,
- type LookupType,
- type TableType,
-} from '~/lib/Api';
-
-export const MAX_NESTED_LEVEL = 5;
-export const excludedFilterColUidt = [
- UITypes.QrCode,
- UITypes.Barcode,
- UITypes.Button,
-];
+import { isNumericCol, numericUITypes, UITypes } from '~/lib';
+import type { Api, ColumnType, FilterType } from '~/lib/Api';
+import { isDateMonthFormat } from '~/lib';
export interface ComparisonOpUiType {
text: string;
@@ -186,7 +163,7 @@ const getLteText = (fieldUiType: UITypes) => {
export const comparisonOpList = (
fieldUiType: UITypes,
- dateFormat?: string
+ _dateFormat?: string
): ComparisonOpUiType[] => [
{
text: 'is checked',
@@ -840,209 +817,6 @@ export const isComparisonOpAllowed = (
return isNullOrEmptyOp ? showNullAndEmptyInFilter : true;
};
-export const isComparisonSubOpAllowed = (
- filter: ColumnFilterType,
- compOp: {
- text: string;
- value: string;
- ignoreVal?: boolean;
- includedTypes?: UITypes[];
- excludedTypes?: UITypes[];
- },
- uidt?: UITypes
-) => {
- if (compOp.includedTypes) {
- // include allowed values only if selected column type matches
- return filter.fk_column_id && compOp.includedTypes.includes(uidt!);
- } else if (compOp.excludedTypes) {
- // include not allowed values only if selected column type not matches
- return filter.fk_column_id && !compOp.excludedTypes.includes(uidt!);
- }
-};
-
-// filter is draft if it's not saved to db yet
-export const isFilterDraft = (filter: Filter, col: ColumnTypeForFilter) => {
- if (filter.id) return false;
-
- if (
- filter.comparison_op &&
- comparisonSubOpList(
- filter.comparison_op,
- parseProp(col?.meta)?.date_format
- ).find((compOp) => compOp.value === filter.comparison_sub_op)?.ignoreVal
- ) {
- return false;
- }
-
- if (
- comparisonOpList(
- (col.filterUidt ?? col.uidt) as UITypes,
- parseProp(col?.meta)?.date_format
- ).find((compOp) => compOp.value === filter.comparison_op)?.ignoreVal
- ) {
- return false;
- }
-
- if (filter.value) {
- return false;
- }
-
- return true;
-};
-
-export const isDynamicFilterAllowed = (
- filter: ColumnFilterType,
- column?: ColumnType,
- dbClientType?: ClientType
-) => {
- if (!column) {
- return false;
- }
- // if virtual column, don't allow dynamic filter
- if (isVirtualCol(column)) return false;
- const sqlUi = SqlUiFactory.create({ client: dbClientType ?? ClientType.PG });
-
- // disable dynamic filter for certain fields like rating, attachment, etc
- if (
- [
- UITypes.Attachment,
- UITypes.Rating,
- UITypes.Checkbox,
- UITypes.QrCode,
- UITypes.Barcode,
- UITypes.Collaborator,
- UITypes.GeoData,
- UITypes.SpecificDBType,
- ].includes(column.uidt as UITypes)
- )
- return false;
-
- const abstractType = sqlUi.getAbstractType(column);
-
- if (!['integer', 'float', 'text', 'string'].includes(abstractType))
- return false;
-
- return (
- !filter.comparison_op ||
- ['eq', 'lt', 'gt', 'lte', 'gte', 'like', 'nlike', 'neq'].includes(
- filter.comparison_op
- )
- );
-};
-
-export const getDynamicColumns = (
- metaColumns: ColumnType[],
- column?: ColumnType,
- dbClientType?: ClientType
-) => {
- if (!column) return [];
- const sqlUi = SqlUiFactory.create({ client: dbClientType ?? ClientType.PG });
-
- return metaColumns.filter((c: ColumnType) => {
- if (
- excludedFilterColUidt.includes(c.uidt as UITypes) ||
- isVirtualCol(c) ||
- (isSystemColumn(c) && !c.pk)
- ) {
- return false;
- }
-
- const dynamicColAbstractType = sqlUi.getAbstractType(c);
-
- const filterColAbstractType = sqlUi.getAbstractType(column);
-
- // treat float and integer as number
- if (
- [dynamicColAbstractType, filterColAbstractType].every((type) =>
- ['float', 'integer'].includes(type)
- )
- ) {
- return true;
- }
-
- // treat text and string as string
- if (
- [dynamicColAbstractType, filterColAbstractType].every((type) =>
- ['text', 'string'].includes(type)
- )
- ) {
- return true;
- }
-
- return filterColAbstractType === dynamicColAbstractType;
- });
-};
-
-export const getFilterUidt = (col: ColumnTypeForFilter): UITypes => {
- if (col.uidt === UITypes.Formula) {
- const formulaUIType = getEquivalentUIType({
- formulaColumn: col,
- });
-
- return (formulaUIType || col.uidt) as UITypes;
- }
- // if column is a lookup column, then use the lookup type extracted from the column
- else if (col.btLookupColumn) {
- return col.btLookupColumn.uidt as UITypes;
- } else {
- return col.uidt as UITypes;
- }
-};
-
-export const composeColumnsForFilter = async ({
- rootMeta,
- getMeta,
-}: {
- rootMeta: TableType;
- getMeta: (metaIdOrTitle: string) => Promise<TableType | null>;
-}) => {
- const result: ColumnTypeForFilter[] = [];
- for (const column of rootMeta.columns!) {
- if (column.uidt !== UITypes.Lookup) {
- result.push({ ...column, filterUidt: getFilterUidt(column) });
- continue;
- }
-
- let nextCol: ColumnType | undefined = column;
- // check all the relation of nested lookup columns is bt or not
- // include the column only if all only if all relations are bt
- while (nextCol && nextCol.uidt === UITypes.Lookup) {
- // extract the relation column meta
- const lookupRelation: ColumnType | undefined = (
- await getMeta(nextCol.fk_model_id!)
- )?.columns?.find(
- (c) =>
- c.id === (nextCol!.colOptions as LookupType).fk_relation_column_id
- );
- // this is less likely to happen but if relation column is not found then break the loop
- if (!lookupRelation) {
- break;
- }
-
- const relatedTableMeta: TableType | null = await getMeta(
- (lookupRelation?.colOptions as LinkToAnotherRecordType)
- .fk_related_model_id!
- );
- nextCol = relatedTableMeta?.columns?.find(
- (c) => c.id === (nextCol!.colOptions as LookupType).fk_lookup_column_id
- );
-
- // if next column is same as root lookup column then break the loop
- // since it's going to be a circular loop
- if (nextCol?.id === column.id) {
- break;
- }
- }
- const columnTypeForFilter: ColumnTypeForFilter = {
- ...column,
- btLookupColumn: nextCol,
- };
- columnTypeForFilter.filterUidt = getFilterUidt(columnTypeForFilter);
- result.push(columnTypeForFilter);
- }
- return result;
-};
-
export const getFilterCount = (filters: FilterType[]) => {
let result = 0;
for (const filter of filters) {
@@ -1054,50 +828,6 @@ export const getFilterCount = (filters: FilterType[]) => {
}
return result;
};
-
-export const adjustFilterWhenColumnChange = ({
- filter,
- column,
- showNullAndEmptyInFilter,
-}: {
- filter: ColumnFilterType;
- column: ColumnTypeForFilter;
- showNullAndEmptyInFilter?: boolean;
-}) => {
- const evalUidt: UITypes = column.filterUidt ?? column.uidt;
- if (isVirtualCol(column)) {
- filter.dynamic = false;
- filter.fk_value_col_id = null;
- } else {
- filter.fk_value_col_id = null;
- }
- filter.comparison_op = comparisonOpList(
- evalUidt,
- parseProp(column.meta)?.date_format
- ).find((compOp) =>
- isComparisonOpAllowed(
- filter,
- compOp,
- evalUidt as UITypes,
- showNullAndEmptyInFilter
- )
- )?.value;
-
- if (
- isDateType(evalUidt) &&
- !['blank', 'notblank'].includes(filter.comparison_op!)
- ) {
- if (filter.comparison_op === 'isWithin') {
- filter.comparison_sub_op = 'pastNumberOfDays';
- } else {
- filter.comparison_sub_op = 'exactDate';
- }
- } else {
- // reset
- filter.comparison_sub_op = null;
- }
-};
-
export const deleteFilterWithSub = async (
$api: Api<unknown>,
filter: FilterType
diff --git a/packages/nocodb/src/services/filter-operator-registry.service.ts b/packages/nocodb/src/services/filter-operator-registry.service.ts
index 0b9eaf38ed..45a1ff3154 100644
--- a/packages/nocodb/src/services/filter-operator-registry.service.ts
+++ b/packages/nocodb/src/services/filter-operator-registry.service.ts
@@ -1,56 +1,6 @@
import { Injectable } from '@nestjs/common';
-
-// Define essential types locally since SDK has import issues
-export enum UITypes {
- SingleLineText = 'SingleLineText',
- LongText = 'LongText',
- Number = 'Number',
- Decimal = 'Decimal',
- Currency = 'Currency',
- Percent = 'Percent',
- Date = 'Date',
- DateTime = 'DateTime',
- SingleSelect = 'SingleSelect',
- MultiSelect = 'MultiSelect',
- User = 'User',
- CreatedBy = 'CreatedBy',
- LastModifiedBy = 'LastModifiedBy',
- Collaborator = 'Collaborator',
- Email = 'Email',
- PhoneNumber = 'PhoneNumber',
- URL = 'URL',
- GeoData = 'GeoData',
- JSON = 'JSON',
- Rating = 'Rating',
- Duration = 'Duration',
- Year = 'Year',
- Time = 'Time',
- CreatedTime = 'CreatedTime',
- LastModifiedTime = 'LastModifiedTime',
- Checkbox = 'Checkbox',
- Attachment = 'Attachment',
- LinkToAnotherRecord = 'LinkToAnotherRecord',
- Links = 'Links',
- Lookup = 'Lookup',
- Rollup = 'Rollup',
- QrCode = 'QrCode',
- Barcode = 'Barcode',
- Button = 'Button',
- Formula = 'Formula',
- Geometry = 'Geometry',
-}
-
-// Define FilterType interface locally
-export interface FilterType {
- id?: string;
- fk_column_id?: string;
- comparison_op?: string;
- comparison_sub_op?: string;
- value?: any;
- logical_op?: string;
- is_group?: boolean;
- children?: FilterType[];
-}
+import { UITypes } from 'nocodb-sdk';
+import type { FilterType } from 'nocodb-sdk';
export interface FilterTransformationResult {
shouldRemove: boolean;
@@ -62,83 +12,59 @@ export interface FilterTransformationResult {
export class FilterOperatorRegistryService {
/**
* Check if an operator is compatible with a column type
- * This method uses a simplified compatibility matrix focused on filter transformation needs
+ * This uses the exact same logic as filterUtils.ts from the SDK
*/
- isOperatorCompatible(operator: string, columnType: UITypes): boolean {
- // Define a focused compatibility matrix for filter transformation
- const compatibilityMatrix: Record<string, UITypes[]> = {
- // Equality operators - work with most types
- eq: [UITypes.SingleLineText, UITypes.LongText, UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.SingleSelect, UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy, UITypes.Collaborator, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON, UITypes.Rating, UITypes.Duration, UITypes.Year, UITypes.Time, UITypes.CreatedTime, UITypes.LastModifiedTime, UITypes.Checkbox, UITypes.Attachment, UITypes.LinkToAnotherRecord, UITypes.Links, UITypes.Lookup, UITypes.Rollup],
- neq: [UITypes.SingleLineText, UITypes.LongText, UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.SingleSelect, UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy, UITypes.Collaborator, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON, UITypes.Rating, UITypes.Duration, UITypes.Year, UITypes.Time, UITypes.CreatedTime, UITypes.LastModifiedTime, UITypes.Checkbox, UITypes.Attachment, UITypes.LinkToAnotherRecord, UITypes.Links, UITypes.Lookup, UITypes.Rollup],
-
- // Text search operators - only for text-like types
- like: [UITypes.SingleLineText, UITypes.LongText, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON],
- nlike: [UITypes.SingleLineText, UITypes.LongText, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON],
-
- // Numeric comparison operators - only for numeric and date types
- gt: [UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year, UITypes.Duration, UITypes.Rating, UITypes.CreatedTime, UITypes.LastModifiedTime],
- gte: [UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year, UITypes.Duration, UITypes.Rating, UITypes.CreatedTime, UITypes.LastModifiedTime],
- lt: [UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year, UITypes.Duration, UITypes.Rating, UITypes.CreatedTime, UITypes.LastModifiedTime],
- lte: [UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year, UITypes.Duration, UITypes.Rating, UITypes.CreatedTime, UITypes.LastModifiedTime],
-
- // Multi-select operators
- anyof: [UITypes.SingleSelect, UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy],
- nanyof: [UITypes.SingleSelect, UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy],
- allof: [UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy],
- nallof: [UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy],
-
- // Checkbox operators
- checked: [UITypes.Checkbox],
- notchecked: [UITypes.Checkbox],
-
- // Null/empty operators - work with most types
- empty: [UITypes.SingleLineText, UITypes.LongText, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON],
- notempty: [UITypes.SingleLineText, UITypes.LongText, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON],
- null: [UITypes.SingleLineText, UITypes.LongText, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON, UITypes.Attachment, UITypes.LinkToAnotherRecord, UITypes.Links, UITypes.Lookup, UITypes.Rollup, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Collaborator],
- notnull: [UITypes.SingleLineText, UITypes.LongText, UITypes.Email, UITypes.PhoneNumber, UITypes.URL, UITypes.GeoData, UITypes.JSON, UITypes.Attachment, UITypes.LinkToAnotherRecord, UITypes.Links, UITypes.Lookup, UITypes.Rollup, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Collaborator],
-
- // Blank operators - work with most types
- blank: [UITypes.SingleLineText, UITypes.LongText, UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year, UITypes.PhoneNumber, UITypes.Email, UITypes.URL, UITypes.GeoData, UITypes.JSON, UITypes.Rating, UITypes.Duration, UITypes.SingleSelect, UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy, UITypes.Collaborator, UITypes.Attachment, UITypes.LinkToAnotherRecord, UITypes.Links, UITypes.Lookup, UITypes.Rollup, UITypes.QrCode, UITypes.Barcode, UITypes.Button, UITypes.Formula, UITypes.Geometry],
- notblank: [UITypes.SingleLineText, UITypes.LongText, UITypes.Number, UITypes.Decimal, UITypes.Currency, UITypes.Percent, UITypes.Date, UITypes.DateTime, UITypes.Time, UITypes.Year, UITypes.PhoneNumber, UITypes.Email, UITypes.URL, UITypes.GeoData, UITypes.JSON, UITypes.Rating, UITypes.Duration, UITypes.SingleSelect, UITypes.MultiSelect, UITypes.User, UITypes.CreatedBy, UITypes.LastModifiedBy, UITypes.Collaborator, UITypes.Attachment, UITypes.LinkToAnotherRecord, UITypes.Links, UITypes.Lookup, UITypes.Rollup, UITypes.QrCode, UITypes.Barcode, UITypes.Button, UITypes.Formula, UITypes.Geometry],
-
- // Date-specific operators
- isWithin: [UITypes.Date, UITypes.DateTime, UITypes.CreatedTime, UITypes.LastModifiedTime]
- };
-
- const allowedTypes = compatibilityMatrix[operator];
- return allowedTypes ? allowedTypes.includes(columnType) : false;
+ isOperatorCompatible(operator: string, columnType: string): boolean {
+ const { comparisonOpList } = require('nocodb-sdk');
+ const operators = comparisonOpList(columnType);
+ return operators.some((op) => op.value === operator);
}
/**
* Get operator compatibility between two column types
*/
- getOperatorCompatibilityBetweenTypes(fromType: UITypes, toType: UITypes) {
- // Get all operators that are compatible with both types
- const allOperators = ['eq', 'neq', 'like', 'nlike', 'gt', 'gte', 'lt', 'lte', 'anyof', 'nanyof', 'allof', 'nallof', 'checked', 'notchecked', 'empty', 'notempty', 'null', 'notnull', 'blank', 'notblank', 'isWithin'];
-
- return allOperators.filter(operator =>
- this.isOperatorCompatible(operator, fromType) && this.isOperatorCompatible(operator, toType)
- ).map(operator => ({
- value: operator,
- text: this.getOperatorDescription(operator)
- }));
+ getOperatorCompatibilityBetweenTypes(fromType: string, toType: string) {
+ const { comparisonOpList } = require('nocodb-sdk');
+
+ const fromOperators = comparisonOpList(fromType);
+ const toOperators = comparisonOpList(toType);
+
+ // Return operators that are compatible with both types
+ return fromOperators.filter((fromOp) =>
+ toOperators.some((toOp) => toOp.value === fromOp.value),
+ );
}
/**
* Get all operators that are compatible with a specific column type
*/
- getCompatibleOperators(columnType: UITypes): string[] {
- const allOperators = ['eq', 'neq', 'like', 'nlike', 'gt', 'gte', 'lt', 'lte', 'anyof', 'nanyof', 'allof', 'nallof', 'checked', 'notchecked', 'empty', 'notempty', 'null', 'notnull', 'blank', 'notblank', 'isWithin'];
-
- return allOperators.filter(operator => this.isOperatorCompatible(operator, columnType));
+ getCompatibleOperators(columnType: string): string[] {
+ const { comparisonOpList } = require('nocodb-sdk');
+
+ const operators = comparisonOpList(columnType);
+ return operators.map((op) => op.value);
}
/**
* Get operator compatibility information for a specific operator
*/
getOperatorCompatibility(operator: string) {
- const description = this.getOperatorDescription(operator);
- return { value: operator, text: description };
+ const { comparisonOpList } = require('nocodb-sdk');
+
+ const allOperators = comparisonOpList(UITypes.SingleLineText);
+ const foundOp = allOperators.find((op) => op.value === operator);
+
+ if (foundOp) {
+ return {
+ value: foundOp.value,
+ text: foundOp.text,
+ ignoreVal: foundOp.ignoreVal,
+ includedTypes: foundOp.includedTypes,
+ excludedTypes: foundOp.excludedTypes,
+ };
+ }
+
+ return { value: operator, text: operator };
}
/**
@@ -146,8 +72,8 @@ export class FilterOperatorRegistryService {
*/
validateFilterCompatibility(
filter: FilterType,
- fromType: UITypes,
- toType: UITypes,
+ fromType: string,
+ toType: string,
): FilterTransformationResult {
const operator = filter.comparison_op;
if (!operator) {
@@ -173,50 +99,25 @@ export class FilterOperatorRegistryService {
* Get all available operators
*/
getAllOperators(): string[] {
- return ['eq', 'neq', 'like', 'nlike', 'gt', 'gte', 'lt', 'lte', 'anyof', 'nanyof', 'allof', 'nallof', 'checked', 'notchecked', 'empty', 'notempty', 'null', 'notnull', 'blank', 'notblank', 'isWithin'];
+ const { comparisonOpList } = require('nocodb-sdk');
+
+ const allOperators = comparisonOpList(UITypes.SingleLineText);
+ return allOperators.map((op) => op.value);
}
/**
* Get operator descriptions
*/
getOperatorDescriptions(): Record<string, string> {
+ const { comparisonOpList } = require('nocodb-sdk');
+
+ const allOperators = comparisonOpList(UITypes.SingleLineText);
const descriptions: Record<string, string> = {};
- const allOperators = this.getAllOperators();
-
- for (const operator of allOperators) {
- descriptions[operator] = this.getOperatorDescription(operator);
+
+ for (const op of allOperators) {
+ descriptions[op.value] = op.text;
}
-
- return descriptions;
- }
- /**
- * Get operator description
- */
- private getOperatorDescription(operator: string): string {
- const descriptions: Record<string, string> = {
- eq: 'Equals',
- neq: 'Not equals',
- like: 'Contains',
- nlike: 'Does not contain',
- gt: 'Greater than',
- gte: 'Greater than or equal',
- lt: 'Less than',
- lte: 'Less than or equal',
- anyof: 'Contains any of',
- nanyof: 'Does not contain any of',
- allof: 'Contains all of',
- nallof: 'Does not contain all of',
- checked: 'Is checked',
- notchecked: 'Is not checked',
- empty: 'Is empty',
- notempty: 'Is not empty',
- null: 'Is null',
- notnull: 'Is not null',
- blank: 'Is blank',
- notblank: 'Is not blank',
- isWithin: 'Is within time period',
- };
- return descriptions[operator] || operator;
+ return descriptions;
}
}