mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-07-11 02:23:48 +08:00
Two sibling resources land together because their migration SQL is
generated as one unit and their API-schema / handler wiring has to go
in the same commit to keep the repo compiling.
group: upgrade the existing group table from `sort_order INT` to
`order_key TEXT` (`scopedOrderKeyIndex('group', 'entityType')`) and
expose it as a first-class resource under /groups. Each entityType
owns an independent orderKey sequence. Reorder delegates to the new
applyScopedMoves helper.
pin: brand-new polymorphic table `(id, entityType, entityId, orderKey,
timestamps)` with UNIQUE(entityType, entityId) enforcing idempotency
at the DB layer. One table serves arbitrarily many consumers — same
precedent as entity_tag. No FK to consumer tables; every consumer
service's delete path must call `pinService.purgeForEntity(tx,
entityType, entityId)` to keep the two tables in sync (signature is
tx-first, the mainstream ORM convention; tagService.removeEntityTags
is the project's historical tx-last outlier and is left as-is).
PinService.pin is idempotent and concurrent-safe: a fast-path SELECT
returns existing rows, and a UNIQUE collision under concurrent INSERT
is caught, classified, and re-SELECTed so the caller never sees a
constraint error.
Handler layer is a thin Zod-parse shell — all scope inference, row
lookup, and orderKey computation live in the services.
Data API Type System
This directory contains type definitions for the DataApi system.
Documentation
- DataApi Overview: docs/references/data/data-api-overview.md
- API Types: api-types.md
- API Design Guidelines: api-design-guidelines.md
Directory Structure
packages/shared/data/api/
├── index.ts # Barrel exports
├── apiTypes.ts # Core request/response types
├── apiPaths.ts # Path template utilities
├── apiErrors.ts # Error handling
└── schemas/
├── index.ts # Schema composition
└── *.ts # Domain-specific schemas
Quick Reference
Import Conventions
// Infrastructure types (via barrel)
import type { DataRequest, DataResponse, ApiClient } from '@shared/data/api'
import { ErrorCode, DataApiError, DataApiErrorFactory } from '@shared/data/api'
// Domain DTOs (directly from schema files)
import type { Topic, CreateTopicDto } from '@shared/data/api/schemas/topic'
import type { Message, CreateMessageDto } from '@shared/data/api/schemas/message'
Adding New Schemas
- Create schema file in
schemas/(e.g.,topic.ts) - Register in
schemas/index.tsusing intersection type - Implement handlers in
src/main/data/api/handlers/