-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation explaining Multiple Repositories design decision #2110
Comments
cc @raymondfeng
I think we need to distinguish two aspects. (1) export class ProductRepository extends DefaultCrudRepository<
Product,
typeof Product.prototype.id
> {
constructor(@inject('datasources.db') dataSource: juggler.DataSource) {
super(Product, dataSource);
}
findByName(name: string): Promise<Product | undefined> {
return this.findOne({where:{name}});
}
} While this is our recommended approach, advanced users are free to structure their application differently. For example, they can use const productRepo = new DefaultCrudRepository<Product, typeof Product.prototype.id>(
Product,
db
); (2) We are creating a new repository instance for each incoming request. From my experience, this is a usual approach in systems built using Dependency Injection. The Repository class is a thin wrapper that's cheap to construct. By having one repository instance for each incoming request, the repository can use instance-level properties to e.g. store cached data. Again, this is a convention, not a requirement. You can reuse the same repository instance for all incoming requests. Having wrote that, it is not possible to use a single DefaultCrudRepository instance to work with multiple models. It is possible to write a repository class that provides data access for multiple models, but such API will be more complex. It's not enough to say LoopBack 4 was designed with extensibility in mind. If a repository supporting multiple models is something you need, then feel free to implement such repository as your own loopback extension. Our CLI does not allow extensions to contribute custom repository classes yet, see #2149
@acrodrig Where in our documentation would you like to see this information? Could you please advise us where to put it? |
Thanks for your answer. Regarding the comment above:
On a closing note I feel more or less the same way about Controllers/REST code. I feels full of boiler-plate code, whereas in LB3 it was completely out of sigh and I could still extend. I do appreciate the efforts of this new version and it feels cleaner in many ways. Let me know if there is any way I can contribute. |
You are not the first one asking for that :) In #2036, we are discussing how to create both a repository and a controller, avoiding boiler-plate. What are your requirements - would you like to avoid boilerplate controller code too, or is it just the repository that's bothering you? Let's continue this discussion to #2036 please.
Makes sense. Would you mind contributing this documentation enhancement yourself? I think you should have enough information by now and since you raised these questions, you are best equipped to answer them in a way that will be useful to future readers like yourself.
See #2036 :) |
Feature proposal
Starting to play/understand lb4 ...
I am writing code that inserts several model/entity instances into a DB. My code constantly needs to create repository instances based on the model in question. It seems overkill.
Can you explain in the documentation why this design decision to have one repository per class/model/entity was chosen? Or is it possible to have a multi-class repository? The
cli
does not seem to allow it.Thanks.
The text was updated successfully, but these errors were encountered: