0

Let's say I have the following AWS SAM resource that creates a DynamoDB table:

EmployeeTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Employee
      AttributeDefinitions:
        - AttributeName: EmployeeId
          AttributeType: S
        - AttributeName: LocationId
          AttributeType: S
        - AttributeName: DepartmentId
          AttributeType: S
      KeySchema:
        - AttributeName: EmployeeId
          KeyType: HASH
      GlobalSecondaryIndexes:
        - IndexName: Location-index
          KeySchema:
            - AttributeName: LocationId
              KeyType: HASH
          Projection:
            ProjectionType: ALL
      BillingMode: PAY_PER_REQUEST

And I want to interact with DynamoDB through Dynamoose.

In Dynamoose, we have the Model and Schema resources that help us define the shape of our table.

Because I already created the table and the schema (attributes, indexes, etc) within the AWS SAM template, how should I use Dynamoose Schema? How should I use indexes?

Thanks!

Maor agai
  • 221
  • 1
  • 3
  • 11

1 Answers1

0

Here is how we can implement the dynamodb table using dynamoose. Where schema can be written like:

const schema = new dynamoose.Schema({
  EmployeeId: {
    type: String,
    required: true,
    hashKey: true,
  },
  LocationId: {
    type: String,
    required: true,
    index: [
      {
        global: true,
        name: 'Location-index',
        throughput: { read: 1, write: 1 },
        project: true,
      },
    ],
  },
  DepartmentId: {
    type: String,
    required: true,
  },
});

And for model, we just need to pass the schema like:

const Employee = dynamoose.model("Employee", schema);
Suleman Elahi
  • 173
  • 3
  • 13
  • I know how to implement it with Dynamoose, but my question is regarding the combination with AWS SAM. How should I combine it together? – Maor agai Jan 27 '23 at 09:12
  • @Maoragai But its not mentioned in the above question. Kindly update it then also elaborate what you mean by combination. – Suleman Elahi Jan 31 '23 at 10:15