UNPKG

undeexcepturi

Version:

TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.

39 lines (29 loc) 1.22 kB
import type { EntityName, EventSubscriber, FlushEventArgs } from '@mikro-orm/core'; import type { SqlEntityManager } from '@mikro-orm/knex'; import { Test2 } from '../entities-sql'; export class Test2Subscriber implements EventSubscriber<Test2> { static readonly log: [string, FlushEventArgs][] = []; getSubscribedEntities(): EntityName<Test2>[] { return [Test2]; } private async fireQuery(method: string, args: FlushEventArgs): Promise<void> { if (!args.uow.getChangeSets().some(cs => cs.entity instanceof Test2)) { return; } const em = args.em as SqlEntityManager; // test we can run queries via QB await em.createQueryBuilder(Test2).where({ name: '' + Math.random() }).execute('all'); // test we can run queries via EM that touch context await em.findOne(Test2, { name: '' + Math.random() }); Test2Subscriber.log.push([method, args]); } async beforeFlush(args: FlushEventArgs): Promise<void> { await this.fireQuery('beforeFlush', args); } async onFlush(args: FlushEventArgs): Promise<void> { await this.fireQuery('onFlush', args); } async afterFlush(args: FlushEventArgs): Promise<void> { await this.fireQuery('afterFlush', args); } }