【问题标题】:what's the TableProps for dynamodb when using aws cdk使用 aws cdk 时 dynamodb 的 TableProps 是什么
【发布时间】:2020-07-01 01:46:13
【问题描述】:

我正在为 dynamodb 使用 aws cdk,想添加一个 dynamodb 表,已经得到此代码,dynamodb 的 TableProps 是什么?我以为是字符串类型的表名,但是好像错了,谁能帮帮我?

https://docs.aws.amazon.com/cdk/api/latest/typescript/api/aws-dynamodb/tableprops.html#aws_dynamodb_TableProps_tableName

import core = require('@aws-cdk/core');
import dynamodb = require('@aws-cdk/aws-dynamodb')

export class HelloCdkStack extends core.Stack {
  constructor(scope: core.App, id: string, props?: core.StackProps) {
    super(scope, id, props);

    new dynamodb.Table(this, 'MyFirstTable', {
        tableName: 'myTable'
      });
  }
}

这是错误

lib/dynamodb.ts:8:46 - error TS2345: Argument of type '{ tableName: string; }' is not assignable to parameter of type 'TableProps'.
  Property 'partitionKey' is missing in type '{ tableName: string; }' but required in type 'TableProps'.

 8     new dynamodb.Table(this, 'MyFirstTable', {
                                                ~
 9         tableName: 'myTable'
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10       });
   ~~~~~~~

  node_modules/@aws-cdk/aws-dynamodb/lib/table.d.ts:19:14
    19     readonly partitionKey: Attribute;
                    ~~~~~~~~~~~~
    'partitionKey' is declared here.

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb aws-cdk


    【解决方案1】:

    试试

    import { AttributeType } from '@aws-cdk/aws-dynamodb';
    
        new dynamodb.Table(this, 'MyFirstTable', {
          tableName: "myTable",
          partitionKey: {
                name: "MyPartitionkey,
                type: AttributeType.STRING
              }
        });
    

    【讨论】:

    • 我在答案中添加了import语句。
    【解决方案2】:

    TableProps 扩展了 TableOptions 并且“partionKey”是使用dynamodb.Table 创建表的必需属性。我认为 aws CDK 文档可以更清楚地说明“TableProps”如何使用“TableOptions”。

    export interface TableProps extends TableOptions

    在“partionKey”部分的 TableOptions 下它说:

    readonly partitionKey: Attribute;

    没有“?” “partionKey”和“:”之间的标记表示该属性是必需的。

    https://docs.aws.amazon.com/cdk/api/latest/typescript/api/aws-dynamodb/tableoptions.html#aws_dynamodb_TableOptions_partitionKey

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-04
      • 2021-09-26
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多