Skip to main content

A unified, trigger-aware database interface for MySQL, PostgreSQL, and MongoDB.

Project description

Flash โ€” Unified Database Interface

Flash is a lightweight Python library that gives you one clean API for MySQL, PostgreSQL, and MongoDB โ€” with built-in trigger hooks, no models required.

from flash import FlashDB

flash = FlashDB("mysql", config)
flash.add("users", {"name": "John", "age": 25})
flash.where("users", {"age": {">": 18}})

๐Ÿ“ฆ Installation

pip install pyflashdb

# With specific driver:
pip install pyflashdb[mysql]      # MySQL
pip install pyflashdb[postgres]   # PostgreSQL
pip install pyflashdb[mongodb]    # MongoDB
pip install pyflashdb[all]        # All drivers

๐Ÿ”Œ Connection

MySQL

from flash import FlashDB

flash = FlashDB("mysql", {
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "1234",
    "database": "mydb"
})

PostgreSQL

flash = FlashDB("postgres", {
    "host": "localhost",
    "port": 5432,
    "user": "postgres",
    "password": "1234",
    "database": "mydb"
})

MongoDB

flash = FlashDB("mongodb", {
    "host": "localhost",
    "port": 27017,
    "database": "mydb"
    # Or use: "uri": "mongodb+srv://..."
})

Context Manager

with FlashDB("mysql", config) as flash:
    flash.add("users", {"name": "John"})
# Connection auto-closed

๐Ÿงฉ CRUD Operations

โž• Insert

# Single record
flash.add("users", {"name": "Alice", "age": 22})

# Bulk insert
flash.bulk_insert("users", [
    {"name": "Alice", "age": 22},
    {"name": "Bob", "age": 30},
])

๐Ÿ“– Read

# All records
flash.all("users")

# With filters
flash.where("users", {"name": "Alice"})
flash.where("users", {"age": {">": 18}})

# Specific fields
flash.select("users", fields=["name", "age"])

# Combined
flash.select("users",
    fields=["name", "age"],
    filters={"age": {">": 18}},
    order_by="age",
    limit=10
)

# Single record
flash.find_one("users", {"name": "Alice"})

# Count
flash.count("users", {"age": {">": 18}})

# Limit
flash.limit("users", 5)

# Paginate
flash.paginate("users", page=2, size=10)
# Returns: {"data": [...], "page": 2, "size": 10, "total": 42}

โœ๏ธ Update

flash.update("users", {"name": "Alice"}, {"age": 23})

๐Ÿ—‘๏ธ Delete

flash.delete("users", {"name": "Alice"})

๐Ÿ” Filter Operators

Flash uses a unified filter syntax that automatically translates to SQL or MongoDB:

Flash Operator SQL MongoDB
> > $gt
< < $lt
>= >= $gte
<= <= $lte
!= != $ne
in IN $in
not in NOT IN $nin
like LIKE $regex
flash.where("users", {"age": {">": 18}})
flash.where("users", {"role": {"in": ["admin", "mod"]}})
flash.where("users", {"name": {"like": "Jo%"}})
flash.where("users", {"score": {">=": 80, "<=": 100}})

๐Ÿ”” Trigger Hooks

Flash supports before/after hooks for all operations โ€” even on MongoDB where native triggers don't exist.

Decorator Style

@flash.before_insert("users")
def validate(data):
    if not data.get("name"):
        raise ValueError("Name is required")

@flash.after_insert("users")
def on_created(data, result):
    print(f"New user created with ID: {result}")

@flash.before_update("users")
def log_update(payload):
    print("Updating:", payload["filters"], "->", payload["data"])

@flash.after_delete("users")
def on_deleted(filters, count):
    print(f"Deleted {count} record(s)")

Programmatic Style

def audit_log(data):
    print("Insert:", data)

flash.add_hook("before", "insert", "users", audit_log)

Available Hooks

Hook When it fires
before_insert(table) Before add() or bulk_insert()
after_insert(table) After add() or bulk_insert()
before_update(table) Before update()
after_update(table) After update()
before_delete(table) Before delete()
after_delete(table) After delete()
before_select(table) Before all(), select(), where()
after_select(table) After any read operation

Manage Hooks

flash.list_hooks()          # See all registered hooks
flash.clear_hooks()         # Remove all hooks
flash.clear_hooks("users")  # Remove hooks for one table

๐Ÿ“„ Schema Operations

# Create table/collection
flash.create_table("users", {
    "id": "int",
    "name": "str",
    "email": "str",
    "age": "int",
    "score": "float"
})

# Drop table
flash.drop_table("users")

# Truncate (keep structure, delete data)
flash.truncate("users")

# List tables
flash.show_tables()

# Describe structure
flash.describe("users")

Supported Type Strings

Flash Type MySQL PostgreSQL
int INT INTEGER
str VARCHAR(255) VARCHAR(255)
text TEXT TEXT
float FLOAT REAL
bool TINYINT(1) BOOLEAN
date DATE DATE
datetime DATETIME TIMESTAMP
json JSON JSONB

๐Ÿ” Transactions (SQL)

flash.begin()
try:
    flash.add("accounts", {"user": "Alice", "balance": 500})
    flash.update("accounts", {"user": "Bob"}, {"balance": 100})
    flash.commit()
except Exception:
    flash.rollback()

๐Ÿงฐ Raw Queries

# SQL
flash.raw("SELECT * FROM users WHERE age > %s", [18])

# MongoDB (pass raw filter dict)
flash.raw({"age": {"$gt": 18}}, table="users")

๐Ÿƒ MongoDB-Specific

# Aggregation pipeline
flash.aggregate("orders", [
    {"$match": {"status": "paid"}},
    {"$group": {"_id": "$user_id", "total": {"$sum": "$amount"}}},
    {"$sort": {"total": -1}}
])

# Create index
flash.create_index("users", "email", unique=True)

๐Ÿ”ง Utilities

flash.ping()    # True if connection is alive
flash.close()   # Close connection

๐Ÿ“ Project Structure

flash/
โ”œโ”€โ”€ flash/
โ”‚   โ”œโ”€โ”€ __init__.py         # Package entry point
โ”‚   โ”œโ”€โ”€ core.py             # FlashDB main class
โ”‚   โ”œโ”€โ”€ filters.py          # Filter/query translation
โ”‚   โ”œโ”€โ”€ triggers.py         # Hook/trigger system
โ”‚   โ”œโ”€โ”€ exceptions.py       # Custom exceptions
โ”‚   โ””โ”€โ”€ adapters/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ mysql_adapter.py
โ”‚       โ”œโ”€โ”€ postgres_adapter.py
โ”‚       โ””โ”€โ”€ mongo_adapter.py
โ”œโ”€โ”€ setup.py
โ””โ”€โ”€ README.md

๐Ÿ“œ License

MIT ยฉ Flash Team

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

pyflashdb-1.0.3.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

pyflashdb-1.0.3-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file pyflashdb-1.0.3.tar.gz.

File metadata

  • Download URL: pyflashdb-1.0.3.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for pyflashdb-1.0.3.tar.gz
Algorithm Hash digest
SHA256 9455e9866dfc4fa36585f821dbf8a55f7d97a9bf1c3f611982feff5663089f7c
MD5 e29ce691861623a65adac9bdb2876f0b
BLAKE2b-256 5cfe72bc3a241f96172197c1c4e1beb3a38633bdde0f41ede0db0162f139e9af

See more details on using hashes here.

File details

Details for the file pyflashdb-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyflashdb-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for pyflashdb-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8858e517656a52e1482afb47ec6d1183868b40fc3d37916ef64442b95729080c
MD5 5765815df0a3494b230f6781455997c7
BLAKE2b-256 a385ab0fbf878932a6573f4230b89a349036758e3d87af99086065f6d01ee1d4

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