NyanSQLite API Reference
Complete documentation for the Pydantic-native NyanSQLite class.
NyanSQLite
python
class NyanSQLite(path: str = ':memory:', wal: bool = True, strict_deserialization: bool = False, statement_cache_size: Optional[int] = None)Pydantic-native SQLite wrapper. Supports automatic schema creation, B-tree indexes, FTS5 full-text search, partial reads/writes, and advanced query operators. Powered by the high-performance apsw backend.
Quick start::
from nyansqlite import NyanSQLite, Indexed, Searchable
from pydantic import BaseModel
class Article(BaseModel):
id: int
author: Indexed[str]
title: Searchable[str]
body: Searchable[str]
views: int = 0
db = NyanSQLite("blog.sqlite")
db.register(Article)
db.insert(Article(id=1, author="neko", title="Hello SQLite", body="…"))
db.search(Article, "SQLite")
db.update(Article, where={"id": 1}, views=42)
db.select(Article, fields=["title", "views"], author="neko")
Constructor
Core Methods
register
python
def register(model: type[BaseModel], *, on_mismatch: str = 'error', allow_destructive: bool = False, backup: bool | str | os.PathLike[str] | None = None, fill: Optional[Mapping[str, Any]] = None) -> NoneParameter
| Parameter | Type | Description |
|---|---|---|
model | type[BaseModel] | |
on_mismatch | str | |
allow_destructive | bool | |
backup | `bool | str |
fill | Optional[Mapping[str, Any]] |
close
python
def close() -> Noneregistered_models
python
def registered_models() -> list[str]CRUD Operations
insert
python
def insert(obj: M) -> MParameter
| Parameter | Type | Description |
|---|---|---|
obj | M |
Returns
Type: M
insert_many
python
def insert_many(objs: list[M]) -> intParameter
| Parameter | Type | Description |
|---|---|---|
objs | list[M] |
Returns
Type: int
update
python
def update(model: type[BaseModel], where: dict[str, Any], **fields: Any) -> intParameter
| Parameter | Type | Description |
|---|---|---|
model | type[BaseModel] | |
where | dict[str, Any] |
Returns
Type: int
Raises
Example
python
db.update(User, where={"id": 1}, age=26, bio="updated")delete
python
def delete(model: type[BaseModel], *filters: str, **kwargs: Any) -> intParameter
| Parameter | Type | Description |
|---|---|---|
model | type[BaseModel] | |
filters | str |
Returns
Type: int
Example
python
db.delete(User, id=42)
db.delete(User, "age > 50")
db.delete(Session, user_id=1, active=True)Query & Search
get
python
def get(model: type[M], *filters: str, **kwargs: Any) -> Optional[M]Parameter
| Parameter | Type | Description |
|---|---|---|
model | type[M] | |
filters | str |
Returns
Type: Optional[M]
Example
python
user = db.get(User, id=1)
user = db.get(User, "age > 30", name="Alice")
user = db.get(User, email="[email protected]")query
python
def query(model: type[M], *filters: str, limit: Optional[int] = None, offset: Optional[int] = None, order_by: Optional[str] = None, desc: bool = False, **kwargs: Any) -> list[M]Parameter
| Parameter | Type | Description |
|---|---|---|
model | type[M] | |
filters | str | |
limit | Optional[int] | |
offset | Optional[int] | |
order_by | Optional[str] | |
desc | bool |
Returns
Type: list[M]
Example
python
db.query(User) # 全ての行
db.query(User, age=25) # 完全一致
db.query(User, "age > 20", limit=10) # 文字列フィルタ
db.query(User, age__gte=20, limit=10) # 演算子サフィックス
db.query(User, order_by="name", desc=True) # ソート
db.query(User, order_by="id", limit=20, offset=40) # ページネーションselect
python
def select(model: type[BaseModel], fields: list[str], *filters: str, limit: Optional[int] = None, offset: Optional[int] = None, order_by: Optional[str] = None, desc: bool = False, **kwargs: Any) -> list[dict[str, Any]]Parameter
| Parameter | Type | Description |
|---|---|---|
model | type[BaseModel] | |
fields | list[str] | |
filters | str | |
limit | Optional[int] | |
offset | Optional[int] | |
order_by | Optional[str] | |
desc | bool |
Returns
Type: list[dict[str, Any]]
Example
python
db.select(Article, ["title", "views"], author="neko", order_by="views", desc=True)
db.select(Article, ["title"], "views > 100")search
python
def search(model: type[M], query: str, *, limit: Optional[int] = None) -> list[M]Parameter
| Parameter | Type | Description |
|---|---|---|
model | type[M] | |
query | str | |
limit | Optional[int] |
Returns
Type: list[M]
Raises
Example
python
db.search(Article, "python sqlite")
db.search(Article, "python sqlite", limit=5)python
db.search(Article, "title:python")count
python
def count(model: type[BaseModel], *filters: str, **kwargs: Any) -> intParameter
| Parameter | Type | Description |
|---|---|---|
model | type[BaseModel] | |
filters | str |
Returns
Type: int
Example
python
total = db.count(User)
adults = db.count(User, "age >= 18")
adults = db.count(User, age__gte=18)exists
python
def exists(model: type[BaseModel], *filters: str, **kwargs: Any) -> boolParameter
| Parameter | Type | Description |
|---|---|---|
model | type[BaseModel] | |
filters | str |
Returns
Type: bool
Example
python
if db.exists(User, email="[email protected]"):
...Maintenance
rebuild_fts
python
def rebuild_fts(model: type[BaseModel]) -> Nonevacuum
python
def vacuum() -> NoneRaw SQL Execution
execute_raw
python
def execute_raw(sql: str, params: tuple = ()) -> list[dict[str, Any]]Parameter
| Parameter | Type | Description |
|---|---|---|
sql | str | |
params | tuple |
Returns
Type: list[dict[str, Any]]
Example
python
db.execute_raw("SELECT count(*) AS n FROM user WHERE age > ?", (18,))Other Methods
plan_migration
python
def plan_migration(model: type[BaseModel], *, fill: Optional[Mapping[str, Any]] = None) -> MigrationPlanReturn the steps needed to bring an existing table in line with model without changing the database. Print the result or read plan.sql to review it. fill is the same mapping you would pass to migrate().
migrate
python
def migrate(model: type[BaseModel], *, allow_destructive: bool = False, dry_run: bool = False, backup: bool | str | os.PathLike[str] | None = None, fill: Optional[Mapping[str, Any]] = None) -> MigrationPlanParameter
| Parameter | Type | Description |
|---|---|---|
model | type[BaseModel] | |
allow_destructive | bool | |
dry_run | bool | |
backup | `bool | str |
fill | Optional[Mapping[str, Any]] |
atomic
python
def atomic() -> Generator[None, None, None]Example
python
with db.atomic():
db.insert(User(id=1, name="Taro"))
# ここで例外が発生するとロールバックされます