Skip to main content

A lightweight ORM built on Pydantic for simple CRUD operations with minimal code

Project description

Ormantism

A tiny, simple ORM built on top of Pydantic.

When you need to perform simple CRUD operations with minimal code.

Offers support for PostgreSQL, MySQL, SQLite (database URL syntax is the same as in SQLAlchemy).

Features

  • Simple Model Declaration: Define your models using familiar Pydantic syntax
  • Automatic Table Creation: Tables are created automatically when first accessed
  • Lazy Loading: Relationships are loaded on-demand for optimal performance
  • Transaction Support: Built-in transaction management with automatic rollback
  • Preloading: Efficiently load related data with JOIN queries
  • Optional Timestamps: Add created_at, updated_at, deleted_at fields automatically

Installation

pip install ormantism

Quick Start

1. Connect to Database

import ormantism

# Connect to a file database
ormantism.connect("sqlite:///my_app.db")

# Or use in-memory database for testing
ormantism.connect("sqlite://:memory:")

# MySQL
ormantism.connect("mysql://login:password@host:port/database")

# PostgresSQL
ormantism.connect("postgresql://login:password@host:port/database")

2. Define Models

from ormantism import Base
from typing import Optional

class User(Base):
    name: str
    email: str
    age: Optional[int] = None

class Post(Base, with_timestamps=True):
    title: str
    content: str
    author: User

3. Create and Save Records

# Create a user
user = User(name="Alice", email="alice@example.com", age=30)
# The record is automatically saved to the database

# Create a post linked to the user
post = Post(title="My First Post", content="Hello World!", author=user)

4. Query Records

# Load by ID
user = User.load(id=1)

# Load by criteria
user = User.load(name="Alice")
user = User.load(email="alice@example.com")

# Load latest post from alice@example.com
latest_post = Post.load(user_id=user.id, last_created=True)

# Load all records
users = User.load_all()

# Load with criteria
users_named_alice = User.load_all(name="Alice")

5. Update Records

user = User.load(id=1)
user.age = 31  # Automatically saved to database
# or
user.update(age=31, email="alice.updated@example.com")

6. Delete Records

user = User.load(id=1)
user.delete()

Advanced Features

Timestamps

Add automatic timestamp tracking to your models:

class Post(Base, with_timestamps=True):
    title: str
    content: str

This adds created_at, updated_at, and deleted_at fields. Soft deletes are used when timestamps are enabled.

Relationships and Lazy Loading

class Author(Base):
    name: str

class Book(Base):
    title: str
    author: Author

# Create records
author = Author(name="Jane Doe")
book = Book(title="My Book", author=author)

# Lazy loading - author is loaded from DB when accessed
book = Book.load(id=1)
print(book.author.name)  # Database query happens here

Preloading (Eager Loading)

Avoid N+1 queries by preloading relationships:

# Load book with author in a single query
book = Book.load(id=1, preload="author")
print(book.author.name)  # No additional database query

# Preload nested relationships
book = Book.load(id=1, preload="author.publisher")

# Preload multiple relationships
book = Book.load(id=1, preload=["author", "category"])

Transactions

from ormantism import transaction

try:
    with transaction() as t:
        user1 = User(name="Alice", email="alice@example.com")
        user2 = User(name="Bob", email="bob@example.com")
        # Both users are saved automatically
        # Transaction commits when exiting the context
except Exception:
    # Transaction is automatically rolled back on any exception
    pass

Querying Examples

# Load single record
user = User.load(name="Alice")
latest_user = User.load(last_created=True)

# Load multiple records
all_users = User.load_all()
users_named_alice = User.load_all(name="Alice")

# Include soft-deleted records (when using timestamps)
all_including_deleted = User.load_all(with_deleted=True)

Model Definition

Basic Model

class User(Base):
    name: str
    email: str
    age: int = 25  # Default value
    bio: Optional[str] = None  # Nullable field

With Timestamps

class Post(Base, with_timestamps=True):
    title: str
    content: str
    # Automatically adds: created_at, updated_at, deleted_at

Supported Field Types

  • int, float, str
  • Optional[T] for nullable fields
  • list, dict (stored as JSON)
  • datetime.datetime
  • enum.Enum
  • Pydantic models (stored as JSON)
  • References to other Base models

Relationships

class Category(Base):
    name: str

class Post(Base):
    title: str
    category: Category  # Foreign key relationship
    tags: Optional[Category] = None  # Nullable relationship

API Reference

Base Class Methods

Creating Records

  • Model() - Create and automatically save a new record

Querying

  • Model.load(**criteria) - Load single record
  • Model.load(last_created=True) - Load most recently created record
  • Model.load_all(**criteria) - Load multiple records
  • Model.load(preload="relationship") - Eager load relationships
  • Model.load(with_deleted=True) - Include soft-deleted records

Updating

  • instance.update(**kwargs) - Update multiple fields
  • instance.field = value - Update single field (auto-saves)

Deleting

  • instance.delete() - Delete record (soft delete if timestamps enabled)

Database Functions

  • ormantism.connect(database_url) - Connect to database
  • ormantism.transaction() - Get transaction context manager

Limitations

  • Simple Queries: Complex queries may require raw SQL
  • No Migrations: Schema changes require manual handling
  • Basic Relationships: Only supports simple foreign key relationships

Requirements

  • Python 3.12+
  • Pydantic

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

ormantism-0.5.8.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

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

ormantism-0.5.8-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file ormantism-0.5.8.tar.gz.

File metadata

  • Download URL: ormantism-0.5.8.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for ormantism-0.5.8.tar.gz
Algorithm Hash digest
SHA256 f0a3b061a47bf33186c3b28cc1faaec691b6d92debdafe1fe7fef5ccb10c4190
MD5 f53ba0f2e2aa13586f86a80c95b20eac
BLAKE2b-256 dee8c9d949f3be1369d9fc2ea5e54fecb17838a8adce350c24a38f90452cc0fc

See more details on using hashes here.

File details

Details for the file ormantism-0.5.8-py3-none-any.whl.

File metadata

  • Download URL: ormantism-0.5.8-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for ormantism-0.5.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6c6d308f4d2f4ef58ebcc9e9bc88ffc0aa36d638c3a0d0c285782aed2d82408c
MD5 dbe4727cc8d9e67b1539b337937bc393
BLAKE2b-256 cbfa02d6d7be975c04893879f1eb2ad7a691cb39972b52b15a7319a64cd3e9e3

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