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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f779b5cec7146d0afe60102f3975184d884680e38ed28dab38ed145947fa30a3
|
|
| MD5 |
8d3ee7be86128620e81f3f12208b9660
|
|
| BLAKE2b-256 |
e9791778fd1a688d43dbf717393d7c32067829ccb10e2836e0e448d2ad7840d1
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cffde90e93819beb4da973efc02f09f4c65ccc8a2736564459835be1a2182180
|
|
| MD5 |
9bf652b665126a71401dadb6037bdb42
|
|
| BLAKE2b-256 |
d98512750338e27d86d5f76d8bf7779a2fc04153b7d83e02871b72bfceb59c4f
|