@aws-amplify/data-schema
Advanced tools
Comparing version 0.12.14 to 0.13.0
@@ -5,3 +5,3 @@ import { IsEmptyStringOrNever } from '@aws-amplify/data-schema-types'; | ||
/** | ||
* Maps array of ModelIndexType to SecondaryIndexIrShape | ||
* Maps array of ModelIndexType to SecondaryIndexIrShape (defined in in data-schema-types) | ||
* */ | ||
@@ -15,4 +15,17 @@ export type SecondaryIndexToIR<Idxs extends ReadonlyArray<ModelIndexTypeShape>, ResolvedFields, Result extends readonly any[] = readonly []> = Idxs extends readonly [ | ||
]> : 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 ModelIndexTypeShape, ResolvedFields> = Idx extends ModelIndexType<any, infer PK extends string, infer SK, infer QueryField extends string | never, any> ? { | ||
queryField: IsEmptyStringOrNever<QueryField> extends true ? `listBy${SkLabelFromTuple<SK, Capitalize<PK>>}` : QueryField; | ||
queryField: IsEmptyStringOrNever<QueryField> extends true ? `listBy${QueryFieldLabelFromTuple<SK, Capitalize<PK>>}` : QueryField; | ||
pk: PK extends keyof ResolvedFields ? { | ||
@@ -23,6 +36,22 @@ [Key in PK]: Exclude<ResolvedFields[PK], null>; | ||
} : never; | ||
type SkLabelFromTuple<T, StrStart extends string = ''> = T extends readonly [ | ||
infer A extends string, | ||
...infer B extends string[] | ||
] ? SkLabelFromTuple<B, `${StrStart}And${Capitalize<A>}`> : StrStart; | ||
/** | ||
* @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, StrStart extends string = ''> = SK extends readonly [infer A extends string, ...infer B extends string[]] ? QueryFieldLabelFromTuple<B, `${StrStart}And${Capitalize<A>}`> : 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, ResolvedFields> = SK extends readonly [ | ||
@@ -29,0 +58,0 @@ infer A extends string, |
@@ -15,5 +15,5 @@ import type { Brand } from '@aws-amplify/data-schema-types'; | ||
name(name: string): ModelIndexType<ModelFieldKeys, PK, SK, QueryField, K | 'name'>; | ||
queryField<QF extends string, MF extends ModelFieldKeys = ModelFieldKeys>(field: QF): ModelIndexType<MF, PK, SK, QF, K | 'queryField'>; | ||
queryField<QF extends string = never, MF extends ModelFieldKeys = ModelFieldKeys>(field: QF): ModelIndexType<MF, PK, SK, QF, K | 'queryField'>; | ||
}, K> & Brand<object, typeof brand>; | ||
export declare function modelIndex<ModelFieldKeys extends string, PK extends ModelFieldKeys, SK, QueryField>(partitionKeyFieldName: PK): ModelIndexType<ModelFieldKeys, PK, SK, QueryField, never>; | ||
export declare function modelIndex<ModelFieldKeys extends string, PK extends ModelFieldKeys, SK = readonly [], QueryField = never>(partitionKeyFieldName: PK): ModelIndexType<ModelFieldKeys, PK, SK, QueryField, never>; | ||
export {}; |
@@ -470,2 +470,13 @@ "use strict"; | ||
/** | ||
* | ||
* @param pk - partition key field name | ||
* @param sk - (optional) array of sort key field names | ||
* @returns default query field name | ||
*/ | ||
const secondaryIndexDefaultQueryField = (pk, sk) => { | ||
const skName = sk?.length ? 'And' + sk?.map(capitalize).join('And') : ''; | ||
const queryField = `listBy${capitalize(pk)}${skName}`; | ||
return queryField; | ||
}; | ||
/** | ||
* Given InternalModelIndexType[] returns a map where the key is the model field to be annotated with an @index directive | ||
@@ -475,5 +486,5 @@ * and the value is an array of transformed Amplify @index directives with all supplied attributes | ||
const transformedSecondaryIndexesForModel = (secondaryIndexes) => { | ||
const indexDirectiveWithAttributes = (sortKeys, indexName, queryField) => { | ||
const indexDirectiveWithAttributes = (partitionKey, sortKeys, indexName, queryField) => { | ||
if (!sortKeys.length && !indexName && !queryField) { | ||
return '@index'; | ||
return `@index(queryField: "${secondaryIndexDefaultQueryField(partitionKey)}")`; | ||
} | ||
@@ -490,2 +501,5 @@ const attributes = []; | ||
} | ||
else { | ||
attributes.push(`queryField: "${secondaryIndexDefaultQueryField(partitionKey, sortKeys)}"`); | ||
} | ||
return `@index(${attributes.join(', ')})`; | ||
@@ -495,3 +509,3 @@ }; | ||
acc[partitionKey] = acc[partitionKey] || []; | ||
acc[partitionKey].push(indexDirectiveWithAttributes(sortKeys, indexName, queryField)); | ||
acc[partitionKey].push(indexDirectiveWithAttributes(partitionKey, sortKeys, indexName, queryField)); | ||
return acc; | ||
@@ -498,0 +512,0 @@ }, {}); |
{ | ||
"name": "@aws-amplify/data-schema", | ||
"version": "0.12.14", | ||
"version": "0.13.0", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
213137
2822