Skip to main content

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 (exclude default sqlite), 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

connectkit_database-2.0.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

connectkit_database-2.0.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file connectkit_database-2.0.1.tar.gz.

File metadata

  • Download URL: connectkit_database-2.0.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.21.0 CPython/3.10.11 Windows/10

File hashes

Hashes for connectkit_database-2.0.1.tar.gz
Algorithm Hash digest
SHA256 a41f01dd1485a7349302e8d10b65c0bb16444565e47f48c6f88444dbe90b12dd
MD5 7978baecf8415b42246f8de063cd4a4d
BLAKE2b-256 0e4ea5e55015f4e4876a8fb49e5a2b96a19b9234f52c0611c181ccd08de23f78

See more details on using hashes here.

File details

Details for the file connectkit_database-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for connectkit_database-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7e6c4ee6eaea60e63d3d1fd358d80802a0a79fbecedf7f2a4fb08bf72b6c591
MD5 d0eb717052c707b92870d63a06af119e
BLAKE2b-256 c57aaf005244f75d54530a02c974532081b16bc685c701cadf252627163e76ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page