Skip to content

Commit

Permalink
feat: impl dal for standalone tegg
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Mar 22, 2024
1 parent 0c382d6 commit a88237a
Show file tree
Hide file tree
Showing 19 changed files with 1,470 additions and 11 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
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;
}
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 a88237a

Please sign in to comment.