TableSync Module¶
This module handles table synchronization between Pydantic models and SQLite.
- class wsqlite.core.sync.TableSync(model, db_path: str, table_name: str | None = None)[source]¶
Bases:
objectHandles table synchronization between Pydantic models and SQLite (sync).
- __init__(model, db_path: str, table_name: str | None = None)[source]¶
Initialize table sync.
- Parameters:
model – Pydantic BaseModel class.
db_path – Path to SQLite database file.
table_name – Optional custom table name.
AsyncTableSync¶
- class wsqlite.core.sync.AsyncTableSync(model, db_path: str, table_name: str | None = None)[source]¶
Bases:
objectHandles table synchronization between Pydantic models and SQLite (async).
- async sync_with_model_async()[source]¶
Sync the table with the Pydantic model, adding new columns if necessary (async).
Methods Overview¶
Method |
Description |
|---|---|
create_if_not_exists() |
Create table if it doesn’t exist |
sync_with_model() |
Sync table with model (add new columns) |
table_exists() |
Check if table exists |
drop_table() |
Drop the table |
get_columns() |
Get list of column names |
create_index() |
Create an index |
drop_index() |
Drop an index |
get_indexes() |
Get list of indexes |
Example Usage¶
from pydantic import BaseModel
from wsqlite import TableSync
class User(BaseModel):
id: int
name: str
email: str
sync = TableSync(User, "database.db")
# Create table
sync.create_if_not_exists()
# Check if exists
if sync.table_exists():
print("Table exists!")
# Get columns
columns = sync.get_columns()
print(f"Columns: {columns}")
# Create index
sync.create_index(["email"], unique=True)
# Get indexes
indexes = sync.get_indexes()
print(f"Indexes: {indexes}")