Overview
Query components enable you to:- Reuse SQL queries across multiple agents and ensembles
- Version queries for reproducibility and rollback
- Organize complex multi-step queries
- Share queries across teams
- A/B test different query strategies
Quick Start
1. Create a Query Component
Create a SQL file:2. Add to Edgit
3. Reference in Your Ensemble
URI Format and Versioning
All query components use the standardized URI format:query://- Protocol identifier for query components{path}- Logical path to the query (e.g.,get-customer-profile,analytics/daily-sales)[@{version}]- Optional version identifier (defaults to@latest)
@latest- Always uses the most recent version@v1- Uses latest patch of major version (v1.x.x)@v1.0.0- Specific semantic version (immutable)@prod- Custom tag for production queries@staging- Custom tag for staging queries
Example URIs
How to Reference in Ensembles
There are three ways to reference queries in your ensembles:1. URI Format (Recommended)
Use thequery:// URI format to reference versioned query components:
2. Template Expression Format
Use${components.query_name@version} to embed query references:
3. Inline Query
For simple operations or during development, use inline SQL directly:Using Query Components
With Dynamic Parameters
Multi-Step Data Workflow
Query Types and Examples
Customer/User Queries
Aggregation Queries
Join Queries
Time Series Queries
Search/Filter Queries
Caching and Performance
Query components are automatically cached for 1 hour (3600 seconds) after first load.Default Caching
Custom Cache TTL
Bypass Cache
Best Practices
1. Use Parameterized Queries
Always use parameters to prevent SQL injection:2. Add Indexes for Performance
3. Optimize with LIMIT
4. Use Clear Naming
5. Comment Complex Queries
6. Version for Changes
Track query changes with semantic versioning:7. Test Query Performance
Versioning Strategy
Development Workflow
Staged Testing
Rollback Strategy
Using ctx API in Agents
When building custom agents with TypeScript handlers, you can access SQL queries through thectx API:
ctx.queries.getSql(name)
Get a SQL query template by name:Using Query with D1
Parameterized Query Execution
Dynamic Query Selection
Batch Query Execution
Query with Result Processing
Troubleshooting
Query Not Found
Error:Component not found: query://[email protected]
Solution:
- Check query exists:
edgit list queries - Check version:
edgit tag list get-customer-profile - Verify deployment:
edgit tag show [email protected]
SQL Syntax Error
Error:SQL parse error in query
Solution:
- Test query directly in database client
- Check parameter names match query placeholders
- Verify SQL dialect matches database engine
Missing Parameters
Error:Parameter not found: ${customer_id}
Solution:
- Ensure all
${param}placeholders have values - Check parameter names match exactly (case-sensitive)
- Provide default values if needed
Performance Issues
Issue: Query takes too long Solutions:- Add indexes on filtered columns
- Limit result set size
- Use EXPLAIN to analyze query plan
- Consider materializing common queries

