hardyscc / nestjs-dynamoose

Dynamoose module for Nest
MIT License
138 stars 24 forks source link

Example with Hash and Range Key #1141

Open jaekunchoi opened 2 weeks ago

jaekunchoi commented 2 weeks ago

Is there a reason why this isn't working? I tried many variations in schema with no luck. Any help would be great


[ ] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[x] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Getting below exception when updating item in DynamoDB table that contains Hash and Range Key.

ValidationException: The provided key element does not match the schema

Expected behavior

Should update the existing data

Minimal reproduction of the problem with instructions

Schema

export const ContentSchema = new dynamoose.Schema({
  task_id: {
    type: String,
    hashKey: true,
    index: {
      type: 'local',
      rangeKey: 'created_at',
    },
  },
});

Interface

export interface ContentKey {
  task_id: string;
  created_at: string;
}

export interface Content extends ContentKey {
  user_id: string;
  keywords: object;
  results: object;
  search_language: string;
  search_location: string;
  seed_keyword: string;
  task_status: string;
  updated_at: string;
}

Service

@Injectable()
export class ContentService {
  private readonly logger = new Logger(ContentService.name);
  constructor(
    private readonly openAiService: OpenAiService,
    @InjectModel('TaskStatus')
    private contentModel: Model<Content, ContentKey>,
    private readonly sseService: SseService,
  ) {}

  // taskKey object is { task_id: '12345', created_at: 'date_string' }
  async updateTask(taskKey: ContentKey, task: TaskDto) {
    const rangeKey = ContentSchema.rangeKey;
    return this.contentModel.update(taskKey, task);
  }
}

I am also creating table separately with Serverless

TaskStatus:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: TaskStatus
        StreamSpecification: 
          StreamViewType: NEW_AND_OLD_IMAGES  
        AttributeDefinitions:
          - AttributeName: task_id
            AttributeType: S
          - AttributeName: user_id
            AttributeType: S
          - AttributeName: created_at
            AttributeType: S
        KeySchema:
          - AttributeName: task_id
            KeyType: HASH
          - AttributeName: created_at
            KeyType: RANGE
        BillingMode: PAY_PER_REQUEST
        GlobalSecondaryIndexes:
        - IndexName: TaskStatus
          KeySchema:
            - AttributeName: user_id
              KeyType: HASH
          Projection:
            ProjectionType: ALL

What is the motivation / use case for changing the behavior?

Environment


NestJS version: X.Y.Z
NestJS Dynamoose version: X.Y.Z
Dynamoose version: X.Y.Z


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

hardyscc commented 1 week ago

should remove created_at from key

export interface ContentKey {
  task_id: string;
-  created_at: string;
}

you also need to remove KeySchema from

        - AttributeName: created_at
          AttributeType: S
      KeySchema:
        - AttributeName: task_id
          KeyType: HASH
-        - AttributeName: created_at
-          KeyType: RANGE
      BillingMode: PAY_PER_REQUEST

please checkout this project out as reference.