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).

  • 👁️ Database Views (@view) - Declarative SQLite View definition with topological depends_on=[...] DDL ordering and read-only ORM querying.

  • 🗂️ 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.4.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.4.0
File Size Uploaded
wsqlite-1.4.0.tar.gz 168.1 kB Details

Built distribution (wheel)

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

Total release size: 207.0 kB

Release files / wsqlite-1.4.0.tar.gz

Download URL wsqlite-1.4.0.tar.gz
Size 168.1 kB
Tags Source
SHA-256 checksum
How to use checksums
44fbbe78811ef737a540e67609db0817d7ff7ff50f6bfe5bee4942a76e432f17
BLAKE2b-256 checksum
How to use checksums
0941670320fb7e1bf20402c8c509b0106b76db5137c716b5c5e296acc01cc25f
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.4.0-py3-none-any.whl

Download URL wsqlite-1.4.0-py3-none-any.whl
Size 38.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
310a181c09b06766c1ec68861201cbc90bee1ffbd4962286c2fb4c7d6f1de802
BLAKE2b-256 checksum
How to use checksums
ab676136898e6360a656044c6be3625ec0e5a5fd4f04cf72ba73f36fbec03613
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

This release

1.4.0 This release

2 release files

1.3.0

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