Database IntegrationsΒΆ
Think of databases as the memory of your application. Just like humans have different ways of organizing information (some prefer notebooks with structured tables, others prefer sticky notes scattered around), applications need different types of databases. SQL databases are like well-organized filing cabinets, while NoSQL databases are like flexible notebooks that adapt to your needs.
Ravyn supports both worlds seamlessly, giving you the freedom to choose the right tool for your data.
What You'll LearnΒΆ
- How to integrate SQL databases with Edgy ORM
- How to integrate MongoDB with Mongoz ODM
- When to use SQL vs NoSQL databases
- Best practices for database integration in Ravyn
Quick StartΒΆ
SQL with EdgyΒΆ
from ravyn import Ravyn
from edgy import Database, Registry
database = Database("sqlite:///db.sqlite")
registry = Registry(database=database)
app = Ravyn()
MongoDB with MongozΒΆ
from ravyn import Ravyn
from mongoz import Client, Registry
client = Client("mongodb://localhost:27017")
registry = Registry(client)
app = Ravyn()
Supported IntegrationsΒΆ
Ravyn provides out-of-the-box support for two powerful database libraries:
Edgy - SQL ORMΒΆ
Edgy is a modern, async-first ORM for SQL databases built on SQLAlchemy Core.
- Type: SQL (Relational)
- Best For: Structured data, complex relationships, transactions
- Databases: PostgreSQL, MySQL, SQLite, and more
- Documentation: edgy.dymmond.com
Mongoz - MongoDB ODMΒΆ
Mongoz is a powerful ODM (Object Document Mapper) for MongoDB.
- Type: NoSQL (Document)
- Best For: Flexible schemas, rapid development, unstructured data
- Database: MongoDB
- Documentation: mongoz.tarsild.io
SQL vs NoSQL: Which to Choose?ΒΆ
| Feature | SQL (Edgy) | NoSQL (Mongoz) |
|---|---|---|
| Schema | Fixed, predefined | Flexible, dynamic |
| Relationships | Strong, enforced | Flexible, embedded |
| Transactions | ACID compliant | Eventually consistent |
| Scalability | Vertical (scale up) | Horizontal (scale out) |
| Best For | Complex queries, relationships | Rapid iteration, flexibility |
| Use Cases | Financial systems, e-commerce | Content management, analytics |
Database AgnosticΒΆ
While Ravyn provides built-in support for Edgy and Mongoz, you're not limited to these choices.
Ravyn is completely database-agnostic. You can use:
- Any ORM: SQLAlchemy, Tortoise ORM, Piccolo, etc.
- Any ODM: Motor, PyMongo, Beanie, etc.
- Raw Drivers: psycopg, asyncpg, aiomysql, etc.
- Multiple Databases: Mix SQL and NoSQL in the same application
The integrations provided are conveniences, not requirements.
Integration FeaturesΒΆ
Both Edgy and Mongoz integrations provide:
User Authentication ModelsΒΆ
Pre-built User models with:
- Password hashing (bcrypt)
- User management functions
- Django-inspired API
- Ready for production
JWT MiddlewareΒΆ
Built-in JWT authentication middleware: - Token generation and validation - User authentication - Protected routes - Refresh token support
Settings IntegrationΒΆ
Seamless integration with Ravyn settings: - Centralized configuration - Environment-based setup - Reusable across applications
Best PracticesΒΆ
1. Use Settings for ConfigurationΒΆ
# settings.py
from ravyn import RavynSettings
from edgy import Database, Registry
class AppSettings(RavynSettings):
@property
def edgy_database(self):
return Database("postgresql://user:pass@localhost/db")
@property
def edgy_registry(self):
return Registry(database=self.edgy_database)
2. Separate Database LogicΒΆ
Keep database operations separate from route handlers:
# Good - separate concerns
@get("/users/{user_id}")
async def get_user(user_id: int) -> dict:
user = await UserService.get_by_id(user_id)
return user.model_dump()
3. Use Connection PoolingΒΆ
Configure appropriate connection pool sizes for production:
database = Database(
"postgresql://localhost/db",
pool_size=20,
max_overflow=10
)
Learn MoreΒΆ
Next StepsΒΆ
Choose your database integration:
- Edgy (SQL) β - Relational databases
- Mongoz (MongoDB) β - Document databases
Or explore specific topics: