Skip to content

Commit

Permalink
refactor: plugin/orm (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Mar 2, 2025
1 parent 74d8be7 commit 65ce750
Show file tree
Hide file tree
Showing 28 changed files with 171 additions and 159 deletions.
1 change: 1 addition & 0 deletions core/aop-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"koa-compose": "^4.1.0"
},
"devDependencies": {
"@types/koa-compose": "3",
"@eggjs/module-test-util": "^3.52.0",
"@eggjs/tegg-loader": "^3.52.0",
"@types/node": "22",
Expand Down
2 changes: 1 addition & 1 deletion core/loader/src/LoaderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class LoaderUtil {
} catch (e: any) {
console.error('[tegg/loader] loadFile %s error:', filePath);
console.error(e);
throw new Error(`[tegg/loader] load ${filePath} failed: ${e.message}`, { cause: e });
throw new Error(`[tegg/loader] load ${filePath} failed: ${e.message}`);
}
const clazzList: EggProtoImplClass[] = [];
const exportNames = Object.keys(exports);
Expand Down
1 change: 1 addition & 0 deletions plugin/aop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scripts": {
"test": "egg-bin test",
"cov": "egg-bin cov",
"clean": "tsc --build --clean",
"tsc": "npm run clean && tsc -p ./tsconfig.pub.json",
"tsc:pub": "npm run tsc",
"prepublishOnly": "npm run tsc"
Expand Down
1 change: 0 additions & 1 deletion plugin/controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"@eggjs/tegg-types": "^3.52.0",
"egg-errors": "^2.3.0",
"globby": "^14.1.0",
"koa-compose": "^3.2.1",
"path-to-regexp": "^1.9.0",
"sdk-base": "^5.0.1"
},
Expand Down
18 changes: 9 additions & 9 deletions plugin/orm/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Application } from 'egg';
import { DataSourceManager } from './lib/DataSourceManager';
import { LeoricRegister } from './lib/LeoricRegister';
import { ModelProtoManager } from './lib/ModelProtoManager';
import { ModelProtoHook } from './lib/ModelProtoHook';
import { EggCore as Application } from '@eggjs/core';
import { DataSourceManager } from './lib/DataSourceManager.js';
import { LeoricRegister } from './lib/LeoricRegister.js';
import { ModelProtoManager } from './lib/ModelProtoManager.js';
import { ModelProtoHook } from './lib/ModelProtoHook.js';
import { MODEL_PROTO_IMPL_TYPE } from '@eggjs/tegg-orm-decorator';
import SingletonModelProto from './lib/SingletonModelProto';
import { SingletonModelObject } from './lib/SingletonModelObject';
import { ORMLoadUnitHook } from './lib/ORMLoadUnitHook';
import SingletonModelProto from './lib/SingletonModelProto.js';
import { SingletonModelObject } from './lib/SingletonModelObject.js';
import { ORMLoadUnitHook } from './lib/ORMLoadUnitHook.js';

export default class OrmAppBootHook {
private readonly app: Application;
Expand All @@ -16,7 +16,7 @@ export default class OrmAppBootHook {
private readonly modelProtoHook: ModelProtoHook;
private readonly ormLoadUnitHook: ORMLoadUnitHook;

constructor(app) {
constructor(app: Application) {
this.app = app;
this.dataSourceManager = new DataSourceManager();
this.modelProtoManager = new ModelProtoManager();
Expand Down
25 changes: 25 additions & 0 deletions plugin/orm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import '@eggjs/tegg-plugin';

import { AttributeOptions } from '@eggjs/tegg-orm-decorator';
import { LeoricRegister } from './lib/LeoricRegister.js';
import { Orm } from './lib/SingletonORM.js';
import type { OrmConfig } from './lib/DataSourceManager.js';
import type { DataType } from './lib/types.js';

declare module '@eggjs/tegg-orm-decorator' {
// @ts-expect-error: DataType is not defined in tegg-orm-decorator
export function Attribute(dataType: DataType, options?: AttributeOptions): (target: any, propertyKey: PropertyKey) => void;
}

declare module '@eggjs/core' {
interface EggAppConfig {
orm: OrmConfig & {
datasources?: OrmConfig[];
};
}

interface EggCore {
leoricRegister: LeoricRegister;
orm: Orm;
}
}
26 changes: 13 additions & 13 deletions plugin/orm/lib/LeoricRegister.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Base } from 'sdk-base';
import { ModelProtoManager } from './ModelProtoManager';
import { DataSourceManager, OrmConfig } from './DataSourceManager';
import Realm, { hookNames } from 'leoric';
import Realm from 'leoric';
import { ModelMetadata, ModelMetadataUtil } from '@eggjs/tegg-orm-decorator';

import { ModelProtoManager } from './ModelProtoManager.js';
import { DataSourceManager, OrmConfig } from './DataSourceManager.js';
import type { RealmType } from './types.js';
export class LeoricRegister extends Base {
private readonly modelProtoManager: ModelProtoManager;
private readonly dataSourceManager: DataSourceManager;
readonly realmMap: Map<string, any>;
readonly realmMap: Map<string, RealmType>;

constructor(modelProtoManager: ModelProtoManager, dataSourceManager: DataSourceManager) {
super();
Expand All @@ -26,7 +26,7 @@ export class LeoricRegister extends Base {
return config;
}

getRealm(config: OrmConfig | undefined): Realm | undefined {
getRealm(config: OrmConfig | undefined): RealmType | undefined {
if (!config?.database) {
return undefined;
}
Expand All @@ -36,20 +36,20 @@ export class LeoricRegister extends Base {

getOrCreateRealm(datasource: string | undefined): any {
const config = this.getConfig(datasource);
let realm: Realm | undefined;
let realm: RealmType | undefined;
if (config) {
realm = this.getRealm(config);
if (realm) {
return realm;
}
}
realm = new (Realm as any)({ ...config });
this.realmMap.set(config!.database, realm);
this.realmMap.set(config!.database, realm!);
return realm;
}

generateLeoricAttributes(metadata: ModelMetadata) {
const attributes = {};
const attributes: Record<string, any> = {};
for (const attribute of metadata.attributes) {
attributes[attribute.propertyName] = {
columnName: attribute.attributeName,
Expand All @@ -71,10 +71,10 @@ export class LeoricRegister extends Base {
realm.models[clazz.name] = clazz;
realm[clazz.name] = clazz;
const attributes = this.generateLeoricAttributes(metadata);
const hooks = {};
for (const hookName of hookNames) {
if (clazz[hookName]) {
hooks[hookName] = clazz[hookName];
const hooks: Record<string, any> = {};
for (const hookName of Realm.hookNames) {
if (clazz[hookName as keyof typeof clazz]) {
hooks[hookName] = clazz[hookName as keyof typeof clazz];
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/orm/lib/ModelProtoHook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LifecycleHook } from '@eggjs/tegg';
import { EggPrototype, EggPrototypeLifecycleContext } from '@eggjs/tegg-metadata';
import { IS_MODEL, ModelMetaBuilder, ModelMetadataUtil } from '@eggjs/tegg-orm-decorator';
import { ModelProtoManager } from './ModelProtoManager';
import { ModelProtoManager } from './ModelProtoManager.js';

export class ModelProtoHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
private readonly modelProtoManager: ModelProtoManager;
Expand Down
2 changes: 1 addition & 1 deletion plugin/orm/lib/ORMLoadUnitHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
LoadUnit,
LoadUnitLifecycleContext,
} from '@eggjs/tegg-metadata';
import { Orm } from './SingletonORM';
import { Orm } from './SingletonORM.js';

const REGISTER_CLAZZ = [
Orm,
Expand Down
2 changes: 1 addition & 1 deletion plugin/orm/lib/SingletonModelObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { EggPrototype } from '@eggjs/tegg-metadata';
import { EggPrototypeName, EggObjectName } from '@eggjs/tegg';
import { Id, IdenticalUtil } from '@eggjs/tegg-lifecycle';
import { Bone } from 'leoric';
import SingletonModelProto from './SingletonModelProto';
import { EGG_CONTEXT } from '@eggjs/egg-module-common';
import SingletonModelProto from './SingletonModelProto.js';

export class SingletonModelObject implements EggObject {
private status: EggObjectStatus = EggObjectStatus.PENDING;
Expand Down
1 change: 1 addition & 0 deletions plugin/orm/lib/SingletonModelProto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Id, IdenticalUtil } from '@eggjs/tegg-lifecycle';
import { Bone } from 'leoric';

export default class SingletonModelProto implements EggPrototype {
[key: symbol]: PropertyDescriptor;
private readonly qualifiers: QualifierInfo[];
readonly accessLevel = AccessLevel.PUBLIC;
id: Id;
Expand Down
8 changes: 4 additions & 4 deletions plugin/orm/lib/SingletonORM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
Inject,
SingletonProto,
} from '@eggjs/tegg';
import { LeoricRegister } from './LeoricRegister';
import Realm from 'leoric';
import { LeoricRegister } from './LeoricRegister.js';
import type { RealmType } from './types.js';

@SingletonProto({
accessLevel: AccessLevel.PUBLIC,
Expand All @@ -14,12 +14,12 @@ export class Orm {
private leoricRegister: LeoricRegister;

// default dataSource
get client(): Realm {
get client(): RealmType {
const defaultConfig = this.leoricRegister.getConfig();
return this.leoricRegister.getRealm(defaultConfig)!;
}

getClient(datasource: string): Realm {
getClient(datasource: string): RealmType {
const config = this.leoricRegister.getConfig(datasource);
if (!config) {
throw new Error(`not found ${datasource} datasource`);
Expand Down
4 changes: 4 additions & 0 deletions plugin/orm/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { AbstractDriver, connect } from 'leoric';

export type DataType = AbstractDriver['DataType'];
export type RealmType = Awaited<ReturnType<typeof connect>>;
44 changes: 25 additions & 19 deletions plugin/orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,26 @@
"lib/**/*.d.ts",
"app/**/*.js",
"app/**/*.d.ts",
"typings/*.d.ts"
"index.js",
"index.d.ts"
],
"types": "typings/index.d.ts",
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./package.json": "./package.json"
},
"scripts": {
"test": "npm run prepare-test && cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
"pretest": "npm run prepare-test",
"test": "egg-bin test",
"precov": "npm run prepare-test",
"cov": "egg-bin cov",
"clean": "tsc -b --clean",
"tsc": "npm run clean && tsc -p ./tsconfig.json",
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
"prepublishOnly": "npm run tsc:pub",
"tsc": "npm run clean && tsc -p ./tsconfig.pub.json",
"tsc:pub": "npm run tsc",
"prepublishOnly": "npm run tsc",
"prepare-test": "node ./test/fixtures/prepare.js"
},
"homepage": "https://github.com/eggjs/tegg",
Expand All @@ -45,9 +56,10 @@
"directory": "plugin/orm"
},
"engines": {
"node": ">=14.0.0"
"node": ">=20.0.0"
},
"dependencies": {
"@eggjs/core": "^6.4.0",
"@eggjs/egg-module-common": "^3.52.0",
"@eggjs/tegg": "^3.52.0",
"@eggjs/tegg-common-util": "^3.52.0",
Expand All @@ -56,27 +68,21 @@
"@eggjs/tegg-metadata": "^3.52.0",
"@eggjs/tegg-orm-decorator": "^3.52.0",
"@eggjs/tegg-runtime": "^3.52.0",
"@types/koa-router": "^7.0.40",
"koa-compose": "^3.2.1",
"leoric": "^2.12.2",
"sdk-base": "^5.0.1"
},
"devDependencies": {
"@eggjs/module-test-util": "^3.52.0",
"@eggjs/router": "^3.0.5",
"@eggjs/tegg-config": "^3.52.0",
"@eggjs/tegg-plugin": "^3.52.0",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.4",
"cross-env": "^7.0.3",
"@types/mocha": "10",
"@types/node": "22",
"egg": "4",
"@eggjs/mock": "6",
"@eggjs/tracer": "^3.0.0",
"koa-router": "^8.0.8",
"mocha": "^10.2.0",
"mysql": "^2.18.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"@eggjs/tracer": "3",
"mysql2": "3",
"ts-node": "10",
"typescript": "5"
},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 6 additions & 4 deletions plugin/orm/test/fixtures/apps/orm-app/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Application } from 'egg';
import Realm from 'leoric';

// @ts-expect-error: the library definition is wrong
import { Logger } from 'leoric';
const Logger = Realm.Logger;

export default class OrmAppHook {
private readonly app: Application;
Expand All @@ -13,10 +15,10 @@ export default class OrmAppHook {
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) {
(realm.driver as any).logger = new Logger({
logQuery(sql: any, _: any, options: any) {
const path = options.Model?.ctx?.path;
app.logger.info('sql: %s path: %s', sql, path);
app.logger.warn('sql: %s path: %s', sql, path);
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = function () {
export default () => {
const config = {
keys: 'test key',
security: {
Expand All @@ -11,7 +9,7 @@ module.exports = function () {
orm: {
datasources: [
{
client: 'mysql',
client: 'mysql2',
database: 'test',
host: '127.0.0.1',
port: 3306,
Expand All @@ -26,7 +24,7 @@ module.exports = function () {
},
},
{
client: 'mysql',
client: 'mysql2',
database: 'apple',
host: '127.0.0.1',
port: 3306,
Expand All @@ -41,7 +39,7 @@ module.exports = function () {
},
},
{
client: 'mysql',
client: 'mysql2',
database: 'banana',
host: '127.0.0.1',
port: 3306,
Expand Down
14 changes: 0 additions & 14 deletions plugin/orm/test/fixtures/apps/orm-app/config/plugin.js

This file was deleted.

14 changes: 14 additions & 0 deletions plugin/orm/test/fixtures/apps/orm-app/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
tracer: {
package: '@eggjs/tracer',
enable: true,
},
tegg: {
package: '@eggjs/tegg-plugin',
enable: true,
},
teggConfig: {
package: '@eggjs/tegg-config',
enable: true,
},
};
Loading

0 comments on commit 65ce750

Please sign in to comment.