From 3e58a17769a8a283daeac07a3100e2fa8c36c1d4 Mon Sep 17 00:00:00 2001 From: dswbx Date: Thu, 24 Apr 2025 04:59:25 -0700 Subject: [PATCH] trigger `repository-find-[one|many]-[before|after]` based on `limit` (#160) --- app/__test__/data/specs/Repository.spec.ts | 7 +++ app/src/data/entities/query/Repository.ts | 52 ++++++++++++---------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/app/__test__/data/specs/Repository.spec.ts b/app/__test__/data/specs/Repository.spec.ts index f50ca837..54254d45 100644 --- a/app/__test__/data/specs/Repository.spec.ts +++ b/app/__test__/data/specs/Repository.spec.ts @@ -266,5 +266,12 @@ describe("[data] Repository (Events)", async () => { expect(events.has(RepositoryEvents.RepositoryFindManyBefore.slug)).toBeTrue(); expect(events.has(RepositoryEvents.RepositoryFindManyAfter.slug)).toBeTrue(); events.clear(); + + // check find one on findMany with limit 1 + await repo.findMany({ where: { id: 1 }, limit: 1 }); + await repo.emgr.executeAsyncs(); + expect(events.has(RepositoryEvents.RepositoryFindOneBefore.slug)).toBeTrue(); + expect(events.has(RepositoryEvents.RepositoryFindOneAfter.slug)).toBeTrue(); + events.clear(); }); }); diff --git a/app/src/data/entities/query/Repository.ts b/app/src/data/entities/query/Repository.ts index d16b7b12..35473ec3 100644 --- a/app/src/data/entities/query/Repository.ts +++ b/app/src/data/entities/query/Repository.ts @@ -302,24 +302,38 @@ export class Repository { + const event = + options.limit === 1 + ? Repository.Events.RepositoryFindOneBefore + : Repository.Events.RepositoryFindManyBefore; + await this.emgr.emit(new event({ entity, options })); + } + + private async triggerFindAfter( + entity: Entity, + options: RepoQuery, + data: EntityData[], + ): Promise { + if (options.limit === 1) { + await this.emgr.emit( + new Repository.Events.RepositoryFindOneAfter({ entity, options, data: data[0]! }), + ); + } else { + await this.emgr.emit( + new Repository.Events.RepositoryFindManyAfter({ entity, options, data }), + ); + } + } + protected async single( qb: RepositoryQB, options: RepoQuery, ): Promise> { - await this.emgr.emit( - new Repository.Events.RepositoryFindOneBefore({ entity: this.entity, options }), - ); - + this.triggerFindBefore(this.entity, options); const { data, ...response } = await this.performQuery(qb); - await this.emgr.emit( - new Repository.Events.RepositoryFindOneAfter({ - entity: this.entity, - options, - data: data[0]!, - }), - ); - + this.triggerFindAfter(this.entity, options, data); return { ...response, data: data[0]! }; } @@ -425,21 +439,11 @@ export class Repository): Promise> { const { qb, options } = this.buildQuery(_options); - - await this.emgr.emit( - new Repository.Events.RepositoryFindManyBefore({ entity: this.entity, options }), - ); + this.triggerFindBefore(this.entity, options); const res = await this.performQuery(qb); - await this.emgr.emit( - new Repository.Events.RepositoryFindManyAfter({ - entity: this.entity, - options, - data: res.data, - }), - ); - + this.triggerFindAfter(this.entity, options, res.data); return res as any; }