/** * @file index.ts * @description Sistema de tipos para Dynamite ORM - Arquitectura con type-safety completo * @autor Miguel Alejandro * @fecha 2025-01-28 */ /** * Marca para identificar campos que no son atributos de BD (relaciones, métodos). */ export declare const NonAttributeBrand: unique symbol; /** * Marca para identificar campos opcionales en create() pero presentes en el modelo. */ export declare const CreationOptionalBrand: unique symbol; /** * Wrapper para marcar relaciones y campos virtuales que no se persisten en BD. * @example * ```typescript * class User extends Table { * @HasMany(() => Post, 'user_id') * declare posts: NonAttribute; * } * ``` */ export type NonAttribute = T & { [NonAttributeBrand]?: true; }; /** * Wrapper para marcar campos opcionales en create() (auto-generados: id, timestamps). * @example * ```typescript * class User extends Table { * @PrimaryKey() id: CreationOptional; * @CreatedAt() created_at: CreationOptional; * name: string; * } * await User.create({ name: 'Juan' }); // id y created_at opcionales * ``` */ export type CreationOptional = T & { [CreationOptionalBrand]?: true; }; /** * Extrae atributos de BD usando intersección: * - Primera parte: atributos REQUERIDOS con modificador `-?` * - Segunda parte: atributos OPCIONALES (solo CreationOptional) * - Ambos excluyen: never, undefined, null, NonAttribute, métodos * @example * ```typescript * class User { * id: CreationOptional; * created_at: CreationOptional; * name: string; * email: string; * posts: NonAttribute; * save(): void; * } * // InferAttributes = { name: string, email: string } & { id?: string, created_at?: number } * // = { name: string, email: string, id?: string, created_at?: number } * ``` */ export type InferAttributes = { [K in keyof T as T[K] extends ((...args: any[]) => any) | { [NonAttributeBrand]?: true; } | { [CreationOptionalBrand]?: true; } ? never : K]-?: Exclude; } & { [K in keyof T as T[K] extends { [CreationOptionalBrand]?: true; } ? K : never]?: Exclude; }; /** * Extrae solo relaciones (NonAttribute) preservando tipo Model | Model[] * Excluye propiedades de Object.prototype para evitar conflictos * @example * ```typescript * class User extends Table { * id: string; * posts: NonAttribute; // Array * profile: NonAttribute; // Individual * settings: NonAttribute; // Nullable * } * // InferRelations = { posts: Post[], profile: Profile, settings: Settings | null } * ``` */ type ObjectBuiltinKeys = keyof Record | 'toString' | 'valueOf' | 'hasOwnProperty' | 'isPrototypeOf' | 'propertyIsEnumerable' | 'toLocaleString' | 'constructor'; export type InferRelations = { [K in keyof T as T[K] extends NonAttribute ? K extends ObjectBuiltinKeys ? never : K : never]: T[K] extends NonAttribute ? P : never; }; /** * Extrae solo relaciones (campos NonAttribute). * Usado internamente para validación. * @example * ```typescript * class User { * id: string; * name: string; * posts: NonAttribute; * profile: NonAttribute; * } * // PickRelations = { posts: Post[], profile: Profile } * ``` */ export type PickRelations = { [K in keyof T as T[K] extends NonAttribute ? K : never]: T[K] extends NonAttribute ? U : never; }; /** * Operadores de comparación soportados. * Solo 8 operadores permitidos según especificación del usuario. */ export type QueryOperator = "=" | "$eq" | "<>" | "!=" | "$ne" | "<" | "$lt" | "<=" | "$lte" | ">" | "$gt" | ">=" | "$gte" | "in" | "$in" | "include" | "$include"; /** * Opciones de query con pre-cache de atributos (A) y relaciones (R) * @template T - Modelo de tabla * @template A - Cache de InferAttributes * @template R - Cache de InferRelations * @example * ```typescript * await User.where({}, { * where: { * name: 'Juan', * age: { $gte: 18, $lte: 65 } * }, * order: 'DESC', * limit: 10, * attributes: ['name', 'email'], * include: { * posts: { * where: { published: true }, * limit: 5, * include: { comments: true } * }, * profile: { * attributes: ['bio', 'avatar'] * } * } * }); * ``` */ export interface WhereOptions, R = InferRelations> { /** Filtros adicionales, que se suman a los del primer argumento de `where()` */ where?: { [K in keyof A]?: A[K] | { [N in QueryOperator]?: A[K]; }; }; /** Orden de resultados */ order?: "ASC" | "DESC" | { [K in keyof A]?: "ASC" | "DESC"; }; /** Número de items a saltar */ offset?: number; /** Alias de offset */ skip?: number; /** Número máximo de items a retornar */ limit?: number; /** Campos a seleccionar */ attributes?: Array; /** Relaciones a cargar (recursivo) */ include?: { [K in keyof R]?: NonNullable extends Array ? true | WhereOptions : true | Pick>, "attributes" | "include">; }; /** Incluir los registros con soft delete; por defecto quedan fuera */ deleted?: boolean; /** Clave devuelta como `cursor` por la consulta anterior; al usarla se ignora `skip` */ cursor?: Record; /** @deprecated Alias de `deleted` */ _includeTrashed?: boolean; } /** * Input para create() - InferAttributes ya maneja opcional/requerido con intersección * @example * ```typescript * class User extends Table { * id: CreationOptional; * created_at: CreationOptional; * name: string; * email: string; * } * // CreateInput = { name: string, email: string, id?: string, created_at?: number } * await User.create({ name: 'Juan', email: 'juan@example.com' }); // id y created_at opcionales * ``` */ export type CreateInput = InferAttributes; /** * Tipo para update() - todos los campos parciales. * @example * ```typescript * await user.update({ name: 'Ana' }); // Todos los campos opcionales * ``` */ export type UpdateInput = Partial>; /** * Picks only keys from T whose value extends V. * Selecciona solo las keys de T cuyo valor extiende V. * @example * ```typescript * class User { name: string; age: number; score: number; } * // PickByType, number> = { age: number, score: number } * ``` */ type StripBrands = T extends number & { [NonAttributeBrand]?: any; } ? number : T extends string & { [NonAttributeBrand]?: any; } ? string : T extends number & { [CreationOptionalBrand]?: any; } ? number : T extends string & { [CreationOptionalBrand]?: any; } ? string : T extends boolean & { [CreationOptionalBrand]?: any; } ? boolean : T; export type PickByType = { [K in keyof T as StripBrands> extends V ? K : never]: T[K]; }; export {};