-
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
How can extensions contribute Entities with persistence behavior and REST API #5476
Comments
Let me share few ideas I considered while working on the spike.
We can also advise the following solution(s):
I see two options how to wire dependencies from the application. Option A: Add wrapper files to the app. Benefits: the repository and the controller will be seen by other Repository file: // src/repositories/installation.repository.ts
import {InstallationRepository as BaseRepository} from 'loopback-component-push';
export class InstallationRepository extends BaseRepository {
constructor(
@inject('datasources.db') // <-- use the datasource name from your app
dataSource: juggler.DataSource,
) {
super(dataSource);
}
} Controller file: // src/controllers/installation.controller.ts
import {InstallationController as BaseController} from 'loopback-component-push';
import {InstallationRepository} from '../repositories';
export class InstallationController extends BaseController {
constructor(
@repository(InstallationRepository)
protected installationRepository: InstallationRepository,
// if the controller requires request/response or other dependencies,
// then we need to repeat them here :(
@inject(RestBindings.Http.CONTEXT) private requestContext: RequestContext,
) {
super(installationRepository, requestContext);
}
} Option B: wire dependencies inside the extension based on the config Benefits: the user does not need to create any additional files.
Component code to put it all together: import {inject} from '@loopback/core';
import {InstallationRepository} from './repositories';
import {InstallationController} from './controllers';
export class PushNotificationComponent implements Component {
controllers = [InstallationController];
repositories = [InstallationRepository];
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
private application: RestApplication,
@config()
config: PushNotificationConfig = {},
) {
inject(config.dataSourceKey)(InstallationRepository, undefined, 0);
}
} I am not sure how this approach will work when the entities contributed by an |
This is tricky because LB4 does not have first-class implementation of operation |
@bajtos Here's my original question As per DRY, I'd like to contain my business logic in extension (loopback component). However, when defining models I'd like to specify belongsTo relationship to a property. In LB3, I'd do the same by defining the following module and override via configuration:
Also, I'd like to know how can I let developer specify the model in case he wants to specify a custom one. In LB3, this would be done like this. https://github.com/pbalan/component-blog/blob/master/lib/models/index.js#L51-L63
Then, in a boot script, we could provide the necessary overrides.
|
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 |
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 |
This story is extracted from #4099, where we researched existing LoopBack 3 components and various techniques they use.
Now we need to write documentation for extension authors to show how to solve the following use cases in LoopBack 4:
Contribute custom entities (Application, Installation) to be persisted via CRUD, exposed via REST and possibly further customized by the app. Customization options include which datasource to use, the base path where REST API is exposed (e.g.
/api/apps
and/api/devices
), additional fields (e.g.Application.tenantId
) and changes in persistence behavior (e.g. via Operation Hooks)Add a custom Operation Hook to given models, with a config option to enable/disable this feature. The list of models can be provided explicitly in the component configuration or obtained dynamically via introspection (e.g. all models having a "belongsTo" relation with the Group model). This may require How can extensions introspect application artifacts #5426 How can extensions introspect application artifacts.
Add new relations, e.g. between an app-provided entity
User
and a component-provided entityFile
. In this variant, the relation is added on small fixed number of models.A model mixing adding new relations (
hasMany ModelEvents
), installing Operation Hooks (to generate model events/audit log entries), adding new Repository APIs (for working with related model events).(The mixin-based design may be difficult to accomplish in LB4, we may want to use introspection and a model setting instead. The trickier part is how to apply changes to models added after the component was mounted.)
For all models with a flag enabled in model settings, setup a custom
afterRemote
hook to modify the HTTP response (e.g. add additional headers).In most cases, the new content will be useful to authors building new LB4 components too, therefore we should structure the content in two parts:
The text was updated successfully, but these errors were encountered: