Database Overview
Orquestra uses Cloudflare D1, a serverless SQLite database distributed at the edge for low-latency data access worldwide.Key Features
- Relational Database: Full SQL support with ACID transactions
- Edge Distribution: Data replicated globally for fast access
- SQLite-based: Standard SQLite syntax and features
- Zero Configuration: Managed by Cloudflare infrastructure
Database Bindings
The D1 database is bound to the Worker asDB:
Schema Migrations
Database schema is managed through migration files inmigrations/:
- 001_initial_schema.sql - Core tables and relationships
- 002_indexes.sql - Performance optimization indexes
- 003_custom_docs_and_known_addresses.sql - Extended features
Running Migrations
Core Tables
users
Stores GitHub-authenticated user accounts. Location:migrations/001_initial_schema.sql:5
Fields
| Field | Type | Description |
|---|---|---|
id | TEXT | UUID primary key |
github_id | INTEGER | GitHub user ID (unique) |
username | TEXT | GitHub username (unique) |
email | TEXT | User email address (unique) |
avatar_url | TEXT | GitHub avatar URL |
created_at | DATETIME | Account creation timestamp |
updated_at | DATETIME | Last update timestamp |
Indexes
Usage Example
projects
Stores IDL project metadata and configuration. Location:migrations/001_initial_schema.sql:16
Fields
| Field | Type | Description |
|---|---|---|
id | TEXT | UUID primary key |
user_id | TEXT | Owner user ID (foreign key) |
name | TEXT | Project display name |
description | TEXT | Project description |
program_id | TEXT | Solana program address |
is_public | BOOLEAN | Public/private visibility |
custom_docs | TEXT | Owner-editable documentation |
created_at | DATETIME | Project creation timestamp |
updated_at | DATETIME | Last update timestamp |
Constraints
- Foreign Key:
user_id→users(id)with CASCADE delete - Unique: Each user can only have one project per program_id
Indexes
Usage Example
idl_versions
Stores IDL version history with JSON and generated documentation. Location:migrations/001_initial_schema.sql:30
Fields
| Field | Type | Description |
|---|---|---|
id | TEXT | UUID primary key |
project_id | TEXT | Parent project ID (foreign key) |
idl_json | TEXT | Full IDL JSON content |
cpi_md | TEXT | Generated CPI documentation (Markdown) |
version | INTEGER | Version number (auto-incrementing) |
created_at | DATETIME | Version upload timestamp |
Constraints
- Foreign Key:
project_id→projects(id)with CASCADE delete
Indexes
Usage Example
api_keys
Stores API authentication keys for programmatic access. Location:migrations/001_initial_schema.sql:41
Fields
| Field | Type | Description |
|---|---|---|
id | TEXT | UUID primary key |
project_id | TEXT | Associated project ID (foreign key) |
key | TEXT | API key (hashed, unique) |
last_used | DATETIME | Last usage timestamp |
created_at | DATETIME | Key creation timestamp |
expires_at | DATETIME | Expiration date (optional) |
Constraints
- Foreign Key:
project_id→projects(id)with CASCADE delete - Unique: Each API key must be unique
Indexes
Usage Example
project_socials
Stores social media links and website URLs for projects. Location:migrations/001_initial_schema.sql:52
Fields
| Field | Type | Description |
|---|---|---|
id | TEXT | UUID primary key |
project_id | TEXT | Associated project ID (unique, foreign key) |
twitter | TEXT | Twitter/X profile URL |
discord | TEXT | Discord server invite |
telegram | TEXT | Telegram group/channel |
github | TEXT | GitHub repository URL |
website | TEXT | Project website URL |
created_at | DATETIME | Record creation timestamp |
updated_at | DATETIME | Last update timestamp |
Constraints
- Foreign Key:
project_id→projects(id)with CASCADE delete - Unique: One-to-one relationship with projects
Indexes
Usage Example
known_addresses
Stores labeled public key addresses for a project (e.g., program accounts, authorities). Location:migrations/003_custom_docs_and_known_addresses.sql:5
Fields
| Field | Type | Description |
|---|---|---|
id | TEXT | UUID primary key |
project_id | TEXT | Associated project ID (foreign key) |
label | TEXT | Human-readable label (e.g., “Treasury”) |
address | TEXT | Solana public key address |
description | TEXT | Optional description |
created_at | DATETIME | Record creation timestamp |
updated_at | DATETIME | Last update timestamp |
Constraints
- Foreign Key:
project_id→projects(id)with CASCADE delete
Indexes
Usage Example
Entity Relationships
Relationship Diagram
Relationships Summary
| Parent | Child | Type | Delete Behavior |
|---|---|---|---|
| users | projects | 1:N | CASCADE |
| projects | idl_versions | 1:N | CASCADE |
| projects | api_keys | 1:N | CASCADE |
| projects | project_socials | 1:1 | CASCADE |
| projects | known_addresses | 1:N | CASCADE |
KV Namespaces
In addition to D1, Orquestra uses Cloudflare KV for caching and IDL storage.IDLS Namespace
Binding:IDLS
Stores raw IDL JSON for fast retrieval without database queries.
Key Patterns
project:{projectId}:idl- Latest IDL JSONproject:{projectId}:version:{version}- Specific version
CACHE Namespace
Binding:CACHE
Caches API responses for public endpoints.
Key Patterns
api:{projectId}:instructions- Instructions listapi:{projectId}:accounts- Account typesapi:{projectId}:docs- Generated documentation
Query Examples
Complex Queries
Get User with Projects
Get Project with Latest IDL and Socials
Get Public Projects with Version Count
Performance Considerations
Indexes
All frequently queried columns have indexes:- User lookups by GitHub ID and username
- Project queries by user_id and program_id
- API key validation
- IDL version retrieval
Caching Strategy
- KV First: Check KV cache before database
- TTL: Set appropriate expiration times
- Invalidation: Clear cache on updates
Query Optimization
- Use prepared statements for parameterized queries
- Avoid N+1 queries with JOINs
- Limit result sets with LIMIT/OFFSET
- Use indexes for WHERE clauses
Database Administration
Access Local Database
Access Production Database
Export Data
Common SQL Queries
Next Steps
- Set up your Local Development environment
- Explore the Project Structure
- Review API Routes in the codebase
- Check Migration Files for full SQL