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
-
nova_api.dao.is_valid_uuidv4(id_: str) → bool[source]¶ Checks that the id_ is indeed a UUIDv4 valid string without dashes.
Parameters: id – The ID to validate. Returns: True if id_ is an UUIDv4, False otherwise.
GenericDAO¶
-
class
nova_api.dao.GenericDAO(fields: dict = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None)[source]¶ Interface class for the implementation of Data Access Objects.
-
__init__(fields: dict = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None) → None[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
__weakref__¶ list of weak references to the object (if defined)
-
create(entity: nova_api.entity.Entity) → str[source]¶ Creates a new row in the database with data from entity.
Raises: NotEntityException – Raised if the entity argument is not of the return_class of this DAO :raises DuplicateEntityException: Raised if an entity with the same ID exists in the database already.
Parameters: entity – The instance to save in the database. Returns: The entity uuid.
-
get(id_: str) → Optional[nova_api.entity.Entity][source]¶ Recovers and entity with id_ from the database. The id_ must be the nova_api generated id_ which is a 32-char uuid v4.
Raises: - InvalidIDTypeException – If the UUID is not a string
- InvalidIDException – If the UUID is not a valid UUID v4 without ‘-‘.
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,
respectively. :return: A tuple with the totol 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.*Invalid filters won’t be considered.
Raises: - NotEntityException – If entity is not a return_class instance and filters are None.
- EntityNotFoundException – If the entity is not found in the database.
- InvalidFiltersException – If filters is not None and is not a dict.
- 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 :return: Number of affected rows.
-
update(entity: nova_api.entity.Entity) → str[source]¶ Updates an entity on the database.
Raises: - NotEntityException – If entity is not a return_class instance.
- EntityNotFoundException – If the entity is not found in the database.
Parameters: entity – The entity with updated values to update on the database.
Returns: The id_ of the updated entity.
-
GenericSQLDAO¶
-
class
nova_api.dao.generic_sql_dao.GenericSQLDAO(database_type: Type[nova_api.persistence.PersistenceHelper] = None, database_instance: nova_api.persistence.PersistenceHelper = 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: nova_api.persistence.PersistenceHelper = 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 database with data from entity.
Raises: NotEntityException – Raised if the entity argument is not of the return_class of this DAO :raises DuplicateEntityException: Raised if an entity with the same ID exists in the database already.
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
-
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: - InvalidIDTypeException – If the UUID is not a string
- InvalidIDException – If the UUID is not a valid UUID v4 without ‘-‘.
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, respectively.
Returns: A tuple with the totol 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.*Invalid filters won’t be considered.
Raises: - NotEntityException – If entity is not a return_class instance and filters are None.
- EntityNotFoundException – If the entity is not found in the database.
- InvalidFiltersException – If filters is not None and is not a dict.
- 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.
-
update(entity: nova_api.entity.Entity) → str[source]¶ Updates an entity on the database.
Raises: - NotEntityException – If entity is not a return_class instance.
- EntityNotFoundException – If the entity is not found in the database.
Parameters: entity – The entity with updated values to update on the database.
Returns: The id_ of the updated entity.
-
MongoDAO¶
-
class
nova_api.dao.mongo_dao.MongoDAO(database='default', fields: dict = None, collection: str = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None, host: str = 'localhost', user: str = 'root', password: str = 'root', database_instance=None)[source]¶ Mongo 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='default', fields: dict = None, collection: str = None, return_class: Type[nova_api.entity.Entity] = <class 'nova_api.entity.Entity'>, prefix: str = None, host: str = 'localhost', user: str = 'root', password: str = 'root', database_instance=None) → 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 database with data from entity.
Raises: NotEntityException – Raised if the entity argument is not of the return_class of this DAO :raises DuplicateEntityException: Raised if an entity with the same ID exists in the database already.
Parameters: entity – The instance to save in the database. Returns: The entity uuid.
-
get(id_: str) → Optional[nova_api.entity.Entity][source]¶ Recovers and entity with id_ from the database. The id_ must be the nova_api generated id_ which is a 32-char uuid v4.
Raises: - InvalidIDTypeException – If the UUID is not a string
- InvalidIDException – If the UUID is not a valid UUID v4 without ‘-‘.
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=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,
respectively. :return: A tuple with the totol 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.*Invalid filters won’t be considered.
Raises: - NotEntityException – If entity is not a return_class instance and filters are None.
- EntityNotFoundException – If the entity is not found in the database.
- InvalidFiltersException – If filters is not None and is not a dict.
- 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 :return: Number of affected rows.
-
update(entity: nova_api.entity.Entity) → str[source]¶ Updates an entity on the database.
Raises: - NotEntityException – If entity is not a return_class instance.
- EntityNotFoundException – If the entity is not found in the database.
Parameters: entity – The entity with updated values to update on the database.
Returns: The id_ of the updated entity.
-