> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt ## Resource: Process Data with Hooks ## Article: Process Data with Hooks ## Article Link: https://dev.wix.com/docs/develop-websites-sdk/code-your-site/work-with-data/data-api/process-data-with-hooks.md ## Article Content: # Process Data with Hooks Data hooks let you run custom code before or after data operations. This article shows you how to create hooks that validate incoming data and calculate derived values. ## Before you begin Make sure you understand the basics of [data hooks](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/work-with-data/data-api/about-data-hooks.md) and their conventions. ## Validate data with a before hook Before hooks run immediately before a data operation executes. They're ideal for validating and transforming data before it's saved to a collection. This example shows how to create a `beforeInsert` hook that validates real estate listing data: 1. Open your site's `backend/data.js` file or create one if it doesn't already exist. 2. Add a `beforeInsert` hook for your Listings collection: ```javascript export function Listings_beforeInsert(item, context) { // Validate that price is a positive number if (!item.price || item.price <= 0) { return Promise.reject("Listing price must be greater than 0"); } // Transform data item.title = item.title.trim().toUpperCase(); // Set default values if (!item.status) { item.status = "available"; } return item; } ``` 3. Test the hook by inserting a listing item from the editor, dashboard, or using the Data API. The hook validates that a valid price is present, transforms the data by trimming and uppercasing the title, and sets default values for optional fields. ## Calculate derived data with an after hook After hooks run immediately after a data operation completes successfully. They're useful for calculating derived values and modifying the returned results. This example shows how to create an `afterQuery` hook that calculates a real estate listing's price per square foot: 1. Open your site's `backend/data.js` file or create one if it doesn't already exist. 2. In your `backend/data.js` file, add an `afterQuery` hook: ```javascript export function Listings_afterQuery(item, context) { // Calculate price per square foot if both values exist if (item.squareFootage > 0) { item.pricePerSqFt = Math.round(item.price / item.squareFootage); } return item; } ``` 3. Test the hook by querying listing items from the editor, dashboard, or using the Data API. The hook calculates the price per square foot and includes it in the returned result. ## See also - [About hooks](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/work-with-data/data-api/about-data-hooks.md) - [Hooks reference documentation](https://dev.wix.com/docs/velo/events-service-plugins/data/hooks/wix-data-hooks/introduction.md)