Skip to content

Commit

Permalink
chore: refactor DataSource with construct inject (#244)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to
[x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Oct 9, 2024
1 parent ccff567 commit 7cffb89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
7 changes: 6 additions & 1 deletion core/dal-runtime/src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export class DataSource<T> implements IDataSource<T> {
this.sqlMap = sqlMap;
}

private generateSql(sqlName: string, data: object): ExecuteSql {
/**
* public for aop execute to implement sql hint append
* @param sqlName

Check warning on line 28 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (16)

Missing JSDoc @param "sqlName" description

Check warning on line 28 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (16)

Missing JSDoc @param "sqlName" description

Check warning on line 28 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (18)

Missing JSDoc @param "sqlName" description

Check warning on line 28 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (18)

Missing JSDoc @param "sqlName" description

Check warning on line 28 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (20)

Missing JSDoc @param "sqlName" description

Check warning on line 28 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (20)

Missing JSDoc @param "sqlName" description

Check warning on line 28 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (22)

Missing JSDoc @param "sqlName" description
* @param data

Check warning on line 29 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (16)

Missing JSDoc @param "data" description

Check warning on line 29 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (16)

Missing JSDoc @param "data" description

Check warning on line 29 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (18)

Missing JSDoc @param "data" description

Check warning on line 29 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (18)

Missing JSDoc @param "data" description

Check warning on line 29 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (20)

Missing JSDoc @param "data" description

Check warning on line 29 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (20)

Missing JSDoc @param "data" description

Check warning on line 29 in core/dal-runtime/src/DataSource.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (22)

Missing JSDoc @param "data" description
*/
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);
Expand Down
53 changes: 12 additions & 41 deletions plugin/dal/lib/DataSource.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import assert from 'node:assert';
import {
AccessLevel, Inject,
LifecycleInit,
AccessLevel, Inject, LoadUnitNameQualifierAttribute,
MultiInstanceInfo,
MultiInstanceProto,
MultiInstancePrototypeGetObjectsContext,
ObjectInfo,
ObjectInitType,
} 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';
Expand Down Expand Up @@ -60,20 +56,17 @@ import { TransactionalAOP } from './TransactionalAOP';
return result;
},
})
export class DataSourceDelegate<T> implements IDataSource<T> {
private dataSource: DataSource<T>;

// register aop here let module dependent teggDal
@Inject({
name: 'transactionalAOP',
})
export class DataSourceDelegate<T> extends DataSource<T> {
// 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);
Expand All @@ -83,30 +76,8 @@ export class DataSourceDelegate<T> implements IDataSource<T> {
const sqlMap = SqlMapManager.instance.get(moduleName, clazzName);
assert(sqlMap, `not found SqlMap ${clazzName} in module ${moduleName}`);

this.dataSource = new DataSource<T>(tableModel as TableModel<T>, mysqlDataSource, sqlMap);
}

async execute(sqlName: string, data?: any): Promise<T[]> {
return this.dataSource.execute(sqlName, data);
}

async executeScalar(sqlName: string, data?: any): Promise<T | null> {
return this.dataSource.executeScalar(sqlName, data);
}

async executeRaw(sqlName: string, data?: any): Promise<any[]> {
return this.dataSource.executeRaw(sqlName, data);
}

async executeRawScalar(sqlName: string, data?: any): Promise<any> {
return this.dataSource.executeRawScalar(sqlName, data);
}

async paginate(sqlName: string, data: any, currentPage: number, perPageCount: number): Promise<PaginateData<T>> {
return this.dataSource.paginate(sqlName, data, currentPage, perPageCount);
}

async count(sqlName: string, data?: any): Promise<number> {
return this.dataSource.count(sqlName, data);
super(tableModel as TableModel<T>, mysqlDataSource, sqlMap);
this.transactionalAOP = transactionalAOP;
this.objInfo = objInfo;
}
}

0 comments on commit 7cffb89

Please sign in to comment.