Drizzle ServiceDrizzle Service

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

FeatureDescription
Type-Safe MutationsFull TypeScript support with compile-time validation
Error HandlingBuilt-in error handling with detailed error information using ErrorFirst pattern
Soft Delete SupportConfigurable soft delete functionality with automatic handling
Lifecycle HooksBefore and after hooks for custom business logic
Data ValidationAutomatic validation of required fields and constraints
Transaction SupportAutomatic transaction handling for data consistency
Optimistic UpdatesSupport for optimistic UI updates with rollback capabilities

Available Operations

Basic Mutation Operations

  • create(data, hooks?) - Create a new record with optional lifecycle hooks
  • update(id, data, hooks?) - Update an existing record by ID
  • delete(id, hooks?) - Soft delete a record (if configured) or hard delete
  • hardDelete(id, hooks?) - Permanently delete a record from the database
  • restore(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:

PropTypeDefault
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:

PropTypeDefault
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