4. Basic Usage

4.1. Model Definition

Define your database schema using Pydantic models:

from pydantic import BaseModel, Field

class User(BaseModel):
    id: int
    name: str = Field(max_length=100)
    email: str
    age: int | None = None
    is_active: bool = True

4.2. Field Types

wpostgresql supports various Pydantic field types:

from datetime import datetime
from typing import Optional

class Product(BaseModel):
    id: int
    name: str
    price: float
    created_at: datetime
    description: Optional[str] = None
    stock: int = 0

4.3. Automatic Table Creation

Tables are automatically created when you initialize WPostgreSQL:

from wpostgresql import WPostgreSQL

db = WPostgreSQL(User, db_config)

The table users will be automatically created if it doesn’t exist.

4.4. Schema Synchronization

The library automatically synchronizes your model schema with the database:

class User(BaseModel):
    id: int
    name: str
    email: str
    new_field: str = "default_value"

db = WPostgreSQL(User, db_config)

If the table already exists and you add a new field, it will be added to the table.

4.5. Working with Data

user = User(id=1, name="Alice", email="alice@example.com")
db.insert(user)

users = db.get_all()
filtered = db.get_by_field(is_active=True)

user.name = "Alice Smith"
db.update(1, user)

db.delete(1)