Buckets:

download
raw
10.8 kB
#!/usr/bin/env python3
"""
Database Switching Example
Demonstrates various ways to dynamically switch databases in jvspatial:
1. Per-operation database switching
2. Context-based switching
3. Default context switching
4. Environment-based switching
5. Application lifecycle switching
"""
import asyncio
import os
from typing import Any, Dict, List, Optional, Tuple
from jvspatial.core import Edge, GraphContext, Node, set_default_context
from jvspatial.db import Database
from jvspatial.db.database import finalize_find_results
# Simple mock database for demonstration
class MockDatabase(Database):
def __init__(self, name: str):
self.name = name
self._data: Dict[str, Any] = {}
print(f"๐Ÿ”ง Created {name} database")
async def clean(self) -> None:
pass
async def save(self, collection: str, data: Dict[str, Any]) -> Dict[str, Any]:
key = f"{collection}:{data['id']}"
self._data[key] = data
print(f"๐Ÿ’พ Saved to {self.name}: {key}")
return data
async def get(self, collection: str, id: str):
key = f"{collection}:{id}"
result = self._data.get(key)
print(
f"๐Ÿ“– Read from {self.name}: {key} -> {'found' if result else 'not found'}"
)
return result
async def delete(self, collection: str, id: str) -> None:
key = f"{collection}:{id}"
self._data.pop(key, None)
print(f"๐Ÿ—‘๏ธ Deleted from {self.name}: {key}")
async def find(
self,
collection: str,
query: Dict[str, Any],
*,
limit: Optional[int] = None,
sort: Optional[List[Tuple[str, int]]] = None,
):
results = []
prefix = f"{collection}:"
for key, doc in self._data.items():
if key.startswith(prefix):
results.append(doc)
out = finalize_find_results(results, sort=sort, limit=limit)
print(f"๐Ÿ” Found {len(out)} items in {self.name}")
return out
class Person(Node):
name: str = ""
age: int = 0
async def demonstrate_database_switching():
"""Show different ways to switch databases dynamically."""
print("๐Ÿ”€ Database Switching Demonstration")
print("=" * 50)
# Method 1: Per-Operation Database Switching
print("\n1๏ธโƒฃ Per-Operation Database Switching")
print("-" * 40)
# Create different database instances
dev_db = MockDatabase(name="Development")
test_db = MockDatabase(name="Testing")
prod_db = MockDatabase(name="Production")
# Use different databases for different operations
dev_ctx = GraphContext(database=dev_db)
test_ctx = GraphContext(database=test_db)
prod_ctx = GraphContext(database=prod_db)
# Set default context for entity-centric operations
set_default_context(dev_ctx)
alice_dev = await Person.create(name="Alice", age=30)
set_default_context(test_ctx)
alice_test = await Person.create(name="Alice", age=30)
set_default_context(prod_ctx)
alice_prod = await Person.create(name="Alice", age=30)
print("โœ… Same data operations on different databases")
# Method 2: Context-Based Switching
print("\n2๏ธโƒฃ Context-Based Database Switching")
print("-" * 40)
async def process_user_data(user_id: str, environment: str):
"""Process user data in different environments."""
if environment == "dev":
ctx = GraphContext(database=dev_db)
elif environment == "test":
ctx = GraphContext(database=test_db)
else:
ctx = GraphContext(database=prod_db)
# Set context for entity-centric operations
set_default_context(ctx)
# Same logic, different database
person = await Person.get(user_id)
if person:
print(f"๐Ÿ“Š Processing {person.name} in {environment} environment")
return person
# Process same user in different environments
await process_user_data(alice_dev.id, "dev")
await process_user_data(alice_test.id, "test")
await process_user_data(alice_prod.id, "prod")
# Method 3: Default Context Switching
print("\n3๏ธโƒฃ Default Context Switching")
print("-" * 40)
# Create a new default context
default_db = MockDatabase(name="Default")
default_ctx = GraphContext(database=default_db)
set_default_context(default_ctx)
# Entity-centric operations use the default context
bob = await Person.create(name="Bob", age=25)
print("โœ… Created Bob using default context")
# Method 4: Environment-Based Switching
print("\n4๏ธโƒฃ Environment-Based Database Switching")
print("-" * 40)
def get_database_for_environment():
"""Get database based on environment variables."""
env = os.getenv("APP_ENV", "development")
if env == "production":
return MockDatabase(name="Production-ENV")
elif env == "testing":
return MockDatabase(name="Testing-ENV")
else:
return MockDatabase(name="Development-ENV")
# Test different environments
environments = ["development", "testing", "production"]
for env in environments:
os.environ["APP_ENV"] = env
db = get_database_for_environment()
ctx = GraphContext(database=db)
set_default_context(ctx)
charlie = await Person.create(name=f"Charlie-{env}", age=35)
print(f"โœ… Created Charlie in {env} environment")
# Clean up environment
if "APP_ENV" in os.environ:
del os.environ["APP_ENV"]
# Method 5: Application Lifecycle Switching
print("\n5๏ธโƒฃ Application Lifecycle Database Switching")
print("-" * 40)
class ApplicationManager:
def __init__(self):
self.current_db = None
self.context = None
async def initialize(self, database: Database):
"""Initialize with a specific database."""
self.current_db = database
self.context = GraphContext(database=self.current_db)
set_default_context(self.context)
print(
f"๐Ÿš€ Application initialized with {database.name if hasattr(database, 'name') else type(database).__name__}"
)
async def switch_database(self, database: Database):
"""Switch to a different database."""
db_name = (
database.name if hasattr(database, "name") else type(database).__name__
)
current_name = (
self.current_db.name
if hasattr(self.current_db, "name")
else type(self.current_db).__name__
)
print(f"๐Ÿ”„ Switching from {current_name} to {db_name}")
# Create new context
new_context = GraphContext(database=database)
# Optional: Migrate data (simplified example)
await self._migrate_data(self.context, new_context)
# Switch
self.current_db = database
self.context = new_context
set_default_context(new_context)
print("โœ… Database switch completed")
async def _migrate_data(self, old_ctx: GraphContext, new_ctx: GraphContext):
"""Simplified data migration between databases."""
# In a real application, you'd implement proper migration logic
print("๐Ÿ“ฆ Migrating data between databases...")
# This is just for demonstration - real migration would be more complex
# Demonstrate application lifecycle switching
app = ApplicationManager()
await app.initialize(MockDatabase(name="Initial-DB"))
# Create some data using entity-centric operations
initial_person = await Person.create(name="Diana", age=40)
print(f"โœ… Created Diana: {initial_person.id}")
# Switch databases during runtime
await app.switch_database(MockDatabase(name="New-DB"))
# Create more data in new database
new_person = await Person.create(name="Eve", age=28)
print(f"โœ… Created Eve in new database: {new_person.id}")
# Method 6: Configuration-Driven Switching
print("\n6๏ธโƒฃ Configuration-Driven Database Switching")
print("-" * 40)
class DatabaseConfig:
"""Configuration-driven database management."""
def __init__(self):
self.configs = {
"cache": {"name": "Cache-DB"},
"analytics": {"name": "Analytics-DB"},
"user_data": {"name": "UserData-DB"},
"audit": {"name": "Audit-DB"},
}
self.databases = {}
self.contexts = {}
async def initialize(self):
"""Initialize all configured databases."""
for purpose, config in self.configs.items():
db_name = config.get("name", f"{purpose}_db")
self.databases[purpose] = MockDatabase(name=db_name)
self.contexts[purpose] = GraphContext(database=self.databases[purpose])
print(f"๐Ÿ”ง Initialized {purpose} database")
async def save_user(self, user_data: dict):
"""Save user to user_data database."""
ctx = self.contexts["user_data"]
set_default_context(ctx)
user = await Person.create(**user_data)
print(f"๐Ÿ‘ค Saved user to user_data database")
return user
async def cache_result(self, key: str, value: dict):
"""Cache result in cache database."""
ctx = self.contexts["cache"]
# In real app, you'd have a cache-specific model
print(f"๐Ÿ’พ Cached {key} in cache database")
async def log_audit(self, action: str, user_id: str):
"""Log audit event."""
ctx = self.contexts["audit"]
print(f"๐Ÿ“ Logged audit: {action} by {user_id}")
# Use configuration-driven approach
db_config = DatabaseConfig()
await db_config.initialize()
# Different operations use different databases automatically
user = await db_config.save_user({"name": "Frank", "age": 45})
await db_config.cache_result("user_profile", {"name": "Frank"})
await db_config.log_audit("user_created", user.id)
print(f"\nโœ… Database switching demonstration completed!")
print(f"\nKey takeaways:")
print(f" โ€ข Databases can be switched per-operation")
print(f" โ€ข Context objects encapsulate database usage")
print(f" โ€ข Default context can be changed at runtime using set_default_context()")
print(f" โ€ข Environment variables enable dynamic configuration")
print(f" โ€ข Application lifecycle switching supports hot-swapping")
print(f" โ€ข Configuration-driven approach separates concerns")
if __name__ == "__main__":
asyncio.run(demonstrate_database_switching())

Xet Storage Details

Size:
10.8 kB
ยท
Xet hash:
516292b25b3988a48352b9b6b36c55bce2fb42bd4bbae7fda4d3d67cef58178e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.