aiodesa.utils package

Submodules

aiodesa.utils.table module

Database Schema Definitions

Module provides classes and functions for defining database schema elements such as primary keys, unique keys, foreign keys, and table schema.

Classes:

  • ForeignKey: Represents a foreign key relationship in a database.

  • PrimaryKey: Represents primary key columns in a database table.

  • UniqueKey: Represents a unique key column in a table.

  • TableSchema: Represents the schema for a database table.

Functions:

  • set_key(): Decorator for setting primary keys, unique keys, and foreign keys on a class.

  • make_schema(): Generate a TableSchema based on the provided data class.

Usage examples can be found in the docstrings of each class and function.

Note

This module is intended for use with data classes and provides a convenient way to define database schema elements in Python code.

class aiodesa.utils.table.ForeignKey(key: str, table: str)[source]

Bases: NamedTuple

Represents a foreign key relationship in a database. :param key: The column name representing the foreign key. :param table: The name of the referenced table.

Example:

@set_key(ForeignKey(key='user_id', table='users'))

Note

Intended to be consumed by set_key()

key: str

Alias for field number 0

table: str

Alias for field number 1

class aiodesa.utils.table.PrimaryKey(column: str)[source]

Bases: NamedTuple

Represents primary key columns in a database table.

Parameters:

column – Primary key identifer.

Example:

# Define a primary key with the column names 'user_id' and 'post_id':
@set_key(PrimaryKey('user_id')

Note

Intended to be consumed by set_key()

column: str

Alias for field number 0

class aiodesa.utils.table.TableSchema(table_name: str, data: str)[source]

Bases: object

Represents the schema for a database table.

Parameters:
  • table_name – The name of the table.

  • data – The SQL data definition language (DDL) statement.

Example:

# Create a TableSchema for a 'users' table
user_table_schema = TableSchema(
    table_name='users',
    data='CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);')

Note

The data attribute contains the SQL data definition language (DDL).

data: str
table_name: str
class aiodesa.utils.table.UniqueKey(column: str)[source]

Bases: NamedTuple

Represents unique key column in a table.

Parameters:

column – column name representing unique key.

Example:

# Define a unique key with the column names 'username' and 'email':
user_unique_key = UniqueKey('username')

Note

Intended to be consumed by set_key()

column: str

Alias for field number 0

aiodesa.utils.table.make_schema(name: str, data_cls: Any) TableSchema[source]

Generate a TableSchema based on the provided data class.

Parameters:
  • name – The name of the table.

  • data_cls – A data class defining the schema for the table.

Returns:

An instance of TableSchema containing the table_name and SQL data definition.

Return type:

TableSchema

Example:

user_table_schema = generate_table_schema(name='users', data_cls=User)

Note

The function returns a TableSchema instance containing the table_name and SQL data definition.

aiodesa.utils.table.set_key(*args: PrimaryKey | UniqueKey | ForeignKey | tuple[ForeignKey, ...])[source]

Decorator for setting keys on a class.

Parameters:
  • *args – The keys to be set. Can include PrimaryKey, UniqueKey,

  • ForeignKey

  • ForeignKeys. (or a tuple of)

Returns:

A decorator function to set keys on a class.

Example:

@dataclass
@set_key(PrimaryKey(
    "username"), UniqueKey("id"), ForeignKey("username", "anothertable"
    ))
class Users:
    username: str
    id: str | None = None
    table_name: str = "users"

Note

Foreign keys can be specified individually or as a tuple.

aiodesa.utils.types module

SQL Data and Type Utilities

This module provides utilities for working with SQL data types and defining data class protocols.

Classes:

  • SQLDataType: Enumeration representing common SQL data types.

  • IsDataclass: Protocol indicating that a class is a data class.

Functions:

  • py_to_sql_type: Determine the SQL type of a Python primitive.

Usage Examples:

  1. SQL Data Types Enumeration:
    sql_type = SQLDataType.VARCHAR
    
  2. Determine SQL Type from Python Primitive:
    sql_type = py_to_sql_type("hello")
    print(sql_type)  # Output: "VARCHAR"
    
  3. Data Class Protocol:
    
    
    class MyDataClass(IsDataclass):

    __dataclass_fields__: ClassVar[Dict]

Note

SQLDataType provides a convenient way to represent common SQL data types. py_to_sql_type is used to determine the SQL type of a Python primitive. IsDataclass is a protocol indicating that a class is a data class.

class aiodesa.utils.util_types.SQLDataType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Enumeration representing common SQL data types.

BIGINT = 'BIGINT'
BOOLEAN = 'BOOLEAN'
CHAR = 'CHAR'
DATE = 'DATE'
DATETIME = 'DATETIME'
DECIMAL = 'DECIMAL'
DOUBLE = 'DOUBLE'
FLOAT = 'FLOAT'
INT = 'INT'
INTEGER = 'INTEGER'
NONE = 'NULL'
PRIMARY = ' PRIMARY KEY'
SMALLINT = 'SMALLINT'
TEXT = 'TEXT'
UNIQUE = ' UNIQUE KEY'
VARCHAR = 'VARCHAR'
aiodesa.utils.util_types.py_to_sql_type(data: Any) str[source]

Determine the SQL type of a python primitive.

Parameters:

data – The Python variable / primitive.

Returns:

The corresponding SQL data type.

Raises:

ValueError – If the data type is not supported.

Example:

sql_type = py_to_sql_type("hello")
print(sql_type)  # Output: "VARCHAR"

Module contents