import { Item } from "../item"; import { ComKey, LocKeyArray, PriKey } from "../key"; import { ItemQuery } from "../query/ItemQuery"; import { AffectedKeys, AllOperationResult, AllOptions, CreateOptions, FindOperationResult, FindOptions, OperationParams, UpdateOptions } from "./Operations"; /** * Get method signature - retrieves single item by key */ export interface GetMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (key: PriKey | ComKey): Promise; } /** * Create method signature - creates new item */ export interface CreateMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (item: Partial>, options?: CreateOptions): Promise; } /** * Update method signature - updates existing item */ export interface UpdateMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (key: PriKey | ComKey, item: Partial>, options?: UpdateOptions): Promise; } /** * Remove method signature - removes item */ export interface RemoveMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (key: PriKey | ComKey): Promise; } /** * Upsert method signature - updates or creates item */ export interface UpsertMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (key: PriKey | ComKey, item: Partial>, locations?: LocKeyArray, options?: UpdateOptions): Promise; } /** * All method signature - retrieves all items matching query with optional pagination. * * @param query - Optional query to filter items (may include limit/offset for backwards compatibility) * @param locations - Optional location hierarchy to scope the query * @param options - Optional pagination options (takes precedence over query limit/offset) * @returns Result containing items and pagination metadata * * @example Without options (backwards compatible) * ```typescript * const result = await operations.all({ compoundCondition: {...} }); * // result.items = [...all matching items...] * // result.metadata.total = result.items.length * ``` * * @example With options (new pattern) * ```typescript * const result = await operations.all( * { compoundCondition: {...} }, * [], * { limit: 50, offset: 0 } * ); * // result.items = [...first 50 items...] * // result.metadata.total = total matching count * // result.metadata.hasMore = true if more items exist * ``` */ export interface AllMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (query?: ItemQuery, locations?: LocKeyArray | [], options?: AllOptions): Promise>; } /** * One method signature - retrieves first item matching query */ export interface OneMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (query?: ItemQuery, locations?: LocKeyArray | []): Promise; } /** * Find method signature - finds multiple items using finder with optional pagination. * * Supports hybrid approach: * - If finder returns FindOperationResult, uses it directly (opt-in) * - If finder returns V[], framework applies post-processing pagination * * @param finder - Name of the finder method * @param params - Parameters for the finder * @param locations - Optional location hierarchy to scope the query * @param options - Optional pagination options (limit, offset) * @returns Result containing items and pagination metadata * * @example * ```typescript * // Without pagination (returns all results) * const result = await operations.find('byEmail', { email: 'test@example.com' }); * * // With pagination * const result = await operations.find('byEmail', { email: 'test@example.com' }, [], { limit: 10, offset: 0 }); * ``` */ export interface FindMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (finder: string, params: OperationParams, locations?: LocKeyArray | [], options?: FindOptions): Promise>; } /** * FindOne method signature - finds single item using finder */ export interface FindOneMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (finder: string, params: OperationParams, locations?: LocKeyArray | []): Promise; } /** * Finder method signature - finds multiple items. * * Supports hybrid approach for pagination: * - **Legacy signature**: Return `Promise` - framework applies post-processing pagination * - **Opt-in signature**: Return `Promise>` - finder handles pagination at source * * @example Legacy finder (framework handles pagination) * ```typescript * const byEmailFinder: FinderMethod = async (params) => { * return await database.findUsers({ email: params.email }); * }; * ``` * * @example Opt-in finder (finder handles pagination) * ```typescript * const byEmailFinder: FinderMethod = async (params, locations, options) => { * const query = buildQuery({ email: params.email }); * const total = await database.count(query); * * if (options?.offset) query.offset(options.offset); * if (options?.limit) query.limit(options.limit); * * const items = await database.find(query); * return { * items, * metadata: { * total, * returned: items.length, * offset: options?.offset ?? 0, * limit: options?.limit, * hasMore: (options?.offset ?? 0) + items.length < total * } * }; * }; * ``` */ export interface FinderMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (params: OperationParams, locations?: LocKeyArray | [], options?: FindOptions): Promise>; } /** * Action operation method signature - executes action on specific item by key */ export interface ActionOperationMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (key: PriKey | ComKey, action: string, params?: OperationParams): Promise<[V, AffectedKeys]>; } /** * Action method signature - user-defined action implementation */ export interface ActionMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (item: V, params: OperationParams): Promise<[V, AffectedKeys]>; } /** * AllAction operation method signature - executes action on all items */ export interface AllActionOperationMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (action: string, params?: OperationParams, locations?: LocKeyArray | []): Promise<[V[], AffectedKeys]>; } /** * All-action method signature - user-defined all-action implementation */ export interface AllActionMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (params: OperationParams, locations?: LocKeyArray | []): Promise<[V[], AffectedKeys]>; } /** * Facet operation method signature - executes facet on specific item by key */ export interface FacetOperationMethod { (key: PriKey | ComKey, facet: string, params?: OperationParams): Promise; } /** * Facet method signature - user-defined facet implementation */ export interface FacetMethod, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { (item: V, params: OperationParams): Promise; } /** * AllFacet operation method signature - executes facet on all items */ export interface AllFacetOperationMethod { (facet: string, params?: OperationParams, locations?: LocKeyArray | []): Promise; } /** * All-facet method signature - user-defined all-facet implementation */ export interface AllFacetMethod { (params: OperationParams, locations?: LocKeyArray | []): Promise; } /** * Extension maps for operations */ export interface OperationsExtensions, S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never> { /** Registered finder methods */ finders?: Record>; /** Registered action methods */ actions?: Record>; /** Registered facet methods */ facets?: Record>; /** Registered all-action methods */ allActions?: Record>; /** Registered all-facet methods */ allFacets?: Record>; }