Skip to content

Commit cecef78

Browse files
authored
feat: dal-runtime templates support pkg alias (#198)
<!-- 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** - Updated import paths for various types across multiple files to ensure consistency and improve maintainability. - Introduced dynamic package references in template files for more flexible code generation. - **Tests** - Adjusted tests to align with the new import paths and dynamic package references. - **Chores** - Enhanced the `CodeGenerator` functionality to support optional properties for better customization. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 5ec94ef commit cecef78

File tree

10 files changed

+25
-16
lines changed

10 files changed

+25
-16
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
1+
export { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds';

core/dal-runtime/src/CodeGenerator.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { SqlGenerator } from './SqlGenerator';
1111
export interface CodeGeneratorOptions {
1212
moduleDir: string;
1313
moduleName: string;
14+
teggPkg?: string;
15+
dalPkg?: string;
1416
}
1517

1618
export enum Templates {
@@ -22,10 +24,14 @@ export enum Templates {
2224
export class CodeGenerator {
2325
private readonly moduleDir: string;
2426
private readonly moduleName: string;
27+
private readonly teggPkg: string;
28+
private readonly dalPkg: string;
2529

2630
constructor(options: CodeGeneratorOptions) {
2731
this.moduleDir = options.moduleDir;
2832
this.moduleName = options.moduleName;
33+
this.teggPkg = options.teggPkg ?? '@eggjs/tegg';
34+
this.dalPkg = options.dalPkg ?? '@eggjs/tegg/dal';
2935
this.createNunjucksEnv();
3036
}
3137

@@ -49,6 +55,8 @@ export class CodeGenerator {
4955
fileName: path.basename(filePath),
5056
clazzName: tableModel.clazz.name,
5157
moduleName: this.moduleName,
58+
teggPkg: this.teggPkg,
59+
dalPkg: this.dalPkg,
5260
id: tableModel.columns.find(t => t.propertyName === 'id'),
5361
primaryIndex: tableModel.getPrimary(),
5462
tableModelPath: TemplateUtil.importPath(tableModelAbsolutePath, path.dirname(filePath)),

core/dal-runtime/src/templates/base_dao.njk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ Optional<{{clazzName}},
6868
>
6969
{% endmacro %}
7070

71-
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
72-
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
73-
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
71+
import type { InsertResult, UpdateResult, DeleteResult } from '{{dalPkg}}';
72+
import { SingletonProto, AccessLevel, Inject } from '{{teggPkg}}';
73+
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '{{dalPkg}}';
7474
import { {{ clazzName }} } from '{{ tableModelPath }}';
7575
// empty-line
7676
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
7777
/**
7878
* 自动生成的 {{ clazzName }}DAO 基类
7979
* @class Base{{ clazzName }}DAO
80-
* @classdesc 该文件由 @eggjs/tegg 自动生成,请**不要**修改它!
80+
* @classdesc 该文件由 {{teggPkg}} 自动生成,请**不要**修改它!
8181
*/
8282
/* istanbul ignore next */
8383
@SingletonProto({

core/dal-runtime/src/templates/dao.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SingletonProto, AccessLevel } from '@eggjs/tegg';
1+
import { SingletonProto, AccessLevel } from '{{teggPkg}}';
22
import { Base{{ clazzName }}DAO } from './base/Base{{ clazzName }}DAO';
33
// empty-line
44
/**

core/dal-runtime/src/templates/extension.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { SqlMap } from '@eggjs/tegg/dal';
1+
import { SqlMap } from '{{dalPkg}}';
22
// empty-line
33
/**
44
* Define Custom SQLs
55
*
6-
* import { SqlMap, SqlType } from '@eggjs/tegg/dal';
6+
* import { SqlMap, SqlType } from '{{dalPkg}}';
77
*
88
* export default {
99
* findByName: {

core/dal-runtime/test/CodeGenerator.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('test/CodeGenerator.test.ts', () => {
1010
const generator = new CodeGenerator({
1111
moduleDir: path.join(__dirname, './fixtures/modules/generate_codes'),
1212
moduleName: 'dal',
13+
dalPkg: '@eggjs/dal-decorator',
1314
});
1415
const fooModel = TableModel.build(Foo);
1516
await generator.generate(fooModel);

core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseFooDAO.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
1+
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/dal-decorator';
22
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
3-
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
3+
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/dal-decorator';
44
import { Foo } from '../../../Foo';
55

66
type Optional<T, K extends keyof T> = Omit < T, K > & Partial<T> ;

core/dal-runtime/test/fixtures/modules/generate_codes/dal/dao/base/BaseMultiPrimaryKeyDAO.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
1+
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/dal-decorator';
22
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
3-
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
3+
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/dal-decorator';
44
import { MultiPrimaryKey } from '../../../MultiPrimaryKey';
55

66
type Optional<T, K extends keyof T> = Omit < T, K > & Partial<T> ;

core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/FooExtension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { SqlMap } from '@eggjs/tegg/dal';
1+
import { SqlMap } from '@eggjs/dal-decorator';
22

33
/**
44
* Define Custom SQLs
55
*
6-
* import { SqlMap, SqlType } from '@eggjs/tegg/dal';
6+
* import { SqlMap, SqlType } from '@eggjs/dal-decorator';
77
*
88
* export default {
99
* findByName: {

core/dal-runtime/test/fixtures/modules/generate_codes/dal/extension/MultiPrimaryKeyExtension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { SqlMap } from '@eggjs/tegg/dal';
1+
import { SqlMap } from '@eggjs/dal-decorator';
22

33
/**
44
* Define Custom SQLs
55
*
6-
* import { SqlMap, SqlType } from '@eggjs/tegg/dal';
6+
* import { SqlMap, SqlType } from '@eggjs/dal-decorator';
77
*
88
* export default {
99
* findByName: {

0 commit comments

Comments
 (0)