vyra_base.storage package¶
Submodules¶
vyra_base.storage.db_access module¶
- class vyra_base.storage.db_access.DBSTATUS(*values)[source]
-
Enumeration of database operation status codes.
- Variables:
SUCCESS – Operation completed successfully.
ERROR – An error occurred during the operation.
NOT_FOUND – Requested resource was not found.
NOT_ALLOWED – Operation is not allowed.
NOT_AUTHORIZED – User is not authorized for this operation.
CONFLICT – Operation conflicts with existing data.
- SUCCESS = 'success'
- ERROR = 'error'
- NOT_FOUND = 'not_found'
- NOT_ALLOWED = 'not_allowed'
- NOT_AUTHORIZED = 'not_authorized'
- CONFLICT = 'conflict'
- class vyra_base.storage.db_access.DBTYPE(*values)[source]
-
Enumeration of supported database types.
- Variables:
SQLITE – SQLite database engine.
MYSQL – MySQL database engine.
POSTGRESQL – PostgreSQL database engine.
- SQLITE = 'sqlite'
- MYSQL = 'mysql'
- POSTGRESQL = 'postgresql'
- class vyra_base.storage.db_access.DBMESSAGE[source]
Bases:
objectCollection of standard database error messages.
- Variables:
DEFAULT_ERROR – Generic error message for database operations.
- DEFAULT_ERROR = 'Something went wrong while processing the query. See the details.'
- class vyra_base.storage.db_access.DbAccess(module_name, db_config_path=None, db_config=None, db_type=DBTYPE.SQLITE)[source]
Bases:
StorageBaseclass for database access.
- __init__(module_name, db_config_path=None, db_config=None, db_type=DBTYPE.SQLITE)[source]
Initialize database object.
- Parameters:
- Raises:
ValueError – If neither db_config_path nor db_config is provided, or if db_config is not a dict.
- Return type:
None
- db_engine: AsyncEngine
- session()[source]
Create a session for the database.
- Returns:
A session object.
- Return type:
Union[sessionmaker,async_sessionmaker]
- async create_all_tables()[source]
Create all database tables that are children of the SQLAlchemy Base class.
This method will create all tables that are defined in the SQLAlchemy Base class. It will also create the tables if they do not exist. This method will not create the tables if they already exist.
- Returns:
Status of the operation.
- Return type:
- async create_selected_table(table_structs)[source]
Create new database table.
- async drop_table(table)[source]
Delete table from database.
- Parameters:
table (
Base) – Table class (SQLAlchemy declarative base).- Returns:
Status of the operation.
- Return type:
vyra_base.storage.db_manipulator module¶
Handling database datatables
- class vyra_base.storage.db_manipulator.DBReturnValue(status=None, value='', details='')[source]
Bases:
objectStandardized return value container for database operations.
Encapsulates the result of database operations with status, value, and details. Provides convenience methods for setting error/success states.
- Variables:
status – Operation status (see DBSTATUS enum).
value – Main return value (data or error message).
details – Additional details about the operation.
- Parameters:
- __init__(status=None, value='', details='')[source]
- error_return(details='')[source]
Set the return value to error status.
- success_return()[source]
Set the return value to success status.
- Returns:
Self with success status set.
- Return type:
DBReturnValue
- class vyra_base.storage.db_manipulator.DbManipulator(**kwargs)[source]
Bases:
objectDatatable class manipulator
- __init__(**kwargs)
Wrapper for synchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- get_table_structure()[source]
Read the datatable structure from the config file.
- Returns:
Table structure information.
- Return type:
DBReturnValue
- async get_by_id(id=-1)[source]
Read line from datatable of database by ‘id’.
If the id is -1 or None, the last line will be read.
- async get_all(filters=None, order_by=None, limit=None)[source]
Read all lines from datatable of database.
- async update(data, filters=None)[source]
Update a line in a datatable of database by given data.
If the table config has a field ‘max_lines’ and the number of lines are greater than this field, the lowest ‘id’ value will be deleted and the new entry will be added by a incremented ‘id’ value.
- async add(data)[source]
Add a new a row in a datatable.
- Parameters:
data (
dict) – Content of the new entry to be added to the table.- Returns:
Add status and details.
- Return type:
DBReturnValue
- async get_one(filters=None)[source]
Read a single row from the database table, optionally filtered.
Returns the first matching model instance using
scalars().first(), consistent withget_all(). Use this instead ofget_by_idwhen you need to filter by arbitrary columns or want a guaranteed model instance (not a raw Row).
- async add_instance(obj)[source]
Add a pre-built model instance to the database.
Use this instead of
add()when you need full control over the object’s fields (e.g. custom primary keys or enum values) and have already constructed the model instance yourself.- Parameters:
obj (
Any) – A model instance (subclass of the declarative base).- Returns:
Add status and details.
- Return type:
DBReturnValue
- async delete(id)[source]
Update a line in a datatable of database by a given ‘id’.
- Parameters:
id (
Any) – Private key of the table element to select the row to be deleted.- Returns:
Delete status and details.
- Return type:
DBReturnValue
- async bulk_add(data)[source]
Add multiple rows to a datatable.
- async bulk_delete(filters)[source]
Delete multiple rows in a datatable by given filters.
- Parameters:
filters (
dict) – Filter elements to identify the rows to be deleted.- Returns:
Bulk delete status and details.
- Return type:
DBReturnValue
- async exists(id)[source]
Check if a row exists in the datatable by a given ‘id’.
- Parameters:
id (
int) – Private key of the table element to check.- Returns:
Existence status.
- Return type:
DBReturnValue
vyra_base.storage.storage module¶
vyra_base.storage.tb_base module¶
- class vyra_base.storage.tb_base.Base(**kwargs)[source]
Bases:
AsyncAttrs,DeclarativeBaseBase class for all SQLAlchemy models in the application.
- Parameters:
kwargs (Any)
- __init__(**kwargs)
A simple constructor that allows initialization from kwargs.
Sets attributes on the constructed instance using the names and values in
kwargs.Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.
- class vyra_base.storage.tb_base.IntEnum(enumtype, *args, **kwargs)[source]
Bases:
TypeDecoratorA SQLAlchemy TypeDecorator for storing Python IntEnum values as integers in the database.
This class allows seamless conversion between Python IntEnum members and their integer representation in the database. When binding parameters, it converts IntEnum members to their integer values. When retrieving results, it converts integers back to the corresponding IntEnum members.
- Parameters:
enumtype (Type[IntEnum]) – The IntEnum class to use for conversion.
- impl
alias of
Integer
- __init__(enumtype, *args, **kwargs)[source]
Construct a
TypeDecorator.Arguments sent here are passed to the constructor of the class assigned to the
implclass level attribute, assuming theimplis a callable, and the resulting object is assigned to theself.implinstance attribute (thus overriding the class attribute of the same name).If the class level
implis not a callable (the unusual case), it will be assigned to the same instance attribute ‘as-is’, ignoring those arguments passed to the constructor.Subclasses can override this to customize the generation of
self.implentirely.
- process_bind_param(value, dialect)[source]
Convert Python enum to database value before storing.
- Parameters:
value – Python enum value to convert.
dialect – SQLAlchemy dialect.
- Returns:
Integer value for database storage.
- process_result_value(value, dialect)[source]
Convert database value back to Python enum after retrieval.
- Parameters:
value – Database integer value.
dialect – SQLAlchemy dialect.
- Returns:
Python enum instance.
Module contents¶
VYRA Base Storage Module
Provides access to SQLite database and Redis storage.
Public API for external developers
Important: All database tables MUST use the tb_ prefix!
Example: tb_parameters, tb_sensor_data, tb_logs
- class vyra_base.storage.Base(**kwargs)[source]
Bases:
AsyncAttrs,DeclarativeBaseBase class for all SQLAlchemy models in the application.
- Parameters:
kwargs (Any)
- __init__(**kwargs)
A simple constructor that allows initialization from kwargs.
Sets attributes on the constructed instance using the names and values in
kwargs.Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.
- class vyra_base.storage.IntEnum(enumtype, *args, **kwargs)[source]
Bases:
TypeDecoratorA SQLAlchemy TypeDecorator for storing Python IntEnum values as integers in the database.
This class allows seamless conversion between Python IntEnum members and their integer representation in the database. When binding parameters, it converts IntEnum members to their integer values. When retrieving results, it converts integers back to the corresponding IntEnum members.
- Parameters:
enumtype (Type[IntEnum]) – The IntEnum class to use for conversion.
- impl
alias of
Integer
- __init__(enumtype, *args, **kwargs)[source]
Construct a
TypeDecorator.Arguments sent here are passed to the constructor of the class assigned to the
implclass level attribute, assuming theimplis a callable, and the resulting object is assigned to theself.implinstance attribute (thus overriding the class attribute of the same name).If the class level
implis not a callable (the unusual case), it will be assigned to the same instance attribute ‘as-is’, ignoring those arguments passed to the constructor.Subclasses can override this to customize the generation of
self.implentirely.
- process_bind_param(value, dialect)[source]
Convert Python enum to database value before storing.
- Parameters:
value – Python enum value to convert.
dialect – SQLAlchemy dialect.
- Returns:
Integer value for database storage.
- process_result_value(value, dialect)[source]
Convert database value back to Python enum after retrieval.
- Parameters:
value – Database integer value.
dialect – SQLAlchemy dialect.
- Returns:
Python enum instance.
- class vyra_base.storage.DbAccess(module_name, db_config_path=None, db_config=None, db_type=DBTYPE.SQLITE)[source]
Bases:
StorageBaseclass for database access.
- __init__(module_name, db_config_path=None, db_config=None, db_type=DBTYPE.SQLITE)[source]
Initialize database object.
- Parameters:
- Raises:
ValueError – If neither db_config_path nor db_config is provided, or if db_config is not a dict.
- Return type:
None
- db_engine: AsyncEngine
- session()[source]
Create a session for the database.
- Returns:
A session object.
- Return type:
Union[sessionmaker,async_sessionmaker]
- async create_all_tables()[source]
Create all database tables that are children of the SQLAlchemy Base class.
This method will create all tables that are defined in the SQLAlchemy Base class. It will also create the tables if they do not exist. This method will not create the tables if they already exist.
- Returns:
Status of the operation.
- Return type:
- async create_selected_table(table_structs)[source]
Create new database table.
- async drop_table(table)[source]
Delete table from database.
- Parameters:
table (
Base) – Table class (SQLAlchemy declarative base).- Returns:
Status of the operation.
- Return type:
- class vyra_base.storage.DBTYPE(*values)[source]
-
Enumeration of supported database types.
- Variables:
SQLITE – SQLite database engine.
MYSQL – MySQL database engine.
POSTGRESQL – PostgreSQL database engine.
- SQLITE = 'sqlite'
- MYSQL = 'mysql'
- POSTGRESQL = 'postgresql'
- class vyra_base.storage.DBSTATUS(*values)[source]
-
Enumeration of database operation status codes.
- Variables:
SUCCESS – Operation completed successfully.
ERROR – An error occurred during the operation.
NOT_FOUND – Requested resource was not found.
NOT_ALLOWED – Operation is not allowed.
NOT_AUTHORIZED – User is not authorized for this operation.
CONFLICT – Operation conflicts with existing data.
- SUCCESS = 'success'
- ERROR = 'error'
- NOT_FOUND = 'not_found'
- NOT_ALLOWED = 'not_allowed'
- NOT_AUTHORIZED = 'not_authorized'
- CONFLICT = 'conflict'
- class vyra_base.storage.DBMESSAGE[source]
Bases:
objectCollection of standard database error messages.
- Variables:
DEFAULT_ERROR – Generic error message for database operations.
- DEFAULT_ERROR = 'Something went wrong while processing the query. See the details.'
- class vyra_base.storage.DbManipulator(**kwargs)[source]
Bases:
objectDatatable class manipulator
- __init__(**kwargs)
Wrapper for synchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- get_table_structure()[source]
Read the datatable structure from the config file.
- Returns:
Table structure information.
- Return type:
DBReturnValue
- async get_by_id(id=-1)[source]
Read line from datatable of database by ‘id’.
If the id is -1 or None, the last line will be read.
- async get_all(filters=None, order_by=None, limit=None)[source]
Read all lines from datatable of database.
- async update(data, filters=None)[source]
Update a line in a datatable of database by given data.
If the table config has a field ‘max_lines’ and the number of lines are greater than this field, the lowest ‘id’ value will be deleted and the new entry will be added by a incremented ‘id’ value.
- async add(data)[source]
Add a new a row in a datatable.
- Parameters:
data (
dict) – Content of the new entry to be added to the table.- Returns:
Add status and details.
- Return type:
DBReturnValue
- async get_one(filters=None)[source]
Read a single row from the database table, optionally filtered.
Returns the first matching model instance using
scalars().first(), consistent withget_all(). Use this instead ofget_by_idwhen you need to filter by arbitrary columns or want a guaranteed model instance (not a raw Row).
- async add_instance(obj)[source]
Add a pre-built model instance to the database.
Use this instead of
add()when you need full control over the object’s fields (e.g. custom primary keys or enum values) and have already constructed the model instance yourself.- Parameters:
obj (
Any) – A model instance (subclass of the declarative base).- Returns:
Add status and details.
- Return type:
DBReturnValue
- async delete(id)[source]
Update a line in a datatable of database by a given ‘id’.
- Parameters:
id (
Any) – Private key of the table element to select the row to be deleted.- Returns:
Delete status and details.
- Return type:
DBReturnValue
- async bulk_add(data)[source]
Add multiple rows to a datatable.
- async bulk_delete(filters)[source]
Delete multiple rows in a datatable by given filters.
- Parameters:
filters (
dict) – Filter elements to identify the rows to be deleted.- Returns:
Bulk delete status and details.
- Return type:
DBReturnValue
- async exists(id)[source]
Check if a row exists in the datatable by a given ‘id’.
- Parameters:
id (
int) – Private key of the table element to check.- Returns:
Existence status.
- Return type:
DBReturnValue
- class vyra_base.storage.DBReturnValue(status=None, value='', details='')[source]
Bases:
objectStandardized return value container for database operations.
Encapsulates the result of database operations with status, value, and details. Provides convenience methods for setting error/success states.
- Variables:
status – Operation status (see DBSTATUS enum).
value – Main return value (data or error message).
details – Additional details about the operation.
- Parameters:
- __init__(status=None, value='', details='')[source]
- error_return(details='')[source]
Set the return value to error status.
- success_return()[source]
Set the return value to success status.
- Returns:
Self with success status set.
- Return type:
DBReturnValue
- class vyra_base.storage.RedisClient(**kwargs)[source]
Bases:
objectUnified Redis Client for vyra_base Combines RedisAccess and RedisManipulator functionality with TLS support and streaming capabilities for professional communication
- __init__(**kwargs)
Wrapper for synchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async connect(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async configure_base_settings(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async close(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async get(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async set(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async delete(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async exists(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async clear(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async get_all_keys(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async get_keys_by_pattern(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async get_type(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async get_length(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async publish_message(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async subscribe_channel(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async subscribe_to_key(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async unsubscribe_from_key(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async health_check(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async subscribe_pattern(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async publish_with_metadata(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async create_pubsub_listener(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async parse_message(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async get_active_listeners(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async remove_listener_channels(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async stop_pubsub_listener(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async publish_event(channel, data, add_metadata=True)[source]
Alias for publish_with_metadata (backward compatibility)
- async create_stream_listener(channels, callback_handler, callback_context=None)[source]
Alias for create_pubsub_listener (backward compatibility)
- async xadd(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xread(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xreadgroup(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xack(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xgroup_create(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xgroup_destroy(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xlen(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xtrim(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- async xpending(**kwargs)
Wrapper for asynchronous functions.
- Parameters:
args – Positional arguments.
kwargs – Keyword arguments.
- Returns:
Result of the wrapped function.
- class vyra_base.storage.REDIS_TYPE(*values)[source]
-
Redis data types supported by the client.
- STRING = 'string'
- HASH = 'hash'
- LIST = 'list'
- SET = 'set'
- class vyra_base.storage.Storage[source]
Bases:
objectBase class for storage access.
This class provides a common interface for different storage backends.
It can be used to access different storage backends like databases, files, etc.
- vyra_base.storage.tb_parameters
alias of
Parameter
- vyra_base.storage.tb_error_logs
alias of
ErrorLog