db

class pydantic_extra.db.DB

Bases: pydantic.BaseModel, abc.ABC

The base class for database configuration

type: str
abstract property connect_str: str | sqlalchemy.URL

string for sqlalchemy.create_engine() (maybe cached_property)

property engine: sqlalchemy.Engine

Creates sqlalchemy.Engine (maybe cached_property)

session() sqlalchemy.orm.Session

Creates sqlalchemy.orm.Session

setup() None

Configures SQLAlchemy for working with this dialect

Deprecated since version 1.1, will be removed in version 2.0: Use setup_engine().

setup_engine(engine: sqlalchemy.Engine) sqlalchemy.Engine

Configures an instance of sqlalchemy.Engine for working with this dialect.

engine() applies this method.

Added in version 1.1.

class pydantic_extra.db.SQLite

Bases: DB

Configuration for the SQLite database

type: Literal['sqlite']
path: pathlib.Path

the path to the database

class pydantic_extra.db.MySQL

Bases: DB, CustomLibraryMixin

Configuration for MySQL and MariaDB databases

type: Literal['mysql', 'mariadb']
host: str

database server address

port: int = 3306

database server port

login: str

login for authorization on the database server

password: SecretStr

password for authorization on the database server

encoding: str = "utf8mb4"

database encoding

database: str

the name of the database on the database server

library: str = "pymysql"

Используемая библиотека. Способы замены.

class pydantic_extra.db.Mysql

Alias for MySQL.

Deprecated since version 1.1, will be removed in version 2.0: Use MySQL.

class pydantic_extra.db.AnyDB

Bases: DB

Configuration for any database

type: Literal['any']
str_: str

In the configuration, use str.

The string for SQLAlchemy

type pydantic_extra.db.T_DB = SQLite | MySQL | AnyDB

Example:

from pydantic import BaseModel, Field

from pydantic_extra.db import T_DB


class Config(BaseModel):
    db: T_DB = Field(discriminator="type")