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.
Backdoor hooks are called before and after a backdoor request is received in a microservice.
override async afterBackdoorHook(res, ctx: CrudContext, query: BackdoorQuery, args: any[]) {
return Promise.resolve();
}
override async beforeBackdoorHook(ctx: CrudContext, query: BackdoorQuery, args: any[]){
return Promise.resolve();
}
override async errorBackdoorHook(error, ctx: CrudContext, query: BackdoorQuery, 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.
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.