Configuration - Service
Most of Eicrud's application configuration is done through the CrudConfigService
.
@Injectable()
export class MyConfigService extends CrudConfigService {
constructor(
public userService: UserService,
public entityManager: EntityManager,
public emailService: EmailService,
protected orm: MikroORM
) {
super({
userService,
entityManager,
emailService,
jwtSecret: process.env.JWT_SECRET,
cacheManager: new BasicMemoryCache(),
orm,
id_field: 'id',
dbAdapter: new MongoDbAdapter(),
});
this.addRoles(roles);
}
}
Global Hooks
You can override some of your CrudConfigService
's methods, to define global hooks for your application.
Controller hooks are called before and after CRUD and CMD operations (at the controller level).
override async afterControllerHook(res, ctx: CrudContext) {
return Promise.resolve();
}
override async beforeControllerHook(ctx: CrudContext){
return Promise.resolve();
}
override async errorControllerHook(error, ctx: CrudContext){
return Promise.resolve();
}
Note
Controller hooks will only trigger on operations initiated by the client. Unlike service-specific hooks that trigger on internal service calls.
MsLink hooks are called before and after a ms-link request is received in a microservice.
override async afterMsLinkHook(res, ctx: CrudContext, query: MsLinkQuery, args: any[]) {
return Promise.resolve();
}
override async beforeMsLinkHook(ctx: CrudContext, query: MsLinkQuery, args: any[]){
return Promise.resolve();
}
override async errorMsLinkHook(error, ctx: CrudContext, query: MsLinkQuery, args: any[]){
return Promise.resolve();
}
Note
- It's better not to await function calls inside hooks when possible, this way errors won't prevent requests from returning data.
- Returning a value in an error hook prevents the error from throwing. The value is sent back to the client.
Services
The CrudConfigService
has access to all your CrudServices via the servicesMap
property.
override async afterControllerHook(res, ctx: CrudContext) {
const profileService: CrudService<Profile> = this.servicesMap['profile'];
}
Events
You can override some "event" methods as well.
onHighTrafficEvent will trigger when an abnormal amount of traffic is detected for a particular user.
override async onHighTrafficEvent(count, user: CrudUser, ctx: CrudContext){
return Promise.resolve();
}