-
Couldn't load subscription status.
- Fork 89
feat: add projection support for @index directive #3359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -368,7 +368,7 @@ export const validateSortDirectionInput = (config: PrimaryKeyDirectiveConfigurat | |
| * appendSecondaryIndex | ||
| */ | ||
| export const appendSecondaryIndex = (config: IndexDirectiveConfiguration, ctx: TransformerContextProvider): void => { | ||
| const { name, object, primaryKeyField } = config; | ||
| const { name, object, primaryKeyField, projection } = config; | ||
| if (isSqlModel(ctx, object.name.value)) { | ||
| return; | ||
| } | ||
|
|
@@ -382,11 +382,19 @@ export const appendSecondaryIndex = (config: IndexDirectiveConfiguration, ctx: T | |
| const partitionKeyType = attrDefs.find((attr) => attr.attributeName === partitionKeyName)?.attributeType ?? 'S'; | ||
| const sortKeyType = sortKeyName ? attrDefs.find((attr) => attr.attributeName === sortKeyName)?.attributeType ?? 'S' : undefined; | ||
|
|
||
| const projectionType = projection?.type || 'ALL'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wanted to check if this is intended. js allows any false values with the || operator. That is, if we had an undefined value, then it would default to ALL (as we want). However, empty or invalid strings, or null values would also end up defaulting to ALL from my understanding. Do we want to add checks for correct types? |
||
| const nonKeyAttributes = projection?.nonKeyAttributes || []; | ||
|
|
||
| if (projectionType === 'INCLUDE' && (!nonKeyAttributes || nonKeyAttributes.length === 0)) { | ||
| throw new Error(`@index '${name}': nonKeyAttributes must be specified when projection type is INCLUDE`); | ||
| } | ||
|
|
||
| if (!ctx.transformParameters.secondaryKeyAsGSI && primaryKeyPartitionKeyName === partitionKeyName) { | ||
| // Create an LSI. | ||
| table.addLocalSecondaryIndex({ | ||
| indexName: name, | ||
| projectionType: 'ALL', | ||
| projectionType, | ||
| ...(projectionType === 'INCLUDE' ? { nonKeyAttributes } : {}), | ||
| sortKey: sortKeyName | ||
| ? { | ||
| name: sortKeyName, | ||
|
|
@@ -398,7 +406,8 @@ export const appendSecondaryIndex = (config: IndexDirectiveConfiguration, ctx: T | |
| // Create a GSI. | ||
| table.addGlobalSecondaryIndex({ | ||
| indexName: name, | ||
| projectionType: 'ALL', | ||
| projectionType, | ||
| ...(projectionType === 'INCLUDE' ? { nonKeyAttributes } : {}), | ||
| partitionKey: { | ||
| name: partitionKeyName, | ||
| type: partitionKeyType, | ||
|
|
@@ -419,7 +428,7 @@ export const appendSecondaryIndex = (config: IndexDirectiveConfiguration, ctx: T | |
| const newIndex = { | ||
| indexName: name, | ||
| keySchema, | ||
| projection: { projectionType: 'ALL' }, | ||
| projection: { projectionType, ...(projectionType === 'INCLUDE' ? { nonKeyAttributes } : {}) }, | ||
| provisionedThroughput: cdk.Fn.conditionIf(ResourceConstants.CONDITIONS.ShouldUsePayPerRequestBilling, cdk.Fn.ref('AWS::NoValue'), { | ||
| ReadCapacityUnits: cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBModelTableReadIOPS), | ||
| WriteCapacityUnits: cdk.Fn.ref(ResourceConstants.PARAMETERS.DynamoDBModelTableWriteIOPS), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Quick question - just trying to understand why we're setting this as Required<> when IndexDirectiveConfiguration properties are defined as optional. Maybe some comments could be added to explain this decision in the file