@eggjs/tegg-orm-plugin 4.0.0-beta.4 → 4.0.0-beta.6

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 (52) hide show
  1. package/README.md +12 -0
  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/app.d.ts +26 -0
  5. package/dist/app.js +44 -0
  6. package/dist/config/config.default.d.ts +19 -0
  7. package/dist/config/config.default.js +5 -0
  8. package/dist/index.d.ts +3 -0
  9. package/dist/index.js +5 -0
  10. package/dist/lib/DataSourceManager.d.ts +23 -0
  11. package/dist/lib/DataSourceManager.js +22 -0
  12. package/dist/lib/LeoricRegister.d.ts +20 -0
  13. package/dist/lib/LeoricRegister.js +68 -0
  14. package/dist/lib/ModelProtoHook.d.ts +12 -0
  15. package/dist/lib/ModelProtoHook.js +20 -0
  16. package/dist/lib/ModelProtoManager.d.ts +15 -0
  17. package/dist/lib/ModelProtoManager.js +21 -0
  18. package/dist/lib/ORMLoadUnitHook.d.ts +9 -0
  19. package/dist/lib/ORMLoadUnitHook.js +17 -0
  20. package/dist/lib/SingletonModelObject.d.ts +23 -0
  21. package/dist/lib/SingletonModelObject.js +49 -0
  22. package/dist/lib/SingletonModelProto.d.ts +27 -0
  23. package/dist/lib/SingletonModelProto.js +41 -0
  24. package/dist/lib/SingletonORM.d.ts +10 -0
  25. package/dist/lib/SingletonORM.js +23 -0
  26. package/dist/lib/types.d.ts +7 -0
  27. package/dist/lib/types.js +3 -0
  28. package/dist/types.d.ts +8 -0
  29. package/dist/types.js +5 -0
  30. package/package.json +54 -49
  31. package/app.d.ts +0 -14
  32. package/app.js +0 -44
  33. package/index.d.ts +0 -21
  34. package/index.js +0 -5
  35. package/lib/DataSourceManager.d.ts +0 -20
  36. package/lib/DataSourceManager.js +0 -21
  37. package/lib/LeoricRegister.d.ts +0 -16
  38. package/lib/LeoricRegister.js +0 -80
  39. package/lib/ModelProtoHook.d.ts +0 -8
  40. package/lib/ModelProtoHook.js +0 -16
  41. package/lib/ModelProtoManager.d.ts +0 -11
  42. package/lib/ModelProtoManager.js +0 -12
  43. package/lib/ORMLoadUnitHook.d.ts +0 -5
  44. package/lib/ORMLoadUnitHook.js +0 -18
  45. package/lib/SingletonModelObject.d.ts +0 -19
  46. package/lib/SingletonModelObject.js +0 -46
  47. package/lib/SingletonModelProto.d.ts +0 -23
  48. package/lib/SingletonModelProto.js +0 -40
  49. package/lib/SingletonORM.d.ts +0 -6
  50. package/lib/SingletonORM.js +0 -36
  51. package/lib/types.d.ts +0 -3
  52. package/lib/types.js +0 -2
package/README.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @eggjs/tegg-orm-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-orm-plugin.svg?style=flat)](https://nodejs.org/en/download/)
7
+
8
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/tegg-orm-plugin.svg?style=flat-square
9
+ [npm-url]: https://npmjs.org/package/@eggjs/tegg-orm-plugin
10
+ [snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-orm-plugin/badge.svg?style=flat-square
11
+ [snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-orm-plugin
12
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-orm-plugin.svg?style=flat-square
13
+ [download-url]: https://npmjs.org/package/@eggjs/tegg-orm-plugin
14
+
3
15
  使用注解的方式来开发 egg 中的 orm
4
16
 
5
17
  ## Install
@@ -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 };
package/dist/app.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { Orm } from "./lib/SingletonORM.js";
2
+ import { LeoricRegister } from "./lib/LeoricRegister.js";
3
+ import { Application } from "egg";
4
+
5
+ //#region src/app.d.ts
6
+ declare class OrmAppBootHook {
7
+ private readonly app;
8
+ private readonly dataSourceManager;
9
+ private readonly leoricRegister;
10
+ private readonly modelProtoManager;
11
+ private readonly modelProtoHook;
12
+ private readonly ormLoadUnitHook;
13
+ constructor(app: Application);
14
+ configWillLoad(): void;
15
+ configDidLoad(): void;
16
+ didLoad(): Promise<void>;
17
+ beforeClose(): Promise<void>;
18
+ }
19
+ declare module 'egg' {
20
+ interface Application {
21
+ leoricRegister: LeoricRegister;
22
+ orm: Orm;
23
+ }
24
+ }
25
+ //#endregion
26
+ export { OrmAppBootHook as default };
package/dist/app.js ADDED
@@ -0,0 +1,44 @@
1
+ import { ModelProtoManager } from "./lib/ModelProtoManager.js";
2
+ import { DataSourceManager } from "./lib/DataSourceManager.js";
3
+ import { LeoricRegister } from "./lib/LeoricRegister.js";
4
+ import "./lib/SingletonORM.js";
5
+ import { ModelProtoHook } from "./lib/ModelProtoHook.js";
6
+ import SingletonModelProto from "./lib/SingletonModelProto.js";
7
+ import { SingletonModelObject } from "./lib/SingletonModelObject.js";
8
+ import { ORMLoadUnitHook } from "./lib/ORMLoadUnitHook.js";
9
+ import { Application } from "egg";
10
+ import { MODEL_PROTO_IMPL_TYPE } from "@eggjs/tegg-orm-decorator";
11
+
12
+ //#region src/app.ts
13
+ var OrmAppBootHook = class {
14
+ constructor(app) {
15
+ this.app = app;
16
+ this.dataSourceManager = new DataSourceManager();
17
+ this.modelProtoManager = new ModelProtoManager();
18
+ this.leoricRegister = new LeoricRegister(this.modelProtoManager, this.dataSourceManager);
19
+ this.modelProtoHook = new ModelProtoHook(this.modelProtoManager);
20
+ this.app.eggPrototypeCreatorFactory.registerPrototypeCreator(MODEL_PROTO_IMPL_TYPE, SingletonModelProto.createProto);
21
+ this.app.leoricRegister = this.leoricRegister;
22
+ this.ormLoadUnitHook = new ORMLoadUnitHook();
23
+ }
24
+ configWillLoad() {
25
+ this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.modelProtoHook);
26
+ this.app.eggObjectFactory.registerEggObjectCreateMethod(SingletonModelProto, SingletonModelObject.createObject);
27
+ this.app.loadUnitLifecycleUtil.registerLifecycle(this.ormLoadUnitHook);
28
+ }
29
+ configDidLoad() {
30
+ const config = this.app.config.orm;
31
+ if (config.datasources) for (const datasource of config.datasources) this.dataSourceManager.addConfig(datasource);
32
+ else this.dataSourceManager.addDefaultConfig(config);
33
+ }
34
+ async didLoad() {
35
+ await this.app.moduleHandler.ready();
36
+ await this.leoricRegister.register();
37
+ }
38
+ async beforeClose() {
39
+ this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.modelProtoHook);
40
+ }
41
+ };
42
+
43
+ //#endregion
44
+ export { OrmAppBootHook as default };
@@ -0,0 +1,19 @@
1
+ import { OrmConfig } from "../lib/DataSourceManager.js";
2
+
3
+ //#region src/config/config.default.d.ts
4
+ interface AppOrmConfig extends OrmConfig {
5
+ datasources?: OrmConfig[];
6
+ }
7
+ declare const _default: {
8
+ orm: AppOrmConfig;
9
+ };
10
+ declare module 'egg' {
11
+ interface EggAppConfig {
12
+ /**
13
+ * orm config
14
+ */
15
+ orm: AppOrmConfig;
16
+ }
17
+ }
18
+ //#endregion
19
+ export { AppOrmConfig, _default as default };
@@ -0,0 +1,5 @@
1
+ //#region src/config/config.default.ts
2
+ var config_default_default = { orm: {} };
3
+
4
+ //#endregion
5
+ export { config_default_default as default };
@@ -0,0 +1,3 @@
1
+ import { Orm } from "./lib/SingletonORM.js";
2
+ import { LeoricRegister } from "./lib/LeoricRegister.js";
3
+ export { LeoricRegister, Orm };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import { LeoricRegister } from "./lib/LeoricRegister.js";
2
+ import { Orm } from "./lib/SingletonORM.js";
3
+ import "./types.js";
4
+
5
+ export { LeoricRegister, Orm };
@@ -0,0 +1,23 @@
1
+ //#region src/lib/DataSourceManager.d.ts
2
+ interface OrmConfig {
3
+ client: string;
4
+ database: string;
5
+ host: string;
6
+ port: number;
7
+ user: string;
8
+ define: object;
9
+ options: object;
10
+ charset: string;
11
+ [key: string]: any;
12
+ }
13
+ declare class DataSourceManager {
14
+ private readonly dataSourceConfigs;
15
+ private defaultDataSourceConfig?;
16
+ constructor();
17
+ addDefaultConfig(config: OrmConfig): void;
18
+ getDefaultConfig(): OrmConfig | undefined;
19
+ addConfig(config: OrmConfig): void;
20
+ getConfig(name: string): OrmConfig | undefined;
21
+ }
22
+ //#endregion
23
+ export { DataSourceManager, OrmConfig };
@@ -0,0 +1,22 @@
1
+ //#region src/lib/DataSourceManager.ts
2
+ var DataSourceManager = class {
3
+ constructor() {
4
+ this.dataSourceConfigs = [];
5
+ }
6
+ addDefaultConfig(config) {
7
+ this.defaultDataSourceConfig = config;
8
+ }
9
+ getDefaultConfig() {
10
+ return this.defaultDataSourceConfig;
11
+ }
12
+ addConfig(config) {
13
+ this.dataSourceConfigs.push(config);
14
+ }
15
+ getConfig(name) {
16
+ if (this.defaultDataSourceConfig?.database === name) return this.defaultDataSourceConfig;
17
+ return this.dataSourceConfigs.find((t) => t.database === name);
18
+ }
19
+ };
20
+
21
+ //#endregion
22
+ export { DataSourceManager };
@@ -0,0 +1,20 @@
1
+ import { RealmType } from "./types.js";
2
+ import { ModelProtoManager } from "./ModelProtoManager.js";
3
+ import { DataSourceManager, OrmConfig } from "./DataSourceManager.js";
4
+ import { ModelMetadata } from "@eggjs/tegg-orm-decorator";
5
+ import { Base } from "sdk-base";
6
+
7
+ //#region src/lib/LeoricRegister.d.ts
8
+ declare class LeoricRegister extends Base {
9
+ private readonly modelProtoManager;
10
+ private readonly dataSourceManager;
11
+ readonly realmMap: Map<string, RealmType>;
12
+ constructor(modelProtoManager: ModelProtoManager, dataSourceManager: DataSourceManager);
13
+ getConfig(datasource?: string): OrmConfig;
14
+ getRealm(config: OrmConfig | undefined): RealmType | undefined;
15
+ getOrCreateRealm(datasource: string | undefined): any;
16
+ generateLeoricAttributes(metadata: ModelMetadata): Record<string, any>;
17
+ register(): Promise<void>;
18
+ }
19
+ //#endregion
20
+ export { LeoricRegister };
@@ -0,0 +1,68 @@
1
+ import "./ModelProtoManager.js";
2
+ import { ModelMetadata, ModelMetadataUtil } from "@eggjs/tegg-orm-decorator";
3
+ import { Base } from "sdk-base";
4
+ import Realm from "leoric";
5
+
6
+ //#region src/lib/LeoricRegister.ts
7
+ var LeoricRegister = class extends Base {
8
+ constructor(modelProtoManager, dataSourceManager) {
9
+ super();
10
+ this.modelProtoManager = modelProtoManager;
11
+ this.dataSourceManager = dataSourceManager;
12
+ this.realmMap = /* @__PURE__ */ new Map();
13
+ }
14
+ getConfig(datasource) {
15
+ let config;
16
+ if (!datasource) config = this.dataSourceManager.getDefaultConfig();
17
+ else config = this.dataSourceManager.getConfig(datasource);
18
+ return config;
19
+ }
20
+ getRealm(config) {
21
+ if (!config?.database) return;
22
+ return this.realmMap.get(config.database);
23
+ }
24
+ getOrCreateRealm(datasource) {
25
+ const config = this.getConfig(datasource);
26
+ let realm;
27
+ if (config) {
28
+ realm = this.getRealm(config);
29
+ if (realm) return realm;
30
+ }
31
+ realm = new Realm({ ...config });
32
+ this.realmMap.set(config.database, realm);
33
+ return realm;
34
+ }
35
+ generateLeoricAttributes(metadata) {
36
+ const attributes = {};
37
+ for (const attribute of metadata.attributes) attributes[attribute.propertyName] = {
38
+ columnName: attribute.attributeName,
39
+ type: attribute.dataType,
40
+ allowNull: attribute.allowNull,
41
+ primaryKey: attribute.primary,
42
+ unique: attribute.unique,
43
+ autoIncrement: attribute.autoIncrement
44
+ };
45
+ return attributes;
46
+ }
47
+ async register() {
48
+ for (const { proto, clazz } of this.modelProtoManager.getProtos()) {
49
+ const metadata = ModelMetadataUtil.getModelMetadata(clazz);
50
+ if (!metadata) throw new Error(`not found metadata for model ${proto.id}`);
51
+ const realm = this.getOrCreateRealm(metadata.dataSource);
52
+ realm.models[clazz.name] = clazz;
53
+ realm[clazz.name] = clazz;
54
+ const attributes = this.generateLeoricAttributes(metadata);
55
+ const hooks = {};
56
+ for (const hookName of Realm.hookNames) if (clazz[hookName]) hooks[hookName] = clazz[hookName];
57
+ clazz.init(attributes, {
58
+ tableName: metadata.tableName,
59
+ hooks
60
+ }, {});
61
+ }
62
+ await Promise.all(Array.from(this.realmMap.values()).map((realm) => realm.connect()));
63
+ this.ready(true);
64
+ }
65
+ };
66
+
67
+ //#endregion
68
+ export { LeoricRegister };
@@ -0,0 +1,12 @@
1
+ import { ModelProtoManager } from "./ModelProtoManager.js";
2
+ import { LifecycleHook } from "@eggjs/tegg";
3
+ import { EggPrototype, EggPrototypeLifecycleContext } from "@eggjs/tegg-metadata";
4
+
5
+ //#region src/lib/ModelProtoHook.d.ts
6
+ declare class ModelProtoHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
7
+ private readonly modelProtoManager;
8
+ constructor(modelProtoManager: ModelProtoManager);
9
+ postCreate(ctx: EggPrototypeLifecycleContext, obj: EggPrototype): Promise<void>;
10
+ }
11
+ //#endregion
12
+ export { ModelProtoHook };
@@ -0,0 +1,20 @@
1
+ import "./ModelProtoManager.js";
2
+ import { IS_MODEL, ModelMetaBuilder, ModelMetadataUtil } from "@eggjs/tegg-orm-decorator";
3
+ import "@eggjs/tegg";
4
+ import "@eggjs/tegg-metadata";
5
+
6
+ //#region src/lib/ModelProtoHook.ts
7
+ var ModelProtoHook = class {
8
+ constructor(modelProtoManager) {
9
+ this.modelProtoManager = modelProtoManager;
10
+ }
11
+ async postCreate(ctx, obj) {
12
+ if (!obj.getMetaData(IS_MODEL)) return;
13
+ const metadata = new ModelMetaBuilder(ctx.clazz).build();
14
+ ModelMetadataUtil.setModelMetadata(ctx.clazz, metadata);
15
+ this.modelProtoManager.addProto(ctx.clazz, obj);
16
+ }
17
+ };
18
+
19
+ //#endregion
20
+ export { ModelProtoHook };
@@ -0,0 +1,15 @@
1
+ import { EggProtoImplClass } from "@eggjs/tegg";
2
+ import { EggPrototype } from "@eggjs/tegg-metadata";
3
+
4
+ //#region src/lib/ModelProtoManager.d.ts
5
+ interface ModelProtoPair {
6
+ proto: EggPrototype;
7
+ clazz: EggProtoImplClass;
8
+ }
9
+ declare class ModelProtoManager {
10
+ private readonly protos;
11
+ addProto(clazz: EggProtoImplClass, proto: EggPrototype): void;
12
+ getProtos(): Array<ModelProtoPair>;
13
+ }
14
+ //#endregion
15
+ export { ModelProtoManager, ModelProtoPair };
@@ -0,0 +1,21 @@
1
+ import "@eggjs/tegg";
2
+ import "@eggjs/tegg-metadata";
3
+
4
+ //#region src/lib/ModelProtoManager.ts
5
+ var ModelProtoManager = class {
6
+ constructor() {
7
+ this.protos = [];
8
+ }
9
+ addProto(clazz, proto) {
10
+ this.protos.push({
11
+ proto,
12
+ clazz
13
+ });
14
+ }
15
+ getProtos() {
16
+ return this.protos.slice();
17
+ }
18
+ };
19
+
20
+ //#endregion
21
+ export { ModelProtoManager };
@@ -0,0 +1,9 @@
1
+ import { LifecycleHook } from "@eggjs/tegg";
2
+ import { LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg-metadata";
3
+
4
+ //#region src/lib/ORMLoadUnitHook.d.ts
5
+ declare class ORMLoadUnitHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
6
+ postCreate(_ctx: LoadUnitLifecycleContext, loadUnit: LoadUnit): Promise<void>;
7
+ }
8
+ //#endregion
9
+ export { ORMLoadUnitHook };
@@ -0,0 +1,17 @@
1
+ import { Orm } from "./SingletonORM.js";
2
+ import "@eggjs/tegg";
3
+ import { EggLoadUnitType, EggPrototypeCreatorFactory, EggPrototypeFactory } from "@eggjs/tegg-metadata";
4
+
5
+ //#region src/lib/ORMLoadUnitHook.ts
6
+ const REGISTER_CLAZZ = [Orm];
7
+ var ORMLoadUnitHook = class {
8
+ async postCreate(_ctx, loadUnit) {
9
+ if (loadUnit.type === EggLoadUnitType.APP) for (const clazz of REGISTER_CLAZZ) {
10
+ const protos = await EggPrototypeCreatorFactory.createProto(clazz, loadUnit);
11
+ for (const proto of protos) EggPrototypeFactory.instance.registerPrototype(proto, loadUnit);
12
+ }
13
+ }
14
+ };
15
+
16
+ //#endregion
17
+ export { ORMLoadUnitHook };
@@ -0,0 +1,23 @@
1
+ import SingletonModelProto from "./SingletonModelProto.js";
2
+ import { EggObjectName, EggPrototypeName } from "@eggjs/tegg";
3
+ import { Bone } from "leoric";
4
+ import { EggPrototype } from "@eggjs/tegg-metadata";
5
+ import { Id } from "@eggjs/tegg-lifecycle";
6
+ import { EggObject } from "@eggjs/tegg-runtime";
7
+
8
+ //#region src/lib/SingletonModelObject.d.ts
9
+ declare class SingletonModelObject implements EggObject {
10
+ private status;
11
+ id: Id;
12
+ readonly name: EggPrototypeName;
13
+ private _obj;
14
+ readonly proto: SingletonModelProto;
15
+ constructor(name: EggObjectName, proto: SingletonModelProto);
16
+ init(): Promise<void>;
17
+ injectProperty(): void;
18
+ get isReady(): boolean;
19
+ get obj(): typeof Bone;
20
+ static createObject(name: EggObjectName, proto: EggPrototype): Promise<SingletonModelObject>;
21
+ }
22
+ //#endregion
23
+ export { SingletonModelObject };
@@ -0,0 +1,49 @@
1
+ import "./SingletonModelProto.js";
2
+ import "@eggjs/tegg";
3
+ import { Bone } from "leoric";
4
+ import "@eggjs/tegg-metadata";
5
+ import { IdenticalUtil } from "@eggjs/tegg-lifecycle";
6
+ import { ContextHandler, EggObjectStatus } from "@eggjs/tegg-runtime";
7
+ import { EGG_CONTEXT } from "@eggjs/egg-module-common";
8
+
9
+ //#region src/lib/SingletonModelObject.ts
10
+ var SingletonModelObject = class SingletonModelObject {
11
+ constructor(name, proto) {
12
+ this.status = EggObjectStatus.PENDING;
13
+ this.name = name;
14
+ this.proto = proto;
15
+ this.id = IdenticalUtil.createObjectId(this.proto.id);
16
+ }
17
+ async init() {
18
+ this._obj = class ContextModelClass extends this.proto.model {
19
+ static get name() {
20
+ return super.name;
21
+ }
22
+ static get ctx() {
23
+ const ctx = ContextHandler.getContext();
24
+ if (ctx) return ctx.get(EGG_CONTEXT);
25
+ }
26
+ get ctx() {
27
+ return ContextModelClass.ctx;
28
+ }
29
+ };
30
+ this.status = EggObjectStatus.READY;
31
+ }
32
+ injectProperty() {
33
+ throw new Error("never call ModelObject#injectProperty");
34
+ }
35
+ get isReady() {
36
+ return this.status === EggObjectStatus.READY;
37
+ }
38
+ get obj() {
39
+ return this._obj;
40
+ }
41
+ static async createObject(name, proto) {
42
+ const modelObject = new SingletonModelObject(name, proto);
43
+ await modelObject.init();
44
+ return modelObject;
45
+ }
46
+ };
47
+
48
+ //#endregion
49
+ export { SingletonModelObject };
@@ -0,0 +1,27 @@
1
+ import { AccessLevel, EggPrototypeName, MetaDataKey, ObjectInitType, QualifierAttribute, QualifierInfo, QualifierValue } from "@eggjs/tegg";
2
+ import { Bone } from "leoric";
3
+ import { EggPrototype, EggPrototypeLifecycleContext, InjectConstructorProto, InjectObjectProto, LoadUnit } from "@eggjs/tegg-metadata";
4
+ import { Id } from "@eggjs/tegg-lifecycle";
5
+
6
+ //#region src/lib/SingletonModelProto.d.ts
7
+ declare class SingletonModelProto implements EggPrototype {
8
+ [key: symbol]: PropertyDescriptor;
9
+ private readonly qualifiers;
10
+ readonly accessLevel = AccessLevel.PUBLIC;
11
+ id: Id;
12
+ readonly initType = ObjectInitType.SINGLETON;
13
+ readonly injectObjects: (InjectObjectProto | InjectConstructorProto)[];
14
+ readonly loadUnitId: string;
15
+ readonly moduleName: string;
16
+ readonly name: EggPrototypeName;
17
+ readonly model: typeof Bone;
18
+ constructor(loadUnit: LoadUnit, model: typeof Bone);
19
+ constructEggObject(): object;
20
+ getMetaData<T>(metadataKey: MetaDataKey): T | undefined;
21
+ verifyQualifier(qualifier: QualifierInfo): boolean;
22
+ verifyQualifiers(qualifiers: QualifierInfo[]): boolean;
23
+ getQualifier(attribute: QualifierAttribute): QualifierValue | undefined;
24
+ static createProto(ctx: EggPrototypeLifecycleContext): SingletonModelProto;
25
+ }
26
+ //#endregion
27
+ export { SingletonModelProto as default };
@@ -0,0 +1,41 @@
1
+ import { AccessLevel, MetadataUtil, ObjectInitType, QualifierUtil } from "@eggjs/tegg";
2
+ import { Bone } from "leoric";
3
+ import "@eggjs/tegg-metadata";
4
+ import { IdenticalUtil } from "@eggjs/tegg-lifecycle";
5
+
6
+ //#region src/lib/SingletonModelProto.ts
7
+ var SingletonModelProto = class SingletonModelProto {
8
+ constructor(loadUnit, model) {
9
+ this.accessLevel = AccessLevel.PUBLIC;
10
+ this.initType = ObjectInitType.SINGLETON;
11
+ this.injectObjects = [];
12
+ this.model = model;
13
+ this.id = IdenticalUtil.createProtoId(loadUnit.id, `leoric:${model.name}`);
14
+ this.loadUnitId = loadUnit.id;
15
+ this.moduleName = loadUnit.name;
16
+ this.name = model.name;
17
+ this.qualifiers = QualifierUtil.getProtoQualifiers(model);
18
+ }
19
+ constructEggObject() {
20
+ return {};
21
+ }
22
+ getMetaData(metadataKey) {
23
+ return MetadataUtil.getMetaData(metadataKey, this.model);
24
+ }
25
+ verifyQualifier(qualifier) {
26
+ return this.qualifiers.find((t) => t.attribute === qualifier.attribute)?.value === qualifier.value;
27
+ }
28
+ verifyQualifiers(qualifiers) {
29
+ for (const qualifier of qualifiers) if (!this.verifyQualifier(qualifier)) return false;
30
+ return true;
31
+ }
32
+ getQualifier(attribute) {
33
+ return this.qualifiers.find((t) => t.attribute === attribute)?.value;
34
+ }
35
+ static createProto(ctx) {
36
+ return new SingletonModelProto(ctx.loadUnit, ctx.clazz);
37
+ }
38
+ };
39
+
40
+ //#endregion
41
+ export { SingletonModelProto as default };
@@ -0,0 +1,10 @@
1
+ import { RealmType } from "./types.js";
2
+
3
+ //#region src/lib/SingletonORM.d.ts
4
+ declare class Orm {
5
+ private leoricRegister;
6
+ get client(): RealmType;
7
+ getClient(datasource: string): RealmType;
8
+ }
9
+ //#endregion
10
+ export { Orm };
@@ -0,0 +1,23 @@
1
+ import { LeoricRegister } from "./LeoricRegister.js";
2
+ import { __decorateMetadata } from "../_virtual/_@oxc-project_runtime@0.93.0/helpers/decorateMetadata.js";
3
+ import { __decorate } from "../_virtual/_@oxc-project_runtime@0.93.0/helpers/decorate.js";
4
+ import { AccessLevel, Inject, SingletonProto } from "@eggjs/tegg";
5
+
6
+ //#region src/lib/SingletonORM.ts
7
+ var _ref;
8
+ let Orm = class Orm$1 {
9
+ get client() {
10
+ const defaultConfig = this.leoricRegister.getConfig();
11
+ return this.leoricRegister.getRealm(defaultConfig);
12
+ }
13
+ getClient(datasource) {
14
+ const config = this.leoricRegister.getConfig(datasource);
15
+ if (!config) throw new Error(`not found ${datasource} datasource`);
16
+ return this.leoricRegister.getOrCreateRealm(config.database);
17
+ }
18
+ };
19
+ __decorate([Inject(), __decorateMetadata("design:type", typeof (_ref = typeof LeoricRegister !== "undefined" && LeoricRegister) === "function" ? _ref : Object)], Orm.prototype, "leoricRegister", void 0);
20
+ Orm = __decorate([SingletonProto({ accessLevel: AccessLevel.PUBLIC })], Orm);
21
+
22
+ //#endregion
23
+ export { Orm };
@@ -0,0 +1,7 @@
1
+ import { AbstractDriver, connect } from "leoric";
2
+
3
+ //#region src/lib/types.d.ts
4
+ type DataType = AbstractDriver['DataType'];
5
+ type RealmType = Awaited<ReturnType<typeof connect>>;
6
+ //#endregion
7
+ export { DataType, RealmType };
@@ -0,0 +1,3 @@
1
+ import { AbstractDriver, connect } from "leoric";
2
+
3
+ export { };
@@ -0,0 +1,8 @@
1
+ import { DataType } from "./lib/types.js";
2
+ import { AttributeOptions } from "@eggjs/tegg-orm-decorator";
3
+ import "@eggjs/tegg-plugin/types";
4
+
5
+ //#region src/types.d.ts
6
+ declare module '@eggjs/tegg-orm-decorator' {
7
+ function Attribute(dataType: DataType, options?: AttributeOptions): (target: any, propertyKey: PropertyKey) => void;
8
+ }
package/dist/types.js ADDED
@@ -0,0 +1,5 @@
1
+ import "./app.js";
2
+ import "@eggjs/tegg-orm-decorator";
3
+ import "@eggjs/tegg-plugin/types";
4
+
5
+ export { };