Skip to content

Commit

Permalink
feat(core): add EntityOptions.repository shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Nov 2, 2022
1 parent 4afde28 commit 2cbb129
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/decorators/Entity.ts
Expand Up @@ -32,4 +32,6 @@ export type EntityOptions<T> = {
// also return type is unknown as it can be either QB instance (which we cannot type here) or array of POJOs (e.g. for mongodb)
expression?: string | ((em: any, where: FilterQuery<T>, options: FindOptions<T, any>) => object);
customRepository?: () => Constructor;
/** shortcut for `customRepository` */
repository?: () => Constructor;
};
4 changes: 4 additions & 0 deletions packages/core/src/typings.ts
Expand Up @@ -363,6 +363,10 @@ export class EntityMetadata<T = any> {
this.collection = name;
}

set repository(repo: () => Constructor<EntityRepository<any>>) {
this.customRepository = repo;
}

sync(initIndexes = false) {
this.root ??= this;
const props = Object.values<EntityProperty<T>>(this.properties).sort((a, b) => this.propertyOrder.get(a.name)! - this.propertyOrder.get(b.name)!);
Expand Down
2 changes: 1 addition & 1 deletion tests/entities/Author.ts
Expand Up @@ -8,7 +8,7 @@ import { Book } from './Book';
import { AuthorRepository } from '../repositories/AuthorRepository';
import { BaseEntity } from './BaseEntity';

@Entity({ customRepository: () => AuthorRepository })
@Entity({ repository: () => AuthorRepository })
@Index({ name: 'custom_idx_1', properties: ['name', 'email'] })
@Filter({
name: 'withoutParams1',
Expand Down
2 changes: 1 addition & 1 deletion tests/entities/Book.ts
Expand Up @@ -6,7 +6,7 @@ import { BookTag } from './book-tag';
import { BaseEntity3 } from './BaseEntity3';
import { BookRepository } from '../repositories/BookRepository';

@Entity({ tableName: 'books-table', customRepository: () => BookRepository })
@Entity({ tableName: 'books-table', repository: () => BookRepository })
@Unique({ properties: ['title', 'author'] })
@Index({ properties: 'title', type: 'fulltext' })
@Index({ options: { point: '2dsphere', title: -1 } })
Expand Down
2 changes: 1 addition & 1 deletion tests/issues/GH1231.test.ts
Expand Up @@ -2,7 +2,7 @@ import { Cascade, Collection, Entity, EntityRepositoryType, ManyToOne, MikroORM,
import type { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { EntityRepository } from '@mikro-orm/postgresql';

@Entity({ tableName: 'teachers', customRepository: () => TeacherRepository })
@Entity({ tableName: 'teachers', repository: () => TeacherRepository })
class Teacher {

constructor(firstName: string, lastName: string) {
Expand Down
10 changes: 5 additions & 5 deletions tests/issues/GH2647.test.ts
Expand Up @@ -13,7 +13,7 @@ class ParticipantRepository extends EntityRepository<Participant> {

}

@Entity({ customRepository: () => ProviderRepository })
@Entity({ repository: () => ProviderRepository })
export class Provider {

[EntityRepositoryType]?: ProviderRepository;
Expand All @@ -27,7 +27,7 @@ export class Provider {

}

@Entity({ customRepository: () => UserRepository })
@Entity({ repository: () => UserRepository })
export class User {

[EntityRepositoryType]?: UserRepository;
Expand All @@ -41,7 +41,7 @@ export class User {

}

@Entity({ customRepository: () => MemberRepository })
@Entity({ repository: () => MemberRepository })
export class Member {

[EntityRepositoryType]?: MemberRepository;
Expand All @@ -59,7 +59,7 @@ export class Member {

}

@Entity({ customRepository: () => SessionRepository })
@Entity({ repository: () => SessionRepository })
export class Session {

[EntityRepositoryType]?: SessionRepository;
Expand All @@ -80,7 +80,7 @@ export class Session {

}

@Entity({ customRepository: () => ParticipantRepository })
@Entity({ repository: () => ParticipantRepository })
export class Participant {

[EntityRepositoryType]?: ParticipantRepository;
Expand Down

0 comments on commit 2cbb129

Please sign in to comment.