@eggjs/tegg-dal-plugin 4.0.0-beta.4 → 4.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +13 -1
  2. package/dist/_virtual/_@oxc-project_runtime@0.93.0/helpers/decorate.js +10 -0
  3. package/dist/_virtual/_@oxc-project_runtime@0.93.0/helpers/decorateMetadata.js +7 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.93.0/helpers/decorateParam.js +9 -0
  5. package/dist/app.d.ts +14 -0
  6. package/dist/app.js +36 -0
  7. package/dist/index.d.ts +9 -0
  8. package/dist/index.js +11 -0
  9. package/dist/lib/DalModuleLoadUnitHook.d.ts +13 -0
  10. package/dist/lib/DalModuleLoadUnitHook.js +40 -0
  11. package/dist/lib/DalTableEggPrototypeHook.d.ts +11 -0
  12. package/dist/lib/DalTableEggPrototypeHook.js +25 -0
  13. package/dist/lib/DataSource.d.ts +12 -0
  14. package/dist/lib/DataSource.js +68 -0
  15. package/dist/lib/MysqlDataSourceManager.d.ts +15 -0
  16. package/dist/lib/MysqlDataSourceManager.js +58 -0
  17. package/dist/lib/SqlMapManager.d.ts +13 -0
  18. package/dist/lib/SqlMapManager.js +27 -0
  19. package/dist/lib/TableModelManager.d.ts +13 -0
  20. package/dist/lib/TableModelManager.js +27 -0
  21. package/dist/lib/TransactionPrototypeHook.d.ts +12 -0
  22. package/dist/lib/TransactionPrototypeHook.js +50 -0
  23. package/dist/lib/TransactionalAOP.d.ts +15 -0
  24. package/dist/lib/TransactionalAOP.js +22 -0
  25. package/dist/types.d.ts +1 -0
  26. package/dist/types.js +3 -0
  27. package/package.json +48 -38
  28. package/app.d.ts +0 -10
  29. package/app.js +0 -38
  30. package/index.d.ts +0 -10
  31. package/index.js +0 -11
  32. package/lib/DalModuleLoadUnitHook.d.ts +0 -9
  33. package/lib/DalModuleLoadUnitHook.js +0 -41
  34. package/lib/DalTableEggPrototypeHook.d.ts +0 -7
  35. package/lib/DalTableEggPrototypeHook.js +0 -21
  36. package/lib/DataSource.d.ts +0 -8
  37. package/lib/DataSource.js +0 -81
  38. package/lib/MysqlDataSourceManager.d.ts +0 -11
  39. package/lib/MysqlDataSourceManager.js +0 -55
  40. package/lib/SqlMapManager.d.ts +0 -9
  41. package/lib/SqlMapManager.js +0 -22
  42. package/lib/TableModelManager.d.ts +0 -9
  43. package/lib/TableModelManager.js +0 -22
  44. package/lib/TransactionPrototypeHook.d.ts +0 -8
  45. package/lib/TransactionPrototypeHook.js +0 -54
  46. package/lib/TransactionalAOP.d.ts +0 -11
  47. package/lib/TransactionalAOP.js +0 -27
package/README.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @eggjs/tegg-dal-plugin
2
2
 
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![Known Vulnerabilities][snyk-image]][snyk-url]
5
+ [![npm download][download-image]][download-url]
6
+ [![Node.js Version](https://img.shields.io/node/v/@eggjs/tegg-dal-plugin.svg?style=flat)](https://nodejs.org/en/download/)
7
+
8
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/tegg-dal-plugin.svg?style=flat-square
9
+ [npm-url]: https://npmjs.org/package/@eggjs/tegg-dal-plugin
10
+ [snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-dal-plugin/badge.svg?style=flat-square
11
+ [snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-dal-plugin
12
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-dal-plugin.svg?style=flat-square
13
+ [download-url]: https://npmjs.org/package/@eggjs/tegg-dal-plugin
14
+
3
15
  @eggjs/tegg-dal-plugin 支持使用注解的方式来开发 egg 中的 dal。
4
16
 
5
17
  ## egg 模式
@@ -567,7 +579,7 @@ export class FooRepository {
567
579
 
568
580
  ```ts
569
581
  // dal/extension/FooExtension.ts
570
- import { SqlMap, SqlType } from '@eggjs/tegg/dal';
582
+ import { type SqlMap, SqlType } from '@eggjs/tegg/dal';
571
583
 
572
584
  export default {
573
585
  findByName: {
@@ -0,0 +1,10 @@
1
+ //#region \0@oxc-project+runtime@0.93.0/helpers/decorate.js
2
+ function __decorate(decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ }
8
+
9
+ //#endregion
10
+ export { __decorate };
@@ -0,0 +1,7 @@
1
+ //#region \0@oxc-project+runtime@0.93.0/helpers/decorateMetadata.js
2
+ function __decorateMetadata(k, v) {
3
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
4
+ }
5
+
6
+ //#endregion
7
+ export { __decorateMetadata };
@@ -0,0 +1,9 @@
1
+ //#region \0@oxc-project+runtime@0.93.0/helpers/decorateParam.js
2
+ function __decorateParam(paramIndex, decorator) {
3
+ return function(target, key) {
4
+ decorator(target, key, paramIndex);
5
+ };
6
+ }
7
+
8
+ //#endregion
9
+ export { __decorateParam };
package/dist/app.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { Application, ILifecycleBoot } from "egg";
2
+
3
+ //#region src/app.d.ts
4
+ declare class DalAppBootHook implements ILifecycleBoot {
5
+ private readonly app;
6
+ private dalTableEggPrototypeHook;
7
+ private dalModuleLoadUnitHook;
8
+ private transactionPrototypeHook;
9
+ constructor(app: Application);
10
+ configWillLoad(): void;
11
+ beforeClose(): Promise<void>;
12
+ }
13
+ //#endregion
14
+ export { DalAppBootHook as default };
package/dist/app.js ADDED
@@ -0,0 +1,36 @@
1
+ import { TableModelManager } from "./lib/TableModelManager.js";
2
+ import { SqlMapManager } from "./lib/SqlMapManager.js";
3
+ import { DalTableEggPrototypeHook } from "./lib/DalTableEggPrototypeHook.js";
4
+ import { MysqlDataSourceManager } from "./lib/MysqlDataSourceManager.js";
5
+ import { DalModuleLoadUnitHook } from "./lib/DalModuleLoadUnitHook.js";
6
+ import { TransactionPrototypeHook } from "./lib/TransactionPrototypeHook.js";
7
+
8
+ //#region src/app.ts
9
+ var DalAppBootHook = class {
10
+ app;
11
+ dalTableEggPrototypeHook;
12
+ dalModuleLoadUnitHook;
13
+ transactionPrototypeHook;
14
+ constructor(app) {
15
+ this.app = app;
16
+ }
17
+ configWillLoad() {
18
+ this.dalModuleLoadUnitHook = new DalModuleLoadUnitHook(this.app.config.env, this.app.moduleConfigs);
19
+ this.dalTableEggPrototypeHook = new DalTableEggPrototypeHook(this.app.logger);
20
+ this.transactionPrototypeHook = new TransactionPrototypeHook(this.app.moduleConfigs, this.app.logger);
21
+ this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.dalTableEggPrototypeHook);
22
+ this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.transactionPrototypeHook);
23
+ this.app.loadUnitLifecycleUtil.registerLifecycle(this.dalModuleLoadUnitHook);
24
+ }
25
+ async beforeClose() {
26
+ if (this.dalTableEggPrototypeHook) this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.dalTableEggPrototypeHook);
27
+ if (this.dalModuleLoadUnitHook) this.app.loadUnitLifecycleUtil.deleteLifecycle(this.dalModuleLoadUnitHook);
28
+ if (this.transactionPrototypeHook) this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.transactionPrototypeHook);
29
+ MysqlDataSourceManager.instance.clear();
30
+ SqlMapManager.instance.clear();
31
+ TableModelManager.instance.clear();
32
+ }
33
+ };
34
+
35
+ //#endregion
36
+ export { DalAppBootHook as default };
@@ -0,0 +1,9 @@
1
+ import { DalModuleLoadUnitHook } from "./lib/DalModuleLoadUnitHook.js";
2
+ import { DalTableEggPrototypeHook } from "./lib/DalTableEggPrototypeHook.js";
3
+ import { TransactionalAOP, TransactionalParams } from "./lib/TransactionalAOP.js";
4
+ import { DataSourceDelegate } from "./lib/DataSource.js";
5
+ import { MysqlDataSourceManager } from "./lib/MysqlDataSourceManager.js";
6
+ import { SqlMapManager } from "./lib/SqlMapManager.js";
7
+ import { TableModelManager } from "./lib/TableModelManager.js";
8
+ import { TransactionPrototypeHook } from "./lib/TransactionPrototypeHook.js";
9
+ export { DalModuleLoadUnitHook, DalTableEggPrototypeHook, DataSourceDelegate, MysqlDataSourceManager, SqlMapManager, TableModelManager, TransactionPrototypeHook, TransactionalAOP, TransactionalParams };
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import { TableModelManager } from "./lib/TableModelManager.js";
2
+ import { SqlMapManager } from "./lib/SqlMapManager.js";
3
+ import { DalTableEggPrototypeHook } from "./lib/DalTableEggPrototypeHook.js";
4
+ import { MysqlDataSourceManager } from "./lib/MysqlDataSourceManager.js";
5
+ import { DalModuleLoadUnitHook } from "./lib/DalModuleLoadUnitHook.js";
6
+ import { TransactionalAOP } from "./lib/TransactionalAOP.js";
7
+ import { TransactionPrototypeHook } from "./lib/TransactionPrototypeHook.js";
8
+ import "./types.js";
9
+ import { DataSourceDelegate } from "./lib/DataSource.js";
10
+
11
+ export { DalModuleLoadUnitHook, DalTableEggPrototypeHook, DataSourceDelegate, MysqlDataSourceManager, SqlMapManager, TableModelManager, TransactionPrototypeHook, TransactionalAOP };
@@ -0,0 +1,13 @@
1
+ import { LifecycleHook, Logger, ModuleConfigHolder } from "@eggjs/tegg";
2
+ import { LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg/helper";
3
+
4
+ //#region src/lib/DalModuleLoadUnitHook.d.ts
5
+ declare class DalModuleLoadUnitHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
6
+ private readonly moduleConfigs;
7
+ private readonly env;
8
+ private readonly logger?;
9
+ constructor(env: string, moduleConfigs: Record<string, ModuleConfigHolder>, logger?: Logger);
10
+ preCreate(_: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise<void>;
11
+ }
12
+ //#endregion
13
+ export { DalModuleLoadUnitHook };
@@ -0,0 +1,40 @@
1
+ import { MysqlDataSourceManager } from "./MysqlDataSourceManager.js";
2
+ import "@eggjs/tegg";
3
+ import "@eggjs/tegg/helper";
4
+ import { DatabaseForker } from "@eggjs/dal-runtime";
5
+
6
+ //#region src/lib/DalModuleLoadUnitHook.ts
7
+ var DalModuleLoadUnitHook = class {
8
+ moduleConfigs;
9
+ env;
10
+ logger;
11
+ constructor(env, moduleConfigs, logger) {
12
+ this.env = env;
13
+ this.moduleConfigs = moduleConfigs;
14
+ this.logger = logger;
15
+ }
16
+ async preCreate(_, loadUnit) {
17
+ const moduleConfigHolder = this.moduleConfigs[loadUnit.name];
18
+ if (!moduleConfigHolder) return;
19
+ const dataSourceConfig = moduleConfigHolder.config.dataSource;
20
+ if (!dataSourceConfig) return;
21
+ await Promise.all(Object.entries(dataSourceConfig).map(async ([name, config]) => {
22
+ const dataSourceOptions = {
23
+ ...config,
24
+ name,
25
+ logger: this.logger
26
+ };
27
+ const forker = new DatabaseForker(this.env, dataSourceOptions);
28
+ if (forker.shouldFork()) await forker.forkDb(loadUnit.unitPath);
29
+ try {
30
+ await MysqlDataSourceManager.instance.createDataSource(loadUnit.name, name, dataSourceOptions);
31
+ } catch (e) {
32
+ if (e instanceof Error) e.message = `create module ${loadUnit.name} datasource ${name} failed: ${e.message}`;
33
+ throw e;
34
+ }
35
+ }));
36
+ }
37
+ };
38
+
39
+ //#endregion
40
+ export { DalModuleLoadUnitHook };
@@ -0,0 +1,11 @@
1
+ import { LifecycleHook, Logger } from "@eggjs/tegg";
2
+ import { EggPrototype, EggPrototypeLifecycleContext } from "@eggjs/tegg/helper";
3
+
4
+ //#region src/lib/DalTableEggPrototypeHook.d.ts
5
+ declare class DalTableEggPrototypeHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
6
+ private readonly logger;
7
+ constructor(logger: Logger);
8
+ preCreate(ctx: EggPrototypeLifecycleContext): Promise<void>;
9
+ }
10
+ //#endregion
11
+ export { DalTableEggPrototypeHook };
@@ -0,0 +1,25 @@
1
+ import { TableModelManager } from "./TableModelManager.js";
2
+ import { SqlMapManager } from "./SqlMapManager.js";
3
+ import "@eggjs/tegg";
4
+ import "@eggjs/tegg/helper";
5
+ import { DaoInfoUtil, TableModel } from "@eggjs/dal-decorator";
6
+ import { SqlMapLoader } from "@eggjs/dal-runtime";
7
+
8
+ //#region src/lib/DalTableEggPrototypeHook.ts
9
+ var DalTableEggPrototypeHook = class {
10
+ logger;
11
+ constructor(logger) {
12
+ this.logger = logger;
13
+ }
14
+ async preCreate(ctx) {
15
+ if (!DaoInfoUtil.getIsDao(ctx.clazz)) return;
16
+ const tableClazz = ctx.clazz.clazzModel;
17
+ const tableModel = TableModel.build(tableClazz);
18
+ TableModelManager.instance.set(ctx.loadUnit.name, tableModel);
19
+ const sqlMap = new SqlMapLoader(tableModel, ctx.clazz, this.logger).load();
20
+ SqlMapManager.instance.set(ctx.loadUnit.name, sqlMap);
21
+ }
22
+ };
23
+
24
+ //#endregion
25
+ export { DalTableEggPrototypeHook };
@@ -0,0 +1,12 @@
1
+ import { TransactionalAOP } from "./TransactionalAOP.js";
2
+ import { ObjectInfo } from "@eggjs/tegg";
3
+ import { DataSource } from "@eggjs/dal-runtime";
4
+
5
+ //#region src/lib/DataSource.d.ts
6
+ declare class DataSourceDelegate<T> extends DataSource<T> {
7
+ private transactionalAOP;
8
+ objInfo: ObjectInfo;
9
+ constructor(transactionalAOP: TransactionalAOP, objInfo: ObjectInfo);
10
+ }
11
+ //#endregion
12
+ export { DataSourceDelegate };
@@ -0,0 +1,68 @@
1
+ import { TableModelManager } from "./TableModelManager.js";
2
+ import { SqlMapManager } from "./SqlMapManager.js";
3
+ import { MysqlDataSourceManager } from "./MysqlDataSourceManager.js";
4
+ import { __decorate } from "../_virtual/_@oxc-project_runtime@0.93.0/helpers/decorate.js";
5
+ import { TransactionalAOP } from "./TransactionalAOP.js";
6
+ import { __decorateMetadata } from "../_virtual/_@oxc-project_runtime@0.93.0/helpers/decorateMetadata.js";
7
+ import { __decorateParam } from "../_virtual/_@oxc-project_runtime@0.93.0/helpers/decorateParam.js";
8
+ import { AccessLevel, Inject, LoadUnitNameQualifierAttribute, MultiInstanceInfo, MultiInstanceProto, ObjectInitType } from "@eggjs/tegg";
9
+ import { EggLoadUnitType, LoaderFactory, ModuleConfigUtil } from "@eggjs/tegg/helper";
10
+ import { DataSource } from "@eggjs/dal-runtime";
11
+ import assert from "node:assert";
12
+ import { DataSourceInjectName, DataSourceQualifierAttribute, TableInfoUtil } from "@eggjs/tegg/dal";
13
+
14
+ //#region src/lib/DataSource.ts
15
+ var _ref;
16
+ let DataSourceDelegate = class DataSourceDelegate$1 extends DataSource {
17
+ transactionalAOP;
18
+ objInfo;
19
+ constructor(transactionalAOP, objInfo) {
20
+ const dataSourceQualifierValue = objInfo.qualifiers.find((t) => t.attribute === DataSourceQualifierAttribute)?.value;
21
+ assert(dataSourceQualifierValue);
22
+ const [moduleName, dataSource, clazzName] = dataSourceQualifierValue.split(".");
23
+ const tableModel = TableModelManager.instance.get(moduleName, clazzName);
24
+ assert(tableModel, `not found table ${dataSourceQualifierValue}`);
25
+ const mysqlDataSource = MysqlDataSourceManager.instance.get(moduleName, dataSource);
26
+ assert(mysqlDataSource, `not found dataSource ${dataSource} in module ${moduleName}`);
27
+ const sqlMap = SqlMapManager.instance.get(moduleName, clazzName);
28
+ assert(sqlMap, `not found SqlMap ${clazzName} in module ${moduleName}`);
29
+ super(tableModel, mysqlDataSource, sqlMap);
30
+ this.transactionalAOP = transactionalAOP;
31
+ this.objInfo = objInfo;
32
+ }
33
+ };
34
+ DataSourceDelegate = __decorate([
35
+ MultiInstanceProto({
36
+ accessLevel: AccessLevel.PUBLIC,
37
+ initType: ObjectInitType.SINGLETON,
38
+ async getObjects(ctx) {
39
+ const config = ModuleConfigUtil.loadModuleConfigSync(ctx.unitPath);
40
+ const dataSources = Object.keys(config?.dataSource || {});
41
+ const result = [];
42
+ const tableClazzList = (await LoaderFactory.createLoader(ctx.unitPath, EggLoadUnitType.MODULE).load()).filter((t) => {
43
+ return TableInfoUtil.getIsTable(t);
44
+ });
45
+ const dataSourceLength = dataSources.length;
46
+ for (const dataSource of dataSources) {
47
+ const moduleClazzList = tableClazzList.filter((clazz) => {
48
+ const dataSourceName = TableInfoUtil.getTableParams(clazz)?.dataSourceName ?? "default";
49
+ return dataSourceLength === 1 || dataSourceName === dataSource;
50
+ });
51
+ for (const clazz of moduleClazzList) result.push({
52
+ name: DataSourceInjectName,
53
+ qualifiers: [{
54
+ attribute: DataSourceQualifierAttribute,
55
+ value: `${ctx.moduleName}.${dataSource}.${clazz.name}`
56
+ }]
57
+ });
58
+ }
59
+ return result;
60
+ }
61
+ }),
62
+ __decorateParam(0, Inject({ name: "transactionalAOP" })),
63
+ __decorateParam(1, MultiInstanceInfo([DataSourceQualifierAttribute, LoadUnitNameQualifierAttribute])),
64
+ __decorateMetadata("design:paramtypes", [typeof (_ref = typeof TransactionalAOP !== "undefined" && TransactionalAOP) === "function" ? _ref : Object, Object])
65
+ ], DataSourceDelegate);
66
+
67
+ //#endregion
68
+ export { DataSourceDelegate };
@@ -0,0 +1,15 @@
1
+ import { DataSourceOptions, MysqlDataSource } from "@eggjs/dal-runtime";
2
+
3
+ //#region src/lib/MysqlDataSourceManager.d.ts
4
+ declare class MysqlDataSourceManager {
5
+ static instance: MysqlDataSourceManager;
6
+ private readonly dataSourceIndices;
7
+ private readonly dataSources;
8
+ constructor();
9
+ get(moduleName: string, dataSourceName: string): MysqlDataSource | undefined;
10
+ createDataSource(moduleName: string, dataSourceName: string, config: DataSourceOptions): Promise<void>;
11
+ clear(): void;
12
+ static createDataSourceKey(dataSourceOptions: DataSourceOptions): string;
13
+ }
14
+ //#endregion
15
+ export { MysqlDataSourceManager };
@@ -0,0 +1,58 @@
1
+ import { MysqlDataSource } from "@eggjs/dal-runtime";
2
+ import crypto from "node:crypto";
3
+
4
+ //#region src/lib/MysqlDataSourceManager.ts
5
+ var MysqlDataSourceManager = class MysqlDataSourceManager {
6
+ static instance = new MysqlDataSourceManager();
7
+ dataSourceIndices;
8
+ dataSources;
9
+ constructor() {
10
+ this.dataSourceIndices = /* @__PURE__ */ new Map();
11
+ this.dataSources = /* @__PURE__ */ new Map();
12
+ }
13
+ get(moduleName, dataSourceName) {
14
+ const dataSourceIndex = this.dataSourceIndices.get(moduleName)?.get(dataSourceName);
15
+ if (dataSourceIndex) return this.dataSources.get(dataSourceIndex);
16
+ }
17
+ async createDataSource(moduleName, dataSourceName, config) {
18
+ const { logger,...dsConfig } = config || {};
19
+ const dataSourceConfig = {
20
+ ...dsConfig,
21
+ name: dataSourceName
22
+ };
23
+ const index = MysqlDataSourceManager.createDataSourceKey(dataSourceConfig);
24
+ let dataSource = this.dataSources.get(index);
25
+ if (!dataSource) {
26
+ dataSource = new MysqlDataSource({
27
+ ...dataSourceConfig,
28
+ logger
29
+ });
30
+ this.dataSources.set(index, dataSource);
31
+ }
32
+ let moduleDataSourceIndices = this.dataSourceIndices.get(moduleName);
33
+ if (!moduleDataSourceIndices) {
34
+ moduleDataSourceIndices = /* @__PURE__ */ new Map();
35
+ this.dataSourceIndices.set(moduleName, moduleDataSourceIndices);
36
+ }
37
+ moduleDataSourceIndices.set(dataSourceName, index);
38
+ await dataSource.ready();
39
+ }
40
+ clear() {
41
+ this.dataSourceIndices.clear();
42
+ }
43
+ static createDataSourceKey(dataSourceOptions) {
44
+ const hash = crypto.createHash("md5");
45
+ const keys = Object.keys(dataSourceOptions).sort();
46
+ for (const key of keys) {
47
+ const value = dataSourceOptions[key];
48
+ if (value) {
49
+ hash.update(key);
50
+ hash.update(String(value));
51
+ }
52
+ }
53
+ return hash.digest("hex");
54
+ }
55
+ };
56
+
57
+ //#endregion
58
+ export { MysqlDataSourceManager };
@@ -0,0 +1,13 @@
1
+ import { TableSqlMap } from "@eggjs/dal-runtime";
2
+
3
+ //#region src/lib/SqlMapManager.d.ts
4
+ declare class SqlMapManager {
5
+ static instance: SqlMapManager;
6
+ private sqlMaps;
7
+ constructor();
8
+ get(moduleName: string, clazzName: string): TableSqlMap | undefined;
9
+ set(moduleName: string, sqlMap: TableSqlMap): void;
10
+ clear(): void;
11
+ }
12
+ //#endregion
13
+ export { SqlMapManager };
@@ -0,0 +1,27 @@
1
+ import { TableSqlMap } from "@eggjs/dal-runtime";
2
+
3
+ //#region src/lib/SqlMapManager.ts
4
+ var SqlMapManager = class SqlMapManager {
5
+ static instance = new SqlMapManager();
6
+ sqlMaps;
7
+ constructor() {
8
+ this.sqlMaps = /* @__PURE__ */ new Map();
9
+ }
10
+ get(moduleName, clazzName) {
11
+ return this.sqlMaps.get(moduleName)?.get(clazzName);
12
+ }
13
+ set(moduleName, sqlMap) {
14
+ let tables = this.sqlMaps.get(moduleName);
15
+ if (!tables) {
16
+ tables = /* @__PURE__ */ new Map();
17
+ this.sqlMaps.set(moduleName, tables);
18
+ }
19
+ tables.set(sqlMap.name, sqlMap);
20
+ }
21
+ clear() {
22
+ this.sqlMaps.clear();
23
+ }
24
+ };
25
+
26
+ //#endregion
27
+ export { SqlMapManager };
@@ -0,0 +1,13 @@
1
+ import { TableModel } from "@eggjs/dal-decorator";
2
+
3
+ //#region src/lib/TableModelManager.d.ts
4
+ declare class TableModelManager {
5
+ static instance: TableModelManager;
6
+ private tableModels;
7
+ constructor();
8
+ get(moduleName: string, clazzName: string): TableModel | undefined;
9
+ set(moduleName: string, tableModel: TableModel): void;
10
+ clear(): void;
11
+ }
12
+ //#endregion
13
+ export { TableModelManager };
@@ -0,0 +1,27 @@
1
+ import { TableModel } from "@eggjs/dal-decorator";
2
+
3
+ //#region src/lib/TableModelManager.ts
4
+ var TableModelManager = class TableModelManager {
5
+ static instance = new TableModelManager();
6
+ tableModels;
7
+ constructor() {
8
+ this.tableModels = /* @__PURE__ */ new Map();
9
+ }
10
+ get(moduleName, clazzName) {
11
+ return this.tableModels.get(moduleName)?.get(clazzName);
12
+ }
13
+ set(moduleName, tableModel) {
14
+ let tables = this.tableModels.get(moduleName);
15
+ if (!tables) {
16
+ tables = /* @__PURE__ */ new Map();
17
+ this.tableModels.set(moduleName, tables);
18
+ }
19
+ tables.set(tableModel.clazz.name, tableModel);
20
+ }
21
+ clear() {
22
+ this.tableModels.clear();
23
+ }
24
+ };
25
+
26
+ //#endregion
27
+ export { TableModelManager };
@@ -0,0 +1,12 @@
1
+ import { LifecycleHook, Logger, ModuleConfigHolder } from "@eggjs/tegg";
2
+ import { EggPrototype, EggPrototypeLifecycleContext } from "@eggjs/tegg-metadata";
3
+
4
+ //#region src/lib/TransactionPrototypeHook.d.ts
5
+ declare class TransactionPrototypeHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
6
+ private readonly moduleConfigs;
7
+ private readonly logger;
8
+ constructor(moduleConfigs: Record<string, ModuleConfigHolder>, logger: Logger);
9
+ preCreate(ctx: EggPrototypeLifecycleContext): Promise<void>;
10
+ }
11
+ //#endregion
12
+ export { TransactionPrototypeHook };
@@ -0,0 +1,50 @@
1
+ import { MysqlDataSourceManager } from "./MysqlDataSourceManager.js";
2
+ import { TransactionalAOP } from "./TransactionalAOP.js";
3
+ import "@eggjs/tegg";
4
+ import assert from "node:assert";
5
+ import "@eggjs/tegg-metadata";
6
+ import { PropagationType, TransactionMetaBuilder } from "@eggjs/tegg/transaction";
7
+ import { Pointcut } from "@eggjs/tegg/aop";
8
+
9
+ //#region src/lib/TransactionPrototypeHook.ts
10
+ var TransactionPrototypeHook = class {
11
+ moduleConfigs;
12
+ logger;
13
+ constructor(moduleConfigs, logger) {
14
+ this.moduleConfigs = moduleConfigs;
15
+ this.logger = logger;
16
+ }
17
+ async preCreate(ctx) {
18
+ const transactionMetadataList = new TransactionMetaBuilder(ctx.clazz).build();
19
+ if (transactionMetadataList.length < 1) return;
20
+ const moduleName = ctx.loadUnit.name;
21
+ for (const transactionMetadata of transactionMetadataList) {
22
+ const clazzName = `${moduleName}.${ctx.clazz.name}.${String(transactionMetadata.method)}`;
23
+ const datasourceConfigs = (this.moduleConfigs[moduleName]?.config)?.dataSource || {};
24
+ let datasourceName;
25
+ if (transactionMetadata.datasourceName) {
26
+ assert(datasourceConfigs[transactionMetadata.datasourceName], `method ${clazzName} specified datasource ${transactionMetadata.datasourceName} not exists`);
27
+ datasourceName = transactionMetadata.datasourceName;
28
+ this.logger.info(`use datasource [${transactionMetadata.datasourceName}] for class ${clazzName}`);
29
+ } else {
30
+ const dataSources = Object.keys(datasourceConfigs);
31
+ if (dataSources.length === 1) datasourceName = dataSources[0];
32
+ else throw new Error(`method ${clazzName} not specified datasource, module ${moduleName} has multi datasource, should specify datasource name`);
33
+ this.logger.info(`use default datasource ${dataSources[0]} for class ${clazzName}`);
34
+ }
35
+ const adviceParams = {
36
+ propagation: transactionMetadata.propagation,
37
+ dataSourceGetter: () => {
38
+ const mysqlDataSource = MysqlDataSourceManager.instance.get(moduleName, datasourceName);
39
+ if (!mysqlDataSource) throw new Error(`method ${clazzName} not found datasource ${datasourceName}`);
40
+ return mysqlDataSource;
41
+ }
42
+ };
43
+ assert(adviceParams.propagation === PropagationType.REQUIRED, "Transactional propagation only support required for now");
44
+ Pointcut(TransactionalAOP, { adviceParams })(ctx.clazz.prototype, transactionMetadata.method);
45
+ }
46
+ }
47
+ };
48
+
49
+ //#endregion
50
+ export { TransactionPrototypeHook };
@@ -0,0 +1,15 @@
1
+ import { EggProtoImplClass } from "@eggjs/tegg";
2
+ import { MysqlDataSource } from "@eggjs/dal-runtime";
3
+ import { PropagationType } from "@eggjs/tegg/transaction";
4
+ import { AdviceContext, IAdvice } from "@eggjs/tegg/aop";
5
+
6
+ //#region src/lib/TransactionalAOP.d.ts
7
+ interface TransactionalParams {
8
+ propagation: PropagationType;
9
+ dataSourceGetter: () => MysqlDataSource;
10
+ }
11
+ declare class TransactionalAOP implements IAdvice<EggProtoImplClass, TransactionalParams> {
12
+ around(ctx: AdviceContext<EggProtoImplClass, TransactionalParams>, next: () => Promise<any>): Promise<void>;
13
+ }
14
+ //#endregion
15
+ export { TransactionalAOP, TransactionalParams };
@@ -0,0 +1,22 @@
1
+ import { __decorate } from "../_virtual/_@oxc-project_runtime@0.93.0/helpers/decorate.js";
2
+ import { AccessLevel, ObjectInitType } from "@eggjs/tegg";
3
+ import { MysqlDataSource } from "@eggjs/dal-runtime";
4
+ import { PropagationType } from "@eggjs/tegg/transaction";
5
+ import { Advice } from "@eggjs/tegg/aop";
6
+
7
+ //#region src/lib/TransactionalAOP.ts
8
+ let TransactionalAOP = class TransactionalAOP$1 {
9
+ async around(ctx, next) {
10
+ const { propagation, dataSourceGetter } = ctx.adviceParams;
11
+ const dataSource = dataSourceGetter();
12
+ if (propagation === PropagationType.ALWAYS_NEW) return await dataSource.beginTransactionScope(next);
13
+ return await dataSource.beginTransactionScope(next);
14
+ }
15
+ };
16
+ TransactionalAOP = __decorate([Advice({
17
+ accessLevel: AccessLevel.PUBLIC,
18
+ initType: ObjectInitType.SINGLETON
19
+ })], TransactionalAOP);
20
+
21
+ //#endregion
22
+ export { TransactionalAOP };
@@ -0,0 +1 @@
1
+ import "@eggjs/tegg-plugin/types";
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ import "@eggjs/tegg-plugin/types";
2
+
3
+ export { };