Skip to main content

wsqlite 🚀

SQLite ORM using Pydantic models - Simple, Type-Safe, and High-Performance.

PyPI version Python versions License Docs


wsqlite is a high-level Python ORM library that provides a clean, type-safe interface for SQLite database operations. By leveraging Pydantic v2, it ensures data integrity and offers a modern development experience with automatic schema synchronization and full async support.

📖 Table of Contents

🌟 Key Features

  • 🕵️ Enterprise Forensic Automation - Ghost table (_forensic_audit_log) audit trail for all data mutations (INSERT, UPDATE, DELETE).
  • 🗂️ Multi-Table Manager & Registry - Multi-model registration with dictionary indexing app[User] and dynamic attribute dispatch app.user.
  • 🔗 Pydantic Integration - Define your database schema using standard Pydantic v2 models.
  • 🔄 Auto Schema Sync - Tables are created and synchronized automatically when your models change.
  • ⚡ Connection Pooling - High-performance, thread-safe connection pool with WAL mode enabled by default.
  • 📝 Intuitive CRUD - Simple methods for insert, get, update, and delete.
  • 🔍 Smart Migration - Automatically adds new columns when detected in the model.
  • 🔒 Advanced Constraints - Native support for Primary Keys, UNIQUE (single & composite), NOT NULL, and Foreign Keys.
  • 🛡️ Type Safety - Full type hints and runtime validation powered by Pydantic.
  • ⚡ Async Ready - Full async/await support for high-performance applications.
  • 🔨 Query Builders - Safe, fluent API for complex queries with JOINs, GROUP BY, and more.
  • 🔄 Versioned Migrations - Robust system for schema versioning and upgrades/rollbacks.

🛠️ Key Technologies & Libraries Used

  • Python 3.9+ - Primary programming language
  • Pydantic v2 - Modern schema definition and data validation
  • sqlite3 & aiosqlite - SQLite connectors for synchronous and asynchronous execution
  • loguru - High-performance structured logging framework
  • click - Command-line interface builder
  • pytest & pytest-cov - Automated unit testing and coverage evaluation

🤔 Why wsqlite?

Most Python ORMs are either too heavy (SQLAlchemy) or too basic (raw sqlite3). wsqlite fills the gap by providing:

  1. Zero Boilerplate: No need to write CREATE TABLE statements or manual migrations for new columns.
  2. Developer Experience: Use the same Pydantic models for your API (FastAPI) and your Database.
  3. Safety First: SQL Injection protection and strict type validation out of the box.
  4. Performance: Optimized connection pooling and WAL mode for concurrent applications.

📊 Performance at a Glance

🏆 wSQLite Benchmark Results:
   - Throughput: ~5,000+ insertions/second
   - Latency: ~0.2ms average
   - Footprint: <10MB memory overhead

📦 Installation

pip install wsqlite

For development or benchmarking:

pip install -e ".[dev,benchmark,stress]"

🚀 Quick Start in 60 Seconds

Define and Insert

from typing import Optional
from pydantic import BaseModel, Field
from wsqlite import WSQLite

class User(BaseModel):
    id: Optional[int] = Field(None, description="primary autoincrement")
    name: str
    email: str = Field(..., description="unique")

# Initializing creates the table automatically
db = WSQLite(User, "app.db")

# Type-safe insertion
db.insert(User(name="John Doe", email="john@example.com"))

# Easy querying
users = db.get_all()
john = db.get_by_field(email="john@example.com")

Async Operations

async def main():
    await db.insert_async(User(name="Jane Doe", email="jane@example.com"))
    users = await db.get_all_async()

🛠️ SQL Constraints Reference

wsqlite uses the description parameter of Pydantic's Field to define SQL constraints in a declarative way.

Constraint Syntax in Field(description="...") Example
Primary Key "primary" id: int = Field(..., description="primary")
Auto-increment "primary autoincrement" id: Optional[int] = Field(None, description="primary autoincrement")
Unique "unique" email: str = Field(..., description="unique")
Not Null "not null" name: str = Field(..., description="not null")
Composite Unique "unique:group_name" col1: str = Field(..., description="unique:my_group")
Foreign Key "references:table.column" user_id: int = Field(..., description="references:user.id")

Example Model with all Constraints

class Employee(BaseModel):
    # Auto-incrementing Primary Key
    id: Optional[int] = Field(None, description="primary autoincrement")

    # Required and Unique
    employee_code: str = Field(..., description="unique not null")

    # Composite Unique (unique together: department + position)
    department: str = Field(..., description="unique:job_role")
    position: str = Field(..., description="unique:job_role")

    # Foreign Key
    office_id: int = Field(..., description="references:office.id")

🎯 Advanced Functionality

Composite Unique Constraints

class Profile(BaseModel):
    username: str = Field(..., description="unique:account")
    provider: str = Field(..., description="unique:account")
# (username, provider) must be unique together

Foreign Keys

class Post(BaseModel):
    title: str
    author_id: int = Field(..., description="references:user.id")

Powerful Query Builder

from wsqlite.builders import QueryBuilder

results = (
    QueryBuilder("users")
    .select("id", "name")
    .join("orders", "users.id = orders.user_id", "LEFT")
    .where("status", "=", "active")
    .group_by("users.id")
    .having("COUNT(orders.id)", ">", 5)
    .execute(conn)
)

📁 Project Structure

  • src/wsqlite/: Core library logic.
  • examples/: Comprehensive usage examples (CRUD, Transactions, Relationships, etc.).
  • test/: Full unit and integration test suite.
  • benchmark/: Performance testing tools.

🧪 Testing & Quality

We maintain high standards with extensive testing:

# Run unit tests
pytest

# Run stress tests
python -m stress_test.run --scenario concurrent --records 100000

📈 Version History

  • v1.2.4 - 337+ tests, 78% coverage. Improved migrations and query builder.
  • v1.2.2 - Added Composite Uniques, Foreign Keys, and Autoid support. Fixed transaction bugs.
  • v1.2.0 - 90%+ test coverage, async connection pool.
  • v1.1.0 - Connection pooling, advanced query builder, migrations.

📝 License & Author

Distributed under the MIT License. Created with ❤️ by William Steve Rodriguez Villamizar.

Metadata

Release files for wsqlite 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wsqlite 1.3.0
File Size Uploaded
wsqlite-1.3.0.tar.gz 166.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for wsqlite 1.3.0
File Interpreter ABI Platform
wsqlite-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 204.6 kB

Release files / wsqlite-1.3.0.tar.gz

Download URL wsqlite-1.3.0.tar.gz
Size 166.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8f3279c3ccd3b0a92af736b97eeaece3b818ac340373239e48654e2f506ba680
BLAKE2b-256 checksum
How to use checksums
f179a2e8f970c0da3fa1054a236e63a69cfce7652b97a0835aa688d42bc39d27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / wsqlite-1.3.0-py3-none-any.whl

Download URL wsqlite-1.3.0-py3-none-any.whl
Size 37.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
98f7e174eab56df1d761ffab2480827f9d9f948244da4dd145aa1b2a64e9e57a
BLAKE2b-256 checksum
How to use checksums
b83319d83fbbcd551cb8a2059f74f3929798149e3f095f2f178f66a6c899ce58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release history Release notifications | RSS feed

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

This release

1.3.0 This release

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page