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

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

@ -5,6 +5,7 @@ import { deserializeJSON, serializeJSON } from '../utils/serialize';
import FormViewColumn from './FormViewColumn'; import FormViewColumn from './FormViewColumn';
import View from './View'; import View from './View';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class FormView implements FormType { export default class FormView implements FormType {
show: boolean; show: boolean;
@ -86,44 +87,34 @@ export default class FormView implements FormType {
) { ) {
// get existing cache // get existing cache
const key = `${CacheScope.FORM_VIEW}:${formId}`; 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) { if (o) {
o.heading = body.heading; o = { ...o, ...updateObj };
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;
// set cache // set cache
await NocoCache.set(key, o); await NocoCache.set(key, o);
o.meta = serializeJSON(body.meta);
} }
if (updateObj.meta) {
updateObj.meta = serializeJSON(updateObj.meta);
}
// update meta // update meta
return await ncMeta.metaUpdate( return await ncMeta.metaUpdate(null, null, MetaTable.FORM_VIEW, updateObj, {
null, fk_view_id: formId,
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,
},
{
fk_view_id: formId,
}
);
} }
async getColumns(ncMeta = Noco.ncMeta) { 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>, body: Partial<FormViewColumn>,
ncMeta = Noco.ncMeta ncMeta = Noco.ncMeta
) { ) {
const insertObj = extractProps(body, [ const updateObj = extractProps(body, [
'label', 'label',
'help', 'help',
'description', 'description',
@ -164,13 +164,13 @@ export default class FormViewColumn implements FormColumnType {
const key = `${CacheScope.FORM_VIEW_COLUMN}:${columnId}`; const key = `${CacheScope.FORM_VIEW_COLUMN}:${columnId}`;
const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); const o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
if (o) { if (o) {
Object.assign(o, insertObj); Object.assign(o, updateObj);
// set cache // set cache
await NocoCache.set(key, o); await NocoCache.set(key, o);
} }
if (insertObj.meta) { if (updateObj.meta) {
insertObj.meta = serializeJSON(insertObj.meta); updateObj.meta = serializeJSON(updateObj.meta);
} }
// update meta // update meta
@ -178,7 +178,7 @@ export default class FormViewColumn implements FormColumnType {
null, null,
null, null,
MetaTable.FORM_VIEW_COLUMNS, MetaTable.FORM_VIEW_COLUMNS,
insertObj, updateObj,
columnId 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 { GalleryColumnType, GalleryType, UITypes } from 'nocodb-sdk';
import View from './View'; import View from './View';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class GalleryView implements GalleryType { export default class GalleryView implements GalleryType {
fk_view_id?: string; fk_view_id?: string;
@ -90,16 +91,19 @@ export default class GalleryView implements GalleryType {
) { ) {
// get existing cache // get existing cache
const key = `${CacheScope.GALLERY_VIEW}:${galleryId}`; 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) { if (o) {
o.next_enabled = body.next_enabled; o = { ...o, ...updateObj };
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;
// set cache // set cache
await NocoCache.set(key, o); await NocoCache.set(key, o);
} }
@ -108,16 +112,7 @@ export default class GalleryView implements GalleryType {
null, null,
null, null,
MetaTable.GALLERY_VIEW, MetaTable.GALLERY_VIEW,
{ updateObj,
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,
},
{ {
fk_view_id: galleryId, fk_view_id: galleryId,
} }

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

@ -3,6 +3,7 @@ import { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import GridViewColumn from './GridViewColumn'; import GridViewColumn from './GridViewColumn';
import View from './View'; import View from './View';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class GridView { export default class GridView {
fk_view_id: string; fk_view_id: string;
@ -69,23 +70,16 @@ export default class GridView {
) { ) {
// get existing cache // get existing cache
const key = `${CacheScope.GRID_VIEW}:${viewId}`; 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) { if (o) {
o.row_height = body.row_height; o = { ...o, ...updateObj };
// set cache // set cache
await NocoCache.set(key, o); await NocoCache.set(key, o);
} }
// update meta // update meta
return await ncMeta.metaUpdate( return await ncMeta.metaUpdate(null, null, MetaTable.GRID_VIEW, updateObj, {
null, fk_view_id: viewId,
null, });
MetaTable.GRID_VIEW,
{
row_height: body.row_height,
},
{
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 NocoCache from '../cache/NocoCache';
import Filter from './Filter'; import Filter from './Filter';
import HookFilter from './HookFilter'; import HookFilter from './HookFilter';
import { extractProps } from '../meta/helpers/extractProps';
export default class Hook implements HookType { export default class Hook implements HookType {
id?: string; id?: string;
@ -18,7 +19,7 @@ export default class Hook implements HookType {
description?: string; description?: string;
env?: string; env?: string;
type?: string; type?: string;
event?: 'After' | 'Before'; event?: 'after' | 'before';
operation?: 'insert' | 'delete' | 'update'; operation?: 'insert' | 'delete' | 'update';
async?: boolean; async?: boolean;
payload?: string; payload?: string;
@ -175,27 +176,41 @@ export default class Hook implements HookType {
hook: Partial<Hook>, hook: Partial<Hook>,
ncMeta = Noco.ncMeta ncMeta = Noco.ncMeta
) { ) {
const updateObj = { const updateObj = extractProps(hook, [
title: hook.title, 'title',
description: hook.description, 'description',
env: hook.env, 'env',
type: hook.type, 'type',
event: hook.event?.toLowerCase?.(), 'event',
operation: hook.operation?.toLowerCase?.(), 'operation',
async: hook.async, 'async',
payload: !!hook.payload, 'payload',
url: hook.url, 'url',
headers: hook.headers, 'headers',
condition: !!hook.condition, 'condition',
notification: 'notification',
hook.notification && typeof hook.notification === 'object' 'retries',
? JSON.stringify(hook.notification) 'retry_interval',
: hook.notification, 'timeout',
retries: hook.retries, 'active',
retry_interval: hook.retry_interval, ]);
timeout: hook.timeout,
active: hook.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 // get existing cache
const key = `${CacheScope.HOOK}:${hookId}`; const key = `${CacheScope.HOOK}:${hookId}`;
@ -203,7 +218,6 @@ export default class Hook implements HookType {
if (o) { if (o) {
// update data // update data
o = { ...o, ...updateObj }; o = { ...o, ...updateObj };
o.notification = updateObj.notification;
// set cache // set cache
await NocoCache.set(key, o); await NocoCache.set(key, o);
} }

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

@ -10,6 +10,7 @@ import {
import View from './View'; import View from './View';
import { FilterType, UITypes } from 'nocodb-sdk'; import { FilterType, UITypes } from 'nocodb-sdk';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class Filter { export default class Filter {
id: string; id: string;
@ -138,15 +139,14 @@ export default class Filter {
} }
static async update(id, filter: Partial<Filter>, ncMeta = Noco.ncMeta) { static async update(id, filter: Partial<Filter>, ncMeta = Noco.ncMeta) {
const updateObj = { const updateObj = extractProps(filter, [
fk_column_id: filter.fk_column_id, 'fk_column_id',
comparison_op: filter.comparison_op, 'comparison_op',
value: filter.value, 'value',
fk_parent_id: filter.fk_parent_id, 'fk_parent_id',
'is_group',
is_group: filter.is_group, 'logical_op',
logical_op: filter.logical_op, ]);
};
// get existing cache // get existing cache
const key = `${CacheScope.FILTER_EXP}:${id}`; const key = `${CacheScope.FILTER_EXP}:${id}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); 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 { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import View from './View'; import View from './View';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class KanbanView implements KanbanType { export default class KanbanView implements KanbanType {
fk_view_id: string; fk_view_id: string;
@ -99,13 +100,20 @@ export default class KanbanView implements KanbanType {
// get existing cache // get existing cache
const key = `${CacheScope.KANBAN_VIEW}:${kanbanId}`; const key = `${CacheScope.KANBAN_VIEW}:${kanbanId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);
const updateObj = {
...body, const updateObj = extractProps(body, [
meta: 'title',
typeof body.meta === 'string' 'fk_cover_image_col_id',
? body.meta 'meta',
: JSON.stringify(body.meta ?? {}), ]);
};
if (updateObj.meta) {
updateObj.meta =
typeof updateObj.meta === 'string'
? updateObj.meta
: JSON.stringify(updateObj.meta ?? {});
}
if (o) { if (o) {
o = { ...o, ...updateObj }; o = { ...o, ...updateObj };
// set cache // 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 { CacheGetType, CacheScope, MetaTable } from '../utils/globals';
import Noco from '../Noco'; import Noco from '../Noco';
import NocoCache from '../cache/NocoCache'; import NocoCache from '../cache/NocoCache';
import { extractProps } from '../meta/helpers/extractProps';
export default class Plugin implements PluginType { export default class Plugin implements PluginType {
id?: string; id?: string;
@ -56,13 +57,15 @@ export default class Plugin implements PluginType {
} }
public static async update(pluginId: string, plugin: Partial<PluginType>) { public static async update(pluginId: string, plugin: Partial<PluginType>) {
const updateObj = { const updateObj = extractProps(plugin, ['input', 'active']);
input:
plugin.input && typeof plugin.input === 'object' if (updateObj.input) {
? JSON.stringify(plugin.input) updateObj.input =
: plugin.input, updateObj.input && typeof updateObj.input === 'object'
active: plugin.active, ? JSON.stringify(updateObj.input)
}; : updateObj.input;
}
// get existing cache // get existing cache
const key = `${CacheScope.PLUGIN}:${pluginId}`; const key = `${CacheScope.PLUGIN}:${pluginId}`;
let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT); let o = await NocoCache.get(key, CacheGetType.TYPE_OBJECT);

Loading…
Cancel
Save