Skip to content

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

refactor: cleanup

Pranav Cf7cb18d80aba
diff --git a/packages/nocodb-sdk/src/lib/columnHelper/columns/Time.spec.ts b/packages/nocodb-sdk/src/lib/columnHelper/columns/Time.spec.ts
index bf24e0a0b0..4e3aac00c7 100644
--- a/packages/nocodb-sdk/src/lib/columnHelper/columns/Time.spec.ts
+++ b/packages/nocodb-sdk/src/lib/columnHelper/columns/Time.spec.ts
@@ -1,6 +1,6 @@
-import { ColumnType } from '~/lib/Api';
-import UITypes from '~/lib/UITypes';
-import { TimeHelper } from './Time';
+import { ColumnType } from "~/lib/Api"
+import UITypes from "~/lib/UITypes"
+import { TimeHelper } from "./Time"
describe('columnHelper', () => {
describe('Time', () => {
@@ -9,17 +9,15 @@ describe('columnHelper', () => {
const column = {
uidt: UITypes.Time,
meta: {
- time_format: 'HH:mm',
- },
+ time_format: 'HH:mm'
+ }
} as ColumnType;
const a = '02:15';
const b = '1999-01-01 02:15:00+07:00';
- const result = new TimeHelper().equalityComparison(a, b, {
- col: column,
- });
- expect(result).toBe(true);
- });
- });
- });
-});
+ const result = new TimeHelper().equalityComparison(a, b, {col: column})
+ expect(result).toBe(true)
+ })
+ })
+ })
+})
diff --git a/packages/nocodb-sdk/src/lib/scripts/index.ts b/packages/nocodb-sdk/src/lib/scripts/index.ts
index d9fafcbb7b..f0d1d65ed3 100644
--- a/packages/nocodb-sdk/src/lib/scripts/index.ts
+++ b/packages/nocodb-sdk/src/lib/scripts/index.ts
@@ -1,5 +1,6 @@
// Placeholder fns for scripts
export const hasInputCalls = (_scriptContent: string): boolean => {
- return false;
-};
+ return false
+}
+
diff --git a/packages/nocodb/src/modules/noco.module.ts b/packages/nocodb/src/modules/noco.module.ts
index 8970976e7f..c0f4335a51 100644
--- a/packages/nocodb/src/modules/noco.module.ts
+++ b/packages/nocodb/src/modules/noco.module.ts
@@ -150,7 +150,6 @@ import { SortsV3Service } from '~/services/v3/sorts-v3.service';
import { TablesV3Service } from '~/services/v3/tables-v3.service';
import { ViewRowColorController } from '~/controllers/view-row-color.controller';
import { AttachmentUrlUploadHandler } from '~/services/emit-handler/attachment-url-upload.handler';
-import { FilterOperatorRegistryService } from '~/services/filter-operator-registry.service';
/* ACL */
import { AclMiddleware } from '~/middlewares/extract-ids/extract-ids.middleware';
@@ -342,9 +341,6 @@ export const nocoModuleMetadata = {
/* emit handlers */
AttachmentUrlUploadHandler,
-
- /* Filter transformation services */
- FilterOperatorRegistryService,
],
exports: [
/* Generic */
@@ -391,9 +387,6 @@ export const nocoModuleMetadata = {
DataAttachmentV3Service,
'IViewsV3Service',
- /* Filter transformation services */
- FilterOperatorRegistryService,
-
AttachmentUrlUploadHandler,
],
};
diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts
index c164bfe419..f0c43ebe9f 100644
--- a/packages/nocodb/src/services/columns.service.ts
+++ b/packages/nocodb/src/services/columns.service.ts
@@ -1,4 +1,4 @@
-import { forwardRef, Inject, Injectable } from '@nestjs/common';
+import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
import { pluralize, singularize } from 'inflection';
import {
AppEvents,
@@ -203,6 +203,8 @@ export interface CustomLinkProps {
@Injectable()
export class ColumnsService implements IColumnsService {
+ protected logger = new Logger(ColumnsService.name);
+
constructor(
protected readonly metaService: MetaService,
protected readonly appHooksService: AppHooksService,
@@ -1972,12 +1974,16 @@ export class ColumnsService implements IColumnsService {
await this.filtersService.transformFiltersForColumnTypeChange(
context,
column.id,
- column.uidt as UITypes,
colBody.uidt as UITypes,
+ column.uidt as UITypes,
+ sqlUi
);
} catch (error) {
// Log error but don't fail the column update
- console.error('Failed to transform filters for column type change:', error);
+ console.error(
+ 'Failed to transform filters for column type change:',
+ error.message,
+ );
}
}
diff --git a/packages/nocodb/src/services/filter-operator-registry.service.ts b/packages/nocodb/src/services/filter-operator-registry.service.ts
deleted file mode 100644
index 45a1ff3154..0000000000
--- a/packages/nocodb/src/services/filter-operator-registry.service.ts
+++ /dev/null
@@ -1,123 +0,0 @@
-import { Injectable } from '@nestjs/common';
-import { UITypes } from 'nocodb-sdk';
-import type { FilterType } from 'nocodb-sdk';
-
-export interface FilterTransformationResult {
- shouldRemove: boolean;
- transformedFilter?: FilterType;
- reason?: string;
-}
-
-@Injectable()
-export class FilterOperatorRegistryService {
- /**
- * Check if an operator is compatible with a column type
- * This uses the exact same logic as filterUtils.ts from the SDK
- */
- 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: 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: 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 { 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 };
- }
-
- /**
- * Validate if a filter is compatible with a column type change
- */
- validateFilterCompatibility(
- filter: FilterType,
- fromType: string,
- toType: string,
- ): FilterTransformationResult {
- const operator = filter.comparison_op;
- if (!operator) {
- return {
- shouldRemove: true,
- reason: 'Filter has no comparison operator',
- };
- }
-
- // Check if operator is compatible with target type
- if (this.isOperatorCompatible(operator, toType)) {
- return { shouldRemove: false, reason: 'Filter is compatible' };
- }
-
- // Operator is not compatible, filter must be removed
- return {
- shouldRemove: true,
- reason: `Operator ${operator} is not compatible with column type ${toType}`,
- };
- }
-
- /**
- * Get all available operators
- */
- getAllOperators(): string[] {
- 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> = {};
-
- for (const op of allOperators) {
- descriptions[op.value] = op.text;
- }
-
- return descriptions;
- }
-}
diff --git a/packages/nocodb/src/services/filters.service.ts b/packages/nocodb/src/services/filters.service.ts
index dc08e6cfb1..d11407b489 100644
--- a/packages/nocodb/src/services/filters.service.ts
+++ b/packages/nocodb/src/services/filters.service.ts
@@ -1,46 +1,24 @@
import { Injectable } from '@nestjs/common';
import RowColorCondition from 'src/models/RowColorCondition';
-import { AppEvents, EventType, UITypes } from 'nocodb-sdk';
+import { Injectable, Logger } from '@nestjs/common';
+import { AppEvents, comparisonOpList, EventType, UITypes } from 'nocodb-sdk';
import type { FilterReqType, FilterType, UserType, ViewType } from 'nocodb-sdk';
import type { NcContext, NcRequest } from '~/interface/config';
import type { ViewWebhookManager } from '~/utils/view-webhook-manager';
import type { MetaService } from 'src/meta/meta.service';
import { AppHooksService } from '~/services/app-hooks/app-hooks.service';
-import { FilterOperatorRegistryService } from '~/services/filter-operator-registry.service';
import { validatePayload } from '~/helpers';
import { NcError } from '~/helpers/catchError';
import NocoSocket from '~/socket/NocoSocket';
import { ViewWebhookManagerBuilder } from '~/utils/view-webhook-manager';
import { Column, Filter, Hook, View } from '~/models';
import Noco from '~/Noco';
-
-export interface FilterTransformationImpact {
- viewId: string;
- viewTitle: string;
- originalFilters: FilterType;
- transformedFilters: FilterType | null;
- removedFilters: FilterType[];
- modifiedFilters: FilterType[];
- reason: string;
-}
-
-export interface ColumnTypeChangeImpact {
- columnId: string;
- columnTitle: string;
- fromType: UITypes;
- toType: UITypes;
- impactedViews: FilterTransformationImpact[];
- totalViewsImpacted: number;
- totalFiltersRemoved: number;
- totalFiltersModified: number;
-}
+import { MetaTable } from '~/utils/globals';
@Injectable()
export class FiltersService {
- constructor(
- protected readonly appHooksService: AppHooksService,
- protected readonly filterOperatorRegistry: FilterOperatorRegistryService,
- ) {}
+ protected readonly logger = new Logger(FiltersService.name);
+ constructor(protected readonly appHooksService: AppHooksService) {}
async hookFilterCreate(
context: NcContext,
@@ -322,680 +300,178 @@ export class FiltersService {
}
/**
- * Analyze the impact of a column type change on all views and filters
- */
- async analyzeColumnTypeChangeImpact(
- context: NcContext,
- columnId: string,
- fromType: UITypes,
- toType: UITypes,
- ncMeta = Noco.ncMeta,
- ): Promise<ColumnTypeChangeImpact> {
- const column = await Column.get(context, { colId: columnId }, ncMeta);
- if (!column) {
- throw new Error(`Column ${columnId} not found`);
- }
-
- // Get all views that reference this column
- const views = await this.getViewsReferencingColumn(
- context,
- columnId,
- ncMeta,
- );
-
- const impactedViews: FilterTransformationImpact[] = [];
- let totalFiltersRemoved = 0;
- let totalFiltersModified = 0;
-
- for (const view of views) {
- const impact = await this.analyzeViewFilterImpact(
- context,
- view,
- columnId,
- fromType,
- toType,
- ncMeta,
- );
-
- if (impact) {
- impactedViews.push(impact);
- totalFiltersRemoved += impact.removedFilters.length;
- totalFiltersModified += impact.modifiedFilters.length;
- }
- }
-
- return {
- columnId,
- columnTitle: column.title,
- fromType,
- toType,
- impactedViews,
- totalViewsImpacted: impactedViews.length,
- totalFiltersRemoved,
- totalFiltersModified,
- };
- }
-
- /**
- * Transform all filters for a column type change
+ * Transform filters for a column type change - remove incompatible ones
+ * Works for all filter types: hook, link, grid, etc.
*/
async transformFiltersForColumnTypeChange(
context: NcContext,
columnId: string,
- fromType: UITypes,
- toType: UITypes,
+ newColumnType: UITypes,
+ oldColumnType: UITypes,
+ sqlUi: any,
ncMeta = Noco.ncMeta,
): Promise<void> {
- const impact = await this.analyzeColumnTypeChangeImpact(
+ // Get all filters that reference this column from all tables
+ const filtersToCheck = await this.getAllFiltersForColumn(
context,
columnId,
- fromType,
- toType,
ncMeta,
);
- // Apply transformations to each impacted view
- for (const viewImpact of impact.impactedViews) {
- await this.applyFilterTransformationToView(
- context,
- viewImpact.viewId,
- viewImpact.transformedFilters,
- ncMeta,
+ // Remove incompatible filters
+ for (const filter of filtersToCheck) {
+ const validation = this.validateFilterForTypeChange(
+ filter,
+ newColumnType,
+ oldColumnType,
+ sqlUi,
);
- }
-
- // Log the transformation
- await this.logFilterTransformation(context, impact, ncMeta);
- }
-
- /**
- * Get all views that reference a specific column
- */
- private async getViewsReferencingColumn(
- context: NcContext,
- columnId: string,
- ncMeta = Noco.ncMeta,
- ): Promise<ViewType[]> {
- // Get the column to find its model ID
- const column = await Column.get(context, { colId: columnId }, ncMeta);
- if (!column) {
- throw new Error(`Column ${columnId} not found`);
- }
-
- // Get all views in the model
- const views = await View.list(context, column.fk_model_id, ncMeta);
-
- const viewsWithFilters: ViewType[] = [];
-
- for (const view of views) {
- try {
- const filters = await Filter.getFilterObject(
- context,
- {
- viewId: view.id,
- },
- ncMeta,
- );
-
- // Check if this view has filters that reference the column
- if (this.viewReferencesColumn(filters, columnId)) {
- viewsWithFilters.push(view);
- }
- } catch (error) {
- // Log warning but continue processing other views
- console.warn(
- `Failed to get filters for view ${view.id}: ${error.message}`,
- );
+ console.log(validation);
+ if (validation.shouldRemove) {
+ await this.deleteFilter(context, filter.id, ncMeta);
}
}
-
- return viewsWithFilters;
}
/**
- * Check if a view's filters reference a specific column
+ * Get all filters that reference a specific column from all filter tables
*/
- private viewReferencesColumn(filters: FilterType, columnId: string): boolean {
- if (!filters || !filters.children) {
- return false;
- }
-
- return this.filterTreeReferencesColumn(filters.children, columnId);
- }
-
- /**
- * Recursively check if a filter tree references a specific column
- */
- private filterTreeReferencesColumn(
- filters: FilterType[],
- columnId: string,
- ): boolean {
- for (const filter of filters) {
- if (filter.fk_column_id === columnId) {
- return true;
- }
-
- if (filter.children && filter.children.length > 0) {
- if (this.filterTreeReferencesColumn(filter.children, columnId)) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- /**
- * Analyze the impact of a column type change on a specific view
- */
- private async analyzeViewFilterImpact(
+ private async getAllFiltersForColumn(
context: NcContext,
- view: ViewType,
columnId: string,
- fromType: UITypes,
- toType: UITypes,
- ncMeta = Noco.ncMeta,
- ): Promise<FilterTransformationImpact | null> {
- try {
- const originalFilters = await Filter.getFilterObject(
- context,
- {
- viewId: view.id,
- },
- ncMeta,
- );
-
- if (!originalFilters || !originalFilters.children) {
- return null;
- }
-
- const transformedFilters = await this.transformFilterTree(
- context,
- originalFilters.children,
- columnId,
- fromType,
- toType,
- ncMeta,
- );
-
- const removedFilters = this.getRemovedFilters(
- originalFilters.children,
- transformedFilters,
- columnId,
- );
-
- const modifiedFilters = this.getModifiedFilters(
- originalFilters.children,
- transformedFilters,
- columnId,
- );
-
- // Only return impact if there are actual changes
- if (removedFilters.length === 0 && modifiedFilters.length === 0) {
- return null;
- }
-
- return {
- viewId: view.id,
- viewTitle: view.title,
- originalFilters,
- transformedFilters:
- transformedFilters.length > 0
- ? {
- ...originalFilters,
- children: transformedFilters,
- }
- : null,
- removedFilters,
- modifiedFilters,
- reason: this.getTransformationReason(
- fromType,
- toType,
- removedFilters,
- modifiedFilters,
- ),
- };
- } catch (error) {
- console.warn(`Failed to analyze view ${view.id}: ${error.message}`);
- return null;
- }
- }
-
- /**
- * Transform a filter tree for a column type change
- */
- private async transformFilterTree(
- context: NcContext,
- filters: FilterType[],
- columnId: string,
- fromType: UITypes,
- toType: UITypes,
ncMeta = Noco.ncMeta,
): Promise<FilterType[]> {
- const transformedFilters: FilterType[] = [];
-
- for (const filter of filters) {
- if (filter.is_group && filter.children) {
- // Recursively transform group filters
- const transformedChildren = await this.transformFilterTree(
- context,
- filter.children,
- columnId,
- fromType,
- toType,
- ncMeta,
- );
-
- if (transformedChildren.length > 0) {
- transformedFilters.push({
- ...filter,
- children: transformedChildren,
- });
- }
- } else if (filter.fk_column_id === columnId) {
- // Transform individual filter
- const transformedFilter = this.transformIndividualFilter(
- filter,
- fromType,
- toType,
- );
-
- if (transformedFilter) {
- transformedFilters.push(transformedFilter);
- }
- } else {
- // Keep filter unchanged
- transformedFilters.push(filter);
- }
- }
+ const filters: FilterType[] = [];
- return transformedFilters;
- }
-
- /**
- * Transform an individual filter for a column type change
- */
- private transformIndividualFilter(
- filter: FilterType,
- fromType: UITypes,
- toType: UITypes,
- ): FilterType | null {
- // Check if operator is still valid for the new type
- if (
- !this.filterOperatorRegistry.isOperatorCompatible(
- filter.comparison_op!,
- toType,
- )
- ) {
- return null; // Remove filter if operator is not compatible
- }
-
- // For incompatible type changes (like number to string, string to number),
- // we simply delete the filter instead of trying to convert it
- if (this.isIncompatibleTypeChange(fromType, toType)) {
- return null; // Remove filter for incompatible type changes
- }
-
- // Keep the filter unchanged if it's compatible
- return filter;
- }
-
- /**
- * Check if a type change is incompatible and should result in filter deletion
- */
- private isIncompatibleTypeChange(
- fromType: UITypes,
- toType: UITypes,
- ): boolean {
- // Number to String or String to Number - incompatible
- if (
- [
- UITypes.Number,
- UITypes.Decimal,
- UITypes.Currency,
- UITypes.Percent,
- UITypes.Year,
- UITypes.Duration,
- UITypes.Rating,
- ].includes(fromType) &&
- [
- UITypes.SingleLineText,
- UITypes.LongText,
- UITypes.Email,
- UITypes.PhoneNumber,
- UITypes.URL,
- UITypes.GeoData,
- UITypes.JSON,
- ].includes(toType)
- ) {
- return true;
- }
-
- if (
- [
- UITypes.SingleLineText,
- UITypes.LongText,
- UITypes.Email,
- UITypes.PhoneNumber,
- UITypes.URL,
- UITypes.GeoData,
- UITypes.JSON,
- ].includes(fromType) &&
- [
- UITypes.Number,
- UITypes.Decimal,
- UITypes.Currency,
- UITypes.Percent,
- UITypes.Year,
- UITypes.Duration,
- UITypes.Rating,
- ].includes(toType)
- ) {
- return true;
- }
-
- // Select to LinkToAnotherRecord - incompatible
- if (
- [UITypes.SingleSelect, UITypes.MultiSelect].includes(fromType) &&
- [UITypes.LinkToAnotherRecord, UITypes.Links].includes(toType)
- ) {
- return true;
- }
-
- // Date/Time to Text - incompatible
- if (
- [
- UITypes.Date,
- UITypes.DateTime,
- UITypes.Time,
- UITypes.CreatedTime,
- UITypes.LastModifiedTime,
- ].includes(fromType) &&
- [
- UITypes.SingleLineText,
- UITypes.LongText,
- UITypes.Email,
- UITypes.PhoneNumber,
- UITypes.URL,
- UITypes.GeoData,
- UITypes.JSON,
- ].includes(toType)
- ) {
- return true;
- }
-
- // Text to Date/Time - incompatible
- if (
- [
- UITypes.SingleLineText,
- UITypes.LongText,
- UITypes.Email,
- UITypes.PhoneNumber,
- UITypes.URL,
- UITypes.GeoData,
- UITypes.JSON,
- ].includes(fromType) &&
- [
- UITypes.Date,
- UITypes.DateTime,
- UITypes.Time,
- UITypes.CreatedTime,
- UITypes.LastModifiedTime,
- ].includes(toType)
- ) {
- return true;
- }
-
- // Checkbox to Text - incompatible
- if (
- fromType === UITypes.Checkbox &&
- [
- UITypes.SingleLineText,
- UITypes.LongText,
- UITypes.Email,
- UITypes.PhoneNumber,
- UITypes.URL,
- UITypes.GeoData,
- UITypes.JSON,
- ].includes(toType)
- ) {
- return true;
- }
-
- // Text to Checkbox - incompatible
- if (
- [
- UITypes.SingleLineText,
- UITypes.LongText,
- UITypes.Email,
- UITypes.PhoneNumber,
- UITypes.URL,
- UITypes.GeoData,
- UITypes.JSON,
- ].includes(fromType) &&
- toType === UITypes.Checkbox
- ) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Get filters that were removed during transformation
- */
- private getRemovedFilters(
- originalFilters: FilterType[],
- transformedFilters: FilterType[],
- columnId: string,
- ): FilterType[] {
- const removed: FilterType[] = [];
-
- for (const originalFilter of originalFilters) {
- if (originalFilter.fk_column_id === columnId) {
- const stillExists = transformedFilters.some(
- (tf) => tf.id === originalFilter.id && tf.fk_column_id === columnId,
- );
-
- if (!stillExists) {
- removed.push(originalFilter);
- }
- }
- }
-
- return removed;
- }
-
- /**
- * Get filters that were modified during transformation
- */
- private getModifiedFilters(
- originalFilters: FilterType[],
- transformedFilters: FilterType[],
- columnId: string,
- ): FilterType[] {
- const modified: FilterType[] = [];
-
- for (const originalFilter of originalFilters) {
- if (originalFilter.fk_column_id === columnId) {
- const transformedFilter = transformedFilters.find(
- (tf) => tf.id === originalFilter.id && tf.fk_column_id === columnId,
- );
-
- if (
- transformedFilter &&
- this.filtersAreDifferent(originalFilter, transformedFilter)
- ) {
- modified.push(originalFilter);
- }
+ try {
+ // Query nc_filter table directly for all filters referencing this column
+ const filterResults = await ncMeta
+ .knex(MetaTable.FILTER_EXP)
+ .where({
+ base_id: context.base_id,
+ ...(context.workspace_id
+ ? { fk_workspace_id: context.workspace_id }
+ : {}),
+ })
+ .where('fk_column_id', columnId)
+ .select('*');
+
+ // Convert to FilterType objects
+ for (const row of filterResults) {
+ filters.push({
+ id: row.id,
+ fk_column_id: row.fk_column_id,
+ comparison_op: row.comparison_op,
+ comparison_sub_op: row.comparison_sub_op,
+ value: row.value,
+ logical_op: row.logical_op,
+ is_group: row.is_group,
+ fk_view_id: row.fk_view_id,
+ fk_hook_id: row.fk_hook_id,
+ fk_parent_id: row.fk_parent_id,
+ children: [], // Will be populated if needed
+ });
}
+ } catch (error) {
+ console.warn(
+ `Failed to query filters for column ${columnId}: ${error.message}`,
+ );
}
- return modified;
- }
-
- /**
- * Check if two filters are different
- */
- private filtersAreDifferent(
- filter1: FilterType,
- filter2: FilterType,
- ): boolean {
- return (
- filter1.comparison_op !== filter2.comparison_op ||
- filter1.comparison_sub_op !== filter2.comparison_sub_op ||
- filter1.value !== filter2.value ||
- filter1.logical_op !== filter2.logical_op
- );
- }
-
- /**
- * Get a human-readable reason for the transformation
- */
- private getTransformationReason(
- fromType: UITypes,
- toType: UITypes,
- removedFilters: FilterType[],
- modifiedFilters: FilterType[],
- ): string {
- if (removedFilters.length > 0 && modifiedFilters.length === 0) {
- return `Column type changed from ${fromType} to ${toType}. ${removedFilters.length} filter(s) removed due to incompatibility.`;
- } else if (removedFilters.length === 0 && modifiedFilters.length > 0) {
- return `Column type changed from ${fromType} to ${toType}. ${modifiedFilters.length} filter(s) kept unchanged.`;
- } else if (removedFilters.length > 0 && modifiedFilters.length > 0) {
- return `Column type changed from ${fromType} to ${toType}. ${removedFilters.length} filter(s) removed due to incompatibility, ${modifiedFilters.length} filter(s) kept unchanged.`;
- } else {
- return `Column type changed from ${fromType} to ${toType}. No filters were affected.`;
- }
+ return filters;
}
/**
- * Apply transformed filters to a view
+ * Delete a filter directly from the database
*/
- private async applyFilterTransformationToView(
+ private async deleteFilter(
context: NcContext,
- viewId: string,
- transformedFilters: FilterType | null,
+ filterId: string,
ncMeta = Noco.ncMeta,
): Promise<void> {
try {
- if (transformedFilters === null) {
- // Remove all filters for this view
- await Filter.deleteAll(context, viewId, ncMeta);
- } else {
- // Update filters for this view
- await this.updateViewFilters(
- context,
- viewId,
- transformedFilters,
- ncMeta,
- );
- }
+ // Delete directly from nc_filters table
+ await Filter.delete(context, filterId, ncMeta);
} catch (error) {
- console.error(
- `Failed to apply filter transformation to view ${viewId}: ${error.message}`,
- );
+ console.warn(`Failed to delete filter ${filterId}: ${error.message}`);
throw error;
}
}
/**
- * Update filters for a view
+ * Check if an operator is compatible with a column type
*/
- private async updateViewFilters(
- context: NcContext,
- viewId: string,
- filters: FilterType,
- ncMeta = Noco.ncMeta,
- ): Promise<void> {
- // First, remove all existing filters
- await Filter.deleteAll(context, viewId, ncMeta);
+ isOperatorCompatible(
+ operator: string,
+ columnType: UITypes,
+ oldColumnType: UITypes,
+ ): boolean {
+ const operators = comparisonOpList(columnType);
+ const oldOperators = comparisonOpList(oldColumnType);
- // Then, insert the new transformed filters
- if (filters.children && filters.children.length > 0) {
- await this.insertFilterTree(
- context,
- viewId,
- filters.children,
- null,
- ncMeta,
- );
- }
+ const isInOldOperators = oldOperators.find((op) => op.value === operator);
+
+ return operators.some(
+ (op) =>
+ op.value === operator &&
+ (isInOldOperators?.text === op.text) && (
+ (op.includedTypes && op.includedTypes?.includes(columnType)) ||
+ (op.excludedTypes && !op.excludedTypes?.includes(columnType)),
+ ),
+ );
}
+ }
/**
- * Recursively insert a filter tree
+ * Validate if a filter should be removed due to column type change
*/
- private async insertFilterTree(
- context: NcContext,
- viewId: string,
- filters: FilterType[],
- parentId: string | null,
- ncMeta = Noco.ncMeta,
- ): Promise<void> {
- for (const filter of filters) {
- const insertData = {
- ...filter,
- fk_view_id: viewId,
- fk_parent_id: parentId,
+ validateFilterForTypeChange(
+ filter: FilterType,
+ newColumnType: UITypes,
+ oldColumnType: UITypes,
+ sqlUI: any,
+ ): { shouldRemove: boolean; reason?: string } {
+ const operator = filter.comparison_op;
+ if (!operator) {
+ return {
+ shouldRemove: true,
+ reason: 'Filter has no comparison operator',
};
-
- const insertedFilter = await Filter.insert(context, insertData, ncMeta);
-
- if (filter.children && filter.children.length > 0) {
- await this.insertFilterTree(
- context,
- viewId,
- filter.children,
- insertedFilter.id,
- ncMeta,
- );
- }
}
- }
- /**
- * Log the filter transformation for audit purposes
- */
- private async logFilterTransformation(
- context: NcContext,
- impact: ColumnTypeChangeImpact,
- ncMeta = Noco.ncMeta,
- ): Promise<void> {
- // This could be extended to write to an audit log table
+ // check if abstract type changed
+ const oldAbstractType = sqlUI.getAbstractType({ uidt: oldColumnType });
+ const newAbstractType = sqlUI.getAbstractType({ uidt: newColumnType });
+
console.log(
- `Filter transformation completed for column ${impact.columnTitle}: ${impact.totalViewsImpacted} views impacted, ${impact.totalFiltersRemoved} filters removed, ${impact.totalFiltersModified} filters modified`,
+ newColumnType,
+ oldColumnType,
+ `Validating filter ${filter.id}: ${oldAbstractType} -> ${newAbstractType}, operator: ${operator}`,
);
- }
-
- /**
- * Get a summary of the transformation impact for user display
- */
- getTransformationSummary(impact: ColumnTypeChangeImpact): string {
- const { totalViewsImpacted, totalFiltersRemoved, totalFiltersModified } =
- impact;
-
- let summary = `Column type change will impact ${totalViewsImpacted} view(s). `;
-
- if (totalFiltersRemoved > 0) {
- summary += `${totalFiltersRemoved} filter(s) will be removed. `;
- }
-
- if (totalFiltersModified > 0) {
- summary += `${totalFiltersModified} filter(s) will be modified. `;
+ if (oldAbstractType !== newAbstractType) {
+ return {
+ shouldRemove: true,
+ reason: `Column abstract type changed from ${oldAbstractType} to ${newAbstractType}`,
+ };
}
- if (totalFiltersRemoved === 0 && totalFiltersModified === 0) {
- summary += 'No filters will be affected.';
+ // Check if operator is compatible with new type
+ if (this.isOperatorCompatible(operator, newColumnType, oldColumnType)) {
+ return { shouldRemove: false };
}
- return summary;
+ // Operator is not compatible, filter must be removed
+ return {
+ shouldRemove: true,
+ reason: `Operator ${operator} is not compatible with column type ${newColumnType}`,
+ };
}
/**
- * Check if a column type change requires user confirmation
+ * Get all operators compatible with a column type
*/
- requiresUserConfirmation(impact: ColumnTypeChangeImpact): boolean {
- return impact.totalFiltersRemoved > 0 || impact.totalFiltersModified > 0;
+ getCompatibleOperators(columnType: UITypes): string[] {
+ const operators = comparisonOpList(columnType);
+ return operators.map((op) => op.value);
}
}