nexusLIMS.tui.common.db_utils#
Database utilities for NexusLIMS TUI applications.
Provides common database query patterns.
Module Contents#
Functions#
Check if a value is unique for a given field. |
|
Get count of session_log entries for an instrument. |
|
Find an instrument that conflicts with a unique field value. |
API#
- nexusLIMS.tui.common.db_utils.check_uniqueness(session: sqlmodel.Session, model: type, field_name: str, value: Any, exclude_pk: Any | None = None) bool[source]#
Check if a value is unique for a given field.
- Parameters:
- Returns:
True if unique, False if duplicate exists
- Return type:
Examples:
>>> from nexusLIMS.db.models import Instrument >>> with get_db_session() as session: ... is_unique = check_uniqueness( ... session, Instrument, "api_url", ... "https://example.com/api/tools/?id=42" ... )
- nexusLIMS.tui.common.db_utils.get_session_log_count(session: sqlmodel.Session, instrument_pid: str) int[source]#
Get count of session_log entries for an instrument.
Useful for warning users before deleting an instrument with associated data.
- Parameters:
- Returns:
Number of session_log entries
- Return type:
Examples:
>>> with get_db_session() as session: ... count = get_session_log_count(session, "FEI-Titan-TEM") ... if count > 0: ... print(f"Warning: {count} session logs will be orphaned")
- nexusLIMS.tui.common.db_utils.find_conflicting_instrument(session: sqlmodel.Session, field_name: str, value: Any, exclude_pid: str | None = None) Instrument | None[source]#
Find an instrument that conflicts with a unique field value.
- Parameters:
- Returns:
Conflicting instrument, or None if no conflict
- Return type:
Instrument | None
Examples:
>>> with get_db_session() as session: ... conflict = find_conflicting_instrument( ... session, "api_url", "https://example.com/api" ... ) ... if conflict: ... print(f"Already used by {conflict.instrument_pid}")