Browse Source

fix(gui): convert parse/stringify methods into pure function

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4630/head
Pranav C 2 years ago
parent
commit
c28f061b8e
  1. 38
      packages/nocodb/src/lib/models/Model.ts
  2. 18
      packages/nocodb/src/lib/models/View.ts
  3. 42
      packages/nocodb/src/lib/utils/modelUtils.ts

38
packages/nocodb/src/lib/models/Model.ts

@ -1,5 +1,5 @@
import Noco from '../Noco';
import { parseMetaProp } from '../utils/modelUtils'
import { parseMetaProp } from '../utils/modelUtils';
import Column from './Column';
import NocoCache from '../cache/NocoCache';
import { XKnex } from '../db/sql-data-mapper';
@ -23,8 +23,6 @@ import { NcError } from '../meta/helpers/catchError';
import Audit from './Audit';
import { sanitize } from '../db/sql-data-mapper/lib/sql/helpers/sanitize';
export default class Model implements TableType {
copy_enabled: boolean;
created_at: Date | number | string;
@ -179,7 +177,10 @@ export default class Model implements TableType {
}
);
parseMetaProp(modelList);
// parse meta of each model
for (const model of modelList) {
model.meta = parseMetaProp(model);
}
if (base_id) {
await NocoCache.setList(
@ -220,7 +221,10 @@ export default class Model implements TableType {
MetaTable.MODELS
);
parseMetaProp(modelList);
// parse meta of each model
for (const model of modelList) {
model.meta = parseMetaProp(model);
}
await NocoCache.setList(CacheScope.MODEL, [project_id], modelList);
}
@ -244,7 +248,7 @@ export default class Model implements TableType {
modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, id);
if (modelData) {
parseMetaProp(modelData);
modelData.meta = parseMetaProp(modelData);
await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData);
}
}
@ -272,25 +276,7 @@ export default class Model implements TableType {
));
if (!modelData) {
modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, k);
parseMetaProp(modelData);
// if (
// this.baseModels?.[modelData.base_id]?.[modelData.db_alias]?.[
// modelData.title
// ]
// ) {
// delete this.baseModels[modelData.base_id][modelData.db_alias][
// modelData.title
// ];
// }
// if (
// this.baseModels?.[modelData.base_id]?.[modelData.db_alias]?.[
// modelData.id
// ]
// ) {
// delete this.baseModels[modelData.base_id][modelData.db_alias][
// modelData.id
// ];
// }
modelData.meta = parseMetaProp(modelData);
}
if (modelData) {
await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData);
@ -324,7 +310,7 @@ export default class Model implements TableType {
table_name,
}
);
parseMetaProp(modelData);
modelData.meta = parseMetaProp(modelData);
await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData);
// modelData.filters = await Filter.getFilterObject({
// viewId: modelData.id

18
packages/nocodb/src/lib/models/View.ts

@ -119,7 +119,7 @@ export default class View implements ViewType {
));
if (!view) {
view = await ncMeta.metaGet2(null, null, MetaTable.VIEWS, viewId);
parseMetaProp(view);
view.meta = parseMetaProp(view);
await NocoCache.set(`${CacheScope.VIEW}:${view.id}`, view);
}
@ -158,7 +158,7 @@ export default class View implements ViewType {
],
}
);
parseMetaProp(view);
view.meta = parseMetaProp(view);
// todo: cache - titleOrId can be viewId so we need a different scope here
await NocoCache.set(
`${CacheScope.VIEW}:${fk_model_id}:${titleOrId}`,
@ -191,7 +191,7 @@ export default class View implements ViewType {
},
null
);
parseMetaProp(view);
view.meta = parseMetaProp(view);
await NocoCache.set(`${CacheScope.VIEW}:${fk_model_id}:default`, view);
}
return view && new View(view);
@ -208,7 +208,9 @@ export default class View implements ViewType {
order: 'asc',
},
});
parseMetaProp(viewsList);
for (const view of viewsList) {
view.meta = parseMetaProp(view);
}
await NocoCache.setList(CacheScope.VIEW, [modelId], viewsList);
}
viewsList.sort(
@ -262,7 +264,7 @@ export default class View implements ViewType {
meta: view.meta ?? {},
};
stringifyMetaProp(insertObj);
insertObj.meta = stringifyMetaProp(insertObj);
// get project and base id if missing
if (!(view.project_id && view.base_id)) {
@ -722,7 +724,7 @@ export default class View implements ViewType {
});
if (view) {
parseMetaProp(view);
view.meta = parseMetaProp(view);
}
return view && new View(view);
@ -870,7 +872,9 @@ export default class View implements ViewType {
}
// if meta data defined then stringify it
stringifyMetaProp(updateObj);
if ('meta' in updateObj) {
updateObj.meta = stringifyMetaProp(updateObj);
}
// set meta
await ncMeta.metaUpdate(null, null, MetaTable.VIEWS, updateObj, viewId);

42
packages/nocodb/src/lib/utils/modelUtils.ts

@ -1,35 +1,23 @@
export function parseMetaProp(
modelOrModelList: { meta: any } | { meta: any }[]
) {
if (!modelOrModelList) return;
export function parseMetaProp(model: { meta: any }): any {
if (!model) return;
// parse meta property
for (const model of Array.isArray(modelOrModelList)
? modelOrModelList
: [modelOrModelList]) {
try {
model.meta =
typeof model.meta === 'string' ? JSON.parse(model.meta) : model.meta;
} catch {
model.meta = {};
}
try {
return typeof model.meta === 'string' ? JSON.parse(model.meta) : model.meta;
} catch {
return {};
}
}
export function stringifyMetaProp(
modelOrModelList: { meta?: any } | { meta?: any }[]
) {
if (!modelOrModelList) return;
export function stringifyMetaProp(model: { meta?: any }): string | void {
if (!model) return;
// parse meta property
for (const model of Array.isArray(modelOrModelList)
? modelOrModelList
: [modelOrModelList]) {
try {
model.meta =
typeof model.meta === 'string' ? model.meta : JSON.stringify(model.meta);
} catch (e) {
model.meta = '{}';
}
// stringify meta property
try {
return typeof model.meta === 'string'
? model.meta
: JSON.stringify(model.meta);
} catch (e) {
return '{}';
}
}

Loading…
Cancel
Save