import type { SchemaDeclaration } from '../../schema.js'; import type { MergeObjects, GetSingleType, Equals } from '../type-utils.js'; type GetCollectionName = keyof { [K in keyof TSchema as Equals extends true ? K : never]: unknown; }; /** * Returns the back-relation key if any column in `TCollectionName` points to `TypeToLookFor` * * @template TSchema - The schema declaration. * @template TCollectionName - The name of the collection to search for back-relation. * @template TypeToLookFor - The type to look for in the relation fields. */ type BackRelationKey = keyof { [K in keyof TSchema[TCollectionName]['relations'] as Equals, TypeToLookFor> extends true ? `${TCollectionName & string}_via_${K & string}` : never]: unknown; }; /** Back-relations that are inferred from the schema */ type ImplicitRelations = { [K1 in keyof TSchema]: { [K2 in keyof TSchema as BackRelationKey]: { type: GetSingleType; isOptional: true; isToMany: true; tableName: K2; }; }; }; type RelationHelper = { type: GetSingleType; isOptional: undefined extends T ? true : false; isToMany: NonNullable extends Array ? true : false; tableName: GetCollectionName>; }; /** Relations that are explicitly defined in the schema */ type ExplicitRelations = { [Collection in keyof TSchema]: { [Relation in keyof TSchema[Collection]['relations']]-?: RelationHelper; }; }; /** parse relations from the schema */ export type SchemaParser = { [K in keyof TSchema]: MergeObjects[K], ExplicitRelations[K]>; }; export {};