Skip to main content

A simple database management abstraction layer built on SQLAlchemy

Project description

🚀 DBFlux: Lightweight Database Management Library

PyPI - Version Python License Downloads

🛠️ Version 1.0.2

🌟 Introduction

DBFlux is a lightweight, easy-to-use library built on top of SQLAlchemy to simplify database operations in Python.

It provides a streamlined interface for connecting to databases, managing sessions, and performing CRUD operations with minimal effort.


Features

  • 🔁 Automatic Transaction Management
  • 🛠️ Session Handling
  • 🔗 Flexibility – Supports multiple database engines via SQLAlchemy
  • ⚡ Lightweight & Efficient
  • 🔍 Advanced Filtering
  • 📥 Data Insertion
  • ✏️ Data Modification
  • 📄 Easy Pagination
  • 🛡️ Safe Deletion
  • 📦 Consistent Output Handling

📚 Requirements

  • Python 3.8+
  • SQLAlchemy >= 2.0

🔧 Installation

Install dbflux via pip:

pip install dbflux

Or install from source:

git clone https://github.com/abbas-bachari/dbflux.git
cd dbflux
pip install .

💡 Quick Start

from dbflux  import Sqlite,DBModel
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.orm import declarative_base
from time import time

Base=declarative_base()
db = Sqlite(db_name="example.db")


class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    name = Column(String(50))
    email = Column(String(100))
    age = Column(Integer)
    def __repr__(self):
        return f"User(id={self.id}, name={self.name}, email={self.email}, age={self.age})"

class Order(Base):
    __tablename__ = "orders"
    order_id = Column(Integer, primary_key=True)
    product = Column(String, nullable=False)
    price = Column(Float, nullable=False)
    time = Column(Integer, nullable=False)
    
    def to_dict(self):
        return {
            "order_id": self.order_id,
            "product": self.product,
            "price": self.price,
            "time": self.time
        }
    
    def __str__(self):
        return json.dumps(self.to_dict(), indent=4,ensure_ascii=False)

    def __repr__(self):
        return f"Order(order_id={self.order_id}, product={self.product}, price={self.price}, time={self.time})"


db.create_tables(Base)


users=DBModel(User,db)
orders=DBModel(Order,db)


users_data=[
    {"id": 1, "name": "Alice", "email": "alice@test.com","age":22},
    {"id": 2, "name": "Bob", "email": "bob@test.com","age":21},
    {"id": 3, "name": "Carol", "email": "carol@test.com","age":18}
]

orders_data=[
    {"order_id": 1, "product": "Product A", "price": 100, "time": time()},
    {"order_id": 2, "product": "Product B", "price": 200, "time": time()},
    {"order_id": 3, "product": "Product C", "price": 300, "time": time()}
]

users.insert(users_data)
orders.insert(orders_data)

💡 Examples Usage DBFactory

Base = declarative_base()

class Order(Base):
    __tablename__ = "orders"

    order_id = Column(Integer, primary_key=True)
    product = Column(String, nullable=False)
    price = Column(Float, nullable=False)
    time = Column(Integer, nullable=False)
    
    def to_dict(self):
        return {
            "order_id": self.order_id,
            "product": self.product,
            "price": self.price,
            "time": self.time
        }
    
    def __str__(self):
        return json.dumps(self.to_dict(), indent=4,ensure_ascii=False)

    def __repr__(self):
        return f"Order(order_id={self.order_id}, product={self.product}, price={self.price}, time={self.time})"
    


factory = DBFactory(db_name="data.db")

db = factory.create("sqlite")

db.create_tables(Base)

orders_db = DBModel(Order ,db)


order = Order(order_id=1, product="Product A", price=100, time=time())

orders_db.insert( order)

orders:list[Order] = orders_db.get(limit=1)

print(orders)

>>> [Order(order_id=1, product=Product A, price=100.0, time=1755924289.1132557)]

print(orders[0])

>>> {
    "order_id": 1,
    "product": "Product A",
    "price": 100.0,
    "time": 1755924289.1132557
    }

🔹 Supported Database Types

Type Aliases
SQLite sqlite
MySQL mysql
PostgreSQL postgres, postgresql
MariaDB mariadb
Oracle oracle
DB2 db2, ibmdb2
Firebird firebird
MSSQL mssql, sqlserver

🔹 Examples for Different Databases

from dbflux.databases import Sqlite, MySQL, PostgreSQL

# Example 1: SQLite
sqlite_db = Sqlite(db_name="data.db")
sqlite_db.create_tables(Base)
sqlite_db.insert(model_class= Order ,data=Order(order_id=10, product="SQLite Product", price=50, time=time()))

# Example 2: MySQL
mysql_db = MySQL(db_name="test_db",username="root", password="password", host="localhost", )
mysql_db.create_tables(Base)
mysql_db.insert(model_class= Order ,data=Order(order_id=11, product="MySQL Product", price=60, time=time()))

# Example 3: PostgreSQL
postgres_db = PostgreSQL(db_name="test_db",username="postgres", password="secret", host="localhost")
postgres_db.create_tables(Base)
postgres_db.insert(model_class= Order ,data=Order(order_id=12, product="PostgreSQL Product", price=70, time=time()))

🎯 Summary of Features

✅ CRUD Operations

✅ Bulk Insert & Bulk Update

✅ Advanced Filtering (OR/AND/Range)

✅ Pagination

✅ JSON Output

✅ Transaction Safety

✅ Direct SQLAlchemy Access via BaseDB


📖 Documentation

For more details, visit the official SQLAlchemy documentation.


📜 License

This project is licensed under the MIT License.


👤 Publisher / ناشر

Abbas Bachari / عباس بچاری


💖 Sponsor

Support development by sponsoring on Github Sponsors.

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

dbflux-1.0.2.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

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

dbflux-1.0.2-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file dbflux-1.0.2.tar.gz.

File metadata

  • Download URL: dbflux-1.0.2.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for dbflux-1.0.2.tar.gz
Algorithm Hash digest
SHA256 332684da6c46758328e73a5543b544a28c52a1759df72aaf399c8b4987242b66
MD5 5a0fde3d60342c35e4cd94df636c5614
BLAKE2b-256 b51f11b2f5ea890515e54e29b4a83ed2379007073594ee1ededd4f7cac52475b

See more details on using hashes here.

File details

Details for the file dbflux-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: dbflux-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for dbflux-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9edda4ea4e7e489d301bd8512896ece5e4b698401d900b2f2ff426f279b52858
MD5 c97b4545a61f0293f6677aca96fe2411
BLAKE2b-256 c7528ce0d62a80344fb202d0346ef42047db3e48b2d881b8860f220d796cac47

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