Skip to main content

SQLite ORM using Pydantic models - simple, type-safe, high-performance SQLite operations with connection pooling

Project description

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

  • ๐Ÿ”— 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.
  • ๐Ÿงช Battle Tested - 300+ tests and built-in stress testing/benchmarking tools.

๐Ÿค” 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.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.

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

wsqlite-1.2.3.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

wsqlite-1.2.3-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file wsqlite-1.2.3.tar.gz.

File metadata

  • Download URL: wsqlite-1.2.3.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for wsqlite-1.2.3.tar.gz
Algorithm Hash digest
SHA256 998d43578e884ed3a19773322d7014c6932c898af3acae82e3cf1584aa4c03a4
MD5 21c91104dfb7eacd1d1391035b879af2
BLAKE2b-256 3f7a45ef7eddbf9c96cd1d67c83525af59a79010790d467920fe5035b6178e95

See more details on using hashes here.

File details

Details for the file wsqlite-1.2.3-py3-none-any.whl.

File metadata

  • Download URL: wsqlite-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for wsqlite-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d63bed8357a9c0694ced4a4d437d1bcae196f574ad5f9ceb0f876fdfbad5f851
MD5 4ed35b028c9197adc92728c3828dfc2a
BLAKE2b-256 ed565f5099c25fd745785db9aa950eda2d12756d830625099e749957883b613e

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