UNPKG

@proofkit/fmodata

Version:

FileMaker OData API client

77 lines (76 loc) 4.39 kB
import { Column } from '../../orm/column.js'; import { CountedListResult } from '../../types.js'; /** * Type-safe orderBy type that provides better DX than odata-query's default. * * Supported forms: * - `keyof T` - single field name (defaults to ascending) * - `[keyof T, 'asc' | 'desc']` - single field with explicit direction * - `Array<[keyof T, 'asc' | 'desc']>` - multiple fields with directions * * This type intentionally EXCLUDES `Array<keyof T>` to avoid ambiguity * between [field1, field2] and [field, direction]. */ export type TypeSafeOrderBy<T> = (keyof T & string) | [keyof T & string, "asc" | "desc"] | [keyof T & string, "asc" | "desc"][]; export type { ExpandConfig } from '../builders/shared-types.js'; export type ExpandedRelations = Record<string, { schema: any; selected: any; nested?: ExpandedRelations; }>; /** * Extract the value type from a Column. * This uses the phantom type stored in Column to get the actual value type (output type for reading). */ type ExtractColumnType<C> = C extends Column<infer T, any, any, any> ? T : never; /** * Map a select object to its return type. * For each key in the select object, extract the type from the corresponding Column. */ type MapSelectToReturnType<TSelect extends Record<string, Column<any, any, any, any>>, _TSchema extends Record<string, any>> = { [K in keyof TSelect]: ExtractColumnType<TSelect[K]>; }; /** * Helper: Resolve a single expand's return type, including nested expands */ export type ResolveExpandType<Exp extends { schema: any; selected: any; nested?: ExpandedRelations; }> = // Handle the selected fields (Exp["selected"] extends Record<string, Column<any, any, any, any>> ? MapSelectToReturnType<Exp["selected"], Exp["schema"]> : Exp["selected"] extends keyof Exp["schema"] ? Pick<Exp["schema"], Exp["selected"]> : Exp["schema"]) & (Exp["nested"] extends ExpandedRelations ? ResolveExpandedRelations<Exp["nested"]> : {}); /** * Helper: Resolve all expanded relations recursively */ export type ResolveExpandedRelations<Exps extends ExpandedRelations> = { [K in keyof Exps]: ResolveExpandType<Exps[K]>[]; }; /** * System columns option for select() method. * Allows explicitly requesting ROWID and/or ROWMODID when using select(). */ export interface SystemColumnsOption { ROWID?: boolean; ROWMODID?: boolean; } export type { CountedListResult } from '../../types.js'; /** * Extract system columns type from SystemColumnsOption. * Returns an object type with ROWID and/or ROWMODID properties when set to true. */ export type SystemColumnsFromOption<T extends SystemColumnsOption | undefined> = (T extends { ROWID: true; } ? { ROWID: number; } : {}) & (T extends { ROWMODID: true; } ? { ROWMODID: number; } : {}); export type QueryReturnType<T extends Record<string, any>, Selected extends keyof T | Record<string, Column<any, any, any, any>>, SingleMode extends "exact" | "maybe" | false, IsCount extends boolean, Expands extends ExpandedRelations, IncludeCount extends boolean, SystemCols extends SystemColumnsOption | undefined = undefined> = IsCount extends true ? number : IncludeCount extends true ? CountedListResult<[ Selected ] extends [Record<string, Column<any, any, any, any>>] ? MapSelectToReturnType<Selected, T> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols> : [Selected] extends [keyof T] ? Pick<T, Selected> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols> : never> : [ Selected ] extends [Record<string, Column<any, any, any, any>>] ? SingleMode extends "exact" ? MapSelectToReturnType<Selected, T> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols> : SingleMode extends "maybe" ? (MapSelectToReturnType<Selected, T> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols>) | null : (MapSelectToReturnType<Selected, T> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols>)[] : [ Selected ] extends [keyof T] ? SingleMode extends "exact" ? Pick<T, Selected> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols> : SingleMode extends "maybe" ? (Pick<T, Selected> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols>) | null : (Pick<T, Selected> & ResolveExpandedRelations<Expands> & SystemColumnsFromOption<SystemCols>)[] : never;