Templates in Eden-MDBS are reusable, parameterized operations that define database queries, API calls, and data transformations. They provide a consistent and secure way to execute operations across different endpoints.
Templates are structured definitions that:
Eden supports four types of template operations:
| Type | Description |
|---|---|
| Read | Query operations that retrieve data |
| Write | Operations that modify data (INSERT, UPDATE, DELETE) |
| Transaction | Multi-operation transactions with rollback |
| TwoPhaseTransaction | Distributed transactions across endpoints |
{
"id": "get_user_orders",
"description": "Retrieve all orders for a specific user",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440000",
"kind": "Read",
"template": {
"query": "SELECT * FROM orders WHERE user_id = {{user_id}} ORDER BY created_at DESC",
"params": ["{{user_id}}"]
},
"endpoint_kind": "Postgres"
}
}curl http://{host}:8000/api/v1/templates \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{
"id": "get_user_orders",
"description": "Retrieve all orders for a specific user",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440000",
"kind": "Read",
"template": {
"query": "SELECT * FROM orders WHERE user_id = {{user_id}}",
"params": ["{{user_id}}"]
},
"endpoint_kind": "Postgres"
}
}'Response:
{
"status": "success",
"message": "success"
}{
"id": "user_orders_summary",
"description": "Get user with order statistics",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440000",
"kind": "Read",
"template": {
"query": "SELECT u.*, COUNT(o.id) as order_count, SUM(o.total) as total_spent FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.id = {{user_id}} GROUP BY u.id",
"params": ["{{user_id}}"]
},
"endpoint_kind": "Postgres"
}
}{
"id": "create_order",
"description": "Create a new order",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440000",
"kind": "Write",
"template": {
"query": "INSERT INTO orders (user_id, product_id, quantity, total) VALUES ({{user_id}}, {{product_id}}, {{quantity}}, {{total}}) RETURNING id",
"params": ["{{user_id}}", "{{product_id}}", "{{quantity}}", "{{total}}"]
},
"endpoint_kind": "Postgres"
}
}{
"id": "cache_user_session",
"description": "Store user session in cache",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440001",
"kind": "Write",
"template": {
"operation": "set",
"key": "session:{{user_id}}",
"value": "{{session_data}}",
"ttl": "{{ttl}}"
},
"endpoint_kind": "Redis"
}
}{
"id": "find_user_documents",
"description": "Find documents by user",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440002",
"kind": "Read",
"template": {
"collection": "documents",
"operation": "find",
"filter": {
"user_id": "{{user_id}}",
"type": "{{doc_type}}"
},
"options": {
"limit": "{{limit}}",
"sort": {"created_at": -1}
}
},
"endpoint_kind": "Mongo"
}
}{
"id": "external_api_call",
"description": "Call external API",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440003",
"kind": "Write",
"template": {
"method": "POST",
"path": "/api/users/{{user_id}}/sync",
"headers": {
"Content-Type": "application/json"
},
"body": {
"data": "{{payload}}"
}
},
"endpoint_kind": "Http"
}
}{
"id": "transfer_funds",
"description": "Transfer funds between accounts",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440000",
"kind": "Transaction",
"template": {
"operations": [
{
"query": "UPDATE accounts SET balance = balance - {{amount}} WHERE id = {{from_account}}",
"params": ["{{amount}}", "{{from_account}}"]
},
{
"query": "UPDATE accounts SET balance = balance + {{amount}} WHERE id = {{to_account}}",
"params": ["{{amount}}", "{{to_account}}"]
},
{
"query": "INSERT INTO transactions (from_id, to_id, amount) VALUES ({{from_account}}, {{to_account}}, {{amount}})",
"params": ["{{from_account}}", "{{to_account}}", "{{amount}}"]
}
]
},
"endpoint_kind": "Postgres"
}
}Templates use Handlebars syntax for parameter substitution.
SELECT * FROM users WHERE id = {{user_id}}SELECT * FROM orders WHERE user_id = {{user_id}}
{{#if status}}
AND status = '{{status}}'
{{/if}}
{{#if date_from}}
AND created_at >= '{{date_from}}'
{{/if}}SELECT * FROM products WHERE id IN (
{{#each product_ids}}
{{this}}{{#unless @last}},{{/unless}}
{{/each}}
)Execute a template with parameters:
curl http://{host}:8000/api/v1/templates/get_user_orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-X POST \
-d '{
"user_id": 12345
}'Response:
{
"status": "success",
"data": {
"rows": [
{
"id": 1001,
"user_id": 12345,
"product_id": 456,
"total": 199.98,
"created_at": "2024-01-15T10:30:00Z"
}
],
"row_count": 1
}
}Preview the rendered template without executing:
curl http://{host}:8000/api/v1/templates/get_user_orders/render \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-X POST \
-d '{
"user_id": 12345
}'Response:
{
"status": "success",
"data": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440000",
"kind": "Read",
"request": {
"query": "SELECT * FROM orders WHERE user_id = 12345",
"params": [12345]
}
}
}curl http://{host}:8000/api/v1/templates/get_user_orders \
-H "Authorization: Bearer $TOKEN"Response:
{
"status": "success",
"data": {
"id": "get_user_orders",
"uuid": "550e8400-e29b-41d4-a716-446655440004",
"description": "Retrieve all orders for a specific user",
"template": {
"endpoint_uuid": "550e8400-e29b-41d4-a716-446655440000",
"kind": "Read",
"template": {
"query": "SELECT * FROM orders WHERE user_id = {{user_id}}",
"params": ["{{user_id}}"]
},
"endpoint_kind": "Postgres"
},
"created_at": "2024-01-15T10:30:00Z",
"updated_at": "2024-01-15T10:30:00Z"
}
}curl http://{host}:8000/api/v1/templates \
-H "Authorization: Bearer $TOKEN"curl http://{host}:8000/api/v1/templates/get_user_orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-X PATCH \
-d '{
"description": "Updated description",
"template": {
"query": "SELECT * FROM orders WHERE user_id = {{user_id}} AND status != '\''cancelled'\'' ORDER BY created_at DESC"
}
}'curl http://{host}:8000/api/v1/templates/get_user_orders \
-H "Authorization: Bearer $TOKEN" \
-X DELETEResponse:
{
"status": "success",
"message": "success"
}Templates integrate with Eden's RBAC system:
| Access Level | Permissions |
|---|---|
| Read | Execute Read templates |
| Write | Execute Read and Write templates |
| Admin | Create, update, delete templates |
{
"error": "Not Found",
"message": "Template get_user_orders not found"
}{
"error": "Bad Request",
"message": "Required parameter missing: user_id"
}{
"error": "Bad Request",
"message": "Handlebars parsing error: Unclosed expression"
}{
"error": "Forbidden",
"message": "Write access required for this template"
}