Last active
January 11, 2021 21:57
-
-
Save kiewic/b175e6a926d3ddd7277463980e8bd3b2 to your computer and use it in GitHub Desktop.
How to insert or delete items from a DynamoDB table using Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
var region = "us-west-2"; | |
var accessKeyId = process.env.DYNAMODB_ACCESS_KEY_ID; | |
var secretAccessKey = process.env.DYNAMODB_SECRET_ACCESS_KEY; | |
var tableName = "your table name"; | |
var dynamoDB = new AWS.DynamoDB({ | |
region: region, | |
accessKeyId: accessKeyId, | |
secretAccessKey: secretAccessKey, | |
}); | |
// One item with two properties: question_id and title. | |
var params = { | |
Item: { | |
question_id: { | |
N: "12345" // Number value. | |
}, | |
title: { | |
S: "Foo foo foo" // String value. | |
} | |
}, | |
ReturnConsumedCapacity: "TOTAL", | |
TableName: tableName, | |
}; | |
dynamoDB.putItem(params, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} | |
else { | |
console.log(data); | |
} | |
}); | |
var fileItem = { | |
Key: { | |
question_id: { | |
N: "1234" // My partition key is a number. | |
} | |
}, | |
TableName: tableName, | |
}; | |
dynamoDB.deleteItem(fileItem, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} | |
else { | |
console.log(data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
deleteItem is not a function