mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-03 16:46:00 +00:00
trigger repository-find-[one|many]-[before|after] based on limit (#160)
This commit is contained in:
@@ -266,5 +266,12 @@ describe("[data] Repository (Events)", async () => {
|
|||||||
expect(events.has(RepositoryEvents.RepositoryFindManyBefore.slug)).toBeTrue();
|
expect(events.has(RepositoryEvents.RepositoryFindManyBefore.slug)).toBeTrue();
|
||||||
expect(events.has(RepositoryEvents.RepositoryFindManyAfter.slug)).toBeTrue();
|
expect(events.has(RepositoryEvents.RepositoryFindManyAfter.slug)).toBeTrue();
|
||||||
events.clear();
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -302,24 +302,38 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async triggerFindBefore(entity: Entity, options: RepoQuery): Promise<void> {
|
||||||
|
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<void> {
|
||||||
|
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(
|
protected async single(
|
||||||
qb: RepositoryQB,
|
qb: RepositoryQB,
|
||||||
options: RepoQuery,
|
options: RepoQuery,
|
||||||
): Promise<RepositoryResponse<EntityData>> {
|
): Promise<RepositoryResponse<EntityData>> {
|
||||||
await this.emgr.emit(
|
this.triggerFindBefore(this.entity, options);
|
||||||
new Repository.Events.RepositoryFindOneBefore({ entity: this.entity, options }),
|
|
||||||
);
|
|
||||||
|
|
||||||
const { data, ...response } = await this.performQuery(qb);
|
const { data, ...response } = await this.performQuery(qb);
|
||||||
|
|
||||||
await this.emgr.emit(
|
this.triggerFindAfter(this.entity, options, data);
|
||||||
new Repository.Events.RepositoryFindOneAfter({
|
|
||||||
entity: this.entity,
|
|
||||||
options,
|
|
||||||
data: data[0]!,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
return { ...response, data: data[0]! };
|
return { ...response, data: data[0]! };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -425,21 +439,11 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
|
|||||||
|
|
||||||
async findMany(_options?: Partial<RepoQuery>): Promise<RepositoryResponse<TBD[TB][]>> {
|
async findMany(_options?: Partial<RepoQuery>): Promise<RepositoryResponse<TBD[TB][]>> {
|
||||||
const { qb, options } = this.buildQuery(_options);
|
const { qb, options } = this.buildQuery(_options);
|
||||||
|
this.triggerFindBefore(this.entity, options);
|
||||||
await this.emgr.emit(
|
|
||||||
new Repository.Events.RepositoryFindManyBefore({ entity: this.entity, options }),
|
|
||||||
);
|
|
||||||
|
|
||||||
const res = await this.performQuery(qb);
|
const res = await this.performQuery(qb);
|
||||||
|
|
||||||
await this.emgr.emit(
|
this.triggerFindAfter(this.entity, options, res.data);
|
||||||
new Repository.Events.RepositoryFindManyAfter({
|
|
||||||
entity: this.entity,
|
|
||||||
options,
|
|
||||||
data: res.data,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
return res as any;
|
return res as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user