Skip to main content

Simple connection manager for sqlalchemy.

Project description

easydbs

Easydbs is a connection manager for people who are too lazy to learn sqlalchemy.
Its goal is to do simple interactions with relational database. (See Supported Databases.md).
This module is made for people that use sqlmodel in their fastapi application and want to manage several database connections.
You can also use this module to simplifies migration between databases.
You can have connections to multiple databases and create a session using your connection as a python decorator.
Is this module useful ? Not really, but it was created before knowing that sqlmodel existed. Check it out.
Easydbs tries also to be like any PEP249 complient database api. Read more.

Basic usage

Define the model of your table with sqlmodel.

from sqlmodel import SQLModel, Field

class Hero(SQLModel, table=True):  
    id: int | None = Field(default=None, primary_key=True)  
    name: str  
    secret_name: str  

Select all the rows of your table.

import easydbs
from sqlmodel import select

sqlite = easydbs.connect(easydbs.SQLITE, database="app.db")
with sqlite.session() as session:
    results = session.exec(select(Hero)).all()
    for row in results:
        print(row)

Use the connection as a decorator

You can use the connection object as a decorator.
You'll have to pass a session parameter that will be automatically close after the end of the function. This will work with sync and async functions.

import easydbs
from sqlmodel import Session

sqlite = easydbs.connect(easydbs.SQLITE, database="app.db")

@sqlite
def insert_hero(session: Session, hero: Hero):
    session.add(hero)
    session.commit()

hero = Hero(id=1, name="Peter Parker", secret_name="Spiderman")
insert_hero(hero)

Connect to the database with arguments or sqlalchemy connection string

import easydbs
postgre = easydbs.connect(
    db_type=easydbs.POSTGRE,
    username="testuser",
    password="testpassword",
    host="localhost",
    port=5433,
    database="testdb",
)
postgre = easydbs.connect(connection_string="postgresql://testuser:testpassword@localhost:5433/testdb")

Create tables

import easydbs

sqlite = easydbs.connect(easydbs.SQLITE, database="app.db")

sqlite.create_tables(tables_names=["hero"]) # Create hero tables if not exists.
sqlite.create_tables() # Create all tables defined in SQLModel.

Multiple connections

We can easily manage several connections.
You can use the connection manager or use the function easydbs.connect. The connection will be automatically added to the connection manager.

import easydbs

cm = easydbs.ConnectionManager()

easydbs.connect(db_type=easydbs.SQLITE, database= 'app.db')
easydbs.connect(
    db_type=easydbs.MYSQL,
    username="testuser",
    password="testpassword",
    host="localhost",
    port=3306,
    database="testdb",
)

for conn in cm.connections():
    conn.create_tables(tables_names=["hero"])
    with conn.session() as session:
        hero = Hero(name="Peter Parker", secret_name="Spiderman")
        session.add(hero)
        session.commit()

Access to connections like a dictionnary.

When you create a connection an id is created with {backend_name}+{database}.

cm["sqlite+app.db"]
cm["mysql+testdb"]

Use with pandas

Because the connections are sqlalchemy connection, you can use them with pandas or polars.

import pandas as pd
import easydbs

sqlite = easydbs.connect(easydbs.SQLITE, database="app.db")

df = pd.read_sql('hero', con=sqlite.engine)

Use easydbs connections like an standard python database api

Like any pep249 complient python api. You can use methods like connect, cursor, commit, rollback etc...

import easydbs

sqlite = easydbs.connect(easydbs.SQLITE, database="app.db")
cursor = sqlite.cursor()
result = cursor.execute("INSERT INTO hero VALUES (2, 'Bruce Wayne', 'Batman')")
sqlite.commit()
sqlite.close()

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

easydbs-1.0.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

easydbs-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file easydbs-1.0.0.tar.gz.

File metadata

  • Download URL: easydbs-1.0.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for easydbs-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a255c9dd464b39cc759a54f19678239f492c8c6579bb98e83b685af662062431
MD5 8b88c8c9bb7ed8d8c29ef0859f62f5eb
BLAKE2b-256 388496fe7aa473d6716dc6a853e800057f5a1a07e5ffdd60d73c047551a7c600

See more details on using hashes here.

File details

Details for the file easydbs-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: easydbs-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for easydbs-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db0c0cd23eb2200d8282edca914e4776ce03e4e1f0615f4abcfb0ad75556744a
MD5 102c2f8db7016d51f22bb2aa30d093ea
BLAKE2b-256 77cce859b948605c96672a22b6d712bc47ca179dfe492877285cec29c678596f

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