import type { ConnectionId } from './connection_id.ts'; import type { Identity } from './identity.ts'; import type { ScheduleAt } from './schedule_at.ts'; import type { TimeDuration } from './time_duration.ts'; import type { Timestamp } from './timestamp.ts'; import type { Uuid } from './uuid.ts'; type DoNotPrettify = | Identity | ConnectionId | Timestamp | TimeDuration | ScheduleAt | Uuid; type Primitive = string | number | boolean | bigint | symbol | null | undefined; /** * Utility to make TS show cleaner types by flattening intersections. * * Primitives pass through unchanged. A branded primitive (`string & Brand`) * is an object type, but mapping it yields a structural record of `String`'s * members that no longer satisfies `string`. The outer `object` check keeps a * generic `Prettify` spreadable. */ export type Prettify = T extends object ? T extends DoNotPrettify | Primitive ? T : { [K in keyof T]: T[K] } & {} : T; /** * Helper function to sets a field in an object */ export type SetField = Prettify< Omit & { [K in F]: V } >; /** * Sets a field in an object * @param x The original object * @param t The object containing the field to set * @returns A new object with the field set */ export function set( x: T, t: { [k in F]: V } ): SetField { return { ...x, ...t } as SetField; } /** * Helper to extract the value types from an object type */ export type Values = T[keyof T]; /** * A helper type to collapse a tuple into a single type if it has only one element. */ export type CollapseTuple = A extends [infer T] ? T : A; /** * Conditional-type helper for distinguishing a single type from a union. */ export type IsUnion = [T] extends [never] ? false : T extends any ? [U] extends [T] ? false : true : false; /** * True when an object type has exactly one known key. * * If keys widen to plain `string`, the exact count is no longer knowable, so * treat it as valid and let narrower call sites or runtime checks handle it. */ export type HasExactlyOneKnownKey = [keyof T] extends [never] ? false : string extends keyof T ? true : IsUnion extends true ? false : true; type CamelCaseImpl = S extends `${infer Head}_${infer Tail}` ? `${Head}${Capitalize>}` : S extends `${infer Head}-${infer Tail}` ? `${Head}${Capitalize>}` : S; /** * Convert "Some_identifier-name" -> "someIdentifierName" * - No spaces; allowed separators: "_" and "-" * - Normalizes the *first* character to lowercase (e.g. "User_Name" -> "userName") */ export type CamelCase = Uncapitalize>; type AccessorTail< K extends string, H extends string, > = K extends `${H}.${infer R}` ? R : never; type DottedHead = K extends `${infer H}.${string}` ? H : never; type PlainKey = K extends `${string}.${string}` ? never : K; /** * Splits dotted keys into nested objects: `{ 'a.b': X, c: Y }` becomes `{ a: { b: X }, c: Y }`. * Plain keys go through `Pick`, which keeps each property's declaration and so its doc comment. */ export type NestAccessors> = Readonly< Pick> > & { readonly [H in DottedHead]: NestAccessors<{ [K in keyof T & string as AccessorTail]: T[K]; }>; }; /** Type safe conversion from "some_identifier-name" to "some_identifier_name" * - No spaces; allowed separators: "_" and "-" * - Normalizes the *first* character to lowercase (e.g. "User_Name" -> "user_name") */ export type SnakeCase = S extends `${infer Head}${infer Tail}` ? Tail extends Uncapitalize ? `${Lowercase}${SnakeCase}` : `${Lowercase}_${SnakeCase}` : Lowercase; type PascalCaseImpl = S extends `${infer Head}_${infer Tail}` ? `${Capitalize}${PascalCaseImpl}` : S extends `${infer Head}-${infer Tail}` ? `${Capitalize}${PascalCaseImpl}` : Capitalize; /** * Convert "some_identifier-name" -> "SomeIdentifierName" * - No spaces; allowed separators: "_" and "-" * - Normalizes the *first* character to uppercase (e.g. "user_name" -> "UserName") */ export type PascalCase = PascalCaseImpl; /** * Check if a metadata type has fields that are incompatible with default values. * Default values cannot be combined with isPrimaryKey, isUnique, or isAutoIncrement. */ export type HasDefaultIncompatibleFields = M extends { isPrimaryKey: true; } ? true : M extends { isUnique: true } ? true : M extends { isAutoIncrement: true } ? true : false; /** * Check if a metadata type has a default value set. */ export type HasDefaultValue = M extends { defaultValue: any } ? true : false; /** * Validate that a column's metadata doesn't have invalid combinations. * Returns the metadata type if valid, or an error type if invalid. */ export type ValidateColumnMetadata = HasDefaultValue extends true ? HasDefaultIncompatibleFields extends true ? InvalidColumnMetadata<'default() cannot be combined with primaryKey(), unique(), or autoInc()'> : M : M; /** * Error type for invalid column metadata combinations. * This type is designed to cause a compile-time error with a descriptive message. */ export type InvalidColumnMetadata = { __error: Message; __brand: 'InvalidColumnMetadata'; };