Wrapper for connection to database
Project description
ConnectKit Database [en|ru]
ConnectKit Database is a wrapper under SQLAlchemy with some utils.
Include pydantic settings, custom json serializer, template code.
Installation
Three types of connectors are supported:
-[x] PostgreSQL (sync/async) -[x] MySQL (MariaDB) (sync/async) -[x] Sqlite3 (sync/async)
By default, the DB connector package is not installed, extensions are specified for installation.
To install sync versions:
pip install ConnectKit-Database[postgresql] # Установка коннектора PostgreSQL
pip install ConnectKit-Database[mysql] # Установка коннектора MySQL/MariaDB
pip install ConnectKit-Database[all] # Установка всех sync коннекторов
To install async versions:
pip install ConnectKit-Database[asyncpg] # Установка коннектора PostgreSQL
pip install ConnectKit-Database[aiomysql] # Установка коннектора MySQL/MariaDB
pip install ConnectKit-Database[aiosqlite] # Установка коннектора Sqlite3
pip install ConnectKit-Database[asyncall] # Установка всех async коннекторов
Usage
Environment variables are used for connection by default. Variables are extracted from the environment:
DB_ADDR= # Address for default connection to postgres or mysql(mariadb) (default: None)
DB_PORT=5432 # Port for default connection to postgres or mysql(mariadb)
DB_ADAPTER=postgresql # Select default connection dialect (from postgresql, mysql and sqlite)
DB_USERNAME=postgres # Username for default connection postgres or mysql(mariadb)
DB_PASSWORD= # Username for default connection postgres or mysql(mariadb) (default: None)
DB_NAME=postgres # Database for postgres or mysql(mariadb), filepath for sqlite
DB_POOL_TIMEOUT=1 # Global pool timeout for creating new session to DB
DB_ECHO: bool = False # Log all sql statements (for debug purposes)
These variables can be overridden:
from database.settings import settings
settings.DB_ECHO = False
!! Attention !! After creating a default connection, changing the settings variables for it is ignored.
To open a connection, the Database
and AsyncDatabase
context managers are used:
from database import Database, AsyncDatabase, AsyncSession
with Database() as db:
db.execute(...)
async with AsyncDatabase() as db:
await db.execute(...)
# For FastAPI:
async def db_dependency() -> AsyncSession:
async with AsyncDatabase() as db:
yield await db.execute(...)
The default Base can be used to create models:
from database import Base
from sqlalchemy.orm import Mapped
class Model(Base):
id: Mapped[int]
To initialize ORM models via Base for default connection:
from database import init_default_base, async_init_default_base
from database import Base
init_default_base(Base.metadata)
await async_init_default_base(Base.metadata)
License
ConnectKit Database is MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file connectkit_database-1.3.6.tar.gz
.
File metadata
- Download URL: connectkit_database-1.3.6.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.19.2 CPython/3.10.11 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16c68ff3bbd18039f2d2beebcf46335b58872364db3001b710dde6e2e8861d0b |
|
MD5 | 27cb80a99a4278b775b9262cc9705812 |
|
BLAKE2b-256 | 753e05bd813f28d08601a0a5478da9ab4f319e5f60c9fa26ad2fed90d4569338 |
File details
Details for the file connectkit_database-1.3.6-py3-none-any.whl
.
File metadata
- Download URL: connectkit_database-1.3.6-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.19.2 CPython/3.10.11 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6489a86a5c9fc69032b28b84879622aa518d094ba492442ca7c3f51dc21ddf9 |
|
MD5 | 1d7b828a395965940dbc43bd0bca7e96 |
|
BLAKE2b-256 | e71257dc2c69d685f7431c774c14229b7f9c1d43ace90b5b7acd5bfd01a6c72e |