-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow components to contribute models + migration guide for LB3 components #5477
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,28 +221,59 @@ export function RepositoryMixin<T extends MixinTarget<Application>>( | |
| nameOrOptions?: string | BindingFromClassOptions, | ||
| ) { | ||
| const binding = super.component(componentCtor, nameOrOptions); | ||
| this.mountComponentRepositories(componentCtor); | ||
| const instance = this.getSync<C & RepositoryComponent>(binding.key); | ||
| this.mountComponentRepositories(instance); | ||
| this.mountComponentModels(instance); | ||
| return binding; | ||
| } | ||
|
|
||
| /** | ||
| * Get an instance of a component and mount all it's | ||
| * repositories. This function is intended to be used internally | ||
| * by component() | ||
| * by `component()`. | ||
| * | ||
| * @param component - The component to mount repositories of | ||
| * NOTE: Calling `mountComponentRepositories` with a component class | ||
| * constructor is deprecated. You should instantiate the component | ||
| * yourself and provide the component instance instead. | ||
| * | ||
| * @param componentInstanceOrClass - The component to mount repositories of | ||
| * @internal | ||
| */ | ||
| mountComponentRepositories(component: Class<unknown>) { | ||
| const componentKey = `${CoreBindings.COMPONENTS}.${component.name}`; | ||
| const compInstance = this.getSync<{ | ||
| repositories?: Class<Repository<Model>>[]; | ||
| }>(componentKey); | ||
| mountComponentRepositories( | ||
| // accept also component class to preserve backwards compatibility | ||
| // TODO(semver-major) Remove support for component class constructor | ||
| componentInstanceOrClass: Class<unknown> | RepositoryComponent, | ||
| ) { | ||
| const component = resolveComponentInstance(this); | ||
|
|
||
| if (compInstance.repositories) { | ||
| for (const repo of compInstance.repositories) { | ||
| if (component.repositories) { | ||
| for (const repo of component.repositories) { | ||
| this.repository(repo); | ||
| } | ||
| } | ||
|
|
||
| // `Readonly<Application>` is a hack to remove protected members | ||
| // and thus allow `this` to be passed as a value for `ctx` | ||
| function resolveComponentInstance(ctx: Readonly<Application>) { | ||
| if (typeof componentInstanceOrClass !== 'function') | ||
| return componentInstanceOrClass; | ||
|
|
||
| const componentName = componentInstanceOrClass.name; | ||
| const componentKey = `${CoreBindings.COMPONENTS}.${componentName}`; | ||
| return ctx.getSync<RepositoryComponent>(componentKey); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Bind all model classes provided by a component. | ||
| * @param component | ||
| * @internal | ||
| */ | ||
| mountComponentModels(component: RepositoryComponent) { | ||
| if (!component.models) return; | ||
| for (const m of component.models) { | ||
| this.model(m); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -287,6 +318,24 @@ export function RepositoryMixin<T extends MixinTarget<Application>>( | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * This interface describes additional Component properties | ||
| * allowing components to contribute Repository-related artifacts. | ||
| */ | ||
| export interface RepositoryComponent { | ||
| /** | ||
| * An optional list of Repository classes to bind for dependency injection | ||
| * via `app.repository()` API. | ||
| */ | ||
| repositories?: Class<Repository<Model>>[]; | ||
|
|
||
| /** | ||
| * An optional list of Model classes to bind for dependency injection | ||
| * via `app.model()` API. | ||
| */ | ||
| models?: Class<Model>[]; | ||
|
Contributor
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. Do we want to add
Member
Author
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. I am not sure. DataSources typically require environment-specific configuration (e.g. database connection string), I am no sure if it makes sense to export them from a component. Either way, such change is out of scope of this pull request - feel free to open a follow-up GH issue to discuss this idea more. |
||
| } | ||
|
|
||
| /** | ||
| * Normalize name or options to `BindingFromClassOptions` | ||
| * @param nameOrOptions - Name or options for binding from class | ||
|
|
||
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.
I cannot use a private method in mixins :(