-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mount clazzExtension/clazzExtension/tableSql to BaseDao (#220)
<!-- 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** - Introduced new data access utilities and decorators to enhance DAO functionality across the application. - Added new paths for extensions, structure, and SQL files in the code generation process. - Enhanced the SQL map loading process with improved type safety and simplified method implementations. - **Refactor** - Updated various components to use new DAO loading mechanisms, removing outdated dependencies. - Refactored data access layers to improve performance and maintainability. - Changed method signatures and class properties to align with updated data access strategies. - **Bug Fixes** - Fixed issues related to incorrect SQL map paths and table model constructions. - **Documentation** - Updated documentation to reflect new features and changes in data access patterns. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
37 changed files
with
249 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Prototype, PrototypeUtil } from '@eggjs/core-decorator'; | ||
import { StackUtil } from '@eggjs/tegg-common-util'; | ||
import { AccessLevel, ObjectInitType } from '@eggjs/tegg-types'; | ||
import type { EggProtoImplClass } from '@eggjs/tegg-types'; | ||
import { DaoInfoUtil } from '../util/DaoInfoUtil'; | ||
|
||
export function Dao() { | ||
return function(constructor: EggProtoImplClass) { | ||
DaoInfoUtil.setIsDao(constructor); | ||
const func = Prototype({ | ||
accessLevel: AccessLevel.PUBLIC, | ||
initType: ObjectInitType.SINGLETON, | ||
}); | ||
func(constructor); | ||
PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BaseDaoType, DAL_IS_DAO } from '@eggjs/tegg-types'; | ||
import type { EggProtoImplClass } from '@eggjs/tegg-types'; | ||
import { MetadataUtil } from '@eggjs/core-decorator'; | ||
|
||
export class DaoInfoUtil { | ||
static setIsDao(clazz: EggProtoImplClass) { | ||
MetadataUtil.defineMetaData(DAL_IS_DAO, true, clazz); | ||
} | ||
|
||
static getIsDao(clazz: EggProtoImplClass): clazz is BaseDaoType { | ||
return MetadataUtil.getOwnMetaData(DAL_IS_DAO, clazz) === true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { EggLoadUnitType } from '@eggjs/tegg-types'; | ||
import { DaoInfoUtil } from '@eggjs/tegg/dal'; | ||
import { BaseDaoType } from '@eggjs/tegg-types/dal'; | ||
import { LoaderFactory } from '@eggjs/tegg/helper'; | ||
|
||
export class DaoLoader { | ||
static loadDaos(moduleDir: string): Array<BaseDaoType> { | ||
const loader = LoaderFactory.createLoader(moduleDir, EggLoadUnitType.MODULE); | ||
const clazzList = loader.load(); | ||
return clazzList.filter((t): t is BaseDaoType => { | ||
return DaoInfoUtil.getIsDao(t); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,26 @@ | ||
import path from 'node:path'; | ||
import { TableModel } from '@eggjs/tegg/dal'; | ||
import { BaseDaoType, TableModel } from '@eggjs/tegg/dal'; | ||
import type { Logger, SqlMap } from '@eggjs/tegg-types'; | ||
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; | ||
private readonly clazzExtension: Record<string, SqlMap>; | ||
|
||
constructor(tableModel: TableModel, moduleDir: string, logger: Logger) { | ||
this.sqlMapPath = path.join(moduleDir, 'dal/extension', `${tableModel.clazz.name}Extension${LoaderUtil.extension}`); | ||
constructor(tableModel: TableModel, baseDaoClazz: BaseDaoType, logger: Logger) { | ||
this.clazzExtension = baseDaoClazz.clazzExtension; | ||
this.logger = logger; | ||
this.tableModel = tableModel; | ||
} | ||
|
||
load(): TableSqlMap { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { default: customSqlMap }: { default: Record<string, SqlMap> } = require(this.sqlMapPath); | ||
const baseSqlMapGenerator = new BaseSqlMapGenerator(this.tableModel, this.logger); | ||
const baseSqlMap = baseSqlMapGenerator.load(); | ||
const sqlMap = { | ||
...baseSqlMap, | ||
...customSqlMap, | ||
}; | ||
return new TableSqlMap(this.tableModel.clazz.name, sqlMap); | ||
} catch (e) { | ||
e.message = `load sql map ${this.sqlMapPath} failed: ${e.message}`; | ||
throw e; | ||
} | ||
const baseSqlMapGenerator = new BaseSqlMapGenerator(this.tableModel, this.logger); | ||
const baseSqlMap = baseSqlMapGenerator.load(); | ||
const sqlMap = { | ||
...baseSqlMap, | ||
...this.clazzExtension, | ||
}; | ||
return new TableSqlMap(this.tableModel.clazz.name, sqlMap); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.