diff --git a/packages/core/src/entity/EntityHelper.ts b/packages/core/src/entity/EntityHelper.ts index 55d452e54168..6b157cbf5a78 100644 --- a/packages/core/src/entity/EntityHelper.ts +++ b/packages/core/src/entity/EntityHelper.ts @@ -1,7 +1,7 @@ import { inspect } from 'util'; import { EntityManager } from '../EntityManager'; -import { AnyEntity, Dictionary, EntityMetadata, EntityProperty } from '../typings'; +import { AnyEntity, Dictionary, EntityMetadata, EntityProperty, Populate } from '../typings'; import { EntityTransformer } from './EntityTransformer'; import { LockMode } from '../unit-of-work'; import { Reference } from './Reference'; @@ -12,7 +12,7 @@ import { WrappedEntity } from './WrappedEntity'; export class EntityHelper { - static async init>(entity: T, populated = true, lockMode?: LockMode): Promise { + static async init, P extends Populate = Populate>(entity: T, populated = true, populate?: P, lockMode?: LockMode): Promise { const wrapped = entity.__helper!; const em = wrapped.__em; @@ -20,7 +20,7 @@ export class EntityHelper { throw ValidationError.entityNotManaged(entity); } - await em.findOne(entity.constructor.name, entity, { refresh: true, lockMode }); + await em.findOne(entity.constructor.name, entity, { refresh: true, lockMode, populate }); wrapped.populated(populated); wrapped.__lazyInitialized = true; diff --git a/packages/core/src/entity/WrappedEntity.ts b/packages/core/src/entity/WrappedEntity.ts index dae13931120a..0e082bb555ff 100644 --- a/packages/core/src/entity/WrappedEntity.ts +++ b/packages/core/src/entity/WrappedEntity.ts @@ -3,12 +3,13 @@ import { EntityManager } from '../EntityManager'; import { Platform } from '../platforms'; import { MetadataStorage } from '../metadata'; import { EntityValidator } from './EntityValidator'; -import { AnyEntity, Dictionary, EntityData, EntityMetadata, Primary } from '../typings'; +import { AnyEntity, Dictionary, EntityData, EntityMetadata, Populate, Primary } from '../typings'; import { IdentifiedReference, Reference } from './Reference'; import { EntityTransformer } from './EntityTransformer'; import { AssignOptions, EntityAssigner } from './EntityAssigner'; import { EntityHelper } from './EntityHelper'; import { Utils } from '../utils'; +import { LockMode } from '../unit-of-work'; export class WrappedEntity, PK extends keyof T> { @@ -64,8 +65,8 @@ export class WrappedEntity, PK extends keyof T> { return EntityAssigner.assign(this.entity, data, options); } - init(populated = true): Promise { - return EntityHelper.init(this.entity, populated) as Promise; + init

= Populate>(populated = true, populate?: P, lockMode?: LockMode): Promise { + return EntityHelper.init(this.entity, populated, populate, lockMode) as Promise; } get __primaryKey(): Primary { diff --git a/packages/core/src/typings.ts b/packages/core/src/typings.ts index d89477647a57..d913614d7862 100644 --- a/packages/core/src/typings.ts +++ b/packages/core/src/typings.ts @@ -66,7 +66,7 @@ export type QBFilterQuery = FilterQuery & Dictionary | FilterQuery, PK extends keyof T, P = never> { isInitialized(): boolean; populated(populated?: boolean): void; - init(populated?: boolean, lockMode?: LockMode): Promise; + init

= Populate>(populated?: boolean, populate?: P, lockMode?: LockMode): Promise; toReference(): IdentifiedReference & LoadedReference; toObject(ignoreFields?: string[]): Dictionary; toJSON(...args: any[]): Dictionary;