mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+131
-168
@@ -1,182 +1,145 @@
|
||||
---
|
||||
description: This rule provides best practices and coding standards for developing applications using AWS DynamoDB. It covers aspects like schema design, performance optimization, security, and testing.
|
||||
globs: *.js,*.ts,*.jsx,*.tsx,*.py
|
||||
description: Provides definitive best practices and actionable code examples for interacting with AWS DynamoDB using the AWS SDK v3, focusing on data modeling, performance, security, and common pitfalls.
|
||||
globs: **/*
|
||||
---
|
||||
- **Minimize the number of tables; prefer single-table design.** Having fewer tables keeps things more scalable, requires less permissions management, and reduces overhead for your DynamoDB application. Consider using a single table with appropriate use of primary and secondary indexes.
|
||||
- **Identify query patterns before schema design.** Understand the application's access patterns before designing the DynamoDB schema. Knowing how the data will be queried is crucial for optimizing performance and cost.
|
||||
- **Use sort order for related items and distribute queries to avoid hot spots.** Group related items together by utilizing sort keys and design data keys to distribute traffic evenly across partitions.
|
||||
- **Prefer queries over scans for efficiency.** DynamoDB queries are more efficient and less costly than scan operations. Always aim to use queries that filter based on partition and sort keys.
|
||||
- **Use caching to reduce read/write costs.** Implement caching strategies, such as AWS ElastiCache or DynamoDB Accelerator (DAX), to minimize DynamoDB read/write costs. Use appropriate invalidation logic.
|
||||
- **Validate data integrity in the application layer.** Since DynamoDB doesn't enforce relationship data or integrity constraints, it's essential to perform strict data validations in the application or business layer.
|
||||
# aws-dynamodb Best Practices
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
This guide outlines the definitive best practices for interacting with AWS DynamoDB, ensuring optimal performance, security, and maintainability. We leverage AWS SDK v3, modern data modeling, and infrastructure-as-code principles.
|
||||
|
||||
- **Directory Structure Best Practices:**
|
||||
- `src/`: Contains the application's source code.
|
||||
- `src/models/`: Defines data models representing DynamoDB entities.
|
||||
- `src/services/`: Implements business logic and interacts with DynamoDB.
|
||||
- `src/repositories/`: Handles DynamoDB data access operations.
|
||||
- `src/utils/`: Contains utility functions (e.g., data validation, error handling).
|
||||
- `tests/`: Includes unit, integration, and end-to-end tests.
|
||||
- `config/`: Stores configuration files (e.g., DynamoDB table names, AWS region).
|
||||
- **File Naming Conventions:**
|
||||
- Use descriptive names for files and directories.
|
||||
- Model files: `user.model.js` or `user.model.ts`
|
||||
- Service files: `user.service.js` or `user.service.ts`
|
||||
- Repository files: `user.repository.js` or `user.repository.ts`
|
||||
- Test files: `user.service.test.js` or `user.service.test.ts`
|
||||
- **Module Organization Best Practices:**
|
||||
- Encapsulate DynamoDB interactions within dedicated modules (e.g., repositories).
|
||||
- Use dependency injection to manage dependencies (e.g., DynamoDB client).
|
||||
- Follow the single responsibility principle for modules.
|
||||
- **Component Architecture Recommendations:**
|
||||
- Adopt a layered architecture (e.g., presentation, application, domain, infrastructure).
|
||||
- Decouple components to improve maintainability and testability.
|
||||
- Use interfaces to define contracts between components.
|
||||
- **Code Splitting Strategies:**
|
||||
- Bundle only the necessary aws-sdk/client-dynamodb code by using tree shaking and only importing the necessary modules.
|
||||
- Implement lazy loading for less frequently used features.
|
||||
## 1. Data Modeling: Single-Table Design is King
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
Embrace single-table design to minimize operational overhead and maximize query flexibility. Only use Global Secondary Indexes (GSIs) when your access patterns *cannot* be satisfied by the primary key.
|
||||
|
||||
- **Design Patterns:**
|
||||
- **Repository Pattern:** Abstract DynamoDB data access logic.
|
||||
- **Data Mapper Pattern:** Transform data between application and DynamoDB formats.
|
||||
- **Single Table Design Pattern:** Minimize tables by using composite keys and attributes to represent different entity types and relationships.
|
||||
- **Composite Keys Pattern:** Combine attributes to create partition and sort keys.
|
||||
- **Recommended Approaches for Common Tasks:**
|
||||
- **Creating Items:** Use the `PutItem` operation.
|
||||
- **Reading Items:** Use the `GetItem` operation for single items and `Query` for sets.
|
||||
- **Updating Items:** Use the `UpdateItem` operation for granular updates.
|
||||
- **Deleting Items:** Use the `DeleteItem` operation.
|
||||
- **Querying Data:** Utilize indexes (GSI/LSI) to optimize query performance. Use projection expressions to return only required attributes.
|
||||
- **Scanning Data (Avoid when possible):** If scanning is unavoidable, use `Limit` and `ExclusiveStartKey` for pagination and consider parallel scans for large datasets.
|
||||
- **Anti-patterns and Code Smells:**
|
||||
- **Over-fetching Data:** Avoid retrieving unnecessary attributes.
|
||||
- **Excessive Scanning:** Design schemas and queries to minimize scans.
|
||||
- **Hardcoding Table Names:** Use configuration files or environment variables.
|
||||
- **Ignoring Error Handling:** Implement robust error handling with retries and logging.
|
||||
- **Insufficient Data Validation:** Validate data before writing to DynamoDB.
|
||||
- **Hot Partitioning:** Design partitions to distribute data evenly and prevent overload of specific partitions.
|
||||
- **State Management Best Practices:**
|
||||
- Use DynamoDB to persist application state (e.g., user sessions, feature flags).
|
||||
- Implement atomic counters and conditional updates to manage concurrent access.
|
||||
- **Error Handling Patterns:**
|
||||
- Use try-catch blocks to handle DynamoDB errors.
|
||||
- Implement retry mechanisms for transient errors (e.g., throttling).
|
||||
- Log errors with sufficient context for debugging.
|
||||
- Use custom exception classes to categorize DynamoDB errors.
|
||||
### ✅ GOOD: Single-Table Design with Composite Primary Keys
|
||||
Model your primary key (Partition Key + Sort Key) to serve your most frequent access patterns. Use generic attribute names like `PK` and `SK` to allow for diverse item types within the same table.
|
||||
|
||||
```typescript
|
||||
// Example: User and Order data in a single table
|
||||
// PK: USER#<userId>, SK: #METADATA (for user details)
|
||||
// PK: USER#<userId>, SK: ORDER#<orderId> (for user's orders)
|
||||
|
||||
interface UserItem {
|
||||
PK: string; // e.g., USER#123
|
||||
SK: string; // e.g., #METADATA
|
||||
userId: string;
|
||||
username: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
interface OrderItem {
|
||||
PK: string; // e.g., USER#123
|
||||
SK: string; // e.g., ORDER#abc-123
|
||||
orderId: string;
|
||||
userId: string;
|
||||
orderDate: string;
|
||||
totalAmount: number;
|
||||
}
|
||||
|
||||
// Access Pattern: Get User by userId -> Query: PK = 'USER#<userId>', SK = '#METADATA'
|
||||
// Access Pattern: Get all Orders for a User -> Query: PK = 'USER#<userId>', SK begins_with 'ORDER#'
|
||||
```
|
||||
|
||||
### ❌ BAD: Multi-Table Design for Related Entities
|
||||
Creating separate tables for every entity type (e.g., `UsersTable`, `OrdersTable`) leads to increased operational complexity and often requires more complex cross-table queries or application-level joins.
|
||||
|
||||
```typescript
|
||||
// Don't do this unless absolutely necessary for strict isolation
|
||||
// UsersTable: PK = userId
|
||||
// OrdersTable: PK = orderId, GSI on userId
|
||||
```
|
||||
|
||||
## 2. Code Organization and Structure
|
||||
|
||||
Organize your DynamoDB interactions into dedicated data access layers (DALs) or repositories. Use the AWS SDK v3's modular clients and `async/await`.
|
||||
|
||||
### ✅ GOOD: Dedicated Data Access Layer with Typed Clients
|
||||
Encapsulate all DynamoDB operations, ensuring consistent error handling, logging, and parameter construction. Use the DocumentClient for simpler JSON interaction.
|
||||
|
||||
```typescript
|
||||
// src/data/userRepository.ts
|
||||
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
|
||||
import { DynamoDBDocumentClient, GetCommand, PutCommand } from "@aws-sdk/lib-dynamodb";
|
||||
|
||||
const client = new DynamoDBClient({ region: process.env.AWS_REGION });
|
||||
const ddbDocClient = DynamoDBDocumentClient.from(client);
|
||||
const TABLE_NAME = process.env.DYNAMODB_TABLE_NAME || "YourAppTable";
|
||||
|
||||
export class UserRepository {
|
||||
static async getUserById(userId: string): Promise<UserItem | undefined> {
|
||||
const params = {
|
||||
TableName: TABLE_NAME,
|
||||
Key: { PK: `USER#${userId}`, SK: '#METADATA' },
|
||||
};
|
||||
const { Item } = await ddbDocClient.send(new GetCommand(params));
|
||||
return Item as UserItem | undefined;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### ❌ BAD: Inline DynamoDB Calls or Generic Clients
|
||||
Avoid scattering DynamoDB logic throughout your application or using untyped, generic clients. This makes maintenance, testing, and refactoring extremely difficult.
|
||||
|
||||
```typescript
|
||||
// src/handlers/createUser.ts - Bad practice
|
||||
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
|
||||
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";
|
||||
|
||||
const client = new DynamoDBClient({});
|
||||
const ddbDocClient = DynamoDBDocumentClient.from(client);
|
||||
|
||||
export const handler = async (event: any) => {
|
||||
// ...
|
||||
await ddbDocClient.send(new PutCommand({
|
||||
TableName: "MyHardcodedTable", // Hardcoded table name
|
||||
Item: { PK: `USER#${event.userId}`, SK: '#METADATA', /* ... */ },
|
||||
}));
|
||||
};
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
- **Optimization Techniques:**
|
||||
- **Minimize Read Capacity Units (RCUs) and Write Capacity Units (WCUs):** Optimize queries and writes to consume fewer RCUs and WCUs. Use projection expressions to return only the attributes you need.
|
||||
- **Use Batch Operations:** Use `BatchGetItem` and `BatchWriteItem` for bulk operations.
|
||||
- **Optimize Data Sizes:** Keep item sizes small to reduce storage costs and improve performance. If items exceed the size limit, consider storing larger attributes in S3 and referencing them in DynamoDB.
|
||||
- **Use Parallel Scans (with caution):** Use parallel scans to speed up full table scans, but be aware of the increased RCUs consumption.
|
||||
- **Optimize Index Usage:** Use indexes effectively to support query patterns. Be mindful of the cost implications of GSI writes.
|
||||
- **Leverage DynamoDB Accelerator (DAX):** Use DAX for in-memory caching to reduce latency and RCU consumption.
|
||||
- **Memory Management Considerations:**
|
||||
- Avoid loading large datasets into memory.
|
||||
- Use pagination to process data in chunks.
|
||||
- Release resources promptly after use.
|
||||
- **Bundle Size Optimization Strategies:**
|
||||
- Use code splitting to reduce the initial bundle size.
|
||||
- Remove unused dependencies.
|
||||
- Optimize image sizes (if applicable).
|
||||
- **Lazy Loading Strategies:**
|
||||
- Load data on demand when needed.
|
||||
- Use placeholders for content that is not immediately visible.
|
||||
Optimize for read/write capacity, minimize data transfer, and leverage eventual consistency where appropriate.
|
||||
|
||||
## 4. Security Best Practices
|
||||
### ✅ GOOD: Use On-Demand Capacity or Provisioned with Auto-Scaling
|
||||
Choose `ON_DEMAND` for unpredictable workloads. For predictable traffic, use `PROVISIONED` with `AutoScaling` to manage costs and performance.
|
||||
|
||||
- **Common Vulnerabilities and Prevention:**
|
||||
- **Injection Attacks:** Prevent NoSQL injection by validating and sanitizing user inputs.
|
||||
- **Unauthorized Access:** Implement proper authentication and authorization mechanisms.
|
||||
- **Data Exposure:** Encrypt sensitive data at rest and in transit.
|
||||
- **Input Validation Best Practices:**
|
||||
- Validate data types, formats, and ranges.
|
||||
- Sanitize inputs to prevent injection attacks.
|
||||
- Use input validation libraries (e.g., Joi, Validator.js).
|
||||
- **Authentication and Authorization Patterns:**
|
||||
- Use AWS Identity and Access Management (IAM) roles and policies to control access to DynamoDB resources.
|
||||
- Implement fine-grained access control using conditional IAM policies.
|
||||
- Consider using AWS Cognito for user authentication and authorization.
|
||||
- **Data Protection Strategies:**
|
||||
- Enable encryption at rest and in transit for DynamoDB tables.
|
||||
- Use AWS Key Management Service (KMS) to manage encryption keys.
|
||||
- Implement data masking and tokenization for sensitive data.
|
||||
- Comply with data privacy regulations (e.g., GDPR, CCPA).
|
||||
- **Secure API Communication:**
|
||||
- Use HTTPS for all API communication.
|
||||
- Implement API authentication and authorization.
|
||||
- Protect against cross-site scripting (XSS) and cross-site request forgery (CSRF) attacks.
|
||||
```yaml
|
||||
# CloudFormation (example)
|
||||
Resources:
|
||||
MyDynamoDBTable:
|
||||
Type: AWS::DynamoDB::Table
|
||||
Properties:
|
||||
TableName: !Sub "${Environment}-MyTable-v1"
|
||||
AttributeDefinitions:
|
||||
- AttributeName: PK
|
||||
AttributeType: S
|
||||
- AttributeName: SK
|
||||
AttributeType: S
|
||||
KeySchema:
|
||||
- AttributeName: PK
|
||||
KeyType: HASH
|
||||
- AttributeName: SK
|
||||
KeyType: RANGE
|
||||
BillingMode: PAY_PER_REQUEST # ON_DEMAND is default for new tables
|
||||
PointInTimeRecoverySpecification: { PointInTimeRecoveryEnabled: true }
|
||||
SSESpecification: { SSEEnabled: true } # Encryption at rest
|
||||
```
|
||||
|
||||
## 5. Testing Approaches
|
||||
### ❌ BAD: Fixed Provisioned Capacity for Spiky Workloads
|
||||
Manually setting fixed provisioned capacity for unpredictable workloads leads to either throttling (under-provisioned) or wasted cost (over-provisioned).
|
||||
|
||||
- **Unit Testing Strategies:**
|
||||
- Mock DynamoDB client using libraries like `aws-sdk-mock` or `jest-mock-extended`.
|
||||
- Test individual functions and modules in isolation.
|
||||
- Verify that functions correctly interact with the mocked DynamoDB client.
|
||||
- **Integration Testing Approaches:**
|
||||
- Test the integration between different modules and components.
|
||||
- Use a local DynamoDB instance (e.g., DynamoDB Local, Docker image) for integration tests.
|
||||
- Verify that data is correctly written to and read from DynamoDB.
|
||||
- **End-to-end Testing Recommendations:**
|
||||
- Test the entire application flow from the user interface to DynamoDB.
|
||||
- Use a staging environment that mirrors the production environment.
|
||||
- Verify that the application meets all functional and non-functional requirements.
|
||||
- **Test Organization Best Practices:**
|
||||
- Organize tests by module or component.
|
||||
- Use descriptive names for test cases.
|
||||
- Follow the AAA (Arrange, Act, Assert) pattern.
|
||||
- **Mocking and Stubbing Techniques:**
|
||||
- Use mocking libraries to create mock objects for DynamoDB client.
|
||||
- Use stubbing to replace real dependencies with controlled test values.
|
||||
- Verify that mocked methods are called with the expected parameters.
|
||||
```yaml
|
||||
# CloudFormation (example of bad practice for spiky workloads)
|
||||
Resources:
|
||||
MyDynamoDBTable:
|
||||
Type: AWS::DynamoDB::Table
|
||||
Properties:
|
||||
# ...
|
||||
BillingMode: PROVISIONED
|
||||
ProvisionedThroughput:
|
||||
ReadCapacityUnits: 10 # Fixed, will throttle or waste money
|
||||
WriteCapacityUnits: 10 # Fixed, will throttle or waste money
|
||||
```
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
- **Frequent Mistakes:**
|
||||
- Forgetting to handle pagination when querying large datasets.
|
||||
- Not understanding DynamoDB capacity units (RCUs and WCUs) and throttling.
|
||||
- Using inefficient query patterns.
|
||||
- Neglecting to implement proper error handling.
|
||||
- Designing schemas that lead to hot partitions.
|
||||
- **Edge Cases:**
|
||||
- Handling large item sizes exceeding DynamoDB's limits.
|
||||
- Dealing with eventual consistency when reading data.
|
||||
- Managing concurrent updates to the same item.
|
||||
- **Version-Specific Issues:**
|
||||
- Be aware of breaking changes in aws-sdk/client-dynamodb versions.
|
||||
- Consult the AWS documentation and release notes for migration guides.
|
||||
- **Compatibility Concerns:**
|
||||
- Ensure compatibility between aws-sdk/client-dynamodb and other libraries.
|
||||
- **Debugging Strategies:**
|
||||
- Use AWS CloudWatch Logs to monitor DynamoDB operations.
|
||||
- Enable DynamoDB Streams to capture changes to DynamoDB tables.
|
||||
- Use the AWS X-Ray service for distributed tracing.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
- **Recommended Development Tools:**
|
||||
- AWS CLI
|
||||
- AWS SDK for Javascript/Typescript
|
||||
- DynamoDB Local (for local development)
|
||||
- NoSQL Workbench for DynamoDB (for data modeling).
|
||||
- **Build Configuration Best Practices:**
|
||||
- Use a build tool (e.g., webpack, Parcel, esbuild) to bundle and optimize code.
|
||||
- Configure environment variables for DynamoDB table names, AWS region, and credentials.
|
||||
- Use `.env` files or AWS Systems Manager Parameter Store to manage configuration data.
|
||||
- **Linting and Formatting Recommendations:**
|
||||
- Use ESLint or TSLint for linting.
|
||||
- Use Prettier for code formatting.
|
||||
- Configure linting and formatting rules to enforce consistent code style.
|
||||
- **Deployment Best Practices:**
|
||||
- Use infrastructure-as-code tools (e.g., AWS CloudFormation, AWS CDK, Terraform) to provision DynamoDB resources.
|
||||
- Implement blue/green deployments or canary deployments to minimize downtime.
|
||||
- Automate deployments using CI/CD pipelines.
|
||||
- **CI/CD Integration Strategies:**
|
||||
- Use CI/CD tools (e.g., Jenkins, GitLab CI, CircleCI, AWS CodePipeline) to automate the build, test, and deployment process.
|
||||
- Run unit and integration tests in the CI/CD pipeline.
|
||||
- Deploy code to staging environments for testing and validation before deploying to production.
|
||||
### ✅ GOOD: Leverage `Query` and `GetItem` with Strong Typing
|
||||
`GetItem` for single item retrieval by primary key. `Query` for items with the same partition key and an optional sort key condition. Always use `ConsistentRead:
|
||||
Reference in New Issue
Block a user