DynamoDB - 在 javascript 中使用嵌套文档创建 Table

DynamoDB - create Table with nested document in javascript

我的问题与 java 的问题类似,但需要有关 javascript aws-sdk 的帮助。找不到任何示例。

table 结构如下所示:

customer:
  customerId: '12345'
  policies:
  - policyNumber: 12345
    status: active

您可以尝试使用 DocumentClient.put() see documentation。下面是我现有代码的示例,已更新以匹配您的有效负载,但我还没有测试它,因为我不想污染我的 table.

   import AWS from 'aws-sdk';

   const params = {
      TableName: 'TBL_CUSTOMER', //or whatever your table name is
      Item: {
         customerId: '12345',
         policies: {
            policyNumber: 12345,
            status: 'active'
         }
      }
   };

   const documentClient = new AWS.DynamoDB.DocumentClient();

   const result = await new Promise((resolve, reject) => {
      documentClient.put(params, function (err, data) {
         if (err) reject(err);
         else resolve(data);
      });
   });
   console.log('result:', result);