-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
This is a follow-up for #3718 and #3922.
Write content for docs/site/migration/models/operation-hooks.md, explain how to migrate operation hooks (see LB3 docs: Operation Hooks).
Explain that we don't have first-class support for Operation Hooks in LB4 yet, refer to #1919
As a temporary solution, describe how to leverage PersistedModel used by our legacy juggler bridge. Example implementation:
export class TodoRepository extends DefaultCrudRepository<
Todo,
typeof Todo.prototype.id,
TodoRelations
> {
constructor(@inject('datasources.db') dataSource: juggler.DataSource) {
super(Todo, dataSource);
this.modelClass.observe('save', async ctx => {
console.log(
'Going to write to an instance of model %s',
ctx.Model.modelName,
);
});
}
}Caveats:
(1)
Repository constructor is executed once for each incoming request. We don't want to install another copy of the same operation hook on each request, we need to find a way how to install hooks only once. Ideally, DefaultCrudRepository should provide a protected method that subclasses can use to supply one-time model setup.
For example:
export class TodoRepository extends DefaultCrudRepository<
Todo,
typeof Todo.prototype.id,
TodoRelations
> {
constructor(@inject('datasources.db') dataSource: juggler.DataSource) {
super(Todo, dataSource);
}
protected setupPersistedModel() {
super.setupPersistedModel();
this.modelClass.observe('save', async ctx => {
console.log(
'Going to write to an instance of model %s',
ctx.Model.modelName,
);
});
}
}(2)
Current TypeScript typings in loopback-datasource-juggler do not contain operation hooks and .observe() method. We need to fix this.
Acceptance criteria
- Add typings for Operation Hooks to loopback-datasource-juggler
- Add a protected method
definePersistedModeltoDefaultCrudRepository, this method should overriden by user's repository class to access the model class where the operation hooks can be applied. - Migration guide describing how to move LB3 operation hooks to LB4 repositories.