loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

Generate model classes by applying custom transformation on existing models #2187

Closed mi5ha closed 3 years ago

mi5ha commented 5 years ago

Was wondering, can you define a Loopback model, so that objects of that model are created by executing some complex query on top of other models? Or models must be 1 to 1 mapped to database tables that they are connected to?

bajtos commented 5 years ago

Hi @mi5ha, in LoopBack 4, models are primarily used to describe data schema. The behavior (e.g. CRUD persistence via a database) is provided by Repositories and Services. Then there are controller that are defining REST API endpoints and implementing them by calling out to repositories and services (typically).

So in your case, the part "objects of that model are created by executing some complex query on top of other models" can be implemented inside a controller (but that's difficult to reuse and test in isolation), inside a custom repository class (although I am not sure if your behavior fits "repository" pattern?) or in a new layer that will sit between controllers and repositories - see https://github.com/strongloop/loopback-next/issues/2239.

This is something we are still investigating, we don't yet have any prescribed/recommended way.

Please let me know if I have answered your question?

I think this can be useful for more people, can you suggest a place in our documentation where you would be looking for this kind of information?

mi5ha commented 5 years ago

Thank you for answering. So in short, "data sources" define where data is physically located, "models" define data structure, and I can manipulate data in repositories and services and create REAST API in controllers. Got it.

Here is the essence of my question. In all our applications, we always have models as they are defined in persistence layer (database) and then we have domain models. I.e. models created by some transformation of one or more database models.

I would expect the following from domain models:

For example in Microsoft Power Bi, you have ability to take basic models and create new models by applying transformations on basic models with Power Query Language: https://goo.gl/gEiSBG. I can check transformation source code and see how domain model was constructed.

Then these domain models can be used anywhere in PowerBi the same way basic models are used. Basically I have defined new data source as a set of transformations of existing models.

Regarding where in the documentation I would expect this info. Not sure, maybe in data sources? If there is a way to declare a data source that is a transformation of existing data source models?

Thank you, Misha

bajtos commented 5 years ago

Sorry for taking so long to respond.

In all our applications, we always have models as they are defined in persistence layer (database) and then we have domain models.

At the moment, it's possible to define domain models decoupled from the database - just create a new class and decorate it with @model() and @property() decorators.

We don't have tooling to generate such models from other models (or database schema).

I see how it could be useful, but to be honest, I don't think we will have time to look into that any time soon.

stale[bot] commented 3 years ago

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] commented 3 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.