NyanSQLite Documentation
A Pydantic-native SQLite wrapper that allows you to use Pydantic models directly as database schemas, providing type safety and high performance.
Table of Contents
Concept
The Problem
Traditional database solutions require learning SQL, managing connections, and handling serialization manually. Also, you often need to write separate validation logic to ensure data integrity.
The Solution
NyanSQLite bridges this gap by mapping Pydantic models directly to SQLite tables:
- Type Safe: Use Pydantic type hints directly as your database schema.
- Auto Serialization: Transparently handle complex types like dict, list, and datetime.
- Django-like Queries: Intuitive syntax for complex filtering without writing SQL.
- Fast Search: Built-in support for SQLite's FTS5 full-text search.
- Async Support: Native
NyanSQLiteAIOclass forasyncio.
Design Principles
- Instant Persistence: Every write operation is immediately persisted to SQLite.
- Thread Safe: Protected by
threading.Lockand async locks for safe concurrent access. - Zero Config: Sensible defaults optimized for performance (WAL mode, etc.).
Installation
bash
pip install nyansqliteRequirements:
- Python 3.9+
- Pydantic 2.0+
Quick Start
Basic Usage
python
from pydantic import BaseModel
from nyansqlite import NyanSQLite, Indexed, Searchable
# 1. Define your schema
class Article(BaseModel):
id: int # 'id' field becomes the primary key automatically
author: Indexed[str] # Indexed column
title: Searchable[str] # Full-text search enabled
body: Searchable[str] # Full-text search enabled
views: int = 0
# 2. Initialize DB & Register model
db = NyanSQLite("blog.db")
db.register(Article)
# 3. Insert data
db.insert(Article(
id=1,
author="neko",
title="Mastering SQLite",
body="NyanSQLite makes data management easy."
))
# 4. Query (Django-like)
articles = db.query(Article, author="neko", views__gte=0)
# 5. Full-text search
results = db.search(Article, "SQLite")
for hit in results:
print(f"Found: {hit.title}")
db.close()Guides
For more detailed information, please refer to the following guides:
- Tutorial: Extended examples including multiple tables and advanced features.
- Validation: Schema validation and data handling with Pydantic.
- Async Support: Using NyanSQLite in asynchronous environments.
- Transactions: Ensuring data integrity and optimizing bulk writes.
- Error Handling: Handling exceptions and troubleshooting.
- Best Practices: Recommended patterns for production use.
- Encryption: Securing your data at rest.
- Exceptions: Complete exception class reference.
API Reference
Complete documentation for all classes and methods.