Browse Source

refactor(nocodb): remove unnecessary async and stack

pull/4969/head
Wing-Kam Wong 2 years ago
parent
commit
c21d00b1dc
  1. 113
      packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0104004.ts

113
packages/nocodb/src/lib/version-upgrader/ncFilterUpgrader_0104004.ts

@ -34,7 +34,8 @@ import { UITypes, SelectOptionsType } from 'nocodb-sdk';
// - remove `like` // - remove `like`
// - migrate `empty`, `null` to `blank` // - migrate `empty`, `null` to `blank`
const removeEqualFilters = async (filter, actions: any[], ncMeta) => { const removeEqualFilters = (filter, ncMeta) => {
let actions = [];
// remove `is equal`, `is not equal` // remove `is equal`, `is not equal`
if (['eq', 'neq'].includes(filter.comparison_op)) { if (['eq', 'neq'].includes(filter.comparison_op)) {
actions.push(Filter.delete(filter.id, ncMeta)); actions.push(Filter.delete(filter.id, ncMeta));
@ -42,7 +43,8 @@ const removeEqualFilters = async (filter, actions: any[], ncMeta) => {
return actions; return actions;
}; };
const removeArithmeticFilters = async (filter, actions: any[], ncMeta) => { const removeArithmeticFilters = (filter, ncMeta) => {
let actions = [];
// remove `>`, `<`, `>=`, `<=` // remove `>`, `<`, `>=`, `<=`
if (['gt', 'lt', 'gte', 'lte'].includes(filter.comparison_op)) { if (['gt', 'lt', 'gte', 'lte'].includes(filter.comparison_op)) {
actions.push(Filter.delete(filter.id, ncMeta)); actions.push(Filter.delete(filter.id, ncMeta));
@ -50,7 +52,8 @@ const removeArithmeticFilters = async (filter, actions: any[], ncMeta) => {
return actions; return actions;
}; };
const removeLikeFilters = async (filter, actions: any[], ncMeta) => { const removeLikeFilters = (filter, ncMeta) => {
let actions = [];
// remove `is like`, `is not like` // remove `is like`, `is not like`
if (['like', 'nlike'].includes(filter.comparison_op)) { if (['like', 'nlike'].includes(filter.comparison_op)) {
actions.push(Filter.delete(filter.id, ncMeta)); actions.push(Filter.delete(filter.id, ncMeta));
@ -58,11 +61,8 @@ const removeLikeFilters = async (filter, actions: any[], ncMeta) => {
return actions; return actions;
}; };
const migrateNullAndEmptyToBlankFilters = async ( const migrateNullAndEmptyToBlankFilters = (filter, ncMeta) => {
filter, let actions = [];
actions: any[],
ncMeta
) => {
if (['empty', 'null'].includes(filter.comparison_op)) { if (['empty', 'null'].includes(filter.comparison_op)) {
// migrate to blank // migrate to blank
actions.push( actions.push(
@ -89,18 +89,12 @@ const migrateNullAndEmptyToBlankFilters = async (
return actions; return actions;
}; };
const migrateMultiSelectEq = async ( const migrateMultiSelectEq = async (filter, col: Column, ncMeta) => {
filter,
actions: any[],
col: Column,
ncMeta
) => {
// only allow eq / neq // only allow eq / neq
if (!['eq', 'neq'].includes(filter.comparison_op)) return actions; if (!['eq', 'neq'].includes(filter.comparison_op)) return;
// if there is no value -> delete this filter // if there is no value -> delete this filter
if (!filter.value) { if (!filter.value) {
actions.push(Filter.delete(filter.id, ncMeta)); return await Filter.delete(filter.id, ncMeta);
return actions;
} }
// options inputted from users // options inputted from users
const options = filter.value.split(','); const options = filter.value.split(',');
@ -116,9 +110,9 @@ const migrateMultiSelectEq = async (
const newFilterValue = validOptions.join(','); const newFilterValue = validOptions.join(',');
// if all inputted options are invalid -> delete this filter // if all inputted options are invalid -> delete this filter
if (!newFilterValue) { if (!newFilterValue) {
actions.push(Filter.delete(filter.id, ncMeta)); return await Filter.delete(filter.id, ncMeta);
return actions;
} }
let actions = [];
if (filter.comparison_op === 'eq') { if (filter.comparison_op === 'eq') {
// migrate to `contains all of` // migrate to `contains all of`
actions.push( actions.push(
@ -144,10 +138,11 @@ const migrateMultiSelectEq = async (
) )
); );
} }
return actions; return await Promise.all(actions);
}; };
const migrateToCheckboxFilter = async (filter, actions: any[], ncMeta) => { const migrateToCheckboxFilter = (filter, ncMeta) => {
let actions = [];
if (['empty', 'null'].includes(filter.comparison_op)) { if (['empty', 'null'].includes(filter.comparison_op)) {
// migrate to not checked // migrate to not checked
actions.push( actions.push(
@ -234,7 +229,6 @@ const migrateToCheckboxFilter = async (filter, actions: any[], ncMeta) => {
async function migrateFilters(ncMeta: NcMetaIO) { async function migrateFilters(ncMeta: NcMetaIO) {
const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP); const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP);
let actions = [];
for (const filter of filters) { for (const filter of filters) {
if (!filter.fk_column_id || filter.is_group) { if (!filter.fk_column_id || filter.is_group) {
continue; continue;
@ -249,7 +243,7 @@ async function migrateFilters(ncMeta: NcMetaIO) {
UITypes.URL, UITypes.URL,
].includes(col.uidt) ].includes(col.uidt)
) { ) {
actions = await removeArithmeticFilters(filter, actions, ncMeta); await Promise.all(removeArithmeticFilters(filter, ncMeta));
} else if ( } else if (
[ [
// numeric fields // numeric fields
@ -264,60 +258,50 @@ async function migrateFilters(ncMeta: NcMetaIO) {
UITypes.SingleSelect, UITypes.SingleSelect,
].includes(col.uidt) ].includes(col.uidt)
) { ) {
actions = await removeLikeFilters(filter, actions, ncMeta); await Promise.all([
actions = await migrateNullAndEmptyToBlankFilters( ...removeLikeFilters(filter, ncMeta),
filter, ...migrateNullAndEmptyToBlankFilters(filter, ncMeta),
actions, ]);
ncMeta
);
} else if (col.uidt === UITypes.Checkbox) { } else if (col.uidt === UITypes.Checkbox) {
actions = await migrateToCheckboxFilter(filter, actions, ncMeta); await Promise.all(migrateToCheckboxFilter(filter, ncMeta));
} else if (col.uidt === UITypes.MultiSelect) { } else if (col.uidt === UITypes.MultiSelect) {
actions = await removeLikeFilters(filter, actions, ncMeta); await Promise.all([
actions = await migrateNullAndEmptyToBlankFilters( ...removeLikeFilters(filter, ncMeta),
filter, ...migrateNullAndEmptyToBlankFilters(filter, ncMeta),
actions, ]);
ncMeta await migrateMultiSelectEq(filter, col, ncMeta);
);
actions = await migrateMultiSelectEq(filter, actions, col, ncMeta);
} else if (col.uidt === UITypes.Attachment) { } else if (col.uidt === UITypes.Attachment) {
actions = await removeArithmeticFilters(filter, actions, ncMeta); await Promise.all([
actions = await removeEqualFilters(filter, actions, ncMeta); ...removeArithmeticFilters(filter, ncMeta),
actions = await migrateNullAndEmptyToBlankFilters( ...removeEqualFilters(filter, ncMeta),
filter, ...migrateNullAndEmptyToBlankFilters(filter, ncMeta),
actions, ]);
ncMeta
);
} else if (col.uidt === UITypes.LinkToAnotherRecord) { } else if (col.uidt === UITypes.LinkToAnotherRecord) {
actions = await removeArithmeticFilters(filter, actions, ncMeta); await Promise.all([
actions = await migrateNullAndEmptyToBlankFilters( ...removeArithmeticFilters(filter, ncMeta),
filter, ...migrateNullAndEmptyToBlankFilters(filter, ncMeta),
actions, ]);
ncMeta
);
} else if (col.uidt === UITypes.Lookup) { } else if (col.uidt === UITypes.Lookup) {
actions = await removeArithmeticFilters(filter, actions, ncMeta); await Promise.all([
actions = await migrateNullAndEmptyToBlankFilters( ...removeArithmeticFilters(filter, ncMeta),
filter, ...migrateNullAndEmptyToBlankFilters(filter, ncMeta),
actions, ]);
ncMeta
);
} else if (col.uidt === UITypes.Duration) { } else if (col.uidt === UITypes.Duration) {
actions = await removeLikeFilters(filter, actions, ncMeta); await Promise.all([
actions = await migrateNullAndEmptyToBlankFilters( ...removeLikeFilters(filter, ncMeta),
filter, ...migrateNullAndEmptyToBlankFilters(filter, ncMeta),
actions, ]);
ncMeta
);
} }
} }
await Promise.all(actions);
} }
async function updateProjectMeta(ncMeta: NcMetaIO) { async function updateProjectMeta(ncMeta: NcMetaIO) {
const projectHasEmptyOrFilters: Record<string, boolean> = {}; const projectHasEmptyOrFilters: Record<string, boolean> = {};
const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP); const filters = await ncMeta.metaList2(null, null, MetaTable.FILTER_EXP);
let actions = []; let actions = [];
for (const filter of filters) { for (const filter of filters) {
if ( if (
['notempty', 'notnull', 'empty', 'null'].includes(filter.comparison_op) ['notempty', 'notnull', 'empty', 'null'].includes(filter.comparison_op)
@ -325,10 +309,13 @@ async function updateProjectMeta(ncMeta: NcMetaIO) {
projectHasEmptyOrFilters[filter.project_id] = true; projectHasEmptyOrFilters[filter.project_id] = true;
} }
} }
const projects = await ncMeta.metaList2(null, null, MetaTable.PROJECT); const projects = await ncMeta.metaList2(null, null, MetaTable.PROJECT);
const defaultProjectMeta = { const defaultProjectMeta = {
showNullAndEmptyInFilter: false, showNullAndEmptyInFilter: false,
}; };
for (const project of projects) { for (const project of projects) {
const oldProjectMeta = project.meta; const oldProjectMeta = project.meta;
let newProjectMeta = defaultProjectMeta; let newProjectMeta = defaultProjectMeta;

Loading…
Cancel
Save