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 built-in sqlite3), extras are specified for installation.

To install sync versions:

pip install ConnectKit-Database[postgresql]  # Install driver for PostgreSQL
pip install ConnectKit-Database[mysql]       # Install driver for MySQL/MariaDB
pip install ConnectKit-Database[all]         # Install all sync drivers

To install async versions:

pip install ConnectKit-Database[asyncpg]        # Install driver for PostgreSQL
pip install ConnectKit-Database[aiomysql]       # Install driver for MySQL/MariaDB
pip install ConnectKit-Database[aiosqlite]      # Install driver for Sqlite3
pip install ConnectKit-Database[asyncall]       # Install all async drivers

Usage


Environment variables are used for connection by default. Variables are extracted from the environment or .env file:

DB_ADDR=               # Address for default connection to postgres or mysql(mariadb)
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=           # Password for default connection postgres or mysql(mariadb)
DB_NAME=postgres       # Database for postgres or mysql(mariadb), filepath for sqlite
DB_POOL_RECYCLE=3600   # Global pool recycle timeout for driver session
DB_ECHO: bool = False  # Global log all sql statements (for debug purposes)

These variables are frozen.

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)

or via context managers:

from database import Database, AsyncDatabase
from database import Base

Database().init_base(Base.metadata)

await (AsyncDatabase().init_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.2.0.tar.gz (8.5 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.2.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: connectkit_database-2.2.0.tar.gz
  • Upload date:
  • Size: 8.5 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.2.0.tar.gz
Algorithm Hash digest
SHA256 c4de5c67ab6de897870ca2bb2bcd592040bd5e909e94127ada5be648ea6c5221
MD5 e8d5d34d244297bff17bc789bf05c78b
BLAKE2b-256 ffe564becd5b3ef2ec056e51497c21c3e0af707e68377fa96301e74f641c23af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for connectkit_database-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac30216245155c2b97bbe61a429e8bcbb2c614c20cda604eab5544ad1e7a3ee8
MD5 f1cde3d82fb67bda2ae4aa32c060b215
BLAKE2b-256 d40e3f8a920abc3ad435dc1ea9c4ba4a460424c235553f761dce5f80a3b8751b

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