mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-26 11:44:28 +08:00
- Add schemaSyncer.ts with Drizzle Kit push integration - Integrate auto schema sync into BaseService.initialize() - Database schema now automatically updates on agent service startup - Users no longer need manual migration commands - Ensures schema consistency across app updates Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering> |
||
|---|---|---|
| .. | ||
| database | ||
| services | ||
| BaseService.ts | ||
| drizzle.config.ts | ||
| index.ts | ||
| README.md | ||
| schemaSyncer.ts | ||
Agents Service - Drizzle ORM Implementation
This service now uses a clean, modern Drizzle ORM implementation for all database operations.
Database Schema
The database schema is defined in /database/schema/ using Drizzle ORM:
agents.schema.ts- Agent table and indexessessions.schema.ts- Sessions and session logs tablesmigrations.schema.ts- Migration tracking (if needed)
Working with the Database
Development Setup
For new development, you can:
-
Use Drizzle Kit to generate migrations from schema:
npx drizzle-kit generate:sqlite --config src/main/services/agents/drizzle.config.ts -
Push schema directly to database (for development):
npx drizzle-kit push:sqlite --config src/main/services/agents/drizzle.config.ts -
Create tables programmatically (if needed): The schema exports can be used with
CREATE TABLEstatements.
Usage
All database operations are now fully type-safe:
import { agentService } from './services'
// Create an agent - fully typed
const agent = await agentService.createAgent({
type: 'custom',
name: 'My Agent',
model: 'claude-3-5-sonnet-20241022'
})
// TypeScript knows the exact shape of the returned data
console.log(agent.id) // ✅ Type-safe
Architecture
- Pure Drizzle ORM: No legacy migration system
- Type Safety: Full TypeScript integration
- Modern Patterns: Schema-first development
- Simplicity: Clean, maintainable codebase
Services
AgentService- CRUD operations for agentsSessionService- Session managementSessionMessageService- Message loggingBaseService- Shared database utilities