Skip to content

Commit

Permalink
fix: fix dao extension in prod (#206)
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


- **Refactor**
- Improved robustness in module constructor handling to dynamically
determine and filter data access objects based on file extensions.
- Modified path construction logic for loading SQL maps to include the
determined file extension.
- Introduced constant for file extension and updated `LoaderUtil` class
to include a static property for holding this value.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Apr 8, 2024
1 parent de920c3 commit 0498e9d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/dal-runtime/src/SqlMapLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { TableModel, SqlMap } from '@eggjs/dal-decorator';
import { Logger } from '@eggjs/tegg';
import { BaseSqlMapGenerator } from './BaseSqlMap';
import { TableSqlMap } from './TableSqlMap';
import { LoaderUtil } from '@eggjs/tegg/helper';

export class SqlMapLoader {
private readonly logger: Logger;
private readonly tableModel: TableModel;
private readonly sqlMapPath: string;

constructor(tableModel: TableModel, moduleDir: string, logger: Logger) {
this.sqlMapPath = path.join(moduleDir, 'dal/extension', `${tableModel.clazz.name}Extension.ts`);
this.sqlMapPath = path.join(moduleDir, 'dal/extension', `${tableModel.clazz.name}Extension${LoaderUtil.extension}`);
this.logger = logger;
this.tableModel = tableModel;
}
Expand Down
4 changes: 4 additions & 0 deletions core/loader/src/LoaderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const Module = module.constructor.length > 1
/* istanbul ignore next */
: BuiltinModule;

const EXTENSION = Object.keys((Module as any)._extensions).find(t => t === '.ts') ? '.ts' : '.js';

interface LoaderUtilConfig {
extraFilePattern?: string[];
}
Expand All @@ -18,6 +20,8 @@ export class LoaderUtil {
this.config = config;
}

static extension = EXTENSION;

static filePattern(): string[] {
const extensions = Object.keys((Module as any)._extensions);
const extensionPattern = extensions.map(t => t.substring(1))
Expand Down
4 changes: 2 additions & 2 deletions plugin/dal/lib/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { EggObject, EggObjectLifeCycleContext } from '@eggjs/tegg-runtime';
import { TableModelManager } from './TableModelManager';
import { MysqlDataSourceManager } from './MysqlDataSourceManager';
import { SqlMapManager } from './SqlMapManager';
import { LoaderUtil } from '@eggjs/tegg/helper';

@MultiInstanceProto({
accessLevel: AccessLevel.PUBLIC,
Expand All @@ -36,8 +37,7 @@ import { SqlMapManager } from './SqlMapManager';
} catch {
return [];
}

const daos = dirents.filter(t => t.endsWith('DAO.ts'));
const daos = dirents.filter(t => t.endsWith(`DAO${LoaderUtil.extension}`));
// eslint-disable-next-line @typescript-eslint/no-var-requires
const daoClazzList = daos.map(t => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down

0 comments on commit 0498e9d

Please sign in to comment.