Developing Mutation Procedures
Build procedures for data creation, update, and deletion operations using tRPC mutation procedures.
What Are tRPC Mutations?
Welcome to tRPC mutation procedures! So far, you've likely used queries to fetch data, like getting a list of users or a single product.
But what if you need to change data? This is where mutations come in. Mutations are API operations designed to modify data on your server.
- Create: Add new records (e.g., create a user).
- Update: Modify existing records (e.g., update a user's email).
- Delete: Remove records (e.g., delete a user).
They are the 'CUD' in CRUD operations!
Mutations vs. Queries
It's important to understand the difference between queries and mutations in tRPC:
- Queries: Used for fetching data. They are typically read-only and should not have side effects (i.e., they don't change data on the server).
- Mutations: Used for changing data. They are designed to have side effects and modify your server's state.
tRPC enforces this separation, helping you build more predictable and robust APIs. When you need to create, update, or delete anything, always reach for a mutation.
All lessons in this course
- Structuring with tRPC Routers
- Implementing Query Procedures
- Developing Mutation Procedures
- Merging and Nesting Routers