DAO module¶
Quick Start¶
For a quick start and a high level description of the usage of this module, refer to Creating your DAO Class.
Module Documentation¶
Module for Data Access Objects implementation
-
nova_api.dao.camel_to_snake(name: str)[source]¶ Converts a camel case name to snake case.
Parameters: name – String in camel case to be converted Returns: String in snake case
GenericDAO¶
-
class
nova_api.dao.GenericDAO(table: str = None, fields: dict = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None, **kwargs)[source]¶ Interface class for the implementation of Data Access Objects.
-
__init__(table: str = None, fields: dict = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None, **kwargs) → None[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
__weakref__¶ list of weak references to the object (if defined)
-
GenericSQLDAO¶
-
class
nova_api.dao.generic_sql_dao.GenericSQLDAO(database_type: Type[nova_api.persistence.PersistenceHelper] = None, database_instance=None, table: str = None, fields: dict = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None, **kwargs)[source]¶ SQL implementation for the GenericDAO interface
-
__class__¶ alias of
abc.ABCMeta
-
__delattr__¶ Implement delattr(self, name).
-
__dir__()¶ Default dir() implementation.
-
__eq__¶ Return self==value.
-
__format__()¶ Default object formatter.
-
__ge__¶ Return self>=value.
-
__getattribute__¶ Return getattr(self, name).
-
__gt__¶ Return self>value.
-
__hash__¶ Return hash(self).
-
__init__(database_type: Type[nova_api.persistence.PersistenceHelper] = None, database_instance=None, table: str = None, fields: dict = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None, **kwargs) → None[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
__init_subclass__()¶ This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
-
__le__¶ Return self<=value.
-
__lt__¶ Return self<value.
-
__ne__¶ Return self!=value.
-
__new__()¶ Create and return a new object. See help(type) for accurate signature.
-
__reduce__()¶ Helper for pickle.
-
__reduce_ex__()¶ Helper for pickle.
-
__repr__¶ Return repr(self).
-
__setattr__¶ Implement setattr(self, name, value).
-
__sizeof__()¶ Size of object in memory, in bytes.
-
__str__¶ Return str(self).
-
__subclasshook__()¶ Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
-
__weakref__¶ list of weak references to the object (if defined)
-
create(entity: nova_api.entity.Entity) → str[source]¶ Creates a new row in the databse with data from entity.
Parameters: entity – The instance to save in the database. Returns: The entity uuid.
-
create_table_if_not_exists() → None[source]¶ Creates the table in the database based on the return_class attributes. The types used in the database will be inferred through predict_db_types or through the field metadata in the “type” key.
Returns: None
-
generate_filters(filters: dict) -> (<class 'str'>, typing.List[str])[source]¶ Converts a dict of filters to apply to a query to a SQL query format.
Example
>>> dao.generate_filters( ... filters={"id_": "12345678901234567890123456789012", ... "creation_datetime": [">", "2020-1-1"]}) ("WHERE id_ = %s AND creation_datetime > %s", ["12345678901234567890123456789012", "2020-1-1"])
Raises: - ValueError – If filters is None.
- TypeError – If filters is not a dict
Parameters: filters – dictionary of filters to apply. The key must be a property of return_class and the value may be only the values, if equality is expected or a list with the comparator and the value.
Returns: a tupĺe with the where statement and the list of params to use
-
get(id_: str) → Optional[nova_api.entity.Entity][source]¶ Recovers one entity with id_ from the database.
The id_ must be the nova_api generated id_ which is a 32-char uuid v4.
Raises: - TypeError – If the UUID is not a string
- ValueError – If the UUID is not a valid UUID.
Parameters: id – The UUID of the instance to recover
Returns: None if no instance is found or a return_class instance if found
-
get_all(length: int = 20, offset: int = 0, filters: dict = None) -> (<class 'int'>, typing.List[nova_api.entity.Entity])[source]¶ - Recovers all instances that match the given filters up to the
- length specified starting from the offset given.
The filters should be given as a dictionary, available keys are the return_class attributes. The values may be only the desired value or a list with the comparator in the first position and the value in the second.
Example
>>> dao.get_all(length=50, offset=0, ... filters={"birthday":[">", "1/1/1998"], ... "name":"John"}) (2, [ent1, ent2])
Parameters: - length – The number of items to select
- offset – The number of items to skip before starting to select
- filters – A dict with the filters to use. The key must be a valid attribute in the entity and the value may either be an specific value or a list with two elements: an operator and a value.
Returns: A tuple with the totoal number of entities in the database and a list of the matched results.
-
remove(entity: nova_api.entity.Entity = None, filters: dict = None) → int[source]¶ Removes entities from database. May be called either with an instance of return_class or a dict of filters. If both are passed, the instance will be removed and the filters won’t be considered.
Raises: - RuntimeError – If entity is not a return_class instance and filters are None or if filters is not None and is not a dict.
- AssertionError – If the entity is not found in the database.
- NoRowsAffectedException – If no rows are affected by the delete query.
Parameters: - entity – return_class instance to delete.
- filters – Filters to apply to delete query in dict format as specified by generate_filters
Returns: Number of affected rows.
-