Skip to content

base_dao

Base type-agnostic DAO implementation that provides basic CRUD operations for SQLAlchemy models.

Attributes

T module-attribute

T = TypeVar('T')

Classes

BaseDao

BaseDao(session: Session)

Bases: ABC

Base type-agnostic DAO implementation that provides basic CRUD operations for SQLAlchemy models.

Attributes

model_class abstractmethod property
model_class: Type[T]

Returns the class representing the model associated with this DAO.

session instance-attribute
session = session

Functions

add
add(obj: T) -> T

Adds an object to the session.

Parameters:

  • obj (T) –

    The object to be added.

Returns:

  • T ( T ) –

    The added object.

delete
delete(obj: Union[T, str]) -> None

Deletes the given object from the database.

Parameters:

  • obj (Union[T, str]) –

    The object to be deleted. It can be either a model object or a UUID.

Raises:

  • ValueError

    If the argument type for ‘obj’ is invalid.

Returns:

  • None

    None

get_all
get_all() -> List[T]

Retrieve all records from the database.

Sorts desc by ‘create_ts’ if the model has a ‘create_ts’ column.

Returns:

  • List[T]

    list[T]: A list of all records.

get_by_pkid
get_by_pkid(pkid: str) -> Optional[T]

Retrieve a record from the database by its primary key.

Parameters:

  • pkid (str) –

    The primary key of the record to retrieve.

Returns:

  • Optional[T]

    Optional[T]: The retrieved record, or None if not found.

get_by_pkids
get_by_pkids(pkids: List[str]) -> List[T]

Retrieve a list of records from the database by their primary keys.

Parameters:

  • pkids (List[str]) –

    The list of primary keys of the records to retrieve.

Returns:

  • List[T]

    List[T]: The retrieved records.

get_ref_by_name
get_ref_by_name(
    name: str,
    filter_deleted: bool = True,
    filter_disabled: bool = True,
) -> Optional[T]

Retrieve a reference by name.

Parameters:

  • name (str) –

    The name of the reference to retrieve.

  • filter_deleted (bool, default: True ) –

    Whether to filter out deleted references. Defaults to True.

  • filter_disabled (bool, default: True ) –

    Whether to filter out disabled references. Defaults to True.

Returns:

  • Optional[T]

    Optional[T]: The reference object if found, otherwise None.

get_ref_by_pkid
get_ref_by_pkid(
    pkid: str,
    filter_deleted: bool = True,
    filter_disabled: bool = True,
) -> Optional[T]

Retrieve a reference object by its primary key ID.

Parameters:

  • pkid (str) –

    The primary key ID of the reference object.

  • filter_deleted (bool, default: True ) –

    Whether to filter out deleted reference objects. Defaults to True.

  • filter_disabled (bool, default: True ) –

    Whether to filter out disabled reference objects. Defaults to True.

Returns:

  • Optional[T]

    Optional[T]: The reference object if found, otherwise None.

resource_query_builder
resource_query_builder(
    *,
    model_class: DeclarativeMeta,
    namespace_key: str,
    resource_key: str,
    filter_deleted: bool = True,
    filter_disabled: bool = True
) -> Query

Builder pattern for SQLAlchemy resource queries.

Resource records managed by the EleanorAI Framework typically have several common traits:

  • They are associated with a namespace.
  • They have a primary key identifier that is globally unique.
  • They have a name attribute that is unique within the namespace.
  • They can be soft-deleted as indicated by the deleted attribute.
  • They can be disabled as indicated by the enabled attribute.

This method constructs a query that considers these traits in a consistent way and should be used for all resources contained within a namespace. When the backend system setting flexible_resource_matching is enabled, the namespace and resource keys will be examined and a choice will be made on how the lookup should proceed.

Info

If the resource namespace is soft-deleted or disabled the query will not return any child resource records, even if they are not soft-deleted and/or enabled.

Warning

This builder should only be used for resources that meet the criteria described above.

Parameters:

  • model_class (DeclarativeMeta) –

    The model class to query.

  • namespace_key (str) –

    The namespace key to match.

  • resource_key (str) –

    The resource key to match.

  • filter_deleted (bool, default: True ) –

    Whether to filter out soft-deleted records. Defaults to True.

  • filter_disabled (bool, default: True ) –

    Whether to filter out disabled records. Defaults to True.

Returns:

  • Query ( Query ) –

    The constructed query.

update
update(obj: T, fetch_new: bool = True) -> Optional[T]

Update an object in the database.

Parameters:

  • obj (T) –

    The object to be updated.

  • fetch_new (bool, default: True ) –

    Flag indicating whether to fetch the existing object from the database. This is the safest operation when performing an update however results in an additional query. Defaults to True.

Returns:

  • Optional[T]

    Optional[T]: The updated object if successful, None otherwise.

OperationNotSupportedException

Bases: Exception

Raised when a common use operation cannot be supported by the DAO implementation.

Functions

probable_hash

probable_hash(value: str) -> bool

probable_pkid

probable_pkid(
    cmp_model: DeclarativeMeta, value: str
) -> bool