From 05f7e37917434e9ab427c3b4dcf253ec6bd68687 Mon Sep 17 00:00:00 2001 From: AlexHladin Date: Wed, 31 Jan 2024 11:50:52 +0200 Subject: [PATCH] refactor: Remove duplicated code in models --- .../sql-mgr/code/models/xc/BaseModelXcMeta.ts | 51 +++++++++++++++++-- .../code/models/xc/ModelXcMetaMssql.ts | 49 +----------------- .../code/models/xc/ModelXcMetaMysql.ts | 48 +---------------- .../code/models/xc/ModelXcMetaOracle.ts | 48 +---------------- .../sql-mgr/code/models/xc/ModelXcMetaPg.ts | 48 +---------------- .../code/models/xc/ModelXcMetaSnowflake.ts | 48 +---------------- .../code/models/xc/ModelXcMetaSqlite.ts | 48 +---------------- .../v1-legacy/BaseApiBuilder.ts | 2 +- 8 files changed, 55 insertions(+), 287 deletions(-) diff --git a/packages/nocodb/src/db/sql-mgr/code/models/xc/BaseModelXcMeta.ts b/packages/nocodb/src/db/sql-mgr/code/models/xc/BaseModelXcMeta.ts index 224c37acc3..831544647c 100644 --- a/packages/nocodb/src/db/sql-mgr/code/models/xc/BaseModelXcMeta.ts +++ b/packages/nocodb/src/db/sql-mgr/code/models/xc/BaseModelXcMeta.ts @@ -101,12 +101,11 @@ abstract class BaseModelXcMeta extends BaseRender { belongsTo: this.ctx.belongsTo, db_type: this.ctx.db_type, type: this.ctx.type, - - v: this.getVitualColumns(), + v: this.getVirtualColumns(), }; } - public getVitualColumns(): any[] { + public getVirtualColumns(): any[] { // todo: handle duplicate relation const virtualColumns = [ ...(this.ctx.hasMany || []).map((hm) => { @@ -138,6 +137,52 @@ abstract class BaseModelXcMeta extends BaseRender { public mapDefaultDisplayValue(columnsArr: any[]): void { mapDefaultDisplayValue(columnsArr); } + + /** + * + * @param args + * @param args.columns + * @param args.relations + * @returns {string} + * @private + */ + protected renderXcColumns(args) { + let str = '[\r\n'; + + for (let i = 0; i < args.columns.length; ++i) { + str += `{\r\n`; + str += `cn: '${args.columns[i].cn}',\r\n`; + str += `type: '${this._getAbstractType(args.columns[i])}',\r\n`; + str += `dt: '${args.columns[i].dt}',\r\n`; + if (args.columns[i].rqd) str += `rqd: ${args.columns[i].rqd},\r\n`; + + if (args.columns[i].cdf) { + str += `default: "${args.columns[i].cdf}",\r\n`; + str += `columnDefault: "${args.columns[i].cdf}",\r\n`; + } + + if (args.columns[i].un) str += `un: ${args.columns[i].un},\r\n`; + + if (args.columns[i].pk) str += `pk: ${args.columns[i].pk},\r\n`; + + if (args.columns[i].ai) str += `ai: ${args.columns[i].ai},\r\n`; + + if (args.columns[i].dtxp) str += `dtxp: "${args.columns[i].dtxp}",\r\n`; + + if (args.columns[i].dtxs) str += `dtxs: ${args.columns[i].dtxs},\r\n`; + + str += `validate: { + func: [], + args: [], + msg: [] + },`; + str += `},\r\n`; + } + + str += ']\r\n'; + + return str; + } } export default BaseModelXcMeta; diff --git a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMssql.ts b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMssql.ts index a9758f9a31..731ec24121 100644 --- a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMssql.ts +++ b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMssql.ts @@ -25,7 +25,7 @@ class ModelXcMetaMssql extends BaseModelXcMeta { /* for complex code provide a func and args - do derivation within the func cbk */ data.columns = { - func: this._renderXcColumns.bind(this), + func: this.renderXcColumns.bind(this), args: { tn: this.ctx.tn, columns: this.ctx.columns, @@ -56,53 +56,6 @@ class ModelXcMetaMssql extends BaseModelXcMeta { return data; } - /** - * - * @param args - * @param args.columns - * @param args.relations - * @returns {string} - * @private - */ - _renderXcColumns(args) { - let str = '[\r\n'; - - for (let i = 0; i < args.columns.length; ++i) { - str += `{\r\n`; - str += `cn: '${args.columns[i].cn}',\r\n`; - str += `type: '${this._getAbstractType(args.columns[i])}',\r\n`; - str += `dt: '${args.columns[i].dt}',\r\n`; - - if (args.columns[i].rqd) str += `rqd: ${args.columns[i].rqd},\r\n`; - - if (args.columns[i].cdf) { - str += `default: "${args.columns[i].cdf}",\r\n`; - str += `columnDefault: "${args.columns[i].cdf}",\r\n`; - } - - if (args.columns[i].un) str += `un: ${args.columns[i].un},\r\n`; - - if (args.columns[i].pk) str += `pk: ${args.columns[i].pk},\r\n`; - - if (args.columns[i].ai) str += `ai: ${args.columns[i].ai},\r\n`; - - if (args.columns[i].dtxp) str += `dtxp: "${args.columns[i].dtxp}",\r\n`; - - if (args.columns[i].dtxs) str += `dtxs: ${args.columns[i].dtxs},\r\n`; - - str += `validate: { - func: [], - args: [], - msg: [] - },`; - str += `},\r\n`; - } - - str += ']\r\n'; - - return str; - } - _getAbstractType(column) { let str = ''; switch (column.dt) { diff --git a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMysql.ts b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMysql.ts index 7ea3705955..290dc97b95 100644 --- a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMysql.ts +++ b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaMysql.ts @@ -25,7 +25,7 @@ class ModelXcMetaMysql extends BaseModelXcMeta { /* for complex code provide a func and args - do derivation within the func cbk */ data.columns = { - func: this._renderXcColumns.bind(this), + func: this.renderXcColumns.bind(this), args: { tn: this.ctx.tn, columns: this.ctx.columns, @@ -57,52 +57,6 @@ class ModelXcMetaMysql extends BaseModelXcMeta { return data; } - /** - * - * @param args - * @param args.columns - * @param args.relations - * @returns {string} - * @private - */ - _renderXcColumns(args) { - let str = '[\r\n'; - - for (let i = 0; i < args.columns.length; ++i) { - str += `{\r\n`; - str += `cn: '${args.columns[i].cn}',\r\n`; - str += `type: '${this._getAbstractType(args.columns[i])}',\r\n`; - str += `dt: '${args.columns[i].dt}',\r\n`; - if (args.columns[i].rqd) str += `rqd: ${args.columns[i].rqd},\r\n`; - - if (args.columns[i].cdf) { - str += `default: "${args.columns[i].cdf}",\r\n`; - str += `columnDefault: "${args.columns[i].cdf}",\r\n`; - } - - if (args.columns[i].un) str += `un: ${args.columns[i].un},\r\n`; - - if (args.columns[i].pk) str += `pk: ${args.columns[i].pk},\r\n`; - - if (args.columns[i].ai) str += `ai: ${args.columns[i].ai},\r\n`; - - if (args.columns[i].dtxp) str += `dtxp: "${args.columns[i].dtxp}",\r\n`; - - if (args.columns[i].dtxs) str += `dtxs: ${args.columns[i].dtxs},\r\n`; - - str += `validate: { - func: [], - args: [], - msg: [] - },`; - str += `},\r\n`; - } - - str += ']\r\n'; - - return str; - } - /* getXcColumnsObject(args) { const columnsArr = []; diff --git a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaOracle.ts b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaOracle.ts index 6b5bf35eda..eeb59c17b6 100644 --- a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaOracle.ts +++ b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaOracle.ts @@ -25,7 +25,7 @@ class ModelXcMetaOracle extends BaseModelXcMeta { /* for complex code provide a func and args - do derivation within the func cbk */ data.columns = { - func: this._renderXcColumns.bind(this), + func: this.renderXcColumns.bind(this), args: { tn: this.ctx.tn, columns: this.ctx.columns, @@ -57,52 +57,6 @@ class ModelXcMetaOracle extends BaseModelXcMeta { return data; } - /** - * - * @param args - * @param args.columns - * @param args.relations - * @returns {string} - * @private - */ - _renderXcColumns(args) { - let str = '[\r\n'; - - for (let i = 0; i < args.columns.length; ++i) { - str += `{\r\n`; - str += `cn: '${args.columns[i].cn}',\r\n`; - str += `type: '${this._getAbstractType(args.columns[i])}',\r\n`; - str += `dt: '${args.columns[i].dt}',\r\n`; - if (args.columns[i].rqd) str += `rqd: ${args.columns[i].rqd},\r\n`; - - if (args.columns[i].cdf) { - str += `default: "${args.columns[i].cdf}",\r\n`; - str += `columnDefault: "${args.columns[i].cdf}",\r\n`; - } - - if (args.columns[i].un) str += `un: ${args.columns[i].un},\r\n`; - - if (args.columns[i].pk) str += `pk: ${args.columns[i].pk},\r\n`; - - if (args.columns[i].ai) str += `ai: ${args.columns[i].ai},\r\n`; - - if (args.columns[i].dtxp) str += `dtxp: "${args.columns[i].dtxp}",\r\n`; - - if (args.columns[i].dtxs) str += `dtxs: ${args.columns[i].dtxs},\r\n`; - - str += `validate: { - func: [], - args: [], - msg: [] - },`; - str += `},\r\n`; - } - - str += ']\r\n'; - - return str; - } - /* getXcColumnsObject(args) { const columnsArr = []; diff --git a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaPg.ts b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaPg.ts index 8a14c520a6..93efd3d4c3 100644 --- a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaPg.ts +++ b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaPg.ts @@ -25,7 +25,7 @@ class ModelXcMetaPg extends BaseModelXcMeta { /* for complex code provide a func and args - do derivation within the func cbk */ data.columns = { - func: this._renderXcColumns.bind(this), + func: this.renderXcColumns.bind(this), args: { tn: this.ctx.tn, columns: this.ctx.columns, @@ -56,52 +56,6 @@ class ModelXcMetaPg extends BaseModelXcMeta { return data; } - /** - * - * @param args - * @param args.columns - * @param args.relations - * @returns {string} - * @private - */ - _renderXcColumns(args) { - let str = '[\r\n'; - - for (let i = 0; i < args.columns.length; ++i) { - str += `{\r\n`; - str += `cn: '${args.columns[i].cn}',\r\n`; - str += `type: '${this._getAbstractType(args.columns[i])}',\r\n`; - str += `dt: '${args.columns[i].dt}',\r\n`; - if (args.columns[i].rqd) str += `rqd: ${args.columns[i].rqd},\r\n`; - - if (args.columns[i].cdf) { - str += `default: "${args.columns[i].cdf}",\r\n`; - str += `columnDefault: "${args.columns[i].cdf}",\r\n`; - } - - if (args.columns[i].un) str += `un: ${args.columns[i].un},\r\n`; - - if (args.columns[i].pk) str += `pk: ${args.columns[i].pk},\r\n`; - - if (args.columns[i].ai) str += `ai: ${args.columns[i].ai},\r\n`; - - if (args.columns[i].dtxp) str += `dtxp: "${args.columns[i].dtxp}",\r\n`; - - if (args.columns[i].dtxs) str += `dtxs: ${args.columns[i].dtxs},\r\n`; - - str += `validate: { - func: [], - args: [], - msg: [] - },`; - str += `},\r\n`; - } - - str += ']\r\n'; - - return str; - } - _getAbstractType(column) { let str = ''; switch (column.dt) { diff --git a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSnowflake.ts b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSnowflake.ts index 0951ea3c0c..9b25a0c352 100644 --- a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSnowflake.ts +++ b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSnowflake.ts @@ -25,7 +25,7 @@ class ModelXcMetaSnowflake extends BaseModelXcMeta { /* for complex code provide a func and args - do derivation within the func cbk */ data.columns = { - func: this._renderXcColumns.bind(this), + func: this.renderXcColumns.bind(this), args: { tn: this.ctx.tn, columns: this.ctx.columns, @@ -56,52 +56,6 @@ class ModelXcMetaSnowflake extends BaseModelXcMeta { return data; } - /** - * - * @param args - * @param args.columns - * @param args.relations - * @returns {string} - * @private - */ - _renderXcColumns(args) { - let str = '[\r\n'; - - for (let i = 0; i < args.columns.length; ++i) { - str += `{\r\n`; - str += `cn: '${args.columns[i].cn}',\r\n`; - str += `type: '${this._getAbstractType(args.columns[i])}',\r\n`; - str += `dt: '${args.columns[i].dt}',\r\n`; - if (args.columns[i].rqd) str += `rqd: ${args.columns[i].rqd},\r\n`; - - if (args.columns[i].cdf) { - str += `default: "${args.columns[i].cdf}",\r\n`; - str += `columnDefault: "${args.columns[i].cdf}",\r\n`; - } - - if (args.columns[i].un) str += `un: ${args.columns[i].un},\r\n`; - - if (args.columns[i].pk) str += `pk: ${args.columns[i].pk},\r\n`; - - if (args.columns[i].ai) str += `ai: ${args.columns[i].ai},\r\n`; - - if (args.columns[i].dtxp) str += `dtxp: "${args.columns[i].dtxp}",\r\n`; - - if (args.columns[i].dtxs) str += `dtxs: ${args.columns[i].dtxs},\r\n`; - - str += `validate: { - func: [], - args: [], - msg: [] - },`; - str += `},\r\n`; - } - - str += ']\r\n'; - - return str; - } - _getAbstractType(column) { let str = ''; switch (column.dt) { diff --git a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSqlite.ts b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSqlite.ts index ec4f1fee93..0a13dba517 100644 --- a/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSqlite.ts +++ b/packages/nocodb/src/db/sql-mgr/code/models/xc/ModelXcMetaSqlite.ts @@ -25,7 +25,7 @@ class ModelXcMetaSqlite extends BaseModelXcMeta { /* for complex code provide a func and args - do derivation within the func cbk */ data.columns = { - func: this._renderXcColumns.bind(this), + func: this.renderXcColumns.bind(this), args: { tn: this.ctx.tn, columns: this.ctx.columns, @@ -56,52 +56,6 @@ class ModelXcMetaSqlite extends BaseModelXcMeta { return data; } - /** - * - * @param args - * @param args.columns - * @param args.relations - * @returns {string} - * @private - */ - _renderXcColumns(args) { - let str = '[\r\n'; - - for (let i = 0; i < args.columns.length; ++i) { - str += `{\r\n`; - str += `cn: '${args.columns[i].cn}',\r\n`; - str += `type: '${this._getAbstractType(args.columns[i])}',\r\n`; - str += `dt: '${args.columns[i].dt}',\r\n`; - if (args.columns[i].rqd) str += `rqd: ${args.columns[i].rqd},\r\n`; - - if (args.columns[i].cdf) { - str += `default: "${args.columns[i].cdf}",\r\n`; - str += `columnDefault: "${args.columns[i].cdf}",\r\n`; - } - - if (args.columns[i].un) str += `un: ${args.columns[i].un},\r\n`; - - if (args.columns[i].pk) str += `pk: ${args.columns[i].pk},\r\n`; - - if (args.columns[i].ai) str += `ai: ${args.columns[i].ai},\r\n`; - - if (args.columns[i].dtxp) str += `dtxp: "${args.columns[i].dtxp}",\r\n`; - - if (args.columns[i].dtxs) str += `dtxs: ${args.columns[i].dtxs},\r\n`; - - str += `validate: { - func: [], - args: [], - msg: [] - },`; - str += `},\r\n`; - } - - str += ']\r\n'; - - return str; - } - protected _getAbstractType(column) { let str = ''; switch (column.dt) { diff --git a/packages/nocodb/src/version-upgrader/v1-legacy/BaseApiBuilder.ts b/packages/nocodb/src/version-upgrader/v1-legacy/BaseApiBuilder.ts index 222f05bb54..64e1e3a994 100644 --- a/packages/nocodb/src/version-upgrader/v1-legacy/BaseApiBuilder.ts +++ b/packages/nocodb/src/version-upgrader/v1-legacy/BaseApiBuilder.ts @@ -373,7 +373,7 @@ export default abstract class BaseApiBuilder { dir: '', ctx, filename: '', - }).getVitualColumns(); + }).getVirtualColumns(); // set default display values ModelXcMetaFactory.create( this.connectionConfig,