amazon-dynamodbnestjsdynamoose

How to use existing DynamoDB tables in Dynamoose.js


We are creating DynamoDB tables in AWS using terraform.

Our application is a Nest.js application using nestjs-dynamoose and is deployed as a Lambda function. The lambda function is started without any error. The create option in dynamoose is set to false.

When trying to access the created tables the this.model.get([...]) never finishes:

const user = await this.model.get({id: instanceId});
console.log(user);
if (user) {
  return await this.model.update({
    value: token,
    id: instanceId,
  });
} else {
  return await this.model.create({id: instanceId, value: token});
}

If we do not create the tables with terraform and let dynamoose create the tables everything works smoothly.

How can we use the already existing tables from dynamoose?


Solution

  • For Models there are two settings you can set: create & waitForActive. It sounds like your table is fully created, active, and ready to go.

    If you set both of those settings to false this should work as you expect.

    Here is an example of some code to achieve that:

    const CatSchema = new dynamoose.Schema({"id": String, "age": Number});
    const Cat = dynamoose.model("Cat", CatSchema, {
        "create": false,
        "waitForActive": false
    });