代码之家  ›  专栏  ›  技术社区  ›  Tony BenBrahim

在dynamodb中处理统一化词典的可能性

  •  0
  • Tony BenBrahim  · 技术社区  · 4 年前

    dynamodb表中有一个项目 documents ,本应为字典,但不能初始化。我有下面的代码来设置字典,如果没有定义,或者因为定义了字典而失败,则向现有字典添加一个键。是否可以将其合并到一个调用中(除了重新设计表以使用复合键)?我也可以对这两个更新进行批量更新(在向第二个更新添加条件之后),但我想知道这样做的合法方式是什么。

    const firstDocumentInput: UpdateCommandInput = {
            TableName: "UserRecords",
            Key: {
                accessToken: accessToken
            },
            UpdateExpression: "SET documents = :doc",
            ConditionExpression: "attribute_not_exists(documents)",
            ExpressionAttributeValues: { ":doc": { s3ObjectKey: documentRecord } }
        }
        await client.send(new UpdateCommand(firstDocumentInput))
            .catch(async err => {
                if (err.name === "ConditionalCheckFailedException") {
                    const subsequentDocumentInput: UpdateCommandInput = {
                        TableName: "UserRecords",
                        Key: {
                            accessToken: accessToken
                        },
                        UpdateExpression: "SET documents.#docId = :doc",
                        ExpressionAttributeNames: { "#docId": s3ObjectKey },
                        ExpressionAttributeValues: { ":doc": documentRecord }
                    }
                    await client.send(new UpdateCommand(subsequentDocumentInput));
                } else {
                    throw err;
                }
            })
    
    0 回复  |  直到 4 年前