Skip to content

Commit

Permalink
feat: dal-runtime templates support pkg alias (#198)
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**
- 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 -->
  • Loading branch information
killagu authored Mar 26, 2024
1 parent 5ec94ef commit cecef78
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion core/dal-decorator/src/type/MySql.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
export { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds';
8 changes: 8 additions & 0 deletions core/dal-runtime/src/CodeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { SqlGenerator } from './SqlGenerator';
export interface CodeGeneratorOptions {
moduleDir: string;
moduleName: string;
teggPkg?: string;
dalPkg?: string;
}

export enum Templates {
Expand All @@ -22,10 +24,14 @@ export enum Templates {
export class CodeGenerator {
private readonly moduleDir: string;
private readonly moduleName: string;
private readonly teggPkg: string;
private readonly dalPkg: string;

constructor(options: CodeGeneratorOptions) {
this.moduleDir = options.moduleDir;
this.moduleName = options.moduleName;
this.teggPkg = options.teggPkg ?? '@eggjs/tegg';
this.dalPkg = options.dalPkg ?? '@eggjs/tegg/dal';
this.createNunjucksEnv();
}

Expand All @@ -49,6 +55,8 @@ export class CodeGenerator {
fileName: path.basename(filePath),
clazzName: tableModel.clazz.name,
moduleName: this.moduleName,
teggPkg: this.teggPkg,
dalPkg: this.dalPkg,
id: tableModel.columns.find(t => t.propertyName === 'id'),
primaryIndex: tableModel.getPrimary(),
tableModelPath: TemplateUtil.importPath(tableModelAbsolutePath, path.dirname(filePath)),
Expand Down
8 changes: 4 additions & 4 deletions core/dal-runtime/src/templates/base_dao.njk
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ Optional<{{clazzName}},
>
{% endmacro %}

import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
import type { InsertResult, UpdateResult, DeleteResult } from '{{dalPkg}}';
import { SingletonProto, AccessLevel, Inject } from '{{teggPkg}}';
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '{{dalPkg}}';
import { {{ clazzName }} } from '{{ tableModelPath }}';
// empty-line
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
/**
* 自动生成的 {{ clazzName }}DAO 基类
* @class Base{{ clazzName }}DAO
* @classdesc 该文件由 @eggjs/tegg 自动生成,请**不要**修改它!
* @classdesc 该文件由 {{teggPkg}} 自动生成,请**不要**修改它!
*/
/* istanbul ignore next */
@SingletonProto({
Expand Down
2 changes: 1 addition & 1 deletion core/dal-runtime/src/templates/dao.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SingletonProto, AccessLevel } from '@eggjs/tegg';
import { SingletonProto, AccessLevel } from '{{teggPkg}}';
import { Base{{ clazzName }}DAO } from './base/Base{{ clazzName }}DAO';
// empty-line
/**
Expand Down
4 changes: 2 additions & 2 deletions core/dal-runtime/src/templates/extension.njk
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SqlMap } from '@eggjs/tegg/dal';
import { SqlMap } from '{{dalPkg}}';
// empty-line
/**
* Define Custom SQLs
*
* import { SqlMap, SqlType } from '@eggjs/tegg/dal';
* import { SqlMap, SqlType } from '{{dalPkg}}';
*
* export default {
* findByName: {
Expand Down
1 change: 1 addition & 0 deletions core/dal-runtime/test/CodeGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('test/CodeGenerator.test.ts', () => {
const generator = new CodeGenerator({
moduleDir: path.join(__dirname, './fixtures/modules/generate_codes'),
moduleName: 'dal',
dalPkg: '@eggjs/dal-decorator',
});
const fooModel = TableModel.build(Foo);
await generator.generate(fooModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/dal-decorator';
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/dal-decorator';
import { Foo } from '../../../Foo';

type Optional<T, K extends keyof T> = Omit < T, K > & Partial<T> ;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/rds/lib/types';
import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs/dal-decorator';
import { SingletonProto, AccessLevel, Inject } from '@eggjs/tegg';
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/tegg/dal';
import { DataSource, DataSourceInjectName, DataSourceQualifier, ColumnTsType } from '@eggjs/dal-decorator';
import { MultiPrimaryKey } from '../../../MultiPrimaryKey';

type Optional<T, K extends keyof T> = Omit < T, K > & Partial<T> ;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SqlMap } from '@eggjs/tegg/dal';
import { SqlMap } from '@eggjs/dal-decorator';

/**
* Define Custom SQLs
*
* import { SqlMap, SqlType } from '@eggjs/tegg/dal';
* import { SqlMap, SqlType } from '@eggjs/dal-decorator';
*
* export default {
* findByName: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SqlMap } from '@eggjs/tegg/dal';
import { SqlMap } from '@eggjs/dal-decorator';

/**
* Define Custom SQLs
*
* import { SqlMap, SqlType } from '@eggjs/tegg/dal';
* import { SqlMap, SqlType } from '@eggjs/dal-decorator';
*
* export default {
* findByName: {
Expand Down

0 comments on commit cecef78

Please sign in to comment.