From 62459463b87afbe935b018c0664fef3f04295662 Mon Sep 17 00:00:00 2001 From: Taranveer Virk Date: Fri, 15 Sep 2017 15:32:56 -0400 Subject: [PATCH] docs(repository): explain usage of RepositoryMixin #425 --- packages/repository/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/repository/README.md b/packages/repository/README.md index f4807f3d3bf3..d8b25eac2e99 100644 --- a/packages/repository/README.md +++ b/packages/repository/README.md @@ -353,6 +353,24 @@ ctx.bind('dataSources.memory').to(ds); ctx.bind('repositories.noteRepo').toClass(MyNoteRepository); ``` +#### Using the Repository Mixin for Application +A Repository Mixin is available for Application that provides convenience methods for binding and instantiating a repository class. Bound instances can be used anywhere in your application using Dependency Injection. An array set to the key `repositories` can be passed to the constructor or the `.repository(RepositoryClass)` function can be used. The mixin will also instantiate any repositories declared by a component in its constructor using the `repositories` key. + +Repositories will be bound to the key `repositories.RepositoryClass` where `RepositoryClass` is the name of the Repository class being bound. +```ts +import { Application } from '@loopback/core'; +import { RepositoryMixin } from '@loopback/repository'; +import { ProductRepository, CategoryRepository } from './repository'; + +// Using the Mixin +class MyApplication extends RepositoryMixin(Application) {} + +// MyRepository will be bound to key `repositories.ProductRepository` +const app = new MyApplication({repositories: [ProductRepository]}); +// MyRepository2 will be bound to key `repositories.CategoryRepository` +app.repository(CategoryRepository); +``` + ### Compose repositories and controllers in a context ```ts