Open MattWlodarski opened 1 month ago
Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance 🙂
Hey @MattWlodarski, Thanks for requesting this. We are marking this as a feature request for the team to evaluate further.
Environment information
Describe the feature
Feature Request: Optimize GSI Creation for Relationships in Amplify Summary In my current schema setup, I'm experiencing resource duplication in DynamoDB due to the creation of multiple Global Secondary Indexes (GSIs) with the same Partition Key. I believe there's an opportunity to enhance the way Amplify handles relationships and GSIs, specifically when querying by a related entity.
Current Schema Here is the relevant part of my schema:
export const OutcomesRecordSchema = a .model({ createdAt: a.string(), // required createdBy: a.id().required(), caseId: a.id().required(), // relationships case: a.belongsTo('Case', 'caseId'), }) .authorization((allow) => [allow.authenticated('userPools')]) .secondaryIndexes((index) => [ index('caseId').sortKeys(['createdAt']).name('byCase').queryField('outcomesRecordByCase'), ]);
Problem As shown, I have a relationship with the Case table, and I want to query outcomes by caseId. However, this setup creates two GSIs in DynamoDB that share the same Partition Key. Each duplicate Partition Key leads to unnecessary resource consumption, which is inefficient.Proposed Improvement I propose that Amplify should automatically add the GSI generated when a relationship is defined to the client (const client = generateClient();). This would eliminate the need to manually create a secondary index for querying, thereby reducing resource duplication.
Additionally, the auto-generated GSI currently lacks a Sort Key. It would be beneficial to provide the ability to define a Sort Key for this GSI directly within the schema, allowing for more flexible querying without creating redundant GSIs.
Use case
Use Case Resource Efficiency:
Scenario: Having multiple GSIs with the same Partition Key leads to resource duplication in DynamoDB. Benefit: Utilizing existing GSIs would reduce operational costs and improve resource management. Simplified Schema:
Scenario: Defining additional secondary indexes for querying complicates the schema. Benefit: Leveraging auto-generated GSIs would streamline schema definitions and enhance maintainability. Enhanced Query Capability:
Scenario: Needing to query by related entity IDs (e.g., caseId) while sorting by attributes like createdAt. Benefit: Allowing Sort Keys on auto-generated GSIs would enable more complex queries without redundancy. Improved Workflow:
Scenario: Developers need to make quick schema changes without complicating GSI management. Benefit: Simplified GSI handling would enable faster development cycles and easier collaboration.