Skip to content
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

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

Closed
mi5ha opened this issue Dec 23, 2018 · 5 comments
Closed

Comments

@mi5ha
Copy link

mi5ha commented Dec 23, 2018

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
Copy link
Member

bajtos commented Jan 25, 2019

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 #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
Copy link
Author

mi5ha commented Feb 6, 2019

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:

  • They are also strongly typed (with TypeScript interface for example)
  • I can create REST API endpoint on top of domain models the same way they are created for basic model
  • There is a place where I can clearly see how domain model is created

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
Copy link
Member

bajtos commented May 24, 2019

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.

@bajtos bajtos changed the title Can Model objects be created from custom query? Generate model classes by applying custom transformation on existing models May 24, 2019
@bajtos bajtos added feature and removed question labels May 24, 2019
@stale
Copy link

stale bot commented Jul 14, 2021

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 stale bot added the stale label Jul 14, 2021
@stale
Copy link

stale bot commented Aug 13, 2021

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.

@stale stale bot closed this as completed Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants