需要在 dynamodb 中创建 table 期间创建 TTL(生存时间)功能

need to create a TTL (time-to-live) feature during create table in dynamodb

我正在使用 nodejs 和 dynamodb。我需要在我的代码中添加 TTL 功能。当我在我的模式文件中添加以下代码并尝试创建 table 时,它给出以下错误。我的 timetolive 字段也没有在 table 上创建。

{
  AttributeDefinitions: [
    { AttributeName: 'foo', AttributeType: 'S' },
    { AttributeName: 'baar', AttributeType: 'N' },
    { AttributeName: 'timetolive', AttributeType: 'N' }
  ],
  KeySchema: [
    { AttributeName: 'foo', KeyType: 'HASH' },
    { AttributeName: 'baar', KeyType: 'RANGE' }
  ],
  ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
  TimeToLiveSpecification: { AttributeName: 'timetolive', Enabled: true },
  TableName: 'mytablename'
}

错误:

UnexpectedParameter: Unexpected key 'TimeToLiveSpecification' found in params

您应该AttributeDefinitions中删除timetoliveAttributeDefinitions 仅适用于主 table 及其索引中的哈希或排序键中使用的属性。所以应该是:

{
  AttributeDefinitions: [
    { AttributeName: 'foo', AttributeType: 'S' },
    { AttributeName: 'baar', AttributeType: 'N' }
  ],
  KeySchema: [
    { AttributeName: 'foo', KeyType: 'HASH' },
    { AttributeName: 'baar', KeyType: 'RANGE' }
  ],
  ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
  TimeToLiveSpecification: { AttributeName: 'timetolive', Enabled: true },
  TableName: 'mytablename'
}