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, high-performance SQLite operations

PyPI version Python versions License

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 path
  • WSQLITE_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

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.1.tar.gz (98.5 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.1-py3-none-any.whl (32.9 kB view details)

Uploaded Python 3

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

Hashes for wsqlite-1.2.1.tar.gz
Algorithm Hash digest
SHA256 7200bfd071a0e02e39826d2d3f1bd6ac6ab84f92c0e898122f428b43d54c26b1
MD5 0de3a68413ae7577b297ba1cac1e713b
BLAKE2b-256 ed9960039f897d8a246bcf00c233c5d168935eb0aed94720806443dffeb02ba8

See more details on using hashes here.

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

Hashes for wsqlite-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0511a7069988bb5fce8f1612ea706b6fcfdccd6eed92074057f78a57cf1ac562
MD5 41c1ae9877d813a904d7ba58e465e512
BLAKE2b-256 5468ce43467ae78ed093a4583e16e7556bd8a05be92b02fa151b216aa339496b

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