-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
199 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import assert from 'assert'; | ||
import { | ||
EggContext, | ||
EggObject, | ||
EggObjectLifeCycleContext, | ||
EggObjectStatus, | ||
} from '@eggjs/tegg-runtime'; | ||
import { EggPrototype } from '@eggjs/tegg-metadata'; | ||
import { EggPrototypeName, EggObjectName } from '@eggjs/tegg'; | ||
import { Id, IdenticalUtil } from '@eggjs/tegg-lifecycle'; | ||
import { Bone } from 'leoric'; | ||
import ContextModelProto from './ContextModelProto'; | ||
import { EGG_CONTEXT } from '@eggjs/egg-module-common'; | ||
|
||
export class ContextModeObject implements EggObject { | ||
private status: EggObjectStatus = EggObjectStatus.PENDING; | ||
id: Id; | ||
readonly name: EggPrototypeName; | ||
private _obj: typeof Bone; | ||
readonly proto: ContextModelProto; | ||
readonly ctx: EggContext; | ||
|
||
constructor(name: EggObjectName, proto: ContextModelProto, ctx: EggContext) { | ||
this.name = name; | ||
this.proto = proto; | ||
this.ctx = ctx; | ||
this.id = IdenticalUtil.createObjectId(this.proto.id, this.ctx.id); | ||
} | ||
|
||
async init() { | ||
const ctx = this.ctx; | ||
const clazz = class ContextModelClass extends this.proto.model { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
static get name() { | ||
return super.name; | ||
} | ||
|
||
static get ctx() { | ||
return ctx.get(EGG_CONTEXT); | ||
} | ||
|
||
// custom setter always execute before define [CTX] when new Instance(super(opts) calling), if custom setter requires ctx, it should not be undefined | ||
get ctx() { | ||
return ctx.get(EGG_CONTEXT); | ||
} | ||
}; | ||
this._obj = clazz; | ||
this.status = EggObjectStatus.READY; | ||
} | ||
|
||
injectProperty() { | ||
throw new Error('never call ModelObject#injectProperty'); | ||
} | ||
|
||
get isReady() { | ||
return this.status === EggObjectStatus.READY; | ||
} | ||
|
||
get obj() { | ||
return this._obj; | ||
} | ||
|
||
static async createObject(name: EggObjectName, proto: EggPrototype, _: EggObjectLifeCycleContext, ctx?: EggContext): Promise<ContextModeObject> { | ||
assert(ctx, 'ctx must be defined for ContextModelObject'); | ||
const modelObject = new ContextModeObject(name, proto as ContextModelProto, ctx); | ||
await modelObject.init(); | ||
return modelObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { EggPrototype, LoadUnit, EggPrototypeLifecycleContext } from '@eggjs/tegg-metadata'; | ||
import { | ||
AccessLevel, | ||
EggPrototypeName, | ||
ObjectInitType, | ||
QualifierInfo, | ||
QualifierUtil, | ||
MetadataUtil, | ||
MetaDataKey, | ||
} from '@eggjs/tegg'; | ||
import { Id, IdenticalUtil } from '@eggjs/tegg-lifecycle'; | ||
import { Bone } from 'leoric'; | ||
|
||
export default class ContextModelProto implements EggPrototype { | ||
private readonly qualifiers: QualifierInfo[]; | ||
readonly accessLevel = AccessLevel.PUBLIC; | ||
id: Id; | ||
readonly initType = ObjectInitType.CONTEXT; | ||
readonly injectObjects = []; | ||
readonly loadUnitId: string; | ||
readonly moduleName: string; | ||
readonly name: EggPrototypeName; | ||
readonly model: typeof Bone; | ||
|
||
constructor(loadUnit: LoadUnit, model: typeof Bone) { | ||
this.model = model; | ||
this.id = IdenticalUtil.createProtoId(loadUnit.id, `leoric:${model.name}`); | ||
this.loadUnitId = loadUnit.id; | ||
this.moduleName = loadUnit.name; | ||
this.name = model.name; | ||
this.qualifiers = QualifierUtil.getProtoQualifiers(model); | ||
} | ||
|
||
constructEggObject(): object { | ||
return {}; | ||
} | ||
|
||
getMetaData<T>(metadataKey: MetaDataKey): T | undefined { | ||
return MetadataUtil.getMetaData(metadataKey, this.model); | ||
} | ||
|
||
verifyQualifier(qualifier: QualifierInfo): boolean { | ||
const selfQualifiers = this.qualifiers.find(t => t.attribute === qualifier.attribute); | ||
return selfQualifiers?.value === qualifier.value; | ||
} | ||
|
||
verifyQualifiers(qualifiers: QualifierInfo[]): boolean { | ||
for (const qualifier of qualifiers) { | ||
if (!this.verifyQualifier(qualifier)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
static createProto(ctx: EggPrototypeLifecycleContext): ContextModelProto { | ||
return new ContextModelProto(ctx.loadUnit, ctx.clazz as typeof Bone); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Application } from 'egg'; | ||
import { Logger } from 'leoric'; | ||
|
||
export default class OrmAppHook { | ||
private readonly app: Application; | ||
|
||
constructor(app: Application) { | ||
this.app = app; | ||
} | ||
|
||
async didLoad() { | ||
await this.app.leoricRegister.ready(); | ||
const app = this.app; | ||
for (const realm of this.app.leoricRegister.realmMap.values()) { | ||
realm.driver.logger = new Logger({ | ||
logQuery(sql, _, options) { | ||
const path = options.Model?.ctx?.path; | ||
app.logger.info('sql: %s path: %s', sql, path); | ||
}, | ||
}); | ||
} | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
plugin/orm/test/fixtures/apps/orm-app/modules/orm-module/AppService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
plugin/orm/test/fixtures/apps/orm-app/modules/orm-module/PkgService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters