DEV Community

William Rodriguez
William Rodriguez

Posted on

Never lose a record: Native soft deletes and recovery.

Never lose a record: Native soft deletes and recovery.

Hard deletes are dangerous. A single accidental query can destroy production customer data. wsqlite's native soft delete keeps historical records intact while filtering them out of standard queries automatically.

Here is how you use Soft Delete & Audit Recovery in production with wsqlite:

from datetime import datetime
from typing import Optional
from pydantic import BaseModel
from wsqlite import WSQLite

class Product(BaseModel):
    id: int
    name: str
    price: float
    deleted_at: Optional[datetime] = None

# Enable soft delete mode
db = WSQLite(Product, "catalog.db", soft_delete=True)
db.insert(Product(id=1, name="Mechanical Keyboard", price=129.99))

# Soft delete: sets deleted_at timestamp
db.delete(1)
print(f"Active products count: {db.count()}")  # Returns 0!

# Instantly restore when needed
db.restore(1)
print(f"Restored products count: {db.count()}")  # Returns 1!
Enter fullscreen mode Exit fullscreen mode

Why developers love wsqlite:

  • Define database tables using standard Pydantic v2 models.
  • Auto-syncs columns on startup without writing manual migrations.
  • Thread-safe connection pooling with WAL mode enabled by default (5,000+ inserts/sec).
  • Full sync and async/await support.

Check out the repo on GitHub!

Top comments (0)