From b945520776922c987034ea6792122d67882b1c36 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 9 Mar 2023 17:17:33 +0800 Subject: [PATCH] refactor(nocodb): load individual packages instead of all packages --- .../lib/db/sql-client/lib/mysql/TidbClient.ts | 4 ++-- .../db/sql-client/lib/mysql/VitessClient.ts | 8 +++----- .../lib/snowflake/SnowflakeClient.ts | 13 ++++-------- .../lib/db/sql-data-mapper/lib/BaseModel.ts | 10 +++++----- .../sql-data-mapper/lib/sql/BaseModelSql.ts | 20 ++++++++++--------- .../sql-data-mapper/lib/sql/BaseModelSqlv2.ts | 11 +++++----- .../gql-schema/xc-ts/BaseGqlXcTsSchema.ts | 6 +++--- .../code/policies/xc/ExpressXcPolicy.ts | 6 +++--- .../src/lib/v1-legacy/gql/GqlApiBuilder.ts | 9 ++++----- 9 files changed, 40 insertions(+), 47 deletions(-) diff --git a/packages/nocodb/src/lib/db/sql-client/lib/mysql/TidbClient.ts b/packages/nocodb/src/lib/db/sql-client/lib/mysql/TidbClient.ts index 25b6c1f37f..37b79f48cf 100644 --- a/packages/nocodb/src/lib/db/sql-client/lib/mysql/TidbClient.ts +++ b/packages/nocodb/src/lib/db/sql-client/lib/mysql/TidbClient.ts @@ -1,4 +1,4 @@ -import _ from 'lodash'; +import mapKeys from 'lodash/mapKeys'; import Debug from '../../../util/Debug'; import Result from '../../../util/Result'; import MysqlClient from './MysqlClient'; @@ -32,7 +32,7 @@ class Tidb extends MysqlClient { for (let i = 0; i < response[0].length; ++i) { let index = response[0][i]; - index = _.mapKeys(index, function (_v, k) { + index = mapKeys(index, function (_v, k) { return k.toLowerCase(); }); indexes.push(index); diff --git a/packages/nocodb/src/lib/db/sql-client/lib/mysql/VitessClient.ts b/packages/nocodb/src/lib/db/sql-client/lib/mysql/VitessClient.ts index 4387d0e55b..f6aa29261d 100644 --- a/packages/nocodb/src/lib/db/sql-client/lib/mysql/VitessClient.ts +++ b/packages/nocodb/src/lib/db/sql-client/lib/mysql/VitessClient.ts @@ -1,4 +1,4 @@ -import _ from 'lodash'; +import mapKeys from 'lodash/mapKeys'; import Debug from '../../../util/Debug'; import Result from '../../../util/Result'; import MysqlClient from './MysqlClient'; @@ -140,9 +140,7 @@ class Vitess extends MysqlClient { for (let i = 0; i < response[0].length; ++i) { const column: any = {}; - response[0][i] = _.mapKeys(response[0][i], (_v, k) => - k.toLowerCase() - ); + response[0][i] = mapKeys(response[0][i], (_v, k) => k.toLowerCase()); column.tn = args.tn; column.cn = response[0][i].cn; @@ -219,7 +217,7 @@ class Vitess extends MysqlClient { for (let i = 0; i < response[0].length; ++i) { let index = response[0][i]; - index = _.mapKeys(index, function (_v, k) { + index = mapKeys(index, function (_v, k) { return k.toLowerCase(); }); indexes.push(index); diff --git a/packages/nocodb/src/lib/db/sql-client/lib/snowflake/SnowflakeClient.ts b/packages/nocodb/src/lib/db/sql-client/lib/snowflake/SnowflakeClient.ts index 0f6dc11f62..8b8e166937 100644 --- a/packages/nocodb/src/lib/db/sql-client/lib/snowflake/SnowflakeClient.ts +++ b/packages/nocodb/src/lib/db/sql-client/lib/snowflake/SnowflakeClient.ts @@ -1,6 +1,6 @@ import { nanoid } from 'nanoid'; -import _ from 'lodash'; -import lodash from 'lodash'; +import find from 'lodash/find'; +import isEmpty from 'lodash/isEmpty'; import KnexClient from '../KnexClient'; import Debug from '../../../util/Debug'; import Result from '../../../util/Result'; @@ -1341,7 +1341,7 @@ class SnowflakeClient extends KnexClient { */ async _getQuery(args) { try { - if (_.isEmpty(this._version)) { + if (isEmpty(this._version)) { const result = await this.version(); this._version = result.data.object; log.debug( @@ -1350,8 +1350,6 @@ class SnowflakeClient extends KnexClient { ); } - // log.debug(this._version, args); - if (this._version.key in this.queries[args.func]) { return this.queries[args.func][this._version.key].sql; } @@ -1831,7 +1829,7 @@ class SnowflakeClient extends KnexClient { let downQuery = ''; for (let i = 0; i < args.columns.length; ++i) { - const oldColumn = lodash.find(originalColumns, { + const oldColumn = find(originalColumns, { cn: args.columns[i].cno, }); @@ -2640,7 +2638,6 @@ function getDefaultValue(n) { case 'dec': return n.cdf; break; - case 'datetime': case 'timestamp': case 'date': @@ -2651,10 +2648,8 @@ function getDefaultValue(n) { ) { return n.cdf; } - // return JSON.stringify(n.cdf); break; default: - // return JSON.stringify(n.cdf); break; } return n.cdf; diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/BaseModel.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/BaseModel.ts index 1f4989c0cd..2a9343f02d 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/BaseModel.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/BaseModel.ts @@ -1,10 +1,10 @@ /* eslint-disable @typescript-eslint/ban-types,prefer-const */ +import groupBy from 'lodash/groupBy'; import type { Knex } from 'knex'; import type Filter from '../../../models/Filter'; import type Sort from '../../../models/Sort'; const autoBind = require('auto-bind'); -const _ = require('lodash'); const Validator = require('validator'); // interface BaseModel { @@ -977,7 +977,7 @@ abstract class BaseModel { ) ); - const gs = _.groupBy(childs, cn); + const gs = groupBy(childs, cn); parent.forEach((row) => { row[child] = gs[row[this.pks?.[0]?.cn]] || []; }); @@ -1133,7 +1133,7 @@ abstract class BaseModel { .whereIn(rcn, parentIds) ); - const gs = _.groupBy(parents, rcn); + const gs = groupBy(parents, rcn); childs.forEach((row) => { row[parent] = gs[row[cn]] && gs[row[cn]][0]; @@ -1179,7 +1179,7 @@ abstract class BaseModel { ) ); - return _.groupBy(childs, cn); + return groupBy(childs, cn); } catch (e) { console.log(e); throw e; @@ -1224,7 +1224,7 @@ abstract class BaseModel { ); return childs.map(({ count }) => count); - // return _.groupBy(childs, cn); + // return groupBy(childs, cn); } catch (e) { console.log(e); throw e; diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSql.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSql.ts index 0f394b6bf9..b0b7b4926f 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSql.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSql.ts @@ -1,5 +1,7 @@ import autoBind from 'auto-bind'; -import _ from 'lodash'; +import groupBy from 'lodash/groupBy'; +import isEmpty from 'lodash/isEmpty'; +import difference from 'lodash/difference'; import Validator from 'validator'; import Papaparse from 'papaparse'; import BaseModel from '../BaseModel'; @@ -315,7 +317,7 @@ class BaseModelSql extends BaseModel { : response?.[ai._cn], this.defaultNestedBtQueryParams ); - response = !_.isEmpty(nestedResponse) ? nestedResponse : response; + response = !isEmpty(nestedResponse) ? nestedResponse : response; } if (Array.isArray(response)) { @@ -1213,7 +1215,7 @@ class BaseModelSql extends BaseModel { private _buildDistinctQuery(columns: string[]) { const query = this.$db; const formulaColumns = this.filterFormulaColumns(columns); - const otherColumns = _.difference(columns, formulaColumns); + const otherColumns = difference(columns, formulaColumns); if (!otherColumns.length && !formulaColumns.length) { query.distinct(this.selectQuery('')).distinct(...this.selectFormulas); } @@ -1330,7 +1332,7 @@ class BaseModelSql extends BaseModel { ) ); - const gs = _.groupBy(childs, _cn); + const gs = groupBy(childs, _cn); parent.forEach((row) => { row[`${this.dbModels?.[child]?._tn || child}List`] = gs[row[_cn] || row[this.pks[0]._cn]] || []; @@ -1408,7 +1410,7 @@ class BaseModelSql extends BaseModel { ) ); - const gs = _.groupBy(childs, `${tn}_${vcn}`); + const gs = groupBy(childs, `${tn}_${vcn}`); return parentIds.map((id) => gs[id] || []); } @@ -2004,7 +2006,7 @@ class BaseModelSql extends BaseModel { .whereIn(rcn, parentIds) ); - const gs = _.groupBy( + const gs = groupBy( parents, this.dbModels[parent]?.columnToAlias?.[rcn] || rcn ); @@ -2083,8 +2085,8 @@ class BaseModelSql extends BaseModel { ) ); - // return _.groupBy(childs, cn); - return _.groupBy(childs, this.dbModels?.[child]?.columnToAlias[cn]); + // return groupBy(childs, cn); + return groupBy(childs, this.dbModels?.[child]?.columnToAlias[cn]); } catch (e) { console.log(e); throw e; @@ -2133,7 +2135,7 @@ class BaseModelSql extends BaseModel { ); return childs.map(({ count }) => count); - // return _.groupBy(childs, cn); + // return groupBy(childs, cn); } catch (e) { console.log(e); throw e; diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts index 9f777a217a..35ebadccc1 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts @@ -1,5 +1,5 @@ import autoBind from 'auto-bind'; -import _ from 'lodash'; +import groupBy from 'lodash/groupBy'; import DataLoader from 'dataloader'; import { AuditOperationSubTypes, @@ -436,7 +436,7 @@ class BaseModelSqlv2 { }) ).getProto(); - return _.groupBy( + return groupBy( children.map((c) => { c.__proto__ = proto; return c; @@ -604,7 +604,6 @@ class BaseModelSqlv2 { .first(); const { count } = await query; return count; - // return _.groupBy(children, cn); } catch (e) { console.log(e); throw e; @@ -682,7 +681,7 @@ class BaseModelSqlv2 { dbDriver: this.dbDriver, }) ).getProto(); - const gs = _.groupBy( + const gs = groupBy( children.map((c) => { c.__proto__ = proto; return c; @@ -799,7 +798,7 @@ class BaseModelSqlv2 { !this.isSqlite ); - const gs = _.groupBy(children, GROUP_COL); + const gs = groupBy(children, GROUP_COL); return parentIds.map((id) => gs?.[id]?.[0] || []); } @@ -1361,7 +1360,7 @@ class BaseModelSqlv2 { }, true ); - const gs = _.groupBy(data, pCol.title); + const gs = groupBy(data, pCol.title); return ids.map(async (id: string) => gs?.[id]?.[0]); } catch (e) { console.log(e); diff --git a/packages/nocodb/src/lib/db/sql-mgr/code/gql-schema/xc-ts/BaseGqlXcTsSchema.ts b/packages/nocodb/src/lib/db/sql-mgr/code/gql-schema/xc-ts/BaseGqlXcTsSchema.ts index 2e6de93b6b..87b24133a8 100644 --- a/packages/nocodb/src/lib/db/sql-mgr/code/gql-schema/xc-ts/BaseGqlXcTsSchema.ts +++ b/packages/nocodb/src/lib/db/sql-mgr/code/gql-schema/xc-ts/BaseGqlXcTsSchema.ts @@ -1,4 +1,4 @@ -import lodash from 'lodash'; +import uniqBy from 'lodash/uniqBy'; import BaseRender from '../../BaseRender'; import { AGG_DEFAULT_COLS, GROUPBY_DEFAULT_COLS } from './schemaHelp'; @@ -140,7 +140,7 @@ abstract class BaseGqlXcTsSchema extends BaseRender { let hasManyRelations = args.hasMany; if (hasManyRelations.length > 1) { - hasManyRelations = lodash.uniqBy(hasManyRelations, (e) => { + hasManyRelations = uniqBy(hasManyRelations, (e) => { return [e.tn, e.rtn].join(); }); } @@ -159,7 +159,7 @@ abstract class BaseGqlXcTsSchema extends BaseRender { let belongsToRelations = args.belongsTo; if (belongsToRelations.length > 1) { - belongsToRelations = lodash.uniqBy(belongsToRelations, (e) => { + belongsToRelations = uniqBy(belongsToRelations, (e) => { return [e.tn, e.rtn].join(); }); } diff --git a/packages/nocodb/src/lib/db/sql-mgr/code/policies/xc/ExpressXcPolicy.ts b/packages/nocodb/src/lib/db/sql-mgr/code/policies/xc/ExpressXcPolicy.ts index e896b3da50..0265a7380b 100644 --- a/packages/nocodb/src/lib/db/sql-mgr/code/policies/xc/ExpressXcPolicy.ts +++ b/packages/nocodb/src/lib/db/sql-mgr/code/policies/xc/ExpressXcPolicy.ts @@ -1,4 +1,4 @@ -import lodash from 'lodash'; +import uniqBy from 'lodash/uniqBy'; import BaseRender from '../../BaseRender'; import type { Acl } from '../../../../../../interface/config'; @@ -59,7 +59,7 @@ class ExpressXcMiddleware extends BaseRender { ? args.relations.filter((r) => r.rtn === args.tn) : []; if (hmRelations.length > 1) - hmRelations = lodash.uniqBy(hmRelations, function (e) { + hmRelations = uniqBy(hmRelations, function (e) { return [e.tn, e.rtn].join(); }); for (let i = 0; i < hmRelations.length; ++i) { @@ -85,7 +85,7 @@ class ExpressXcMiddleware extends BaseRender { ? args.relations.filter((r) => r.tn === args.tn) : []; if (btRelations.length > 1) - btRelations = lodash.uniqBy(btRelations, function (e) { + btRelations = uniqBy(btRelations, function (e) { return [e.tn, e.rtn].join(); }); for (let i = 0; i < btRelations.length; ++i) { diff --git a/packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts b/packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts index c58d960bf2..e079f802a9 100644 --- a/packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts +++ b/packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts @@ -3,7 +3,7 @@ import debug from 'debug'; import { Router } from 'express'; import { execute } from 'graphql'; import { GraphQLJSON } from 'graphql-type-json'; -import _ from 'lodash'; +import groupBy from 'lodash/groupBy'; import { BaseType } from 'xc-core-ts'; import ExpressXcTsPolicyGql from '../../db/sql-mgr/code/gql-policies/xc-ts/ExpressXcTsPolicyGql'; import GqlXcSchemaFactory from '../../db/sql-mgr/code/gql-schema/xc-ts/GqlXcSchemaFactory'; @@ -716,7 +716,7 @@ export class GqlApiBuilder extends BaseApiBuilder implements XcMetaMgr { limit: ids.length, where: `(${belongsToRel.rcn},in,${ids.join(',')})`, }); - const gs = _.groupBy(data, rcolNameAlias); + const gs = groupBy(data, rcolNameAlias); return ids.map( async (id: string) => gs?.[id]?.[0] && new self.types[belongsToRel.rtn](gs[id][0]) @@ -2102,7 +2102,7 @@ export class GqlApiBuilder extends BaseApiBuilder implements XcMetaMgr { where: `(${currentRelation.rcn},in,${ids.join(',')})`, limit: ids.length }) - const gs = _.groupBy(data, currentRelation.rcn); + const gs = groupBy(data, currentRelation.rcn); return ids.map(async id => gs?.[id]?.[0] && new self.types[currentRelation.rtn](gs[id][0])) }, [mw.postLoaderMiddleware] @@ -2729,8 +2729,7 @@ export class GqlApiBuilder extends BaseApiBuilder implements XcMetaMgr { private generateLoaderFromStringBody(fnBody: string[]): any { this.log(`generateLoaderFromStringBody : `); - // @ts-ignore - const _ = require('lodash'); + if (!(fnBody && Array.isArray(fnBody) && fnBody.length)) { return; }