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. 34
      packages/nocodb/src/lib/utils/modelUtils.ts

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

@ -1,5 +1,5 @@
import Noco from '../Noco'; import Noco from '../Noco';
import { parseMetaProp } from '../utils/modelUtils' import { parseMetaProp } from '../utils/modelUtils';
import Column from './Column'; import Column from './Column';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { XKnex } from '../db/sql-data-mapper'; import { XKnex } from '../db/sql-data-mapper';
@ -23,8 +23,6 @@ import { NcError } from '../meta/helpers/catchError';
import Audit from './Audit'; import Audit from './Audit';
import { sanitize } from '../db/sql-data-mapper/lib/sql/helpers/sanitize'; import { sanitize } from '../db/sql-data-mapper/lib/sql/helpers/sanitize';
export default class Model implements TableType { export default class Model implements TableType {
copy_enabled: boolean; copy_enabled: boolean;
created_at: Date | number | string; 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) { if (base_id) {
await NocoCache.setList( await NocoCache.setList(
@ -220,7 +221,10 @@ export default class Model implements TableType {
MetaTable.MODELS 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); 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); modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, id);
if (modelData) { if (modelData) {
parseMetaProp(modelData); modelData.meta = parseMetaProp(modelData);
await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData); await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData);
} }
} }
@ -272,25 +276,7 @@ export default class Model implements TableType {
)); ));
if (!modelData) { if (!modelData) {
modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, k); modelData = await ncMeta.metaGet2(null, null, MetaTable.MODELS, k);
parseMetaProp(modelData); modelData.meta = 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
// ];
// }
} }
if (modelData) { if (modelData) {
await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData); await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData);
@ -324,7 +310,7 @@ export default class Model implements TableType {
table_name, table_name,
} }
); );
parseMetaProp(modelData); modelData.meta = parseMetaProp(modelData);
await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData); await NocoCache.set(`${CacheScope.MODEL}:${modelData.id}`, modelData);
// modelData.filters = await Filter.getFilterObject({ // modelData.filters = await Filter.getFilterObject({
// viewId: modelData.id // viewId: modelData.id

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

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

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

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

Loading…
Cancel
Save