import { DMLSchema, IDmlEntity, IDmlEntityConfig, InferDmlEntityNameFromConfig, Prettify, SnakeCase } from "@medusajs/types"; import { PrimaryKeyModifier } from "../../dml/properties/primary-key"; /** * Utils */ type FlattenUnion = T extends { [K in keyof T]: infer U; } ? { [K in keyof T]: U; } : never; type UnionToIntersection = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; type UnionToOvlds = UnionToIntersection void : never>; type PopUnion = UnionToOvlds extends (a: infer A) => void ? A : never; type IsUnion = [T] extends [UnionToIntersection] ? false : true; type UnionToArray = IsUnion extends true ? UnionToArray>, [PopUnion, ...A]> : [T, ...A]; /** * End of utils */ /** * Linkable keys */ type InferLinkableKeyName = Property extends PrimaryKeyModifier ? `${Lowercase>>}_${Key & string}` : never; type InferSchemaLinkableKeys = T extends IDmlEntity ? { [K in keyof Schema as Schema[K] extends PrimaryKeyModifier ? InferLinkableKeyName : never]: Capitalize>; } : {}; type InferSchemasLinkableKeys[]> = { [K in keyof T]: InferSchemaLinkableKeys; }; type AggregateSchemasLinkableKeys[]> = { [K in keyof InferSchemasLinkableKeys]: InferSchemasLinkableKeys[K]; }; /** * From an array of DmlEntity, returns a formatted object with the linkable keys * * @example: * * const user = model.define("user", { * id: model.id(), * name: model.text(), * }) * * const car = model.define("car", { * id: model.id(), * number_plate: model.text().primaryKey(), * test: model.text(), * }) * * const linkableKeys = buildLinkableKeysFromDmlObjects([user, car]) // { user_id: 'user', car_number_plate: 'car' } * */ export type InferLinkableKeys[]> = UnionToIntersection>[0]>; /** * End Linkable keys */ /** * Links config */ /** * From a union infer an Array and return the last element */ type InferLastLink = UnionToArray[keyof InferSchemaLinksConfig]> extends [...any, infer V] ? V : never; type InferLastPrimaryKey = InferLastLink extends { primaryKey: infer PrimaryKey; } ? PrimaryKey : string; type InferLastLinkable = InferLastLink extends { linkable: infer Linkable; } ? Linkable : string; type InferPrimaryKeyNameOrNever = Schema[Key] extends PrimaryKeyModifier ? Key : never; type InferSchemaLinksConfig = T extends IDmlEntity ? { [K in keyof Schema as InferPrimaryKeyNameOrNever]: { serviceName: ServiceName; field: T extends IDmlEntity ? InferDmlEntityNameFromConfig : string; linkable: InferLinkableKeyName; primaryKey: K; }; } : {}; /** * From an array of DmlEntity, returns a formatted links object. * the toJSON of each object representation will return the last linkable definition * as the default. To specify a specific linkable, you can chain until the desired linkable property. * * * @example: * * const user = model.define("user", { * id: model.id(), * name: model.text(), * }) * * const car = model.define("car", { * id: model.id(), * number_plate: model.text().primaryKey(), * test: model.text(), * }) * * const linkConfig = buildLinkConfigFromModelObjects([user, car]) * // { * // user: { * // id: { * // serviceName: 'userService', * // field: 'user', * // linkable: 'user_id', * // primaryKey: 'id' * // }, * // toJSON() { ... } * // }, * // car: { * // number_plate: { * // serviceName: 'userService', * // field: 'car', * // linkable: 'car_number_plate', * // primaryKey: 'number_plate' * // }, * // toJSON() { ... } * // } * // } * */ export type InfersLinksConfig>> = Prettify<{ [K in keyof T as T[K] extends IDmlEntity ? InferDmlEntityNameFromConfig : never]: Prettify & { toJSON: () => { serviceName: ServiceName; field: T[K] extends IDmlEntity ? InferDmlEntityNameFromConfig : string; linkable: InferLastLinkable; primaryKey: InferLastPrimaryKey; }; }>; }>; export {}; /** * End Links config */ //# sourceMappingURL=links-config.d.ts.map