Skip to content

Commit

Permalink
feat: modify repository.extend method for chaining repositories (type…
Browse files Browse the repository at this point in the history
…orm#10256)

* modify repository.extend method. so chainning call this method is possible

* npm run format

* add @ts-ignore at a line

* Object.assign is no more needed

* change code to remove @ts-ignore
  • Loading branch information
hunjin-choi authored Aug 19, 2023
1 parent fbd45db commit ca29c0f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,20 +675,25 @@ export class Repository<Entity extends ObjectLiteral> {
* Extends repository with provided functions.
*/
extend<CustomRepository>(
custom: CustomRepository & ThisType<this & CustomRepository>,
customs: CustomRepository & ThisType<this & CustomRepository>,
): this & CustomRepository {
// return {
// ...this,
// ...custom
// };
const thisRepo = this.constructor as new (...args: any[]) => typeof this
const thisRepo: any = this.constructor
const { target, manager, queryRunner } = this
const cls = new (class extends thisRepo {})(
target,
manager,
queryRunner,
)
Object.assign(cls, custom)
return cls as any
const ChildClass = class extends thisRepo {
constructor(
target: EntityTarget<Entity>,
manager: EntityManager,
queryRunner?: QueryRunner,
) {
super(target, manager, queryRunner)
}
}
for (const custom in customs)
ChildClass.prototype[custom] = customs[custom]
return new ChildClass(target, manager, queryRunner) as any
}
}

0 comments on commit ca29c0f

Please sign in to comment.