Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/example-todo/test/acceptance/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ describe('Application', () => {
}

async function givenTodoRepository() {
// TODO(bajtos) enhance RepositoryMixin to provide repository getter
// Example usage:
// todoRepo = await app.getRepository<TodoRepository>(TodoRepository.name)
// See https://github.com/strongloop/loopback-next/issues/745
todoRepo = await app.get<TodoRepository>('repositories.TodoRepository');
todoRepo = await app.getRepository(TodoRepository);
}

async function givenTodoInstance(todo?: Partial<Todo>) {
Expand Down
14 changes: 14 additions & 0 deletions packages/repository/src/repository-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ export function RepositoryMixin<T extends Class<any>>(superClass: T) {
.tag('repository');
}

/**
* Retrieve the repository instance from the given Repository class
*
* @param repo The repository class to retrieve the instance of
*/
// tslint:disable-next-line:no-any
async getRepository<R extends Repository<any>>(repo: Class<R>): Promise<R> {
return await this.get(`repositories.${repo.name}`);
}

/**
* Add the dataSource to this application.
*
Expand Down Expand Up @@ -139,5 +149,9 @@ export function RepositoryMixin<T extends Class<any>>(superClass: T) {
export interface AppWithRepository extends Application {
// tslint:disable-next-line:no-any
repository(repo: Class<any>): void;
// tslint:disable-next-line:no-any
getRepository<R extends Repository<any>>(repo: Class<R>): Promise<R>;
dataSource(dataSource: juggler.DataSource, name?: string): void;
component(component: Class<{}>): void;
mountComponentRepository(component: Class<{}>): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ describe('RepositoryMixin', () => {
expectNoteRepoToBeBound(myApp);
});

it('mixed class has .getRepository()', () => {
const myApp = new AppWithRepoMixin();
expect(typeof myApp.getRepository).to.eql('function');
});

it('gets repository instance from app.getRepository()', async () => {
const myApp = new AppWithRepoMixin();
myApp
.bind('repositories.NoteRepo')
.toClass(NoteRepo)
.tag('repository');

const repoInstance = await myApp.getRepository(NoteRepo);

expect(repoInstance).to.be.instanceOf(NoteRepo);
});

it('binds user defined component without repository', () => {
class EmptyTestComponent {}

Expand Down