import { ModelIndexType } from '../ModelIndex'; type ModelIndexTypeShape = ModelIndexType; export type PrimaryIndexFieldsToIR, ResolvedFields> = IdxFields extends readonly [ infer PK, ...infer SK extends never | readonly string[] ] ? { pk: PK extends keyof ResolvedFields ? { [Key in PK]: Exclude & (string | number); } : never; sk: unknown extends SK ? never : ResolvedSortKeyFields; compositeSk: SK['length'] extends 0 | 1 ? never : CompositeSkFieldName, SK[0]>; } : never; type ArrayShift> = Arr extends readonly [ infer _Shift, ...infer Rest ] ? Rest : never; type CompositeSkFieldName = SK extends readonly [infer A extends string, ...infer B extends string[]] ? CompositeSkFieldName}`> : Result; /** * Maps array of ModelIndexType to SecondaryIndexIrShape (defined in in data-schema-types) * */ export type SecondaryIndexToIR, ResolvedFields, Result extends readonly any[] = readonly []> = Idxs extends readonly [ infer First extends ModelIndexTypeShape, ...infer Rest extends ReadonlyArray ] ? SecondaryIndexToIR ]> : Result; /** * @typeParam Idx - accepts a single ModelIndexType * @typeParam ResolvedFields - resolved model fields * * @returns an IR with the following shape: * { * queryField: string; * pk: { [fieldName: string]: string | number } * sk: { [fieldName: string]: string | number } | never * } * * @remarks - the IR type alias is defined as SecondaryIndexIrShape in data-schema-types */ type SingleIndexIrFromType = Idx extends ModelIndexType ? { defaultQueryFieldSuffix: QueryFieldLabelFromTuple>; queryField: QueryField; pk: PK extends keyof ResolvedFields ? { [Key in PK]: Exclude; } : never; sk: unknown extends SK ? never : ResolvedSortKeyFields; compositeSk: SK['length'] extends 0 | 1 ? never : CompositeSkFieldName, SK[0]>; } : never; /** * @typeParam SK - tuple of SortKey field names, e.g. ['viewCount', 'createdAt'] * @typeParam StrStart - initial string value; expects capitalized Partition Key field name * * @returns Query field name: concatenated PascalCase string with an `And` separator * @example * QueryFieldLabelFromTuple<['viewCount', 'createdAt'], 'Title'> => 'TitleAndViewCountAndCreatedAt' */ type QueryFieldLabelFromTuple = SK extends readonly [infer A extends string, ...infer B extends string[]] ? QueryFieldLabelFromTuple}`> : StrStart; /** * @typeParam SK - tuple of SortKey field names, e.g. ['viewCount', 'createdAt'] * @typeParam ResolvedFields - resolved model fields * * @returns object type where the key is the sort key field name and the value is the resolved model field type * @example * { * viewCount: number; * createdAt: string; * } */ type ResolvedSortKeyFields = SK extends readonly [ infer A extends string, ...infer B extends string[] ] ? A extends keyof ResolvedFields ? { [Key in A]: Exclude; } & (B extends readonly never[] ? unknown : ResolvedSortKeyFields) : never : never; export {};