Skip to content

Commit

Permalink
chore: make generateSql async (#248)
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**
- Enhanced SQL generation process with asynchronous handling for
improved performance and reliability.
- **Bug Fixes**
- Ensured that all SQL generation operations complete before executing
dependent database queries, preventing potential timing issues.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Oct 10, 2024
1 parent 34730f2 commit 89ac89b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/dal-runtime/src/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class DataSource<T> implements IDataSource<T> {
* @param sqlName

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

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-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-macos (20)

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

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-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-macos (20)

Missing JSDoc @param "data" description
*/
generateSql(sqlName: string, data: object): ExecuteSql {
async generateSql(sqlName: string, data: object): Promise<ExecuteSql> {
const sql = this.sqlMap.generate(sqlName, data, this.mysqlDataSource.timezone!);
const sqlType = this.sqlMap.getType(sqlName);
const template = this.sqlMap.getTemplateString(sqlName);
Expand All @@ -41,20 +41,20 @@ export class DataSource<T> implements IDataSource<T> {

async count(sqlName: string, data?: any): Promise<number> {
const newData = Object.assign({ $$count: true }, data);
const executeSql = this.generateSql(sqlName, newData);
const executeSql = await this.generateSql(sqlName, newData);
return await this.#paginateCount(executeSql.sql);
}

async execute(sqlName: string, data?: any): Promise<Array<T>> {
const executeSql = this.generateSql(sqlName, data);
const executeSql = await this.generateSql(sqlName, data);
const rows = await this.mysqlDataSource.query(executeSql.sql);
return rows.map(t => {
return TableModelInstanceBuilder.buildInstance(this.tableModel, t);
});
}

async executeRaw(sqlName: string, data?: any): Promise<Array<any>> {
const executeSql = this.generateSql(sqlName, data);
const executeSql = await this.generateSql(sqlName, data);
return await this.mysqlDataSource.query(executeSql.sql);
}

Expand All @@ -72,8 +72,8 @@ export class DataSource<T> implements IDataSource<T> {

async paginate(sqlName: string, data: any, currentPage: number, perPageCount: number): Promise<PaginateData<T>> {
const limit = `LIMIT ${(currentPage - 1) * perPageCount}, ${perPageCount}`;
const sql = this.generateSql(sqlName, data).sql + ' ' + limit;
const countSql = this.generateSql(sqlName, Object.assign({ $$count: true }, data)).sql;
const sql = (await this.generateSql(sqlName, data)).sql + ' ' + limit;
const countSql = (await this.generateSql(sqlName, Object.assign({ $$count: true }, data))).sql;


const ret = await Promise.all([
Expand Down

0 comments on commit 89ac89b

Please sign in to comment.