Skip to content

Commit

Permalink
feat: impl dal for standalone tegg (#197)
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 logging system across various components for consistency and
improved performance.
- **New Features**
- Enhanced Data Access Layer (DAL) functionalities for improved data
handling.
	- Added classes for lifecycle management and custom SQL extensions.
- **Documentation**
- Restructured and refined documentation for better clarity and
readability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Mar 22, 2024
1 parent 0c382d6 commit 56b259d
Show file tree
Hide file tree
Showing 22 changed files with 1,522 additions and 23 deletions.
6 changes: 3 additions & 3 deletions core/dal-runtime/src/BaseSqlMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { ColumnType, IndexType, SqlType, SqlMap, TableModel } from '@eggjs/dal-decorator';
import { type EggLogger } from 'egg';
import { Logger } from '@eggjs/tegg';
import { TemplateUtil } from './TemplateUtil';

export interface GenerateSqlMap {
Expand All @@ -11,9 +11,9 @@ export interface GenerateSqlMap {

export class BaseSqlMapGenerator {
private readonly tableModel: TableModel;
private readonly logger: EggLogger;
private readonly logger: Logger;

constructor(tableModel: TableModel, logger: EggLogger) {
constructor(tableModel: TableModel, logger: Logger) {
this.tableModel = tableModel;
this.logger = logger;
}
Expand Down
6 changes: 3 additions & 3 deletions core/dal-runtime/src/SqlMapLoader.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import path from 'node:path';
import { TableModel, SqlMap } from '@eggjs/dal-decorator';
import { type EggLogger } from 'egg';
import { Logger } from '@eggjs/tegg';
import { BaseSqlMapGenerator } from './BaseSqlMap';
import { TableSqlMap } from './TableSqlMap';

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

constructor(tableModel: TableModel, moduleDir: string, logger: EggLogger) {
constructor(tableModel: TableModel, moduleDir: string, logger: Logger) {
this.sqlMapPath = path.join(moduleDir, 'dal/extension', `${tableModel.clazz.name}Extension.ts`);
this.logger = logger;
this.tableModel = tableModel;
Expand Down
1 change: 0 additions & 1 deletion core/dal-runtime/test/DataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('test/Datasource.test.ts', () => {

const sqlGenerator = new SqlGenerator();
const createTableSql = sqlGenerator.generate(tableModel);
console.log('create table: ', createTableSql);

await mysql.query(createTableSql);

Expand Down
8 changes: 8 additions & 0 deletions core/tegg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ export * as aop from '@eggjs/aop-decorator';
export * as orm from '@eggjs/tegg-orm-decorator';
export * as schedule from '@eggjs/tegg-schedule-decorator';
export { RuntimeConfig, ModuleConfigs, ModuleConfigHolder } from '@eggjs/tegg-common-util';

export interface Logger {
debug(message?: any, ...optionalParams: any[]): void;
log(message?: any, ...optionalParams: any[]): void;
info(message?: any, ...optionalParams: any[]): void;
warn(message?: any, ...optionalParams: any[]): void;
error(message?: any, ...optionalParams: any[]): void;
}
5 changes: 1 addition & 4 deletions plugin/controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ npm i --save @eggjs/tegg-controller-plugin
```json
// tsconfig.json
{
"compilerOptions": {
// 注解特性需要显示打开
"experimentalDecorators": true
}
"extends": "@eggjs/tsconfig"
}
```

Expand Down
30 changes: 23 additions & 7 deletions plugin/dal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

@eggjs/tegg-dal-plugin 支持使用注解的方式来开发 egg 中的 dal。

## Install
## egg 模式

### Install
```shell
# tegg 注解
npm i --save @eggjs/tegg
Expand All @@ -13,18 +14,15 @@ npm i --save @eggjs/tegg-plugin
npm i --save @eggjs/tegg-dal-plugin
```

## Prepare
### Prepare
```json
// tsconfig.json
{
"compilerOptions": {
// 注解特性需要显示打开
"experimentalDecorators": true
}
"extends": "@eggjs/tsconfig"
}
```

## Config
### Config

```js
// config/plugin.js
Expand All @@ -39,6 +37,24 @@ exports.teggDal = {
};
```

## standalone 模式

### Install
```shell
# tegg 注解
npm i --save @eggjs/tegg
# tegg dal 插件
npm i --save @eggjs/tegg-dal-plugin
```

### Prepare
```json
// tsconfig.json
{
"extends": "@eggjs/tsconfig"
}
```

## Usage

### module.yml
Expand Down
2 changes: 2 additions & 0 deletions plugin/dal/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference path='./typings/index.d.ts'/>

import { Application } from 'egg';
import { DalTableEggPrototypeHook } from './lib/DalTableEggPrototypeHook';
import { MysqlDataSourceManager } from './lib/MysqlDataSourceManager';
Expand Down
6 changes: 3 additions & 3 deletions plugin/dal/lib/DalTableEggPrototypeHook.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { LifecycleHook } from '@eggjs/tegg-lifecycle';
import { EggPrototype, EggPrototypeLifecycleContext } from '@eggjs/tegg-metadata';
import { TableInfoUtil, TableModel } from '@eggjs/dal-decorator';
import { EggLogger } from 'egg';
import { Logger } from '@eggjs/tegg';
import { SqlMapLoader } from '@eggjs/dal-runtime';
import { TableModelManager } from './TableModelManager';
import { SqlMapManager } from './SqlMapManager';

export class DalTableEggPrototypeHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
private readonly logger: EggLogger;
private readonly logger: Logger;

constructor(logger: EggLogger) {
constructor(logger: Logger) {
this.logger = logger;
}

Expand Down
1 change: 1 addition & 0 deletions plugin/dal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@eggjs/tegg": "^3.33.1"
},
"devDependencies": {
"@eggjs/tegg-config": "^3.33.1",
"@eggjs/tegg-plugin": "^3.33.1",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.4",
Expand Down
3 changes: 2 additions & 1 deletion standalone/standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"@eggjs/tegg-lifecycle": "^3.33.1",
"@eggjs/tegg-loader": "^3.33.1",
"@eggjs/tegg-metadata": "^3.33.1",
"@eggjs/tegg-runtime": "^3.33.1"
"@eggjs/tegg-runtime": "^3.33.1",
"@eggjs/tegg-dal-plugin": "^3.33.1"
},
"publishConfig": {
"access": "public"
Expand Down
33 changes: 32 additions & 1 deletion standalone/standalone/src/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import {
LoadUnitInstanceFactory,
ModuleLoadUnitInstance,
} from '@eggjs/tegg-runtime';
import { EggProtoImplClass, PrototypeUtil, ModuleConfigHolder, ModuleConfigs, ConfigSourceQualifierAttribute } from '@eggjs/tegg';
import {
EggProtoImplClass,
PrototypeUtil,
ModuleConfigHolder,
ModuleConfigs,
ConfigSourceQualifierAttribute,
Logger,
} from '@eggjs/tegg';
import { StandaloneUtil, MainRunner } from '@eggjs/tegg/standalone';
import { CrosscutAdviceFactory } from '@eggjs/tegg/aop';
import { EggObjectAopHook, EggPrototypeCrossCutHook, LoadUnitAopHook } from '@eggjs/tegg-aop-runtime';
Expand All @@ -23,6 +30,11 @@ import { StandaloneContext } from './StandaloneContext';
import { StandaloneContextHandler } from './StandaloneContextHandler';
import { ConfigSourceLoadUnitHook } from './ConfigSourceLoadUnitHook';
import { LoadUnitInnerClassHook } from './LoadUnitInnerClassHook';
import { DalTableEggPrototypeHook } from '@eggjs/tegg-dal-plugin/lib/DalTableEggPrototypeHook';
import { DalModuleLoadUnitHook } from '@eggjs/tegg-dal-plugin/lib/DalModuleLoadUnitHook';
import { MysqlDataSourceManager } from '@eggjs/tegg-dal-plugin/lib/MysqlDataSourceManager';
import { SqlMapManager } from '@eggjs/tegg-dal-plugin/lib/SqlMapManager';
import { TableModelManager } from '@eggjs/tegg-dal-plugin/lib/TableModelManager';

export interface ModuleDependency extends ReadModuleReferenceOptions {
baseDir: string;
Expand Down Expand Up @@ -50,6 +62,8 @@ export class Runner {
private runnerProto: EggPrototype;
private configSourceEggPrototypeHook: ConfigSourceLoadUnitHook;
private loadUnitMultiInstanceProtoHook: LoadUnitMultiInstanceProtoHook;
private dalTableEggPrototypeHook: DalTableEggPrototypeHook;
private dalModuleLoadUnitHook: DalModuleLoadUnitHook;

private readonly loadUnitInnerClassHook: LoadUnitInnerClassHook;
private readonly crosscutAdviceFactory: CrosscutAdviceFactory;
Expand Down Expand Up @@ -139,6 +153,13 @@ export class Runner {

this.loadUnitMultiInstanceProtoHook = new LoadUnitMultiInstanceProtoHook();
LoadUnitLifecycleUtil.registerLifecycle(this.loadUnitMultiInstanceProtoHook);

this.dalModuleLoadUnitHook = new DalModuleLoadUnitHook(this.moduleConfigs);
const loggerInnerObject = this.innerObjects.logger && this.innerObjects.logger[0];
const logger = loggerInnerObject?.obj || console;
this.dalTableEggPrototypeHook = new DalTableEggPrototypeHook(logger as Logger);
EggPrototypeLifecycleUtil.registerLifecycle(this.dalTableEggPrototypeHook);
LoadUnitLifecycleUtil.registerLifecycle(this.dalModuleLoadUnitHook);
}

async load() {
Expand Down Expand Up @@ -229,5 +250,15 @@ export class Runner {
if (this.loadUnitMultiInstanceProtoHook) {
LoadUnitLifecycleUtil.deleteLifecycle(this.loadUnitMultiInstanceProtoHook);
}

if (this.dalTableEggPrototypeHook) {
await EggPrototypeLifecycleUtil.deleteLifecycle(this.dalTableEggPrototypeHook);
}
if (this.dalModuleLoadUnitHook) {
await LoadUnitLifecycleUtil.deleteLifecycle(this.dalModuleLoadUnitHook);
}
MysqlDataSourceManager.instance.clear();
SqlMapManager.instance.clear();
TableModelManager.instance.clear();
}
}
Loading

0 comments on commit 56b259d

Please sign in to comment.