Skip to content

Commit

Permalink
fix(core): fix pull request review
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhussein committed May 13, 2020
1 parent 5163af1 commit e9b0066
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/entity/EntityFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ export class EntityFactory {
/**
* denormalize PK to value required by driver (e.g. ObjectId)
*/
private denormalizePrimaryKey<T extends AnyEntity<T>>(data: EntityData<T>, primaryKey: string, metadata: EntityProperty ): void {
private denormalizePrimaryKey<T extends AnyEntity<T>>(data: EntityData<T>, primaryKey: string, prop: EntityProperty<T> ): void {
const platform = this.driver.getPlatform();
const pk = platform.getSerializedPrimaryKeyField(primaryKey);

if (Utils.isDefined(data[pk], true) || Utils.isDefined(data[primaryKey], true)) {
let id = data[pk] || data[primaryKey];
if(metadata.type.toLowerCase() === 'objectid') {

if(prop.type.toLowerCase() === 'objectid') {
id = platform.denormalizePrimaryKey(id);
}
delete data[pk];
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/entity/EntityRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ export class EntityRepository<T extends AnyEntity<T>> {
}

async findOne(where: FilterQuery<T>, populate?: string[] | boolean, orderBy?: QueryOrderMap): Promise<T | null>;
async findOne(where: FilterQuery<T>, populate?: FindOneOptions<T>, orderBy?: QueryOrderMap): Promise<T | null>;
async findOne(where: FilterQuery<T>, populate: string[] | boolean | FindOneOptions<T> = [], orderBy?: QueryOrderMap): Promise<T | null> {
async findOne(where: FilterQuery<T>, populate?: FindOneOptions, orderBy?: QueryOrderMap): Promise<T | null>;
async findOne(where: FilterQuery<T>, populate: string[] | boolean | FindOneOptions = [], orderBy?: QueryOrderMap): Promise<T | null> {
return this.em.findOne<T>(this.entityName, where, populate as string[], orderBy);
}

async findOneOrFail(where: FilterQuery<T>, populate?: string[] | boolean, orderBy?: QueryOrderMap): Promise<T>;
async findOneOrFail(where: FilterQuery<T>, populate?: FindOneOrFailOptions<T>, orderBy?: QueryOrderMap): Promise<T>;
async findOneOrFail(where: FilterQuery<T>, populate: string[] | boolean | FindOneOrFailOptions<T> = [], orderBy?: QueryOrderMap): Promise<T> {
async findOneOrFail(where: FilterQuery<T>, populate?: FindOneOrFailOptions, orderBy?: QueryOrderMap): Promise<T>;
async findOneOrFail(where: FilterQuery<T>, populate: string[] | boolean | FindOneOrFailOptions = [], orderBy?: QueryOrderMap): Promise<T> {
return this.em.findOneOrFail<T>(this.entityName, where, populate as string[], orderBy);
}

async find(where: FilterQuery<T>, options?: FindOptions<T>): Promise<T[]>;
async find(where: FilterQuery<T>, options?: FindOptions): Promise<T[]>;
async find(where: FilterQuery<T>, populate?: string[] | boolean, orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<T[]>;
async find(where: FilterQuery<T>, populate: string[] | boolean | FindOptions<T> = [], orderBy: QueryOrderMap = {}, limit?: number, offset?: number): Promise<T[]> {
async find(where: FilterQuery<T>, populate: string[] | boolean | FindOptions = [], orderBy: QueryOrderMap = {}, limit?: number, offset?: number): Promise<T[]> {
return this.em.find<T>(this.entityName, where as FilterQuery<T>, populate as string[], orderBy, limit, offset);
}

async findAndCount(where: FilterQuery<T>, options?: FindOptions<T>): Promise<[T[], number]>;
async findAndCount(where: FilterQuery<T>, options?: FindOptions): Promise<[T[], number]>;
async findAndCount(where: FilterQuery<T>, populate?: string[] | boolean, orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<[T[], number]>;
async findAndCount(where: FilterQuery<T>, populate: string[] | boolean | FindOptions<T> = [], orderBy: QueryOrderMap = {}, limit?: number, offset?: number): Promise<[T[], number]> {
async findAndCount(where: FilterQuery<T>, populate: string[] | boolean | FindOptions = [], orderBy: QueryOrderMap = {}, limit?: number, offset?: number): Promise<[T[], number]> {
return this.em.findAndCount<T>(this.entityName, where as FilterQuery<T>, populate as string[], orderBy, limit, offset);
}

async findAll(options?: FindOptions<T>): Promise<T[]>;
async findAll(options?: FindOptions): Promise<T[]>;
async findAll(populate?: string[] | boolean | true, orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<T[]>;
async findAll(populate: string[] | boolean | true | FindOptions<T> = [], orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<T[]> {
async findAll(populate: string[] | boolean | true | FindOptions = [], orderBy?: QueryOrderMap, limit?: number, offset?: number): Promise<T[]> {
return this.em.find<T>(this.entityName, {}, populate as string[], orderBy, limit, offset);
}

Expand Down
1 change: 1 addition & 0 deletions packages/mongodb/src/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export class MongoDriver extends DatabaseDriver<MongoConnection> {

private buildFilterById<T extends AnyEntity<T>>(entityName: string, id: string): FilterQuery<T> {
const meta = this.metadata.get(entityName);

if (meta.properties[meta.primaryKeys[0]].type.toLowerCase() === 'objectid') {
return { _id: new ObjectId(id) } as FilterQuery<T>;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/entities-sql/Author2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Author2 extends BaseEntity2 {
address?: Address2;

@ManyToMany({ entity: () => Author2, pivotTable: 'author_to_friend' })
friends: Collection<Author2> = new Collection<Author2>(this);
friends = new Collection<Author2>(this);

@ManyToMany(() => Author2)
following: Collection<Author2> = new Collection<Author2>(this);
Expand Down

0 comments on commit e9b0066

Please sign in to comment.