Skip to content

Commit

Permalink
feat(core): expose populate parameter in wrap(e).init()
Browse files Browse the repository at this point in the history
Closes #814
  • Loading branch information
B4nan committed Sep 5, 2020
1 parent 57652a3 commit d33432a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions 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';
Expand All @@ -12,15 +12,15 @@ import { WrappedEntity } from './WrappedEntity';

export class EntityHelper {

static async init<T extends AnyEntity<T>>(entity: T, populated = true, lockMode?: LockMode): Promise<T> {
static async init<T extends AnyEntity<T>, P extends Populate<T> = Populate<T>>(entity: T, populated = true, populate?: P, lockMode?: LockMode): Promise<T> {
const wrapped = entity.__helper!;
const em = wrapped.__em;

if (!em) {
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;

Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/entity/WrappedEntity.ts
Expand Up @@ -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<T extends AnyEntity<T>, PK extends keyof T> {

Expand Down Expand Up @@ -64,8 +65,8 @@ export class WrappedEntity<T extends AnyEntity<T>, PK extends keyof T> {
return EntityAssigner.assign(this.entity, data, options);
}

init(populated = true): Promise<T> {
return EntityHelper.init<T>(this.entity, populated) as Promise<T>;
init<P extends Populate<T> = Populate<T>>(populated = true, populate?: P, lockMode?: LockMode): Promise<T> {
return EntityHelper.init<T>(this.entity, populated, populate, lockMode) as Promise<T>;
}

get __primaryKey(): Primary<T> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/typings.ts
Expand Up @@ -66,7 +66,7 @@ export type QBFilterQuery<T = any> = FilterQuery<T> & Dictionary | FilterQuery<T
export interface IWrappedEntity<T extends AnyEntity<T>, PK extends keyof T, P = never> {
isInitialized(): boolean;
populated(populated?: boolean): void;
init(populated?: boolean, lockMode?: LockMode): Promise<T>;
init<P extends Populate<T> = Populate<T>>(populated?: boolean, populate?: P, lockMode?: LockMode): Promise<T>;
toReference<PK2 extends PK = never, P2 extends P = never>(): IdentifiedReference<T, PK2> & LoadedReference<T, P2>;
toObject(ignoreFields?: string[]): Dictionary;
toJSON(...args: any[]): Dictionary;
Expand Down

0 comments on commit d33432a

Please sign in to comment.