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 flashdb

# With specific driver:
pip install flashdb[mysql]      # MySQL
pip install flashdb[postgres]   # PostgreSQL
pip install flashdb[mongodb]    # MongoDB
pip install flashdb[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.2.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.2-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyflashdb-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 f779b5cec7146d0afe60102f3975184d884680e38ed28dab38ed145947fa30a3
MD5 8d3ee7be86128620e81f3f12208b9660
BLAKE2b-256 e9791778fd1a688d43dbf717393d7c32067829ccb10e2836e0e448d2ad7840d1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyflashdb-1.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cffde90e93819beb4da973efc02f09f4c65ccc8a2736564459835be1a2182180
MD5 9bf652b665126a71401dadb6037bdb42
BLAKE2b-256 d98512750338e27d86d5f76d8bf7779a2fc04153b7d83e02871b72bfceb59c4f

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