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.4.tar.gz (33.9 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.4-py3-none-any.whl (35.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wsqlite-1.2.4.tar.gz
  • Upload date:
  • Size: 33.9 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.4.tar.gz
Algorithm Hash digest
SHA256 3745b7cb13f02ed3e712f01ffb321477bfd85d77f5ffcb591df767176d4b8dd9
MD5 d48b8e31d68ee5f410fbc891cc654285
BLAKE2b-256 00aff4bd3c292328e0a97a59f15b030a7de32a2b364f0cf0ad56590f6ecc46bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wsqlite-1.2.4-py3-none-any.whl
  • Upload date:
  • Size: 35.9 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1529528cf11c0d3d21b7df1012cd8b5958ea83c547788b0b88d3f426be9ac642
MD5 e1ebf5307b24998c7a988b84ef11314f
BLAKE2b-256 4ef7d3c61ad1308251f5cd1d6330be2eeefa689b7357546ed2a1e241d02f80d7

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