Browse Source

fix: use cache update on missing places

pull/7727/head
mertmit 10 months ago
parent
commit
012a23563e
  1. 17
      packages/nocodb/src/models/BaseUser.ts
  2. 17
      packages/nocodb/src/models/CalendarViewColumn.ts
  3. 41
      packages/nocodb/src/models/Column.ts
  4. 25
      packages/nocodb/src/models/FormViewColumn.ts
  5. 136
      packages/nocodb/src/models/Model.ts
  6. 20
      packages/nocodb/src/models/ModelRoleVisibility.ts
  7. 15
      packages/nocodb/src/models/Sort.ts
  8. 28
      packages/nocodb/src/models/Source.ts
  9. 116
      packages/nocodb/src/models/View.ts
  10. 27
      packages/nocodb/src/utils/modelUtils.ts

17
packages/nocodb/src/models/BaseUser.ts

@ -205,17 +205,8 @@ export default class BaseUser {
roles: string,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.BASE_USER}:${baseId}:${userId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o.roles = roles;
// set cache
await NocoCache.set(key, o);
}
// set meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.PROJECT_USERS,
@ -227,6 +218,12 @@ export default class BaseUser {
base_id: baseId,
},
);
await NocoCache.update(`${CacheScope.BASE_USER}:${baseId}:${userId}`, {
roles,
});
return res;
}
static async update(

17
packages/nocodb/src/models/CalendarViewColumn.ts

@ -162,21 +162,20 @@ export default class CalendarViewColumn {
'italic',
]);
// get existing cache
const key = `${CacheScope.CALENDAR_VIEW_COLUMN}:${columnId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
Object.assign(o, updateObj);
// set cache
await NocoCache.set(key, o);
}
// update meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.CALENDAR_VIEW_COLUMNS,
updateObj,
columnId,
);
await NocoCache.update(
`${CacheScope.CALENDAR_VIEW_COLUMN}:${columnId}`,
updateObj,
);
return res;
}
}

41
packages/nocodb/src/models/Column.ts

@ -32,7 +32,6 @@ import {
parseMetaProp,
prepareForDb,
prepareForResponse,
stringifyMetaProp,
} from '~/utils/modelUtils';
import { getFormulasReferredTheColumn } from '~/helpers/formulaHelpers';
@ -1139,15 +1138,6 @@ export default class Column<T = any> implements ColumnType {
{ title }: { title: string },
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.COLUMN}:${colId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.title = title;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null, //column.base_id || column.source_id,
@ -1159,6 +1149,8 @@ export default class Column<T = any> implements ColumnType {
colId,
);
await NocoCache.update(`${CacheScope.COLUMN}:${colId}`, { title });
const column = await Column.get({ colId }, ncMeta);
await View.clearSingleQueryCache(column.fk_model_id);
@ -1219,15 +1211,6 @@ export default class Column<T = any> implements ColumnType {
system = true,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.COLUMN}:${colId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.system = system;
// set cache
await NocoCache.set(key, o);
}
// update system field in meta db
await ncMeta.metaUpdate(
null,
@ -1238,6 +1221,8 @@ export default class Column<T = any> implements ColumnType {
},
colId,
);
await NocoCache.update(`${CacheScope.COLUMN}:${colId}`, { system });
}
static getMaxColumnNameLength(sqlClientType: string) {
@ -1257,25 +1242,19 @@ export default class Column<T = any> implements ColumnType {
{ colId, meta }: { colId: string; meta: any },
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.COLUMN}:${colId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update meta
o.meta = meta;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
null,
MetaTable.COLUMNS,
{
meta: stringifyMetaProp({ meta }),
},
prepareForDb({ meta }),
colId,
);
await NocoCache.update(
`${CacheScope.COLUMN}:${colId}`,
prepareForResponse({ meta }),
);
}
static async bulkInsert(

25
packages/nocodb/src/models/FormViewColumn.ts

@ -10,6 +10,7 @@ import NocoCache from '~/cache/NocoCache';
import { extractProps } from '~/helpers/extractProps';
import { deserializeJSON, serializeJSON } from '~/utils/serialize';
import { CacheGetType, CacheScope, MetaTable } from '~/utils/globals';
import { prepareForDb, prepareForResponse } from '~/utils/modelUtils';
export default class FormViewColumn implements FormColumnType {
id?: string;
@ -168,26 +169,20 @@ export default class FormViewColumn implements FormColumnType {
'enable_scanner',
]);
// get existing cache
const key = `${CacheScope.FORM_VIEW_COLUMN}:${columnId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
Object.assign(o, updateObj);
// set cache
await NocoCache.set(key, o);
}
if (updateObj.meta) {
updateObj.meta = serializeJSON(updateObj.meta);
}
// update meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.FORM_VIEW_COLUMNS,
updateObj,
prepareForDb(updateObj),
columnId,
);
await NocoCache.update(
`${CacheScope.FORM_VIEW_COLUMN}:${columnId}`,
prepareForResponse(updateObj),
);
return res;
}
}

136
packages/nocodb/src/models/Model.ts

@ -26,7 +26,11 @@ import {
import NocoCache from '~/cache/NocoCache';
import Noco from '~/Noco';
import { BaseModelSqlv2 } from '~/db/BaseModelSqlv2';
import { parseMetaProp } from '~/utils/modelUtils';
import {
parseMetaProp,
prepareForDb,
prepareForResponse,
} from '~/utils/modelUtils';
export default class Model implements TableType {
copy_enabled: BoolType;
@ -641,27 +645,8 @@ export default class Model implements TableType {
if (!table_name) {
NcError.badRequest("Missing 'table_name' property in body");
}
// get existing cache
const key = `${CacheScope.MODEL}:${tableId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
let oldModel = { ...o };
// update alias
if (o) {
o.title = title;
o.table_name = table_name;
// set cache
await NocoCache.set(key, o);
} else {
oldModel = await this.get(tableId);
}
// delete alias cache
await NocoCache.del([
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.id}`,
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.source_id}:${oldModel.id}`,
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.title}`,
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.source_id}:${oldModel.title}`,
]);
const oldModel = await this.get(tableId, ncMeta);
// set meta
const res = await ncMeta.metaUpdate(
@ -675,6 +660,19 @@ export default class Model implements TableType {
tableId,
);
await NocoCache.update(`${CacheScope.MODEL}:${tableId}`, {
title,
table_name,
});
// delete alias cache
await NocoCache.del([
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.id}`,
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.source_id}:${oldModel.id}`,
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.title}`,
`${CacheScope.MODEL_ALIAS}:${oldModel.base_id}:${oldModel.source_id}:${oldModel.title}`,
]);
// clear all the cached query under this model
await View.clearSingleQueryCache(tableId);
@ -693,17 +691,8 @@ export default class Model implements TableType {
}
static async markAsMmTable(tableId, isMm = true, ncMeta = Noco.ncMeta) {
// get existing cache
const key = `${CacheScope.MODEL}:${tableId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
// update alias
if (o) {
o.mm = isMm;
// set cache
await NocoCache.set(key, o);
}
// set meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.MODELS,
@ -712,6 +701,12 @@ export default class Model implements TableType {
},
tableId,
);
await NocoCache.update(`${CacheScope.MODEL}:${tableId}`, {
mm: isMm,
});
return res;
}
async getAliasColMapping() {
@ -737,16 +732,8 @@ export default class Model implements TableType {
order: number,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.MODEL}:${tableId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o.order = order;
// set cache
await NocoCache.set(key, o);
}
// set meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.MODELS,
@ -755,6 +742,12 @@ export default class Model implements TableType {
},
tableId,
);
await NocoCache.update(`${CacheScope.MODEL}:${tableId}`, {
order,
});
return res;
}
static async updatePrimaryColumn(
@ -769,14 +762,6 @@ export default class Model implements TableType {
// drop existing primary column/s
for (const col of model.columns?.filter((c) => c.pv) || []) {
// get existing cache
const key = `${CacheScope.COLUMN}:${col.id}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o.pv = false;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -787,16 +772,12 @@ export default class Model implements TableType {
},
col.id,
);
}
// get existing cache
const key = `${CacheScope.COLUMN}:${newPvCol.id}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o.pv = true;
// set cache
await NocoCache.set(key, o);
await NocoCache.update(`${CacheScope.COLUMN}:${col.id}`, {
pv: false,
});
}
// set meta
await ncMeta.metaUpdate(
null,
@ -808,6 +789,10 @@ export default class Model implements TableType {
newPvCol.id,
);
await NocoCache.update(`${CacheScope.COLUMN}:${newPvCol.id}`, {
pv: true,
});
const grid_views_with_column = await ncMeta.metaList2(
null,
null,
@ -829,14 +814,6 @@ export default class Model implements TableType {
}
static async setAsMm(id: any, ncMeta = Noco.ncMeta) {
// get existing cache
const key = `${CacheScope.MODEL}:${id}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
o.mm = true;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -847,6 +824,10 @@ export default class Model implements TableType {
},
id,
);
await NocoCache.update(`${CacheScope.MODEL}:${id}`, {
mm: true,
});
}
static async getByAliasOrId(
@ -976,26 +957,25 @@ export default class Model implements TableType {
meta: string | Record<string, any>,
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.MODEL}:${tableId}`;
const existingCache = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (existingCache) {
try {
existingCache.meta = typeof meta === 'string' ? JSON.parse(meta) : meta;
// set cache
await NocoCache.set(key, existingCache);
} catch {}
}
// set meta
return await ncMeta.metaUpdate(
const res = await ncMeta.metaUpdate(
null,
null,
MetaTable.MODELS,
{
meta: typeof meta === 'object' ? JSON.stringify(meta) : meta,
},
prepareForDb({
meta,
}),
tableId,
);
await NocoCache.update(
`${CacheScope.MODEL}:${tableId}`,
prepareForResponse({
meta,
}),
);
return res;
}
static async getNonDefaultViewsCountAndReset(

20
packages/nocodb/src/models/ModelRoleVisibility.ts

@ -86,17 +86,8 @@ export default class ModelRoleVisibility implements ModelRoleVisibilityType {
role: string,
body: { disabled: any },
) {
// get existing cache
const key = `${CacheScope.MODEL_ROLE_VISIBILITY}:${fk_view_id}:${role}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.disabled = body.disabled;
// set cache
await NocoCache.set(key, o);
}
// set meta
return await Noco.ncMeta.metaUpdate(
const res = await Noco.ncMeta.metaUpdate(
null,
null,
MetaTable.MODEL_ROLE_VISIBILITY,
@ -108,6 +99,15 @@ export default class ModelRoleVisibility implements ModelRoleVisibilityType {
role,
},
);
await NocoCache.update(
`${CacheScope.MODEL_ROLE_VISIBILITY}:${fk_view_id}:${role}`,
{
disabled: body.disabled,
},
);
return res;
}
async delete() {

15
packages/nocodb/src/models/Sort.ts

@ -148,16 +148,6 @@ export default class Sort {
}
public static async update(sortId, body, ncMeta = Noco.ncMeta) {
// get existing cache
const key = `${CacheScope.SORT}:${sortId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update fk_column_id & direction
o.fk_column_id = body.fk_column_id;
o.direction = body.direction;
// set cache
await NocoCache.set(key, o);
}
// set meta
const res = await ncMeta.metaUpdate(
null,
@ -170,6 +160,11 @@ export default class Sort {
sortId,
);
await NocoCache.update(`${CacheScope.SORT}:${sortId}`, {
fk_column_id: body.fk_column_id,
direction: body.direction,
});
// on update, delete any optimised single query cache
{
const sort = await this.get(sortId, ncMeta);

28
packages/nocodb/src/models/Source.ts

@ -478,15 +478,7 @@ export default class Source implements SourceType {
if (!this.erd_uuid) {
const uuid = uuidv4();
this.erd_uuid = uuid;
// get existing cache
const key = `${CacheScope.BASE}:${this.id}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.erd_uuid = uuid;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -497,6 +489,10 @@ export default class Source implements SourceType {
},
this.id,
);
await NocoCache.update(`${CacheScope.BASE}:${this.id}`, {
erd_uuid: this.erd_uuid,
});
}
return this;
}
@ -504,15 +500,7 @@ export default class Source implements SourceType {
async disableShareErd(ncMeta = Noco.ncMeta) {
if (this.erd_uuid) {
this.erd_uuid = null;
// get existing cache
const key = `${CacheScope.BASE}:${this.id}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.erd_uuid = null;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -523,6 +511,10 @@ export default class Source implements SourceType {
},
this.id,
);
await NocoCache.update(`${CacheScope.BASE}:${this.id}`, {
erd_uuid: this.erd_uuid,
});
}
return this;
}

116
packages/nocodb/src/models/View.ts

@ -26,7 +26,12 @@ import {
MetaTable,
} from '~/utils/globals';
import Noco from '~/Noco';
import { parseMetaProp, stringifyMetaProp } from '~/utils/modelUtils';
import {
parseMetaProp,
prepareForDb,
prepareForResponse,
stringifyMetaProp,
} from '~/utils/modelUtils';
const { v4: uuidv4 } = require('uuid');
@ -981,15 +986,7 @@ export default class View implements ViewType {
if (!view.uuid) {
const uuid = uuidv4();
view.uuid = uuid;
// get existing cache
const key = `${CacheScope.VIEW}:${view.id}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.uuid = uuid;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -1000,32 +997,35 @@ export default class View implements ViewType {
},
viewId,
);
await NocoCache.update(`${CacheScope.VIEW}:${view.id}`, {
uuid: view.uuid,
});
}
if (!view.meta || !('allowCSVDownload' in view.meta)) {
const defaultMeta = {
...(view.meta ?? {}),
allowCSVDownload: true,
};
// get existing cache
const key = `${CacheScope.VIEW}:${view.id}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.meta = defaultMeta;
// set cache
await NocoCache.set(key, o);
}
view.meta = defaultMeta;
// set meta
await ncMeta.metaUpdate(
null,
null,
MetaTable.VIEWS,
{
meta: JSON.stringify(defaultMeta),
},
prepareForDb({
meta: defaultMeta,
}),
viewId,
);
view.meta = defaultMeta;
await NocoCache.update(
`${CacheScope.VIEW}:${view.id}`,
prepareForResponse({
meta: defaultMeta,
}),
);
}
return view;
}
@ -1035,15 +1035,6 @@ export default class View implements ViewType {
{ password }: { password: string },
ncMeta = Noco.ncMeta,
) {
// get existing cache
const key = `${CacheScope.VIEW}:${viewId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.password = password;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -1054,18 +1045,13 @@ export default class View implements ViewType {
},
viewId,
);
await NocoCache.update(`${CacheScope.VIEW}:${viewId}`, {
password,
});
}
static async sharedViewDelete(viewId, ncMeta = Noco.ncMeta) {
// get existing cache
const key = `${CacheScope.VIEW}:${viewId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
// update data
o.uuid = null;
// set cache
await NocoCache.set(key, o);
}
// set meta
await ncMeta.metaUpdate(
null,
@ -1076,6 +1062,10 @@ export default class View implements ViewType {
},
viewId,
);
await NocoCache.update(`${CacheScope.VIEW}:${viewId}`, {
uuid: null,
});
}
static async update(
@ -1101,37 +1091,33 @@ export default class View implements ViewType {
'uuid',
]);
// get existing cache
const key = `${CacheScope.VIEW}:${viewId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
let oldView = { ...o };
if (o) {
// update data
o = {
...o,
...updateObj,
};
if (o.is_default) {
await NocoCache.set(`${CacheScope.VIEW}:${o.fk_model_id}:default`, o);
}
// set cache
await NocoCache.set(key, o);
} else {
oldView = await this.get(viewId);
}
const oldView = await this.get(viewId, ncMeta);
// set meta
await ncMeta.metaUpdate(
null,
null,
MetaTable.VIEWS,
prepareForDb(updateObj),
viewId,
);
// reset alias cache
await NocoCache.del(
`${CacheScope.VIEW}:${oldView.fk_model_id}:${oldView.title}`,
);
// if meta data defined then stringify it
if ('meta' in updateObj) {
updateObj.meta = stringifyMetaProp(updateObj);
}
await NocoCache.update(
`${CacheScope.VIEW}:${viewId}`,
prepareForResponse(updateObj),
);
// set meta
await ncMeta.metaUpdate(null, null, MetaTable.VIEWS, updateObj, viewId);
if (oldView.is_default) {
await NocoCache.update(
`${CacheScope.VIEW}:${oldView.fk_model_id}:default`,
prepareForResponse(updateObj),
);
}
const view = await this.get(viewId);

27
packages/nocodb/src/utils/modelUtils.ts

@ -24,22 +24,37 @@ export function stringifyMetaProp(model: any, propName = 'meta'): string {
}
}
export function prepareForDb(model: any) {
export function prepareForDb(model: any, props: string | string[] = 'meta') {
if (!model) return model;
if (model.meta) {
model.meta = stringifyMetaProp(model);
if (typeof props === 'string') {
props = [props];
}
props.forEach((prop) => {
if (prop in model) {
model[prop] = stringifyMetaProp(model, prop);
}
});
return model;
}
export function prepareForResponse(model: any) {
export function prepareForResponse(
model: any,
props: string | string[] = 'meta',
) {
if (!model) return model;
if (model.meta) {
model.meta = parseMetaProp(model);
if (typeof props === 'string') {
props = [props];
}
props.forEach((prop) => {
if (prop in model) {
model[prop] = parseMetaProp(model, prop);
}
});
return model;
}

Loading…
Cancel
Save