Skip to main content

Lightweight ORM for MySQL and YugabyteDB

Project description

mydborm

PyPI version Python License: MIT

mydborm is a lightweight, developer-friendly ORM for MySQL 8+ and YugabyteDB (YSQL).
Zero bloat. Declarative models. Full CRUD. Schema migrations. CLI included.


Features

  • Declarative model definitions with field validation
  • Full CRUD — create, get, all, filter, update, delete, count, exists
  • Schema migration engine with history tracking
  • Dual database support — MySQL and YugabyteDB
  • Thread-safe connection manager with context manager support
  • DATABASE_URL environment variable support
  • Rich CLI — ping, inspect, tables, migrate
  • Zero mandatory dependencies beyond database drivers
  • Python 3.8+ compatible, platform independent

Installation

pip install mydborm

With CLI support:

pip install mydborm[cli]

Quickstart

1. Configure connection

from mydborm import db

# Direct config
db.configure(
    dialect  = "mysql",       # or "yugabyte"
    host     = "127.0.0.1",
    port     = 3306,
    user     = "root",
    password = "yourpassword",
    database = "mydb",
)

# Or via environment variable
# export DATABASE_URL="mysql://root:password@localhost:3306/mydb"
db.from_env()

2. Define models

from mydborm import BaseModel, IntField, StrField, BoolField, FloatField

class User(BaseModel):
    __tablename__ = "users"
    id       = IntField(primary_key=True)
    username = StrField(max_length=100, nullable=False)
    email    = StrField(max_length=255, nullable=False, unique=True)
    active   = BoolField(default=True)

class Product(BaseModel):
    __tablename__ = "products"
    id     = IntField(primary_key=True)
    name   = StrField(max_length=100, nullable=False)
    price  = FloatField(nullable=False)
    active = BoolField(default=True)

3. Run migrations

from mydborm.migrations import migrate, migration_status

migrate(User,    description="Create users table")
migrate(Product, description="Create products table")

for m in migration_status():
    print(m["description"], "→", "Applied" if not m["rolled_back"] else "Rolled back")

4. CRUD operations

# Create
uid = User.create(username="alice", email="alice@example.com", active=True)

# Read
user  = User.get(id=uid)
users = User.all()
devs  = User.filter(active=True)

# Update
User.update({"active": False}, id=uid)

# Delete
User.delete(id=uid)

# Aggregate
count  = User.count()
exists = User.exists(email="alice@example.com")

Field types

Field SQL Type (MySQL) SQL Type (YugabyteDB)
IntField INT INTEGER
StrField VARCHAR(n) VARCHAR(n)
TextField TEXT TEXT
BoolField TINYINT(1) BOOLEAN
FloatField FLOAT FLOAT
DecimalField DECIMAL(p,s) DECIMAL(p,s)
DateField DATE DATE
DateTimeField DATETIME TIMESTAMP
JSONField JSON JSONB
ForeignKeyField INT INTEGER

CLI commands

# Show version
mydborm version

# Test connectivity
mydborm ping --dialect mysql --host 127.0.0.1 --port 3306 --password root

# List all tables
mydborm tables --dialect mysql --port 3306 --password root

# Inspect schema
mydborm inspect --dialect mysql --port 3306 --password root

# Run migration for a model
mydborm migrate --dialect mysql --port 3306 --password root \
  --model myapp.models.User

# Show migration history
mydborm migrate --status --dialect mysql --port 3306 --password root

# Rollback last migration
mydborm migrate --rollback --dialect mysql --port 3306 --password root \
  --model myapp.models.User

YugabyteDB support

db.configure(
    dialect  = "yugabyte",
    host     = "127.0.0.1",
    port     = 5433,
    user     = "yugabyte",
    password = "yugabyte",
    database = "yugabyte",
)

mydborm automatically uses YSQL-compatible SQL:

  • Double-quoted identifiers
  • SERIAL primary keys
  • Native BOOLEAN
  • JSONB instead of JSON
  • RETURNING id on INSERT

Docker quickstart

services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydb
    ports:
      - "3306:3306"

  yugabyte:
    image: yugabytedb/yugabyte:latest
    command: bash -c "bin/yugabyted start --daemon=false"
    ports:
      - "5433:5433"
docker compose up -d

Project structure

mydborm/
├── mydborm/
│   ├── __init__.py       # Public API
│   ├── db.py             # Connection manager
│   ├── fields.py         # Field types
│   ├── model.py          # BaseModel + CRUD
│   ├── migrations.py     # Schema migration engine
│   ├── cli.py            # CLI commands
│   └── dialects/
│       ├── mysql.py      # MySQL SQL generation
│       └── yugabyte.py   # YugabyteDB SQL generation
├── tests/                # pytest test suite
├── examples/             # Usage examples
└── pyproject.toml

Running tests

pip install mydborm[dev]
pytest

Author

Atikrant Upadhye
PyPI · GitHub


License

MIT License — see LICENSE for details.

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

mydborm-0.2.0.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

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

mydborm-0.2.0-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file mydborm-0.2.0.tar.gz.

File metadata

  • Download URL: mydborm-0.2.0.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for mydborm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fdd95b1d445f4fc46e51f882a2144c12f71b18b45bf5188b719f8751c31044bb
MD5 cc5fc0a62b3873bbbf1f223fd93c2d7b
BLAKE2b-256 583e8428231b775bfe4442b9e76eb563ec25eb819baaf10fd90c107fba1cf121

See more details on using hashes here.

File details

Details for the file mydborm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mydborm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for mydborm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c663ea650e6855705c52da8d005b4413df195bd2635ecde9076bf6d80559a0eb
MD5 48bae24474370540f41b17f34bf3e5eb
BLAKE2b-256 9d5cdf49bef8bc8022c65cc5ddbd2269e3e44b83ee6d03ed8ee03ec14e1587d4

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