cherry-studio/src/main/data/services/base/IBaseService.ts
fullex 81bb8e7981 feat: implement new pagination types and enhance API documentation
- Introduced `OffsetPaginationParams`, `CursorPaginationParams`, and their corresponding response types to standardize pagination handling across the API.
- Updated existing API types and hooks to support both offset and cursor-based pagination, improving data fetching capabilities.
- Enhanced documentation with detailed usage examples for pagination, including request parameters and response structures, to aid developers in implementing pagination effectively.
- Refactored related components to utilize the new pagination types, ensuring consistency and clarity in data management.
2026-01-04 21:12:41 +08:00

114 lines
2.8 KiB
TypeScript

import type { CursorPaginationParams, OffsetPaginationParams, ServiceOptions } from '@shared/data/api/apiTypes'
/**
* Base pagination params for service layer (supports both modes)
*/
type BasePaginationParams = (OffsetPaginationParams | CursorPaginationParams) & Record<string, any>
/**
* Standard service interface for data operations
* Defines the contract that all services should implement
*/
export interface IBaseService<T = any, TCreate = any, TUpdate = any> {
/**
* Find entity by ID
*/
findById(id: string, options?: ServiceOptions): Promise<T | null>
/**
* Find multiple entities with pagination
*/
findMany(
params: BasePaginationParams,
options?: ServiceOptions
): Promise<{
items: T[]
total?: number
page?: number
nextCursor?: string
}>
/**
* Create new entity
*/
create(data: TCreate, options?: ServiceOptions): Promise<T>
/**
* Update existing entity
*/
update(id: string, data: TUpdate, options?: ServiceOptions): Promise<T>
/**
* Delete entity (hard or soft delete depending on implementation)
*/
delete(id: string, options?: ServiceOptions): Promise<void>
/**
* Check if entity exists
*/
exists(id: string, options?: ServiceOptions): Promise<boolean>
}
/**
* Extended service interface for soft delete operations
*/
export interface ISoftDeleteService<T = any, TCreate = any, TUpdate = any> extends IBaseService<T, TCreate, TUpdate> {
/**
* Archive entity (soft delete)
*/
archive(id: string, options?: ServiceOptions): Promise<T | void>
/**
* Restore archived entity
*/
restore(id: string, options?: ServiceOptions): Promise<T | void>
}
/**
* Extended service interface for search operations
*/
export interface ISearchableService<T = any, TCreate = any, TUpdate = any> extends IBaseService<T, TCreate, TUpdate> {
/**
* Search entities by query
*/
search(
query: string,
params?: BasePaginationParams,
options?: ServiceOptions
): Promise<{
items: T[]
total?: number
page?: number
nextCursor?: string
}>
}
/**
* Service interface for hierarchical data (parent-child relationships)
*/
export interface IHierarchicalService<TParent = any, TChild = any, TChildCreate = any> extends IBaseService<TParent> {
/**
* Get child entities for a parent
*/
getChildren(
parentId: string,
params?: BasePaginationParams,
options?: ServiceOptions
): Promise<{
items: TChild[]
total?: number
page?: number
nextCursor?: string
}>
/**
* Add child entity to parent
*/
addChild(parentId: string, childData: TChildCreate, options?: ServiceOptions): Promise<TChild>
/**
* Remove child entity from parent
*/
removeChild(parentId: string, childId: string, options?: ServiceOptions): Promise<void>
}