Examples Overview
This section provides practical examples demonstrating various features and use cases of dqlitepy. Each example is a complete, runnable project with its own dependencies and setup instructions.
Available Examples
1. Simple Node Example
Learn the basics by creating and managing a single dqlite node with basic SQL operations.
What You'll Learn:
- Node initialization and configuration
- Starting and stopping nodes
- Basic SQL operations (CREATE, INSERT, SELECT)
- Graceful shutdown procedures
2. Multi-Node Cluster Example
Set up a distributed dqlite cluster with multiple nodes for high availability.
What You'll Learn:
- Multi-node cluster configuration
- Node coordination and communication
- Cluster status monitoring
- Distributed consensus basics
3. Cluster with Client API Example
Use the Client API to dynamically manage cluster membership from outside the cluster.
What You'll Learn:
- Client API usage and patterns
- Dynamic node addition and removal
- Leader election monitoring
- Cluster management best practices
4. SQLAlchemy ORM Example
Integrate dqlitepy with SQLAlchemy ORM for distributed database operations with Python objects.
What You'll Learn:
- SQLAlchemy dialect configuration
- ORM model definitions
- CRUD operations via SQLAlchemy
- Relationship handling
- Automatic replication with ORM
5. FastAPI Integration Example
This comprehensive example demonstrates how to build a production-ready REST API using FastAPI with dqlitepy and SQLAlchemy. It includes cluster management, Docker support, and a complete CLI for database operations.
What You'll Learn:
- Building REST APIs with FastAPI and dqlitepy
Quick Start
All examples are located in the examples/ directory of the repository and follow a consistent structure:
# Clone the repository
git clone https://github.com/vantagecompute/dqlitepy.git
cd dqlitepy/examples
# Run any example with its quickstart script
cd example_name
bash quickstart.sh
General Prerequisites
- Python 3.12 or higher
uvpackage manager installed
Installing uv
curl -LsSf https://astral.sh/uv/install.sh | sh
Example Structure
Each example follows this structure:
example_name/
├── README.md # Detailed documentation
├── pyproject.toml # Dependencies and metadata
├── quickstart.sh # One-command setup script
└── example_package/ # Python package
├── __init__.py
└── main.py # Entry point
Running Examples
Method 1: Quick Start (Recommended)
Use the provided quickstart script:
cd example_name
bash quickstart.sh
The script will automatically:
- Install dqlitepy from the repository
- Install example-specific dependencies
- Run the example
Method 2: Manual Installation
Install and run manually:
cd example_name
uv pip install -e .
example-name-script # Uses the installed entry point
Method 3: Direct Execution
Run directly without installation:
cd example_name
uv run python -m example_package.main
Learning Path
We recommend exploring the examples in this order:
- Simple Node - Foundation concepts
- Multi-Node Cluster - Distributed system basics
- Cluster with Client - Advanced cluster management
- SQLAlchemy ORM - ORM integration patterns
- FastAPI Integration - Production application development
Troubleshooting
Import Errors
If you encounter import errors, ensure dqlitepy is installed:
cd ../.. # Go to repository root
uv pip install -e .
Port Conflicts
Examples use ports 9001-9003 for dqlite nodes. If these ports are occupied:
- Stop conflicting processes:
lsof -i :9001-9003 - Or modify port numbers in the example code
Permission Issues
Ensure data directories are writable:
chmod -R 755 /tmp/dqlite*
Virtual Environment Issues
If you have virtual environment conflicts:
# Remove existing venv
rm -rf .venv
# Reinstall
uv pip install -e .
Getting Help
- Check the Troubleshooting Guide
- Review the API Reference
- Open an issue on GitHub
Contributing
Found an issue or want to add an example? Contributions are welcome!
- Fork the repository
- Create a new example following the established structure
- Test your example thoroughly
- Submit a pull request
Next Steps
- Explore the Architecture Documentation to understand how dqlitepy works
- Read the Usage Guide for detailed API information
- Check out the Clustering Guide for production deployment patterns