Methods
Comprehensive mutation operations for creating, updating, and deleting data with support for soft deletes and data validation
The Drizzle Service provides robust mutation operations that allow you to create, update, and delete data in your database safely and efficiently. These operations include comprehensive error handling, hooks for custom logic, and built-in support for soft deletes.
Key Features
Feature | Description |
---|---|
Type-Safe Mutations | Full TypeScript support with compile-time validation |
Error Handling | Built-in error handling with detailed error information using ErrorFirst pattern |
Soft Delete Support | Configurable soft delete functionality with automatic handling |
Lifecycle Hooks | Before and after hooks for custom business logic |
Data Validation | Automatic validation of required fields and constraints |
Transaction Support | Automatic transaction handling for data consistency |
Optimistic Updates | Support for optimistic UI updates with rollback capabilities |
Available Operations
Basic Mutation Operations
create(data, hooks?)
- Create a new record with optional lifecycle hooksupdate(id, data, hooks?)
- Update an existing record by IDdelete(id, hooks?)
- Soft delete a record (if configured) or hard deletehardDelete(id, hooks?)
- Permanently delete a record from the databaserestore(id, hooks?)
- Restore a soft-deleted record
Return Types
All mutation operations return a Handler<T>
type, which is a tuple containing either:
[null, result]
- Success case with the operation result[error, null]
- Error case with detailed error information
This pattern enables comprehensive error handling without throwing exceptions.
Service Hooks
Mutation operations support lifecycle hooks that allow you to inject custom logic:
Prop | Type | Default |
---|---|---|
onError? | (error: Error) => Promise<void> | - |
afterAction? | (data: T['$inferSelect']) => Promise<void> | - |
beforeAction? | (data: T['$inferSelect']) => Promise<void> | - |
Soft Delete Configuration
Soft deletes can be configured when creating the service instance:
Prop | Type | Default |
---|---|---|
notDeletedValue? | T['$inferSelect'][field] | null | - |
deletedValue | T['$inferSelect'][field] | 'NOT_NULL' | - |
field | keyof T['$inferSelect'] | - |
Error Handling
The service provides comprehensive error handling with detailed error information:
- Validation Errors: Missing required fields, invalid data types
- Constraint Violations: Unique key violations, foreign key constraints
- Database Errors: Connection issues, permission problems
- Business Logic Errors: Custom validation failures from hooks