Raxol.Database (Raxol v0.5.0)

View Source

Provides a safe interface for database operations.

This module:

  • Wraps all database operations with retry logic
  • Provides convenience functions for common database operations
  • Handles database errors gracefully

Summary

Functions

Gets all records of a schema with retry logic.

Creates a record with retry logic.

Deletes a record with retry logic.

Gets a record by ID with retry logic.

Checks if the database connection is healthy.

Restarts the database connection if it becomes unhealthy.

Safely executes a database query with retry logic.

Executes a transaction with retry logic.

Updates a record with retry logic.

Functions

all(schema, clauses \\ [])

Gets all records of a schema with retry logic.

Parameters

  • schema - The schema module
  • clauses - Optional where clauses

Returns

  • {:ok, records} - Records found
  • {:error, reason} - Query failed

create(schema, attrs)

Creates a record with retry logic.

Parameters

  • schema - The schema module
  • attrs - The attributes for the new record

Returns

  • {:ok, record} - Record created
  • {:error, changeset} - Validation failed
  • {:error, reason} - Query failed

delete(record)

Deletes a record with retry logic.

Parameters

  • record - The record to delete

Returns

  • {:ok, record} - Record deleted
  • {:error, changeset} - Deletion validation failed
  • {:error, reason} - Query failed

get(schema, id)

Gets a record by ID with retry logic.

Parameters

  • schema - The schema module
  • id - The record ID

Returns

  • {:ok, record} - Record found
  • {:error, :not_found} - Record not found
  • {:error, reason} - Query failed

healthy?()

Checks if the database connection is healthy.

Returns

  • true - Connection is healthy
  • false - Connection is unhealthy

restart_connection()

Restarts the database connection if it becomes unhealthy.

Returns

  • :ok - Connection restart attempted

safe_query(fun)

Safely executes a database query with retry logic.

Parameters

  • fun - Function containing database operations

Returns

  • {:ok, result} - Operation succeeded
  • {:error, reason} - Operation failed after retries

transaction(fun)

Executes a transaction with retry logic.

Parameters

  • fun - Function containing transaction operations

Returns

  • {:ok, result} - Transaction succeeded
  • {:error, reason} - Transaction failed

update(schema, record, attrs)

Updates a record with retry logic.

Parameters

  • schema - The schema module
  • record - The record to update
  • attrs - The new attributes

Returns

  • {:ok, record} - Record updated
  • {:error, changeset} - Validation failed
  • {:error, reason} - Query failed