From 7cffb8942555f500cb44f06768fc576021fa407a Mon Sep 17 00:00:00 2001 From: killa Date: Wed, 9 Oct 2024 11:17:55 +0800 Subject: [PATCH] chore: refactor DataSource with construct inject (#244) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) ##### Description of change ## Summary by CodeRabbit - **New Features** - The `generateSql` method is now publicly accessible, enhancing SQL hinting capabilities. - The `DataSourceDelegate` class has been restructured to extend the `DataSource` class, simplifying its implementation. - **Bug Fixes** - Removed redundant methods for executing SQL commands, improving code clarity. - **Documentation** - Updated comments and annotations to reflect changes in method visibility and class structure. --- core/dal-runtime/src/DataSource.ts | 7 +++- plugin/dal/lib/DataSource.ts | 53 +++++++----------------------- 2 files changed, 18 insertions(+), 42 deletions(-) diff --git a/core/dal-runtime/src/DataSource.ts b/core/dal-runtime/src/DataSource.ts index 8fcdb851..61412eb1 100644 --- a/core/dal-runtime/src/DataSource.ts +++ b/core/dal-runtime/src/DataSource.ts @@ -23,7 +23,12 @@ export class DataSource implements IDataSource { this.sqlMap = sqlMap; } - private generateSql(sqlName: string, data: object): ExecuteSql { + /** + * public for aop execute to implement sql hint append + * @param sqlName + * @param data + */ + generateSql(sqlName: string, data: object): ExecuteSql { const sql = this.sqlMap.generate(sqlName, data, this.mysqlDataSource.timezone!); const sqlType = this.sqlMap.getType(sqlName); const template = this.sqlMap.getTemplateString(sqlName); diff --git a/plugin/dal/lib/DataSource.ts b/plugin/dal/lib/DataSource.ts index ba8eeeea..d79fdb25 100644 --- a/plugin/dal/lib/DataSource.ts +++ b/plugin/dal/lib/DataSource.ts @@ -1,7 +1,7 @@ import assert from 'node:assert'; import { - AccessLevel, Inject, - LifecycleInit, + AccessLevel, Inject, LoadUnitNameQualifierAttribute, + MultiInstanceInfo, MultiInstanceProto, MultiInstancePrototypeGetObjectsContext, ObjectInfo, @@ -9,16 +9,12 @@ import { } from '@eggjs/tegg'; import { EggLoadUnitType, - EggObject, - EggObjectLifeCycleContext, LoaderFactory, ModuleConfigUtil, } from '@eggjs/tegg/helper'; import { - DataSource as IDataSource, DataSourceInjectName, DataSourceQualifierAttribute, - PaginateData, TableInfoUtil, TableModel, } from '@eggjs/tegg/dal'; @@ -60,20 +56,17 @@ import { TransactionalAOP } from './TransactionalAOP'; return result; }, }) -export class DataSourceDelegate implements IDataSource { - private dataSource: DataSource; - - // register aop here let module dependent teggDal - @Inject({ - name: 'transactionalAOP', - }) +export class DataSourceDelegate extends DataSource { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore private transactionalAOP: TransactionalAOP; + objInfo: ObjectInfo; - @LifecycleInit() - async init(_: EggObjectLifeCycleContext, obj: EggObject) { - const dataSourceQualifierValue = obj.proto.getQualifier(DataSourceQualifierAttribute); + constructor( + @Inject({ name: 'transactionalAOP' }) transactionalAOP: TransactionalAOP, + @MultiInstanceInfo([ DataSourceQualifierAttribute, LoadUnitNameQualifierAttribute ]) objInfo: ObjectInfo) { + const dataSourceQualifierValue = objInfo.qualifiers.find(t => t.attribute === DataSourceQualifierAttribute)?.value; + assert(dataSourceQualifierValue); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [ moduleName, dataSource, clazzName ] = (dataSourceQualifierValue as string).split('.'); const tableModel = TableModelManager.instance.get(moduleName, clazzName); @@ -83,30 +76,8 @@ export class DataSourceDelegate implements IDataSource { const sqlMap = SqlMapManager.instance.get(moduleName, clazzName); assert(sqlMap, `not found SqlMap ${clazzName} in module ${moduleName}`); - this.dataSource = new DataSource(tableModel as TableModel, mysqlDataSource, sqlMap); - } - - async execute(sqlName: string, data?: any): Promise { - return this.dataSource.execute(sqlName, data); - } - - async executeScalar(sqlName: string, data?: any): Promise { - return this.dataSource.executeScalar(sqlName, data); - } - - async executeRaw(sqlName: string, data?: any): Promise { - return this.dataSource.executeRaw(sqlName, data); - } - - async executeRawScalar(sqlName: string, data?: any): Promise { - return this.dataSource.executeRawScalar(sqlName, data); - } - - async paginate(sqlName: string, data: any, currentPage: number, perPageCount: number): Promise> { - return this.dataSource.paginate(sqlName, data, currentPage, perPageCount); - } - - async count(sqlName: string, data?: any): Promise { - return this.dataSource.count(sqlName, data); + super(tableModel as TableModel, mysqlDataSource, sqlMap); + this.transactionalAOP = transactionalAOP; + this.objInfo = objInfo; } }