Browse Source

refactor(nocodb): extract remove filters logic

pull/5106/head
Wing-Kam Wong 2 years ago
parent
commit
bb3a83c662
  1. 170
      packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0104003.ts

170
packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0104003.ts

@ -14,9 +14,88 @@ import { UITypes } from 'nocodb-sdk';
// - remove `like`, `null`, `equal` and `empty` for multiSelect columns
// - remove `>`, `<`, `>=`, `<=`, `is empty`, `is not empty`, `is equal`, `is not equal` for attachment columns
const removeEqualFilters = async (
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `is equal`, `is not equal`
if (['eq', 'neq'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta));
if (migrateFn) migrateFn();
}
return actions;
};
const removeArithmeticFilters = async (
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `>`, `<`, `>=`, `<=`
if (['gt', 'lt', 'gte', 'lte'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta));
if (migrateFn) migrateFn();
}
return actions;
};
const removeLikeFilters = async (
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `is like`, `is not like`
if (['like', 'nlike'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta));
if (migrateFn) migrateFn();
}
return actions;
};
const removeNullFilters = async (
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `is null`, `is not null`
if (['null', 'notnull'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta));
if (migrateFn) migrateFn();
}
return actions;
};
const removeEmptyFilters = async (
filter,
actions: any[],
ncMeta,
migrateFn = () => {}
) => {
// remove `is empty`, `is not empty`
if (['empty', 'notempty'].includes(filter.comparison_op))
if (migrateFn) migrateFn();
{
actions.push(await Filter.delete(filter, ncMeta));
}
return actions;
};
const migrateToBlankFilter = async () => {
// TODO:
};
const migrateToCheckboxFilter = async () => {
// TODO
};
export default async function ({ ncMeta }: NcUpgraderCtx) {
const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP);
const actions = [];
let actions = [];
for (const filter of filters) {
const col = await Column.get({ colId: filter.fk_column_id }, ncMeta);
if (!col || !col.uidt) {
@ -32,10 +111,7 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
UITypes.URL,
].includes(col.uidt)
) {
// remove `>`, `<`, `>=`, `<=` for text-based columns
if (['gt', 'lt', 'gte', 'lte'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta));
}
actions = await removeArithmeticFilters(filter, actions, ncMeta);
} else if (
[
// numeric fields
@ -50,49 +126,53 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
UITypes.SingleSelect,
].includes(col.uidt)
) {
if (['like', 'nlike'].includes(filter.comparison_op)) {
// remove `is like`, `is not like`
actions.push(await Filter.delete(filter, ncMeta));
} else if (
['null', 'notnull', 'empty', 'notempty'].includes(filter.comparison_op)
) {
// remove `is null`, `is not null`, `is empty`, `is not empty`
actions.push(await Filter.delete(filter, ncMeta));
// TODO: migrate to blank / not blank
}
actions = await removeLikeFilters(filter, actions, ncMeta);
actions = await removeNullFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
actions = await removeEmptyFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
} else if (col.uidt === UITypes.Checkbox) {
if (['eq', 'neq'].includes(filter.comparison_op)) {
actions.push(await Filter.delete(filter, ncMeta));
// TODO: migrate to `checked` or `not-checked`
} else if (['null', 'notnull'].includes(filter.comparison_op)) {
// remove `is null`, `is not null`
actions.push(await Filter.delete(filter, ncMeta));
}
actions = await removeEqualFilters(
filter,
actions,
ncMeta,
migrateToCheckboxFilter
);
actions = await removeNullFilters(filter, actions, ncMeta);
} else if (col.uidt === UITypes.MultiSelect) {
if (['eq', 'neq'].includes(filter.comparison_op)) {
// TODO: migrate to "contains any of" or "contains all of"
// TODO: migrate to "doesnt contain any of" or "doesnt contain all of"
} else if (['like', 'nlike'].includes(filter.comparison_op)) {
// remove `is like`, `is not like`
actions.push(await Filter.delete(filter, ncMeta));
} else if (
['null', 'notnull', 'empty', 'notempty'].includes(filter.comparison_op)
) {
// remove `is null`, `is not null`, `is empty`, `is not empty`
actions.push(await Filter.delete(filter, ncMeta));
// TODO: migrate to blank / not blank
}
// TODO: migrate to "contains any of" or "contains all of"
// TODO: migrate to "doesnt contain any of" or "doesnt contain all of"
actions = await removeEqualFilters(filter, actions, ncMeta);
actions = await removeLikeFilters(filter, actions, ncMeta);
actions = await removeNullFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
actions = await removeEmptyFilters(
filter,
actions,
ncMeta,
migrateToBlankFilter
);
} else if (col.uidt === UITypes.Attachment) {
// remove `>`, `<`, `>=`, `<=`
// remove `is empty`, `is not empty`
// remove `is equal`, `is not equal`
if (
['gt', 'lt', 'gte', 'lte', 'empty', 'notempty', 'eq', 'neq'].includes(
filter.comparison_op
)
) {
actions.push(await Filter.delete(filter, ncMeta));
}
actions = await removeArithmeticFilters(filter, actions, ncMeta);
actions = await removeEmptyFilters(filter, actions, ncMeta);
actions = await removeEqualFilters(
filter,
actions,
ncMeta,
migrateToCheckboxFilter
);
}
}
await Promise.all(actions);

Loading…
Cancel
Save