- Collection hooks, execute custom code before or after CRUD operations
- Collection overrides, completely replace the default behavior of CUD operations
These two features are almost identical, but they are executed at very different stages of the customizations.
This means that Collection hooks will be executed even if you choose to use Collection override.
Collection hooks
Collection hooks allow you to execute custom code before or after CRUD operations, giving you the ability to enforce business rules, or integrate with external services.Features
- Pre and Post operation execution: Execute custom logic before or after a specific collection operation.
- Flexible trigger points: Hook into any of the standard CRUD operations (
list,create,update,delete,aggregate). - Contextual information: Access to a rich high-level context providing details about the operation, enabling precise and informed logic execution.
How it works
Any given Collection should implement all of the following functions:listcreateupdatedeleteaggregate
- A lifecycle position (
Before|After) - An action type (
List|Create|Update|Delete|Aggregate) - A callback, that will receive a context matching the provided hook position and hook definition.
Basic use cases
In the following example, we want to prevent a set of users from updating any records of theTransactions table. We want to check if the user email is allowed to update a record via an external API call.
User is created in the database, send them a welcome email.
Collection overrides
Collection overrides provide the ability to completely replace the default behavior of CUD operations. This feature allows for custom implementations ofcreate, update, and delete operations, giving you full control over data handling.
Features
- Complete control over CUD: Directly replace the standard behavior of
create,update, anddeleteoperations with custom logic. - Custom operation logic: Implement entirely custom workflows or integrate external services directly into your CUD operations.
- Full operation context: Receive detailed low-level context about the operation, enabling complex logic and integrations.
How it works
Collection overrides allow you to define custom behavior that will entirely replace the default implementation of thecreate, update, and delete operations.
To define an override for a Collection, you must specify the handler function that will be executed instead of the default operation. The custom handler function will receive a context object containing relevant information for the operation.