Data hooks run custom code before or after specific interactions with your collections. Hooks let you intercept database operations at key points in their lifecycle. This allows you to implement custom logic, validation, and computation without modifying the core data operations.
You can use hooks to:
You can currently create hooks only through the Velo API, but they trigger for any data operation. This includes operations you perform through:
wix-data Velo APIThere are 3 main categories of hooks that run at different points in the data operation lifecycle. All hooks receive parameters specific to their type along with a context object, and must return appropriate values as described in the hooks reference documentation.
Before hooks run immediately before a data operation executes. They can modify the data being processed or block the operation by returning a rejected promise. This makes them ideal for validation, data transformation, and access control.
After hooks run immediately after a data operation completes successfully. They can modify the returned results but can't affect what's actually stored in the database. After hooks are useful for post-processing results, calculated data, and updating related data in other collections or external systems.
Failure hooks run when data operations encounter errors. They help with error handling, cleanup operations, and providing custom error responses to site visitors.
Data hooks must always follow the naming and file conventions listed below.
All data hooks must be defined in a file named data.js located in a site's backend.
Hook functions use a specific naming pattern that combines the collection name with the hook type:
For example, if you have a collection with the ID Listings and want to create a beforeInsert hook: