-
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
docs: dynamic datasource, model, repository, and controller #5630
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,52 @@ export class Customer { | |
} | ||
``` | ||
|
||
## Defining a Model at runtime | ||
|
||
Models can be created at runtime using the `defineModelClass()` helper function | ||
from the `@loopback/repository` class. It expects a base model to extend | ||
(typically `Model` or `Entity`), followed by a `ModelDefinition` object as shown | ||
in the example below. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain what is the second type parameter for? |
||
|
||
```ts | ||
const bookDef = new ModelDefinition('Book') | ||
.addProperty('id', {type: 'number', id: true}) | ||
.addProperty('title', {type: 'string'}); | ||
const BookModel = defineModelClass<typeof Entity, {id: number; title?: string}>( | ||
Entity, // Base model | ||
bookDef, // ModelDefinition | ||
); | ||
``` | ||
|
||
You will notice that we are specifying generic parameters for the | ||
`defineModelClass()` function. The first parameter is the base model, the second | ||
one is an interface providing the TypeScript description for the properties of | ||
the model we are defining. If the interface is not specified, the generated | ||
class will have only members inherited from the base model class, which | ||
typically means no properties. | ||
|
||
In case you need to use an existing Model as the base class, specify the Model | ||
as the base class instead of `Entity`. | ||
|
||
```ts | ||
// Assuming User is a pre-existing Model class in the app | ||
bajtos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import {User} from './user.model'; | ||
import DynamicModelCtor from '@loopback/repository'; | ||
const StudentModel = defineModelClass< | ||
typeof User, | ||
// id being provided by the base class User | ||
{university?: string} | ||
>(User, studentDef); | ||
``` | ||
|
||
raymondfeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
If you want make this new Model available from other parts of the app, you can | ||
call `app.model(StudentModel)` to create a binding for it. | ||
|
||
{% include note.html content=" | ||
The `app.model()` method is available only on application classes with | ||
`RepositoryMixin` applied. | ||
" %} | ||
|
||
## Model Discovery | ||
|
||
LoopBack can automatically create model definitions by discovering the schema of | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should mention Class factory to allow parameterized decorations per https://loopback.io/doc/en/lb4/core-tutorial-part10.html (Class factory to allow parameterized decorations).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really convinced. If I am understanding correctly, the content at the link describes a general programming pattern, not something specific to Controllers. If that's the case, we should avoid adding non-specific content to avoid noise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class factory is part of the programming model pattern and it's being asked by our users. It gives our users more control. The change is applying to
Controllers.md
and I don't think that's noise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got the context, I thought it was about runtime controllers.
Added Class factory to allow parameterized decorations at an appropriate location.