Skip to content

Utilities

home / developer / concepts / database / utilities

Overview

Tux provides utility functions that simplify database access across the application. These utilities handle context resolution and service discovery, providing consistent patterns for accessing database services and controllers from Discord contexts.

The utilities serve as the bridge between Discord interactions and the database layer, providing context resolution, controller access, service resolution, type safety, and graceful error handling.

Architecture

Utility Function Organization

Utilities are organized with clear separation of concerns, proper typing, import guards to prevent circular dependencies, and focused functions with single responsibilities.

Context Resolution Flow

Utilities resolve database access through multi-step fallback patterns that prioritize bot attributes, provide legacy support, and fail gracefully.

Service Resolution Flow

Utilities resolve database services through prioritized fallback patterns checking for service attributes on bot instances.

Fallback Patterns

Utilities provide robust fallback patterns for reliability with logging, migration support, and graceful degradation.

Key Concepts

Getting Services from Discord Contexts

Utilities extract database services from Discord interaction contexts, supporting both slash commands and text commands with automatic context type detection.

Resolving Bot Instances

Bot resolution handles different Discord context types through attribute checking with multiple fallback attempts for compatibility.

Service Discovery

Utilities automatically discover database services from bot instances through attribute inspection with prioritized fallback patterns.

Controller Creation Utilities

Utilities provide convenient controller creation with enhanced features like context awareness, security, audit trails, and caching.

Usage Patterns

Using Utility Functions

Always use utility functions instead of direct access for consistent service resolution and proper error handling.

Creating Controllers from Context

Leverage enhanced controller creation for context-aware database operations with automatic feature injection.

Service Layer Direct Access

Use service layer access for low-level operations and direct SQL queries when controller abstraction isn't needed.

Fallback Pattern Usage

Implement fallback patterns for robust operation that works even with partial database access availability.

Service Resolution from Contexts

Automatic service resolution from Discord contexts eliminates manual service passing and injection.

Best Practices

Use Utility Functions Instead of Direct Access

Utilities provide abstraction, consistency, maintainability, testability, and centralized service resolution.

Handle None Returns Gracefully

Always check for None returns and provide user-friendly error messages or fallback behavior.

Use Utility Functions for Service Access

Leverage utilities for automatic service resolution, consistent access patterns, and proper error handling.

Fallback to Direct Access When Needed

Support migration scenarios and legacy systems through fallback patterns with logging and graceful degradation.

Document Utility Dependencies

Clearly document required bot attributes, service configurations, and fallback behaviors in utility usage.

Test Utility Functions

Include utility function testing in test suites to validate service resolution and error handling.

Log Utility Usage

Add appropriate logging for debugging utility resolution paths and fallback usage.

Avoid Utility Function Abuse

Cache utility results to prevent redundant resolution calls and optimize performance.