Skip to content

Commit

Permalink
feat: support EventSubscriberModel with provide (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jun 14, 2021
1 parent f6f4af9 commit 05431d2
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 115 deletions.
29 changes: 23 additions & 6 deletions packages/orm/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
import { Configuration, listModule, Config } from '@midwayjs/decorator';
import {
Config,
Configuration,
getClassMetadata,
listModule,
} from '@midwayjs/decorator';
import {
Connection,
ConnectionOptions,
createConnection,
getConnection,
getRepository,
ConnectionOptions,
Connection,
} from 'typeorm';
import {
CONNECTION_KEY,
ENTITY_MODEL_KEY,
EVENT_SUBSCRIBER_KEY,
CONNECTION_KEY,
ORM_MODEL_KEY,
} from '.';
import { ORM_HOOK_KEY, OrmConnectionHook } from './hook';
Expand All @@ -31,8 +36,7 @@ export class OrmConfiguration implements ILifeCycle {
ORM_MODEL_KEY,
(key: { modelKey; connectionName }) => {
// return getConnection(key.connectionName).getRepository(key.modelKey);
const repo = getRepository(key.modelKey, key.connectionName);
return repo;
return getRepository(key.modelKey, key.connectionName);
}
);

Expand Down Expand Up @@ -68,6 +72,19 @@ export class OrmConfiguration implements ILifeCycle {
}
return getConnection(instanceName);
});

// get event model
const eventModules = listModule(EVENT_SUBSCRIBER_KEY);
for (const eventModule of eventModules) {
const eventModuleMetadata = getClassMetadata(
EVENT_SUBSCRIBER_KEY,
eventModule
);
const module = await container.getAsync(eventModule);
getConnection(
eventModuleMetadata.connectionName || 'default'
).subscribers.push(module);
}
}

async onStop(container: IMidwayContainer) {
Expand Down
14 changes: 6 additions & 8 deletions packages/orm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'typeorm';
import { ViewEntityOptions } from 'typeorm/decorator/options/ViewEntityOptions';
import { saveModule, attachClassMetadata } from '@midwayjs/core';
import { saveClassMetadata } from '@midwayjs/decorator';

export const CONNECTION_KEY = 'orm:getConnection';
export const ENTITY_MODEL_KEY = 'entity_model_key';
Expand Down Expand Up @@ -135,15 +136,12 @@ export function InjectEntityModel(modelKey?: any, connectionName = 'default') {
* EventSubscriber - typeorm
* implements EntitySubscriberInterface
*/
export function EventSubscriberModel(): ClassDecorator {
export function EventSubscriberModel(
options: { connectionName?: string } = {}
): ClassDecorator {
return function (target) {
if (typeof target === 'function') {
saveModule(EVENT_SUBSCRIBER_KEY, target);
} else {
saveModule(EVENT_SUBSCRIBER_KEY, (target as any).constructor);
}

getMetadataArgsStorage().entitySubscribers.push({ target });
saveModule(EVENT_SUBSCRIBER_KEY, target);
saveClassMetadata(EVENT_SUBSCRIBER_KEY, options, target);
};
}

Expand Down
107 changes: 54 additions & 53 deletions packages/orm/test/fixtures/base-fn-multiple-db/src/event.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
import { EventSubscriberModel } from '../../../../src';
import { EntitySubscriberInterface, InsertEvent, UpdateEvent, RemoveEvent } from 'typeorm';
import { Provide } from '@midwayjs/decorator';


@EventSubscriberModel()
@Provide()
@EventSubscriberModel({connectionName: 'test'})
export class EverythingSubscriber implements EntitySubscriberInterface {
/**
* Called before entity insertion.
*/
beforeInsert(event: InsertEvent<any>) {
console.log(`BEFORE ENTITY INSERTED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeUpdate(event: UpdateEvent<any>) {
console.log(`BEFORE ENTITY UPDATED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeRemove(event: RemoveEvent<any>) {
console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}
/**
* Called after entity insertion.
*/
afterInsert(event: InsertEvent<any>) {
console.log(`AFTER ENTITY INSERTED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterUpdate(event: UpdateEvent<any>) {
console.log(`AFTER ENTITY UPDATED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterRemove(event: RemoveEvent<any>) {
console.log(`AFTER ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}

/**
* Called after entity is loaded.
*/
afterLoad(entity: any) {
console.log(`AFTER ENTITY LOADED: `, entity);
}

}

/**
* Called before entity insertion.
*/
beforeInsert(event: InsertEvent<any>) {
console.log(`BEFORE ENTITY INSERTED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeUpdate(event: UpdateEvent<any>) {
console.log(`BEFORE ENTITY UPDATED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeRemove(event: RemoveEvent<any>) {
console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterInsert(event: InsertEvent<any>) {
console.log(`AFTER ENTITY INSERTED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterUpdate(event: UpdateEvent<any>) {
console.log(`AFTER ENTITY UPDATED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterRemove(event: RemoveEvent<any>) {
console.log(`AFTER ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}

/**
* Called after entity is loaded.
*/
afterLoad(entity: any) {
console.log(`AFTER ENTITY LOADED: `, entity);
}

}
98 changes: 50 additions & 48 deletions packages/orm/test/fixtures/base-fn/src/event.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
import { EventSubscriberModel } from '../../../../src';
import { EntitySubscriberInterface, InsertEvent, UpdateEvent, RemoveEvent } from 'typeorm';
import { Provide } from '@midwayjs/decorator';

@Provide()
@EventSubscriberModel()
export class EverythingSubscriber implements EntitySubscriberInterface {

/**
* Called before entity insertion.
*/
beforeInsert(event: InsertEvent<any>) {
console.log(`BEFORE ENTITY INSERTED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeUpdate(event: UpdateEvent<any>) {
console.log(`BEFORE ENTITY UPDATED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeRemove(event: RemoveEvent<any>) {
console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterInsert(event: InsertEvent<any>) {
console.log(`AFTER ENTITY INSERTED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterUpdate(event: UpdateEvent<any>) {
console.log(`AFTER ENTITY UPDATED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterRemove(event: RemoveEvent<any>) {
console.log(`AFTER ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}

/**
* Called after entity is loaded.
*/
afterLoad(entity: any) {
console.log(`AFTER ENTITY LOADED: `, entity);
}
/**
* Called before entity insertion.
*/
beforeInsert(event: InsertEvent<any>) {
console.log(`BEFORE ENTITY INSERTED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeUpdate(event: UpdateEvent<any>) {
console.log(`BEFORE ENTITY UPDATED: `, event.entity);
}

/**
* Called before entity insertion.
*/
beforeRemove(event: RemoveEvent<any>) {
console.log(`BEFORE ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterInsert(event: InsertEvent<any>) {
console.log(`AFTER ENTITY INSERTED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterUpdate(event: UpdateEvent<any>) {
console.log(`AFTER ENTITY UPDATED: `, event.entity);
}

/**
* Called after entity insertion.
*/
afterRemove(event: RemoveEvent<any>) {
console.log(`AFTER ENTITY WITH ID ${event.entityId} REMOVED: `, event.entity);
}

/**
* Called after entity is loaded.
*/
afterLoad(entity: any) {
console.log(`AFTER ENTITY LOADED: `, entity);
}

}

0 comments on commit 05431d2

Please sign in to comment.