如何向 DynamoDB CDK 表添加属性 [TypeScript]

huangapple go评论75阅读模式
英文:

How to add attribute to DynamoDB CDK table [TypeScript]

问题

我是新手,正在尝试创建一个用于 DynamoDB 表的 CDK。我的表设计如下:

@DynamoDBTable(tableName = "ccd")
public class abcd {
    @DynamoDBHashKey(attributeName = "id")
    private String id;

    @DynamoDBAttribute(attributeName = "ack")
    @DynamoDBIndexHashKey(attributeName = "ack", globalSecondaryIndexName = "ack-i")
    private String acknowledged;

    @DynamoDBAttribute(attributeName = "Go")
    private String go;

    @DynamoDBAttribute(attributeName = "ac")
    private long ac;

    @DynamoDBAttribute(attributeName = "amt")
    private long amt;

    @DynamoDBAttribute(attributeName = "cr")
    private int cr;

    @DynamoDBAttribute(attributeName = "ttl")
    private long ttl;
}

在我的 CDK 中,app.ts 大致如下:

export class MyCdkAppStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // 创建 DynamoDB 表
    const table = new dynamodb.Table(this, 'MyTable', {
      tableName: 'ccd',
      partitionKey: {
        name: 'id',
        type: dynamodb.AttributeType.STRING,
      },
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

    // 为 "ACK" 属性添加全局二级索引
    table.addGlobalSecondaryIndex({
      indexName: 'ack-i',
      partitionKey: {
        name: 'ack',
        type: dynamodb.AttributeType.STRING,
      },
    });
  }
}

你想要如何添加其他属性到表中,比如 Go、ac、ttl 等?

当我尝试使用 table.addAttributesattributeDefinitions: [] 时,在我的 IDE 中出现错误。

英文:

I am new to creating CDK and I am trying to create a CDK for a dynamoDB table.
I have table design like this.

@DynamoDBTable(tableName = "ccd")
public class abcd {
    @DynamoDBHashKey(attributeName = "id")
    private String id;
 
    @DynamoDBAttribute(attributeName = "ack")
    @DynamoDBIndexHashKey(attributeName = "ack", globalSecondaryIndexName = "ack-i")
    private String acknowledged;
 
    @DynamoDBAttribute(attributeName = "Go")
    private String go;
 
    @DynamoDBAttribute(attributeName = "ac")
    private long ac;
 
    @DynamoDBAttribute(attributeName = "amt")
    private long amt;
   
    @DynamoDBAttribute(attributeName = "cr")
    private int cr;
   
    @DynamoDBAttribute(attributeName = "ttl")
    private long ttl;
}

In the CDK my app.ts is something like this

export class MyCdkAppStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Create the DynamoDB table
    const table = new dynamodb.Table(this, 'MyTable', {
      tableName: 'ccd',
      partitionKey: {
        name: 'id',
        type: dynamodb.AttributeType.STRING,
      },
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

    // Add global secondary index for "ACK" attribute
    table.addGlobalSecondaryIndex({
      indexName: 'ack-i',
      partitionKey: {
        name: 'ack',
        type: dynamodb.AttributeType.STRING,
      },
    });
}

How should I add other attributed to the table ? For example Go, ac, ttl etc.

When I am doing table.addAttributes or attributeDefinitions : []
I am getting error in my IDE.

答案1

得分: 1

你不需要将非关键属性添加到表定义中。由于DynamoDB是无模式的,您只需要定义键,所有其他属性可以在插入或更新数据时按需要进行更改。

英文:

You don't add non-key attributes to your tables definition. As DynamoDB is schemaless, you need only define your keys, all other attributes can be altered and changed as you like on a per-item basis while inserting or updating the data.

huangapple
  • 本文由 发表于 2023年7月17日 22:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定