Browse Source

refactor(nocodb): use extractProps to extract the props

pull/5168/head
Wing-Kam Wong 2 years ago
parent
commit
22e100a389
  1. 62
      packages/nocodb/src/lib/models/Column.ts
  2. 55
      packages/nocodb/src/lib/models/FormView.ts
  3. 10
      packages/nocodb/src/lib/models/FormViewColumn.ts
  4. 33
      packages/nocodb/src/lib/models/GalleryView.ts
  5. 18
      packages/nocodb/src/lib/models/GridView.ts
  6. 60
      packages/nocodb/src/lib/models/Hook.ts
  7. 18
      packages/nocodb/src/lib/models/HookFilter.ts
  8. 22
      packages/nocodb/src/lib/models/KanbanView.ts
  9. 17
      packages/nocodb/src/lib/models/Plugin.ts

62
packages/nocodb/src/lib/models/Column.ts

@ -25,6 +25,7 @@ import addFormulaErrorIfMissingColumn from '../meta/helpers/addFormulaErrorIfMis
import { NcError } from '../meta/helpers/catchError';
import QrCodeColumn from './QrCodeColumn';
import BarcodeColumn from './BarcodeColumn';
import { extractProps } from '../meta/helpers/extractProps';
export default class Column<T = any> implements ColumnType {
public fk_model_id: string;
@ -873,7 +874,11 @@ export default class Column<T = any> implements ColumnType {
);
}
static async update(colId: string, column: any, ncMeta = Noco.ncMeta) {
static async update(
colId: string,
column: Partial<Column>,
ncMeta = Noco.ncMeta
) {
const oldCol = await Column.get({ colId }, ncMeta);
switch (oldCol.uidt) {
@ -965,33 +970,34 @@ export default class Column<T = any> implements ColumnType {
break;
}
}
const updateObj = {
column_name: column.column_name,
title: column.title,
uidt: column.uidt,
dt: column.dt,
np: column.np,
ns: column.ns,
clen: column.clen,
cop: column.cop,
pk: column.pk,
rqd: column.rqd,
un: column.un,
ct: column.ct,
ai: column.ai,
unique: column.unique,
cdf: column.cdf,
cc: column.cc,
csn: column.csn,
dtx: column.dtx,
dtxp: column.dtxp,
dtxs: column.dtxs,
au: column.au,
pv: column.pv,
system: column.system,
validate: null,
meta: column.meta,
};
const updateObj = extractProps(column, [
'column_name',
'title',
'uidt',
'dt',
'np',
'ns',
'clen',
'cop',
'pk',
'rqd',
'un',
'ct',
'ai',
'unique',
'cdf',
'cc',
'csn',
'dtx',
'dtxp',
'dtxs',
'au',
'pv',
'system',
'validate',
'meta',
]);
if (column.validate) {
if (typeof column.validate === 'string')

55
packages/nocodb/src/lib/models/FormView.ts

@ -5,6 +5,7 @@ import { deserializeJSON, serializeJSON } from '../utils/serialize';
import FormViewColumn from './FormViewColumn';
import View from './View';
import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class FormView implements FormType {
show: boolean;
@ -86,44 +87,34 @@ export default class FormView implements FormType {
) {
// get existing cache
const key = `${CacheScope.FORM_VIEW}:${formId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, [
'heading',
'subheading',
'success_msg',
'redirect_url',
'redirect_after_secs',
'email',
'banner_image_url',
'logo_url',
'submit_another_form',
'show_blank_form',
'meta',
]);
if (o) {
o.heading = body.heading;
o.subheading = body.subheading;
o.success_msg = body.success_msg;
o.redirect_url = body.redirect_url;
o.redirect_after_secs = body.redirect_after_secs;
o.email = body.email;
o.banner_image_url = body.banner_image_url;
o.logo_url = body.logo_url;
o.submit_another_form = body.submit_another_form;
o.show_blank_form = body.show_blank_form;
o.meta = body.meta;
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
o.meta = serializeJSON(body.meta);
}
if (updateObj.meta) {
updateObj.meta = serializeJSON(updateObj.meta);
}
// update meta
return await ncMeta.metaUpdate(
null,
null,
MetaTable.FORM_VIEW,
{
heading: body.heading,
subheading: body.subheading,
success_msg: body.success_msg,
redirect_url: body.redirect_url,
redirect_after_secs: body.redirect_after_secs,
email: body.email,
banner_image_url: body.banner_image_url,
logo_url: body.logo_url,
submit_another_form: body.submit_another_form,
show_blank_form: body.show_blank_form,
},
{
return await ncMeta.metaUpdate(null, null, MetaTable.FORM_VIEW, updateObj, {
fk_view_id: formId,
}
);
});
}
async getColumns(ncMeta = Noco.ncMeta) {

10
packages/nocodb/src/lib/models/FormViewColumn.ts

@ -150,7 +150,7 @@ export default class FormViewColumn implements FormColumnType {
body: Partial<FormViewColumn>,
ncMeta = Noco.ncMeta
) {
const insertObj = extractProps(body, [
const updateObj = extractProps(body, [
'label',
'help',
'description',
@ -164,13 +164,13 @@ export default class FormViewColumn implements FormColumnType {
const key = `${CacheScope.FORM_VIEW_COLUMN}:${columnId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) {
Object.assign(o, insertObj);
Object.assign(o, updateObj);
// set cache
await NocoCache.set(key, o);
}
if (insertObj.meta) {
insertObj.meta = serializeJSON(insertObj.meta);
if (updateObj.meta) {
updateObj.meta = serializeJSON(updateObj.meta);
}
// update meta
@ -178,7 +178,7 @@ export default class FormViewColumn implements FormColumnType {
null,
null,
MetaTable.FORM_VIEW_COLUMNS,
insertObj,
updateObj,
columnId
);
}

33
packages/nocodb/src/lib/models/GalleryView.ts

@ -3,6 +3,7 @@ import { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import { GalleryColumnType, GalleryType, UITypes } from 'nocodb-sdk';
import View from './View';
import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class GalleryView implements GalleryType {
fk_view_id?: string;
@ -90,16 +91,19 @@ export default class GalleryView implements GalleryType {
) {
// get existing cache
const key = `${CacheScope.GALLERY_VIEW}:${galleryId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, [
'next_enabled',
'prev_enabled',
'cover_image_idx',
'cover_image',
'restrict_types',
'restrict_size',
'restrict_number',
'fk_cover_image_col_id',
]);
if (o) {
o.next_enabled = body.next_enabled;
o.prev_enabled = body.prev_enabled;
o.cover_image_idx = body.cover_image_idx;
o.cover_image = body.cover_image;
o.restrict_types = body.restrict_types;
o.restrict_size = body.restrict_size;
o.restrict_number = body.restrict_number;
o.fk_cover_image_col_id = body.fk_cover_image_col_id;
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
@ -108,16 +112,7 @@ export default class GalleryView implements GalleryType {
null,
null,
MetaTable.GALLERY_VIEW,
{
next_enabled: body.next_enabled,
prev_enabled: body.prev_enabled,
cover_image_idx: body.cover_image_idx,
cover_image: body.cover_image,
restrict_types: body.restrict_types,
restrict_size: body.restrict_size,
restrict_number: body.restrict_number,
fk_cover_image_col_id: body.fk_cover_image_col_id,
},
updateObj,
{
fk_view_id: galleryId,
}

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

@ -3,6 +3,7 @@ import { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import GridViewColumn from './GridViewColumn';
import View from './View';
import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class GridView {
fk_view_id: string;
@ -69,23 +70,16 @@ export default class GridView {
) {
// get existing cache
const key = `${CacheScope.GRID_VIEW}:${viewId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = extractProps(body, ['row_height']);
if (o) {
o.row_height = body.row_height;
o = { ...o, ...updateObj };
// set cache
await NocoCache.set(key, o);
}
// update meta
return await ncMeta.metaUpdate(
null,
null,
MetaTable.GRID_VIEW,
{
row_height: body.row_height,
},
{
return await ncMeta.metaUpdate(null, null, MetaTable.GRID_VIEW, updateObj, {
fk_view_id: viewId,
}
);
});
}
}

60
packages/nocodb/src/lib/models/Hook.ts

@ -10,6 +10,7 @@ import Model from './Model';
import NocoCache from '../cache/NocoCache';
import Filter from './Filter';
import HookFilter from './HookFilter';
import { extractProps } from '../meta/helpers/extractProps';
export default class Hook implements HookType {
id?: string;
@ -18,7 +19,7 @@ export default class Hook implements HookType {
description?: string;
env?: string;
type?: string;
event?: 'After' | 'Before';
event?: 'after' | 'before';
operation?: 'insert' | 'delete' | 'update';
async?: boolean;
payload?: string;
@ -175,27 +176,41 @@ export default class Hook implements HookType {
hook: Partial<Hook>,
ncMeta = Noco.ncMeta
) {
const updateObj = {
title: hook.title,
description: hook.description,
env: hook.env,
type: hook.type,
event: hook.event?.toLowerCase?.(),
operation: hook.operation?.toLowerCase?.(),
async: hook.async,
payload: !!hook.payload,
url: hook.url,
headers: hook.headers,
condition: !!hook.condition,
notification:
hook.notification && typeof hook.notification === 'object'
? JSON.stringify(hook.notification)
: hook.notification,
retries: hook.retries,
retry_interval: hook.retry_interval,
timeout: hook.timeout,
active: hook.active,
};
const updateObj = extractProps(hook, [
'title',
'description',
'env',
'type',
'event',
'operation',
'async',
'payload',
'url',
'headers',
'condition',
'notification',
'retries',
'retry_interval',
'timeout',
'active',
]);
if (updateObj.event) {
updateObj.event = updateObj.event.toLowerCase() as 'after' | 'before';
}
if (updateObj.operation) {
updateObj.operation = updateObj.operation.toLowerCase() as
| 'insert'
| 'delete'
| 'update';
}
if (updateObj.notification) {
updateObj.notification && typeof updateObj.notification === 'object'
? JSON.stringify(updateObj.notification)
: updateObj.notification;
}
// get existing cache
const key = `${CacheScope.HOOK}:${hookId}`;
@ -203,7 +218,6 @@ export default class Hook implements HookType {
if (o) {
// update data
o = { ...o, ...updateObj };
o.notification = updateObj.notification;
// set cache
await NocoCache.set(key, o);
}

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

@ -10,6 +10,7 @@ import {
import View from './View';
import { FilterType, UITypes } from 'nocodb-sdk';
import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class Filter {
id: string;
@ -138,15 +139,14 @@ export default class Filter {
}
static async update(id, filter: Partial<Filter>, ncMeta = Noco.ncMeta) {
const updateObj = {
fk_column_id: filter.fk_column_id,
comparison_op: filter.comparison_op,
value: filter.value,
fk_parent_id: filter.fk_parent_id,
is_group: filter.is_group,
logical_op: filter.logical_op,
};
const updateObj = extractProps(filter, [
'fk_column_id',
'comparison_op',
'value',
'fk_parent_id',
'is_group',
'logical_op',
]);
// get existing cache
const key = `${CacheScope.FILTER_EXP}:${id}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);

22
packages/nocodb/src/lib/models/KanbanView.ts

@ -3,6 +3,7 @@ import { KanbanType, UITypes } from 'nocodb-sdk';
import { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import View from './View';
import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class KanbanView implements KanbanType {
fk_view_id: string;
@ -99,13 +100,20 @@ export default class KanbanView implements KanbanType {
// get existing cache
const key = `${CacheScope.KANBAN_VIEW}:${kanbanId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = {
...body,
meta:
typeof body.meta === 'string'
? body.meta
: JSON.stringify(body.meta ?? {}),
};
const updateObj = extractProps(body, [
'title',
'fk_cover_image_col_id',
'meta',
]);
if (updateObj.meta) {
updateObj.meta =
typeof updateObj.meta === 'string'
? updateObj.meta
: JSON.stringify(updateObj.meta ?? {});
}
if (o) {
o = { ...o, ...updateObj };
// set cache

17
packages/nocodb/src/lib/models/Plugin.ts

@ -2,6 +2,7 @@ import { PluginType } from 'nocodb-sdk';
import { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import Noco from '../Noco';
import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class Plugin implements PluginType {
id?: string;
@ -56,13 +57,15 @@ export default class Plugin implements PluginType {
}
public static async update(pluginId: string, plugin: Partial<PluginType>) {
const updateObj = {
input:
plugin.input && typeof plugin.input === 'object'
? JSON.stringify(plugin.input)
: plugin.input,
active: plugin.active,
};
const updateObj = extractProps(plugin, ['input', 'active']);
if (updateObj.input) {
updateObj.input =
updateObj.input && typeof updateObj.input === 'object'
? JSON.stringify(updateObj.input)
: updateObj.input;
}
// get existing cache
const key = `${CacheScope.PLUGIN}:${pluginId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);

Loading…
Cancel
Save