Skip to main content

Database ORM Connections and Queries

Project description

Databases Session Connections and ORM Queries

Donate License PyPi PyPI Downloads Code style: black Build Status Python

Install only sqlite

pip install ddcDatabases

Install All databases

pip install ddcDatabases[all]

Install MSSQL

pip install ddcDatabases[mssql]

Install MYSQL

pip install ddcDatabases[mysql]

Install PostgreSQL

pip install ddcDatabases[pgsql]

Install Oracle

pip install ddcDatabases[oracle]

Install MONGODB

pip install ddcDatabases[mongodb]

Databases

  • Parameters for all classes are declared as OPTIONAL falling back to .env file variables
  • All examples are using db_utils.py
  • By default, the MSSQL class will open a session to the database, but the engine can be available at session.bind
  • SYNC sessions defaults:
    • autoflush is True
    • expire_on_commit is True
    • echo is False
  • ASYNC sessions defaults:
    • autoflush is True
    • expire_on_commit is False
    • echo is False

SQLITE

class Sqlite(
    filepath: Optional[str] = None,
    echo: Optional[bool] = None,
    autoflush: Optional[bool] = None,
    expire_on_commit: Optional[bool] = None,
    extra_engine_args: Optional[dict] = None,
)

Session

import sqlalchemy as sa
from ddcDatabases import DBUtils, Sqlite
with Sqlite() as session:
    utils = DBUtils(session)
    stmt = sa.select(TableModel).where(TableModel.id == 1)
    results = utils.fetchall(stmt)
    for row in results:
        print(row)

MSSQL

class MSSQL(        
    host: Optional[str] = None,
    port: Optional[int] = None,
    user: Optional[str] = None,
    password: Optional[str] = None,
    database: Optional[str] = None,
    schema: Optional[str] = None,
    echo: Optional[bool] = None,
    pool_size: Optional[int] = None,
    max_overflow: Optional[int] = None,
    autoflush: Optional[bool] = None,
    expire_on_commit: Optional[bool] = None,
    extra_engine_args: Optional[dict] = None,
)

Sync Example

import sqlalchemy as sa
from ddcDatabases import DBUtils, MSSQL
with MSSQL() as session:
    stmt = sa.select(TableModel).where(TableModel.id == 1)
    db_utils = DBUtils(session)
    results = db_utils.fetchall(stmt)
    for row in results:
        print(row)

Async Example

import sqlalchemy as sa
from ddcDatabases import DBUtilsAsync, MSSQL
async with MSSQL() as session:
    stmt = sa.select(TableModel).where(TableModel.id == 1)
    db_utils = DBUtilsAsync(session)
    results = await db_utils.fetchall(stmt)
    for row in results:
        print(row)

PostgreSQL or MySQL

class PostgreSQL(
    host: Optional[str] = None,
    port: Optional[int] = None,
    user: Optional[str] = None,
    password: Optional[str] = None,
    database: Optional[str] = None,
    echo: Optional[bool] = None,
    autoflush: Optional[bool] = None,
    expire_on_commit: Optional[bool] = None,
    engine_args: Optional[dict] = None,
)

Sync Examples

import sqlalchemy as sa
from ddcDatabases import DBUtils, PostgreSQL
with PostgreSQL() as session:
    stmt = sa.select(TableModel).where(TableModel.id == 1)
    db_utils = DBUtils(session)
    results = db_utils.fetchall(stmt)
    for row in results:
        print(row)
import sqlalchemy as sa
from ddcDatabases import DBUtils, MySQL
with MySQL() as session:
    stmt = sa.text("SELECT * FROM users")
    db_utils = DBUtils(session)
    results = db_utils.fetchall(stmt)
    for row in results:
        print(row)

Async Example

import sqlalchemy as sa
from ddcDatabases import DBUtilsAsync, PostgreSQL
async with PostgreSQL() as session:
    stmt = sa.select(TableModel).where(TableModel.id == 1)
    db_utils = DBUtilsAsync(session)
    results = await db_utils.fetchall(stmt)
    for row in results:
        print(row)

Oracle

class Oracle(
    host: Optional[str] = None,
    port: Optional[int] = None,
    user: Optional[str] = None,
    password: Optional[str] = None,
    servicename: Optional[str] = None,
    echo: Optional[bool] = None,
    autoflush: Optional[bool] = None,
    expire_on_commit: Optional[bool] = None,
    extra_engine_args: Optional[dict] = None,

Sync Example using arguments instead of .env file

credentials = {
    "host": "127.0.0.1",
    "user": "system",
    "password": "oracle",
    "servicename": "xe",
    "echo": False,
}

import sqlalchemy as sa
from ddcDatabases import DBUtils, Oracle
with Oracle(**credentials) as session:
    stmt = sa.text("SELECT * FROM system.help")
    db_utils = DBUtils(session)
    results = db_utils.fetchall(stmt)
    for row in results:
        print(row)

MongoDB

class PostgreSQL(
    host: Optional[str] = None,
    port: Optional[int] = None,
    user: Optional[str] = None,
    password: Optional[str] = None,
    database: Optional[str] = None,
    batch_size: Optional[int] = None,
    limit: Optional[int] = None,
)

Sync Example using arguments instead of .env file

credentials = {
    "host": "127.0.0.1",
    "user": "admin",
    "password": "admin",
    "database": "admin",
}

from ddcDatabases.mongodb import MongoDB
from bson.objectid import ObjectId
with MongoDB(**credentials) as mongodb:
    query = {"_id": ObjectId("6772cf60f27e7e068e9d8985")}
    collection = "movies"
    with mongodb.cursor(collection, query) as cursor:
        for each in cursor:
            print(each)

ORM Engines

Using PostgreSQL as example

Sync Engine

from ddcDatabases import PostgreSQL
with PostgreSQL() as session:
    engine = session.bind
    ...

Async Engine

from ddcDatabases import PostgreSQL
async with PostgreSQL() as session:
    engine = await session.bind
    ...

ORM DBUtils and DBUtilsAsync

  • Take an open session as parameter
  • Can use SQLAlchemy statements
  • Execute function can be used to update, insert or any SQLAlchemy.text
from ddcDatabases import DBUtils
db_utils = DBUtils(session)
db_utils.fetchall(stmt)                     # returns a list of RowMapping
db_utils.fetchvalue(stmt)                   # fetch a single value, returning as string
db_utils.insert(stmt)                       # insert into model table
db_utils.deleteall(model)                   # delete all records from model
db_utils.insertbulk(model, list[dict])      # insert records into model from a list of dicts
db_utils.execute(stmt)                      # this is the actual execute from session

Source Code

Build

poetry build -f wheel

Run Tests and Get Coverage Report using Poe

poetry update --with test
poe tests

License

Released under the MIT License

Buy me a cup of coffee

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

ddcdatabases-1.0.24.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

ddcdatabases-1.0.24-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file ddcdatabases-1.0.24.tar.gz.

File metadata

  • Download URL: ddcdatabases-1.0.24.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for ddcdatabases-1.0.24.tar.gz
Algorithm Hash digest
SHA256 b1e97d6d901f883dd23fb37b7f9a359f9d696bb5f49a2b6358916f24d6bf26ab
MD5 a2fec1dea2176b2b3a5d1cafa29fd0df
BLAKE2b-256 9bfd98d715f6c672ab2ee9c964d57a3a835bec5c5b97a482a0f7694dfb61b21a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddcdatabases-1.0.24.tar.gz:

Publisher: workflow.yml on ddc/ddcDatabases

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ddcdatabases-1.0.24-py3-none-any.whl.

File metadata

  • Download URL: ddcdatabases-1.0.24-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for ddcdatabases-1.0.24-py3-none-any.whl
Algorithm Hash digest
SHA256 8fde3cc72165ae2a6fd7cfe5a6e1c59aa0db0f91bc1e7063b2792e86cdc8c875
MD5 c4a420dbc8bb4a962ef995adf4301b92
BLAKE2b-256 dd5dc679070569ec8be8345c04bd812b470f39a6f9d22fb32c61c50e92fe3d99

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddcdatabases-1.0.24-py3-none-any.whl:

Publisher: workflow.yml on ddc/ddcDatabases

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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