Skip to content

Commit

Permalink
fix: use loader to load TableClazzList (#219)
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**
- Enhanced the data loading process to improve efficiency and
reliability.
- Optimized the filtering mechanism for table classes to ensure more
accurate data handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Apr 28, 2024
1 parent ac91cdc commit 15ef977
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions plugin/dal/lib/DataSource.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import assert from 'node:assert';
import path from 'node:path';
import fs from 'node:fs';
import {
AccessLevel,
LifecycleInit,
MultiInstanceProto,
MultiInstancePrototypeGetObjectsContext,
ObjectInfo,
ObjectInitType,
LifecycleInit,
} from '@eggjs/tegg';
import { ModuleConfigUtil, LoaderUtil, EggObject, EggObjectLifeCycleContext } from '@eggjs/tegg/helper';
import {
EggLoadUnitType,
EggObject,
EggObjectLifeCycleContext,
LoaderFactory,
ModuleConfigUtil,
} from '@eggjs/tegg/helper';
import {
DataSource as IDataSource,
DataSourceInjectName,
DataSourceQualifierAttribute,
PaginateData,
TableInfoUtil,
DataSource as IDataSource, TableModel, PaginateData,
TableModel,
} from '@eggjs/tegg/dal';
import { DataSource } from '@eggjs/dal-runtime';
import { TableModelManager } from './TableModelManager';
Expand All @@ -28,25 +34,12 @@ import { SqlMapManager } from './SqlMapManager';
const config = ModuleConfigUtil.loadModuleConfigSync(ctx.unitPath) as any | undefined;
const dataSources = Object.keys(config?.dataSource || {});
const result: ObjectInfo[] = [];
const daoDir = path.join(ctx.unitPath, 'dal/dao');
let dirents: string[];
try {
dirents = fs.readdirSync(daoDir);
} catch {
return [];
}
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
return require(path.join(daoDir, t)).default;
});
const tableClazzList = daoClazzList.map(t => {
// eslint-disable-next-line no-proto
return Object.getPrototypeOf(t).clazzModel;
const loader = LoaderFactory.createLoader(ctx.unitPath, EggLoadUnitType.MODULE);
const clazzList = loader.load();
const tableClazzList = clazzList.filter(t => {
return TableInfoUtil.getIsTable(t);
});
const dataSourceLength = dataSources.length;

for (const dataSource of dataSources) {
const moduleClazzList = tableClazzList.filter(clazz => {
const tableParams = TableInfoUtil.getTableParams(clazz);
Expand Down

0 comments on commit 15ef977

Please sign in to comment.