joop.dao

Data Access Object (DAO) module for joop.

This module provides abstract classes for creating Data Access Objects (DAOs) to manage Pydantic and SQLModel-based models. DAOs provide a layer of abstraction between the representation of the object (especially at the storage layer) and the business logic code that uses or manipulates the object.

Classes:
DAO:

An abstract wrapper for a Pydantic model or any class derived from it.

SQLDAO:

An abstract class for SQL models, extending the DAO class.

Classes

DAO()

An abstract wrapper for a Pydantic model or any class derived from it.

SQLDAO()

An abstract class for SQL models, extending the DAO class.

class joop.dao.DAO[source]

Bases: object

An abstract wrapper for a Pydantic model or any class derived from it.

The DAO class provides methods to interact with the underlying model, including converting it to a dictionary and retrieving its fields. It is typically constructed around an existing model using the from_model method.

_modeltype

The type of the model, defaulting to pydantic.BaseModel.

Type:

Type

Properties:

model (pydantic.BaseModel): The underlying Pydantic model instance.

from_model(model

pydantic.BaseModel) -> ‘DAO’: Creates a DAO instance from a given Pydantic model.

to_dict() dict[source]

Converts the underlying model to a dictionary using aliases for field names.

get_model_fields() List[str][source]

Retrieves the names of all fields defined in the _modeltype.

classmethod from_model(model: BaseModel) DAO[source]
classmethod get_model_fields() List[str][source]

Get the names of all fields defined in the _modeltype.

Returns:

A list of field names.

Return type:

List[str]

Raises:

TypeError – If _modeltype is not a subclass of pydantic.BaseModel.

property model: BaseModel
to_dict() dict[source]

Convert the underlying model to a dictionary, using aliases for field names.

Returns:

A dictionary representation of the model with aliases as keys.

Return type:

dict

Raises:
  • ValueError – If no model is set for this DAO instance.

  • TypeError – If the model does not have Pydantic fields.

class joop.dao.SQLDAO[source]

Bases: DAO

An abstract class for SQL models, extending the DAO class.

The SQLDAO class provides additional methods for interacting with SQLModel-based models, such as retrieving all records from the database.

_modeltype

The type of the model, defaulting to sqlmodel.SQLModel.

Type:

Type

get_all(session

sqlmodel.Session) -> List[‘SQLDAO’]: Retrieves all records from the database for the given model type and returns them as a list of SQLDAO instances.

classmethod from_model(model: BaseModel) DAO
classmethod get_all(session: Session) List[SQLDAO][source]

Retrieve all records from the database for the given model type and return them as a list of SQLDAO instances.

Parameters:

session (sqlmodel.Session) – The database session to use for the query.

Returns:

A list of SQLDAO instances for the model type.

Return type:

List[SQLDAO]

Raises:

TypeError – If _modeltype is not a subclass of sqlmodel.SQLModel.

classmethod get_model_fields() List[str]

Get the names of all fields defined in the _modeltype.

Returns:

A list of field names.

Return type:

List[str]

Raises:

TypeError – If _modeltype is not a subclass of pydantic.BaseModel.

property model: BaseModel
to_dict() dict

Convert the underlying model to a dictionary, using aliases for field names.

Returns:

A dictionary representation of the model with aliases as keys.

Return type:

dict

Raises:
  • ValueError – If no model is set for this DAO instance.

  • TypeError – If the model does not have Pydantic fields.