// The factory vocabulary as types: what a factory is declared with, what each strategy hands back, // and what an override may name. Kept apart from the runtime so `factories.ts` holds one job — the // row pipeline — and a caller reading the contract reads it here. import type { ColumnMap, Insertable } from '@ultimat3/entity'; /** * The members of `entity()`'s value a factory reads, structurally: `@ultimat3/entity` is a * dynamic-only dependency of this barrel. `$table` is the PHYSICAL table. The optional members are * what a real entity adds — the default persister, tenancy and `references()` need them, a * hand-built lookalike in a unit test may leave them out. */ export interface EntityLike { readonly $table: string; readonly $columns: Readonly>; readonly $primaryKey?: readonly string[]; readonly $tenantColumn?: string | null; $references?(): readonly { readonly property: string; readonly targetProperty: string }[]; $parse?(value: unknown): unknown; } /** The seeded generators a `defaults` or trait body draws from. Never `Math.random` directly. */ export interface FactoryIds { uuid(): string; number(): number; } /** No members: the default for every optional map a factory declares. */ export type None = Record; /** What every body and callback of one row sees. `index` IS the sequence: 1, 2, 3 … per factory. */ export interface FactoryContext { readonly index: number; readonly ids: FactoryIds; /** The declared transient defaults, overridden by the call. Never written to the row. */ readonly transient: TTransient; } /** A named partial. A function form gets the row's index, the seeded generators and the transients. */ export type Trait = | Partial | ((index: number, ids: FactoryIds, transient: TTransient) => Partial); export type TraitMap = Readonly< Record> >; /** * The structural face of any factory, as an association holds it. Method syntax on purpose: a * factory's `build(over?: Overrides<…>)` is assignable here only through method bivariance. */ export interface FactoryLike { readonly entity: EntityLike; build(over?: object): TBuilt; create(over?: object): Promise; reset(): void; } /** `belongsTo(orgs, 'orgId')`: the parent built with the strategy the child is built with. */ export interface BelongsTo { readonly kind: 'belongsTo'; readonly parent: FactoryLike; readonly foreignKey: TKey; /** The parent's column the key holds. Unset: the child entity's `references()`, else `id`. */ readonly references: string | undefined; } /** `hasMany(comments, 'postId')`: children written AFTER the parent, the key set from it. */ export interface HasMany { readonly kind: 'hasMany'; readonly child: FactoryLike; readonly foreignKey: TKey; readonly references: string | undefined; /** Children made when the call names none. Default 0. */ readonly count: number; } // `object`, not `unknown`: a `FactoryLike` is invariant in nothing that matters here, and // the map's job is only to say "every value is an association"; each one's own type is kept. export type BelongsToMap = Readonly< Record> >; export type HasManyMap = Readonly>>; type ParentValue = B extends BelongsTo ? TBuilt | TRow : never; type ManyValue = H extends HasMany ? number | readonly Partial[] : never; type ManyBuilt = H extends HasMany ? TBuilt : never; type ManyStored = H extends HasMany ? TRow : never; export type ForeignKeysOf = { [K in keyof TBelongs]: TBelongs[K] extends BelongsTo ? F : never; }[keyof TBelongs]; /** * One call's overrides: columns, transients, a parent row per `belongsTo` name (`{ org: row }`, * reused rather than made), and per `hasMany` name a count or one override per child. */ export type FactoryOverrides = Partial & Partial & { readonly [K in keyof TBelongs]?: ParentValue } & { readonly [K in keyof TMany]?: ManyValue; }; /** * `build()`'s answer: the attributes, each `hasMany` name holding the children built with them — * as a NON-enumerable property, so the row itself is only its columns. */ export type Built = TAttrs & { readonly [K in keyof TMany]: readonly ManyBuilt[]; }; /** `create()`'s answer: the row AS STORED, each `hasMany` name (non-enumerable) its stored children. */ export type Created = TRow & { readonly [K in keyof TMany]: readonly ManyStored[]; }; /** * `attributesFor()`'s answer — an input, not a row: no parent key. The primary key is dropped at * runtime (`$primaryKey`); the type omits `id`, the one key name `entity()` gives a type to see. */ export type AttributesFor = Omit | 'id'>; /** The stored row of a real `entity()` (`$row`); a lookalike's is what its factory builds. */ export type StoredOf = E extends { readonly $row: infer R extends object } ? R : TAttrs; /** * What `defaults` may name for an `entity()`: its columns, each as its write shape. A column the * entity does not declare, or a value of the wrong kind, is a compile error at the factory. */ export type WriteBound = E extends { readonly $columns: infer C extends ColumnMap } ? Partial> : object; /** * Any value at all (`{} | null | undefined` is `unknown`, so `Record` fits), but * spelled as a union so the checker sees it include `string` — `unknown` alone would not keep a literal. */ // biome-ignore lint/complexity/noBannedTypes: `{}` is what makes the union admit `unknown`. export type AttrValue = string | number | boolean | bigint | symbol | {} | null | undefined; /** * The bound on what `defaults` returns. Its per-key `AttrValue` is what keeps `status: 'open'` a * literal: TypeScript widens a literal unless the slot it lands in is a type variable whose * constraint holds `string`, and `WriteBound` is a conditional over an unfixed `E` that never * is. Self-referential (`keyof A`) rather than a `Record`, so an `interface` still satisfies it. */ export type DefaultsBound = { readonly [K in keyof A]: AttrValue } & WriteBound; type Widened = V extends string ? string : V extends number ? number : V extends boolean ? boolean : V extends bigint ? bigint : V; /** * The literals `DefaultsBound` kept, widened back once checked: `title: \`t${n}\`` must not make * `build({ title: 'x' })` a type error. A column's own union returns through `Partial`. */ export type SettledAttrs = { [K in keyof A]: Widened };