Skip to content

Commit

Permalink
fix: populate typing
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Aug 30, 2022
1 parent 7a94d09 commit b0e163e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
10 changes: 7 additions & 3 deletions source/orm/orm.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ModuleMetadata } from '@bechara/nestjs-core';
import { EntityData, EventArgs, FilterQuery, FindOptions, Populate } from '@mikro-orm/core';
import { EntityData, EventArgs, FilterQuery, FindOptions } from '@mikro-orm/core';
import { AutoPath } from '@mikro-orm/core/typings';
import { MikroOrmModuleOptions } from '@mikro-orm/nestjs';

import { SchemaModuleOptions } from '../schema/schema.interface';
Expand Down Expand Up @@ -60,17 +61,20 @@ export interface OrmRunWithinContextParams<T> {
operation: () => Promise<T>;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export interface OrmReadOptions<Entity, P extends string> extends FindOptions<Entity, P> {
populate?: AutoPath<Entity, P>[] | boolean | string[];
findOrFail?: boolean;
}

export interface OrmUpsertOptions<Entity, P extends string> {
populate?: Populate<Entity, P>;
populate?: AutoPath<Entity, P>[] | boolean | string[];
uniqueKey?: (keyof Entity)[];
disallowUpdate?: boolean;
disallowRetry?: boolean;
}

export interface OrmDeleteOptions<Entity, P extends string> {
populate?: Populate<Entity, P>;
populate?: AutoPath<Entity, P>[] | boolean | string[];
}
4 changes: 2 additions & 2 deletions source/orm/orm.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('OrmModule', () => {
},
]);

const populate: any = [ 'address' ];
const populate = [ 'address' ];
const [ user1 ] = await userRepository.readBy({ name: 'ONE_TO_ONE_NESTED_1' }, { populate });
const [ user2 ] = await userRepository.readBy({ name: 'ONE_TO_ONE_NESTED_2' }, { populate });

Expand Down Expand Up @@ -296,7 +296,7 @@ describe('OrmModule', () => {
uniqueKey: [ 'id' ],
});

const populate: any = [ 'user', 'products' ];
const populate = [ 'user', 'products' ];
const order1 = await orderRepository.readByIdOrFail('UPSERT_MN_ORDER_1', { populate });
const order2 = await orderRepository.readByIdOrFail('UPSERT_MN_ORDER_2', { populate });
const order3 = await orderRepository.readByIdOrFail('UPSERT_MN_ORDER_3', { populate });
Expand Down
7 changes: 3 additions & 4 deletions source/orm/orm.repository/orm.repository.read.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConflictException, NotFoundException } from '@bechara/nestjs-core';
import { EntityManager, EntityName } from '@mikro-orm/core';
import { EntityManager, EntityName, FindOptions } from '@mikro-orm/core';

import { OrmPagination } from '../orm.dto';
import { OrmReadOptions, OrmReadPaginatedParams, OrmReadParams, OrmRepositoryOptions } from '../orm.interface';
Expand Down Expand Up @@ -34,7 +34,7 @@ export abstract class OrmReadRepository<Entity> extends OrmBaseRepository<Entity
options.populate ??= this.repositoryOptions.defaultPopulate as any ?? false;

try {
readEntities = await this.entityManager.find(this.entityName, params, options);
readEntities = await this.entityManager.find(this.entityName, params, options as FindOptions<Entity, P>);
readEntities ??= [ ];
}
catch (e) {
Expand Down Expand Up @@ -142,12 +142,11 @@ export abstract class OrmReadRepository<Entity> extends OrmBaseRepository<Entity
* @param params
*/
public async readPaginatedBy(params: OrmReadPaginatedParams<Entity>): Promise<OrmPagination<Entity>> {
const { limit: bLimit, offset: bOffset, count: hasCount, sort, order, populate: bPopulate, ...remainder } = params;
const { limit: bLimit, offset: bOffset, count: hasCount, sort, order, populate, ...remainder } = params;

const limit = bLimit ?? 100;
const offset = bOffset ?? 0;
const orderBy = sort && order ? [ { [sort]: order } ] : undefined;
const populate = bPopulate as any;

const readParams: OrmReadParams<Entity> = remainder as any;
const readPromise = this.readBy(readParams, { orderBy, limit, offset, populate });
Expand Down
3 changes: 2 additions & 1 deletion test/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export class UserController {
response: { type: User },
})
public getById(@Param('id') id: string): Promise<User> {
return this.userRepository.readByIdOrFail(id, { populate: [ 'address' ] });
const populate = [ 'address' ];
return this.userRepository.readByIdOrFail(id, { populate });
}

@Post({
Expand Down

0 comments on commit b0e163e

Please sign in to comment.