Drizzle ServiceDrizzle Service

API Reference

Complete API reference for mutation operations including create, update, delete, and restore methods

create()

Creates a new entity in the database.

Parameters

PropTypeDefault
hooks?
ServiceHooks<T>
-
data
T['$inferInsert']
-

Returns

Returns a Handler<T['$inferSelect']> - a promise that resolves to a tuple [Error | null, T['$inferSelect'] | null].

Example

const [error, todo] = await todosService.create({
  title: 'Complete documentation',
  userId: 'user-123',
  tenant: 'company-abc',
  status: 'todo',
  priority: 'high'
})

if (error) {
  console.error('Creation failed:', error.message)
} else {
  console.log('Created todo:', todo.id)
  console.log('Todo title:', todo.title)
  console.log('Status:', todo.status) // Default: 'todo'
  console.log('Priority:', todo.priority) // Default: 'medium'
}

update()

Updates an existing entity in the database.

Parameters

PropTypeDefault
hooks?
ServiceHooks<T>
-
data
Partial<Omit<T['$inferInsert'], 'createdAt' | 'id'>>
-
id
IdType<T, TOpts>
-

Returns

Returns a Handler<T['$inferSelect']> - a promise that resolves to a tuple [Error | null, T['$inferSelect'] | null].

Example

const [error, updatedTodo] = await todosService.update(todoId, {
  title: 'Updated task title',
  status: 'in-progress',
  priority: 'low'
})

if (error) {
  console.error('Update failed:', error.message)
} else {
  console.log('Updated todo:', updatedTodo.title)
  console.log('New status:', updatedTodo.status)
}

delete()

Performs a soft delete on an entity (marks as deleted without removing from database).

Parameters

PropTypeDefault
hooks?
ServiceHooks<T>
-
id
IdType<T, TOpts>
-

Returns

Returns a promise that resolves to an object with success status and optional message:

Promise<{ readonly success: boolean; readonly message?: string }>

Example

const { success, message } = await todosService.delete(todoId)

if (success) {
  console.log('Todo soft deleted:', message)
  // Contains: 'successfully soft deleted'
  
  // Verify the record is soft deleted but still exists
  const deleted = await todosService.findById(todoId)
  console.log('Status after delete:', deleted?.status) // 'canceled'
} else {
  console.error('Delete failed')
}

hardDelete()

Performs a hard delete on an entity (permanently removes from database).

Parameters

PropTypeDefault
hooks?
ServiceHooks<T>
-
id
IdType<T, TOpts>
-

Returns

Returns a promise that resolves to an object with success status and optional message:

Promise<{ readonly success: boolean; readonly message?: string }>

Example

const { success, message } = await todosService.hardDelete(todoId)

if (success) {
  console.log('Todo permanently deleted:', message)
  
  // Verify the record is completely removed
  const deleted = await todosService.findById(todoId)
  console.log('Record exists:', deleted) // null
} else {
  console.error('Hard delete failed')
}

restore()

Performs a restore operation on a soft-deleted entity (marks as not deleted).

Parameters

PropTypeDefault
hooks?
ServiceHooks<T>
-
id
IdType<T, TOpts>
-

Returns

Returns a promise that resolves to an object with success status and optional message:

Promise<{ readonly success: boolean; readonly message?: string }>

Example

// First delete a todo
await todosService.delete(todoId)

// Then restore it
const restoreResult = await todosService.restore(todoId)

if (restoreResult.success) {
  console.log('Todo restored:', restoreResult.message)
  
  // Verify restoration
  const restored = await todosService.findById(todoId)
  console.log('Status after restore:', restored?.status) // Back to original status
} else {
  console.error('Restore failed')
}

ServiceHooks<T>

Interface for lifecycle hooks that can be executed during mutation operations.

PropTypeDefault
onError?
(error: Error) => Promise<void>
-
afterAction?
(data: T['$inferSelect']) => Promise<void>
-
beforeAction?
(data: T['$inferSelect']) => Promise<void>
-

Hook Example

const hooks = {
  beforeAction: async (data) => {
    console.log('About to create:', data)
  },
  afterAction: async (data) => {
    console.log('Created successfully:', data.id)
  },
  onError: async (error) => {
    console.error('Operation failed:', error.message)
  }
}

const [error, todo] = await todosService.create(todoData, hooks)