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.
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
- ๐ค Why wsqlite?
- ๐ Performance
- ๐ฆ Installation
- ๐ Quick Start
- ๐ ๏ธ SQL Constraints Reference
- ๐ฏ Advanced Functionality
- ๐ Project Structure
- ๐งช Testing & Quality
- ๐ License & Author
๐ 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, anddelete. - ๐ 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/awaitsupport 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:
- Zero Boilerplate: No need to write
CREATE TABLEstatements or manual migrations for new columns. - Developer Experience: Use the same Pydantic models for your API (FastAPI) and your Database.
- Safety First: SQL Injection protection and strict type validation out of the box.
- 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.
- ๐ง wisrovi.rodriguez@gmail.com
- ๐ GitHub | LinkedIn
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
998d43578e884ed3a19773322d7014c6932c898af3acae82e3cf1584aa4c03a4
|
|
| MD5 |
21c91104dfb7eacd1d1391035b879af2
|
|
| BLAKE2b-256 |
3f7a45ef7eddbf9c96cd1d67c83525af59a79010790d467920fe5035b6178e95
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d63bed8357a9c0694ced4a4d437d1bcae196f574ad5f9ceb0f876fdfbad5f851
|
|
| MD5 |
4ed35b028c9197adc92728c3828dfc2a
|
|
| BLAKE2b-256 |
ed565f5099c25fd745785db9aa950eda2d12756d830625099e749957883b613e
|