Skip to content

Latest commit

 

History

History

README.md

sqlite-worker Examples

This directory contains comprehensive examples demonstrating real-world use cases and advanced techniques for sqlite-worker.

📚 Available Examples

Build a complete RESTful API with thread-safe database operations.

Key Features:

  • Full CRUD operations
  • Request/response validation with Pydantic
  • Automatic API documentation
  • Production-ready error handling

Use Cases: Web APIs, microservices, backend services


Process online store transactions in batches with proper error handling.

Key Features:

  • High-volume transaction processing
  • Inventory management
  • Transaction safety with rollback
  • Sales reporting and CSV export

Use Cases: E-commerce, data pipelines, bulk operations


Distributed task queue with priority scheduling and automatic retries.

Key Features:

  • Priority-based scheduling
  • Scheduled tasks
  • Automatic retry logic
  • Multi-worker support

Use Cases: Background jobs, async processing, scheduled tasks


Query optimization techniques for maximum performance.

Key Features:

  • Index strategies
  • Bulk insert optimization
  • Query planning analysis
  • PRAGMA tuning

Use Cases: Performance optimization, large databases, analytics


🚀 Quick Start

Each example is self-contained with its own README. To run an example:

# Navigate to the example directory
cd examples/fastapi_integration

# Install dependencies (if any)
pip install -r requirements.txt

# Run the example
python main.py

📖 Framework Integrations

See the Framework Integrations directory for examples using:

  • Flask
  • Django
  • FastAPI
  • Streamlit

🎓 Learning Path

Beginners:

  1. Start with FastAPI Integration for basic CRUD operations
  2. Move to Batch Processing for transaction handling
  3. Explore Task Queue for async patterns

Advanced Users:

  1. Review Advanced Optimization for performance tuning
  2. Study Framework Integrations for production patterns
  3. Check out the starter templates for rapid development

💡 Common Patterns

Thread-Safe Database Access

All examples demonstrate proper thread-safe usage:

from sqlite_worker import SqliteWorker

worker = SqliteWorker("database.db")
# Use worker from any thread safely

Transaction Management

with worker.transaction():
    # All operations are atomic
    worker.insert("table1", data1)
    worker.update("table2", data2)
    # Commits on success, rolls back on error

Error Handling

try:
    token = worker.execute(query, params)
    results = worker.fetch_results(token)
except Exception as e:
    # Handle errors appropriately
    print(f"Database error: {e}")

🎯 Use Case Guide

Scenario Recommended Example
Building a REST API FastAPI Integration
Processing bulk data Batch Processing
Background jobs Task Queue
Performance issues Advanced Optimization
Web application Framework Integrations

📝 Best Practices

All examples follow these best practices:

  1. Initialize with optimal settings

    worker = SqliteWorker(
        db_path,
        execute_init=[
            "PRAGMA journal_mode=WAL;",
            "PRAGMA synchronous=NORMAL;",
            "PRAGMA temp_store=MEMORY;"
        ]
    )
  2. Use transactions for multi-step operations

  3. Implement proper error handling

  4. Close connections when done

  5. Use parameterized queries (prevents SQL injection)

🔧 Customization

Each example can be customized for your needs:

  • Modify database schemas
  • Add custom business logic
  • Integrate with external services
  • Scale with multiple workers

📚 Additional Resources

🤝 Contributing

Have a great example to share? We welcome contributions!

  1. Create your example following the existing structure
  2. Include a detailed README
  3. Add tests if applicable
  4. Submit a pull request

See CONTRIBUTING.md for guidelines.

📧 Support

📄 License

All examples are provided under the same license as sqlite-worker. See LICENSE for details.