Blueprints¶
The blueprint system enables dynamic schema extension for domain models.
Overview¶
Blueprints allow you to define additional fields for models at runtime based on data stored in the graph database. This enables flexible, user-defined schemas without code changes.
How It Works¶
- Blueprint definitions are stored in the graph with a JSON Schema payload
- At runtime,
get_model()queries matchingBlueprintnodes from the graph get_model()callspydantic.create_modelto return a subclass with the extra fields added- The extended model validates data according to the blueprint schema
Basic Usage¶
from imbi_common import blueprints, models, graph
async def example(db: graph.Graph) -> None:
# Store a blueprint in the graph
blueprint = models.Blueprint(
name="Cloud Provider",
slug="cloud-provider",
type="Project",
description="Cloud provider details",
json_schema={
"type": "object",
"properties": {
"cloud_provider": {
"type": "string",
"enum": ["AWS", "GCP", "Azure"]
},
"cloud_region": {
"type": "string"
}
},
"required": ["cloud_provider"]
}
)
await db.create(blueprint)
# Get extended model for Project — blueprints whose type == "Project"
# are fetched and applied
ProjectModel = await blueprints.get_model(db, models.Project)
# ProjectModel now has cloud_provider and cloud_region fields
# and validates according to the blueprint schema
Filtering by Context¶
Blueprints can be filtered by project_type or environment:
# Only apply blueprints that match the given project type
ProjectModel = await blueprints.get_model(
db,
models.Project,
context={"project_type": "microservice"},
)
API Reference¶
get_model
async
¶
get_model(
database: Graph,
model: type[ModelType],
context: dict[str, str | list[str]] | None = None,
) -> type[ModelType]
Return a model class with matching blueprints applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[ModelType]
|
The base Pydantic model class to extend. |
required |
context
|
dict[str, str | list[str]] | None
|
Optional filter context. When provided, only
blueprints whose |
None
|