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, high-performance SQLite operations
High-level Python ORM library that provides a clean, type-safe interface for SQLite database operations using Pydantic models for schema definition.
๐ Key Features
- ๐ Pydantic Integration - Define database schema using Pydantic v2 models
- ๐ Auto Table Creation - Tables created/synchronized automatically with model changes
- โก Connection Pooling - High-performance thread-safe connection pool with WAL mode
- ๐ CRUD Operations - Simple insert, get, update, delete methods
- ๐ Column Sync - Automatically adds new columns when model changes
- ๐ Constraints Support - Primary Key, UNIQUE, NOT NULL, Foreign Keys
- ๐ก๏ธ Type Safety - Full type hints and Pydantic validation
- โก Async Support - Full async/await for high-performance applications
- ๐จ Query Builder - Safe query construction with SQL injection prevention
- ๐ Advanced Query Builder - JOINs, GROUP BY, HAVING, UNION support
- ๐ Migrations - Version-based schema migration system
- ๐งช Stress Testing - Built-in benchmarks and performance testing
- ๐ป CLI Tool - Command-line interface for common operations
- ๐ Battle Tested - Comprehensive unit and integration tests
๐ Performance
๐ wSQLite (Pool) Benchmark Results:
Ops/sec: ~5,000+ insertions/second
Latency: ~0.2ms average
Memory: <10MB overhead
๐ฆ Installation
pip install wsqlite
Development installation with dev tools:
pip install -e ".[dev]"
With benchmarking tools:
pip install -e ".[benchmark,stress]"
๐ Quick Start
Basic Usage
from pydantic import BaseModel
from wsqlite import WSQLite
class User(BaseModel):
id: int
name: str
email: str
# Create database - table is created automatically
db = WSQLite(User, "database.db")
# Insert data
db.insert(User(id=1, name="John", email="john@example.com"))
# Query data
users = db.get_all()
john = db.get_by_field(name="John")
# Update and delete
db.update(1, User(id=1, name="Johnny", email="johnny@example.com"))
db.delete(1)
With Connection Pooling
from wsqlite import WSQLite
db = WSQLite(User, "database.db", pool_size=20, min_pool_size=2)
Async Operations
import asyncio
from wsqlite import WSQLite
async def main():
db = WSQLite(User, "database.db")
await db.insert_async(User(id=1, name="John", email="john@example.com"))
users = await db.get_all_async()
asyncio.run(main())
Advanced Query Builder
from wsqlite.builders import AdvancedQueryBuilder
results = (
AdvancedQueryBuilder("users")
.select("id", "name", "email")
.join("orders", "users.id = orders.user_id", "LEFT")
.where("status", "=", "active")
.group_by("users.id")
.having("COUNT(orders.id)", ">", 5)
.order_by("users.name")
.limit(100)
.execute(conn)
)
Database Migrations
from wsqlite import WSQLite
from wsqlite.migrations import MigrationManager
manager = MigrationManager("app.db")
@manager.migration(1, "Create initial schema")
def m1(ctx):
ctx.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
manager.migrate_up()
๐งช Testing
Run tests:
pytest
Run stress tests:
python -m stress_test.run --scenario concurrent --records 100000 --threads 50
Run benchmarks:
python -m benchmark.run --all --report html
๐ Project Structure
wsqlite/
โโโ src/wsqlite/ # Main library
โ โโโ __init__.py
โ โโโ core/ # Core database operations
โ โ โโโ connection.py # Connection management
โ โ โโโ pool.py # Connection pooling
โ โ โโโ repository.py # CRUD operations
โ โ โโโ sync.py # Table sync
โ โโโ builders/ # Query builders
โ โโโ exceptions.py # Custom exceptions
โ โโโ migrations.py # Schema migrations
โ โโโ types/ # SQL type mapping
โ โโโ validators.py # Type validation
โ โโโ cli/ # CLI tool
โโโ examples/ # Usage examples
โโโ test/ # Test suite
โโโ stress_test/ # Stress testing
โโโ benchmark/ # Benchmarking
โโโ pyproject.toml
๐ฏ Advanced Features
Connection Pool
from wsqlite import ConnectionPool, WSQLite
pool = ConnectionPool("app.db", min_size=2, max_size=20)
db = WSQLite(User, "app.db", pool_size=10)
# Use pool directly
with pool.connection() as conn:
cursor = conn.execute("SELECT * FROM users")
Transactions
from wsqlite import WSQLite
db = WSQLite(User, "database.db")
# Simple transaction
db.execute_transaction([
("INSERT INTO users VALUES (?, ?)", (1, "John")),
("INSERT INTO orders VALUES (?, ?)", (1, 100)),
])
# Function-based transaction
result = db.with_transaction(lambda txn: txn.execute("SELECT COUNT(*) FROM users"))
Bulk Operations
# Bulk insert
users = [User(id=i, name=f"User{i}", email=f"user{i}@test.com") for i in range(10000)]
db.insert_many(users)
# Bulk update
updates = [(User(id=i, name=f"Updated{i}", email=f"updated{i}@test.com"), i) for i in range(100)]
db.update_many(updates)
๐ง Configuration
Environment Variables
WSQLITE_DB_PATH- Default database pathWSQLITE_LOG_LEVEL- Logging level (default: INFO)
Pool Configuration
db = WSQLite(
User,
"database.db",
pool_size=20, # Max connections
min_pool_size=2, # Min connections
use_pool=True # Enable pooling
)
๐ Version History
- v1.2.0 - 90%+ test coverage, async connection pool, comprehensive testing suite
- v1.1.0 - Connection pooling, advanced query builder, migrations, stress testing
- v1.0.0 - Initial stable release
๐งช Test Coverage
317 tests | 79% code coverage
Run with coverage:
pytest --cov=wsqlite --cov-report=term-missing
๐ License
MIT License - see LICENSE file.
๐ค Author
William Steve Rodriguez Villamizar
- Email: wisrovi.rodriguez@gmail.com
- GitHub: wisrovi
- LinkedIn: wisrovi
Project details
Release history Release notifications | RSS feed
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.1.tar.gz.
File metadata
- Download URL: wsqlite-1.2.1.tar.gz
- Upload date:
- Size: 98.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7200bfd071a0e02e39826d2d3f1bd6ac6ab84f92c0e898122f428b43d54c26b1
|
|
| MD5 |
0de3a68413ae7577b297ba1cac1e713b
|
|
| BLAKE2b-256 |
ed9960039f897d8a246bcf00c233c5d168935eb0aed94720806443dffeb02ba8
|
File details
Details for the file wsqlite-1.2.1-py3-none-any.whl.
File metadata
- Download URL: wsqlite-1.2.1-py3-none-any.whl
- Upload date:
- Size: 32.9 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 |
0511a7069988bb5fce8f1612ea706b6fcfdccd6eed92074057f78a57cf1ac562
|
|
| MD5 |
41c1ae9877d813a904d7ba58e465e512
|
|
| BLAKE2b-256 |
5468ce43467ae78ed093a4583e16e7556bd8a05be92b02fa151b216aa339496b
|