Last active
March 14, 2023 03:37
-
-
Save mojodna/152e1875a3ad784bcf03 to your computer and use it in GitHub Desktop.
DynamoDB samples
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
"use strict"; | |
var AWS = require("aws-sdk"); | |
AWS.config.update({ | |
region: process.env.AWS_DEFAULT_REGION || AWS.config.region || "us-east-1" | |
}); | |
var dynamodb = new AWS.DynamoDB(); | |
var TABLE_NAME = "test"; | |
dynamodb.batchGetItem({ | |
RequestItems: { | |
test: { // table name | |
Keys: [ | |
{ | |
key: { | |
S: "42" | |
} | |
}, | |
{ | |
key: { | |
S: "11" | |
} | |
} | |
], | |
ConsistentRead: true | |
} | |
} | |
}, function(err, data) { | |
if (err) { | |
throw err; | |
} | |
console.log("data:", data); | |
data.Responses[TABLE_NAME].forEach(function(item) { | |
try { | |
var result = JSON.parse(item.result.S); | |
console.log("%s:", item.key.S, result); | |
} catch (err) { | |
throw err; | |
} | |
}); | |
}); |
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
"use strict"; | |
var AWS = require("aws-sdk"); | |
AWS.config.update({ | |
region: process.env.AWS_DEFAULT_REGION || AWS.config.region || "us-east-1" | |
}); | |
var dynamodb = new AWS.DynamoDB(); | |
dynamodb.createTable({ | |
AttributeDefinitions: [ | |
{ | |
AttributeName: "key", | |
AttributeType: "S" | |
} | |
], | |
KeySchema: [ | |
{ | |
AttributeName: "key", | |
KeyType: "HASH" | |
} | |
], | |
ProvisionedThroughput: { | |
ReadCapacityUnits: 1, | |
WriteCapacityUnits: 1 | |
}, | |
TableName: "test" | |
}, function(err, data) { | |
if (err) { | |
throw err; | |
} | |
console.log("data:", data); | |
}); |
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
"use strict"; | |
var AWS = require("aws-sdk"); | |
AWS.config.update({ | |
region: process.env.AWS_DEFAULT_REGION || AWS.config.region || "us-east-1" | |
}); | |
var dynamodb = new AWS.DynamoDB(); | |
dynamodb.getItem({ | |
TableName: "test", | |
Key: { | |
key: { | |
S: "42" | |
} | |
}, | |
ConsistentRead: true | |
}, function(err, data) { | |
if (err) { | |
throw err; | |
} | |
console.log("data:", data); | |
try { | |
var result = JSON.parse(data.Item.result.S); | |
console.log("%s:", data.Item.key.S, result); | |
} catch (err) { | |
throw err; | |
} | |
}); |
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
"use strict"; | |
var AWS = require("aws-sdk"); | |
AWS.config.update({ | |
region: process.env.AWS_DEFAULT_REGION || AWS.config.region || "us-east-1" | |
}); | |
var dd = new AWS.DynamoDB(); | |
var result = { | |
task: "task name 2", | |
id: 321 | |
}; | |
dd.putItem({ | |
TableName: "test", | |
Item: { | |
key: { | |
S: "11" | |
}, | |
result: { | |
S: JSON.stringify(result) | |
} | |
} | |
}, function(err, data) { | |
if (err) { | |
throw err; | |
} | |
console.log("data:", data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment