Skip to content

Hooks - Service hooks

Eicrud offers a variety of CRUD hooks to perform actions triggered by entity modification or query.

Note

These hooks are specific to each CRUDServices. For global hooks see the controller hooks.

Warning

Any error thrown in a hook cancels the underlying operation. Consider using try...catch blocks to avoid side effects.

Note

Returning a value in an error hook prevents the error from throwing. The value is sent back to the client.

Create Hooks

beforeCreateHook

user.hooks.ts
override async beforeCreateHook(this: UserService, data: User[], ctx) {
    // before User creation

    return data;
}

afterCreateHook

user.hooks.ts
override async afterCreateHook(this: UserService, result: any[], data: User[], ctx) {
    // after User creation

    return result;
}

errorCreateHook

user.hooks.ts
override async errorCreateHook(this: UserService, data: User[], ctx: CrudContext, error: any) {
    // error User creation

    return null;
}

Read hooks

beforeReadHook

user.hooks.ts
override async beforeReadHook(this: UserService, query: User, ctx){
    // before User read

    return query;
}

afterReadHook

user.hooks.ts
override async afterReadHook(this: UserService, result, query: User, ctx){
    // after User read

    return result;
}

errorReadHook

user.hooks.ts
override async errorReadHook(this: UserService, query: User, ctx: CrudContext, error: any) {
    // error User read

    return null;
}

Update Hooks

beforeUpdateHook

user.hooks.ts
override async beforeUpdateHook(this: UserService,
    updates: { query: User; data: User }[],
    ctx
) {
    // before User update

    return updates;
}

afterUpdateHook

user.hooks.ts
override async afterUpdateHook(this: UserService, 
    results: any[],
    updates: { query: User; data: User }[],
    ctx,
) {
    // after User update

    return results;
}

errorUpdateHook

user.hooks.ts
override async errorUpdateHook(this: UserService, 
    updates: { query: User; data: User }[],
    ctx: CrudContext,
    error: any,
) {
    // error User update

    return null;
}

Delete Hooks

beforeDeleteHook

user.hooks.ts
override async beforeDeleteHook(this: UserService, query: User, ctx: CrudContext){
        // before User delete

        return query;
}

afterDeleteHook

user.hooks.ts
override async afterDeleteHook(this: UserService, result, query: User, ctx: CrudContext){
        // after User delete

        return result;
}

errorDeleteHook

user.hooks.ts
override async errorDeleteHook(this: UserService, query: User, ctx: CrudContext, error: any){
    // error User delete

    return null;
}

Controller Hook

user.hooks.ts
override async errorControllerHook(this: UserService, error: any, ctx: CrudContext){
        //after User error

}

Note

Controller hooks are called by the controller and not the service. This means they'll only catch errors that result from a controller call.