-
Notifications
You must be signed in to change notification settings - 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 2 commits
2478372
1e98b61
f64e4ad
57ead08
24d86a1
bf104bb
7637050
3088d85
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?
Member
Author
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. I am trying to keep this as loose as possible while defaulting to ALL in most error cases. 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. I think we should be having stricter checks. In the case an unintended value is sent, we want to catch that ideally instead of defaulting to ALL (and not letting the user know). @svidgen what do you think? |
||
| 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
Uh oh!
There was an error while loading. Please reload this page.
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.
This is due to the introduction of the possibly undefined
projectionproperty in theIndexDirectiveConfigurationtype (found here). ThegetArgumentsfunction signature isgetArguments = <T>(defaultValue: Required<T>,...), so the first argument has to be cast asRequired.