DynamoDb Nodejs - 放置和更新表

hyp*_*ack 2 node.js amazon-dynamodb

我有下表dynamoDB,我想value1在第一个实例上写入并value2留空。然后我运行额外的代码,然后才想用value2. 我是新手dynamodb所以在这方面遇到了一些麻烦。

我将如何设置我的表,以便第一次写入只能包含value1第二次写入更新项目value2

就目前而言,尝试更新时出现以下错误: ValidationException: The provided key element does not match the schema

我运行以下脚本来创建我的Id-map-table

var params3 = {
    TableName : "my-Id-map",
    KeySchema: [
        { AttributeName: "value1", KeyType: "HASH" },
        { AttributeName: "value2", KeyType: "RANGE"}
    ],
    AttributeDefinitions: [
        { AttributeName: "value1", AttributeType: "S" },
        { AttributeName: "value2", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
};

dynamodb.describeTable({TableName:"my-Id-map"}, function(err,result) {
     if (err) {
        createIdMapTable();
     }
   else {
        console.log("Table my-Id-map already exists");
    }
});

function createIdMapTable()
{
    dynamodb.createTable(params3, function(err, data) {
        if (err) {
            console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我按以下方式执行第一次写入:

itemManager.saveItemsId = function (value2, value1, callback) {

  value2 = value2.toString();
  value1 = value1.toString();

  var params = {
    TableName: 'my-Id-map',
    Item:{
      "value1": value1,
      "value2": value2
    }
  };

  dynamodb.put(params, callback);
};
Run Code Online (Sandbox Code Playgroud)

我想要update我的物品,例如:

    itemManager.updateItemId = function (value2, value1, callback) {
  value2 = value2.toString();
  value1 = value1.toString();

  var params = {
    TableName: 'my-Id-map',
    Key : {
      "value1" : {
        "S" : value1
      }
    },
    UpdateExpression : "SET #attrName =:attrValue",
    ExpressionAttributeNames : {
      "#attrName" : "value2"
    },
    ExpressionAttributeValues : {
      ":attrValue" : {
        "S" : value2
      }
    }
  };

  dynamodb.update(params, callback);
};
Run Code Online (Sandbox Code Playgroud)

arj*_*ori 5

正如我所看到的,您的 createTable() 和 describeTable() 语法是正确的,但更新和插入命令是错误的。

在您的插入命令中应该有 putItems() 并且在更新中应该有 updateItem。

我在这里给你语法。这是putItem 的语法,即插入命令

itemManager.saveItemsId = function(value2, value1, callback) {

value2 = value2.toString();
value1 = value1.toString();

var params = {
    TableName: 'my-Id-map',
    Item: {
        'value1': {
            'S': value1
        },
        'vallue2': {
            'S': value2
        }
    }
};
dynamodb.putItem(params, function(err, result) {
    if (err) {
        console.log("Error")
    } else {
        console.log("Data saved Success")
    }
})
Run Code Online (Sandbox Code Playgroud)

};

对于更新表:

itemManager.updateItemId = function(value2, value1, callback) {

value2 = value2.toString();
value1 = value1.toString();

var params = {
    TableName: 'my-Id-map',
    Key: {
        id: {
            'S': id
        }
    },
    UpdateExpression: 'SET #attrName =:attrValue',
    ExpressionAttributeNames: {
        "#attrName": "value2"
    },
    ExpressionAttributeValues: {
        ':attrValue': {
            'S': value2
        },
    }
};
dynamodb.updateItem(params, function(err, data) {
    if (err) {
        console.log("Error while update")

    } else {
        console.log("Data updated Successfully")
    }
})
Run Code Online (Sandbox Code Playgroud)

};

你也可以参考这个链接dynamodbSyntax