@stacksavings/dynamodb
v1.2.4
Published
DynamoDB Client
Downloads
19
Keywords
Readme
Stacksavings - DynamoDB Client
Hemos creado un paquete "@stacksavings/dynamodb" para poder usar los metodos de interacción con la bade de datos DynamoDB.
Table of contents
Getting Started
Prerrequisitos
Cree un archivo de credenciales en ~/.aws/credentials en Mac/Linux o bien en C:\Users\USERNAME.aws\credentials en Windows.
[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key
region = your_region
Instalando
Para poder instalar el cliente solo basta con ejecutar el comando:
npm i @stacksavings/dynamodb
Uso
Para empezar a usarlo solo debe de incluirlo en tu código asi:
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB()
Tambien se puede configurar el endpoint de la Base de Datos para que apunte a una instancia local o remota. Por defecto el endpoint esta local por el puerto 8000.
[Instancia local]
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB({endpoint: 'http://localhost:8000'})
[Instancia remota]
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB({endpoint: 'https://ec2.ap-southeast-1.amazonaws.com'})
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -help
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB({region: 'us-east-2'})
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB({
endpoint: 'https://ec2.ap-southeast-1.amazonaws.com',
region: 'us-east-2'
})
Métodos
- createTable:
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB()
const logger = require('@stacksavings/utils').log()
const parameters = {
TableName: "Test",
KeySchema: [
{ AttributeName: "id", "KeyType": "HASH" }
],
AttributeDefinitions: [
{ AttributeName: "id", "AttributeType": "S" }
],
ProvisionedThroughput: {
"ReadCapacityUnits": 100,
"WriteCapacityUnits": 100
}
}
const promise = dynamoDB.createTable(parameters)
promise.then(data => {
logger.info(data)
}, err => {
logger.error(err)
})
Salida:
{
code: true,
message: "Table <TABLE NAME> created!",
data: {
"TableDescription":{
"AttributeDefinitions":[
{
"AttributeName":"id",
"AttributeType":"S"
}
],
"TableName":"Test",
"KeySchema":[
{
"AttributeName":"id",
"KeyType":"HASH"
}
],
"TableStatus":"CREATING",
"CreationDateTime":"2018-01-15T00:47:17.262Z",
"ProvisionedThroughput":{
"NumberOfDecreasesToday":0,
"ReadCapacityUnits":100,
"WriteCapacityUnits":100
},
"TableSizeBytes":0,
"ItemCount":0,
"TableArn":"arn:aws:dynamodb:us-east-1:281171835363:table/Test",
"TableId":"21f71a7a-dbc6-486a-b1ab-194f5944387c"
}
}
}
- insertItems:
const uuid = require('uuid')
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB()
const logger = require('@stacksavings/utils').log()
var timeStamp = String(Math.floor(Date.now() / 1000));
const parameters = {
TableName: "Test",
Item: {
id: uuid.v1(),
currencyPair: "BTC_ETH",
timestamp: timeStamp
}
}
const promise = dynamoDB.insertItems(parameters)
promise.then(data => {
logger.info(data)
}, err => {
logger.error(err)
})
Salida:
{
code: true,
message: "Insert success!"
}
- insertItemsBath:
const uuid = require('uuid')
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB()
const logger = require('@stacksavings/utils').log()
var timeStamp = String(Math.floor(Date.now() / 1000));
const parameters = {
RequestItems: {
"Test": [
{
PutRequest: {
Item: {
id: uuid.v1(),
currencyPair: "BTC_ETH",
timestamp: timeStamp
}
}
},
{
PutRequest: {
Item: {
id: uuid.v1(),
currencyPair: "BTC_ETH2",
timestamp: timeStamp
}
}
},
...
]
}
}
const promise = dynamoDB.insertItemsBath(parameters)
promise.then(data => {
logger.info(data)
}, err => {
logger.error(err)
})
Salida:
{
code: true,
message: "Insert success!"
}
- scanItems:
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB()
const logger = require('@stacksavings/utils').log()
const parameters = {
TableName: 'Test',
AttributesToGet: [ "currencyPair", "id", ... ]
}
const promise = dynamoDB.scanItems(parameters)
promise.then(data => {
logger.info(data)
}, err => {
logger.error(err)
})
Salida:
{
code: true,
message: "Scan done!",
Items: <Field data>
}
- getItems:
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB()
const logger = require('@stacksavings/utils').log()
const parameters = {
TableName: "Test",
ProjectionExpression: "currencyPair",
ExpressionAttributeNames:{
"#id": "id"
},
ExpressionAttributeValues: {
':id': '1cedd7a0-fa26-11e7-81e3-23cdc36fe8fb'
},
KeyConditionExpression: '#id = :id'
}
const promise = dynamoDB.getItems(parameters)
promise.then(data => {
logger.info(data)
}, err => {
logger.error(err)
})
Salida:
{
code: true,
message: "Query done!",
Items: <Field data>
}
- deleteTable:
var dynamoDB = require('@stacksavings/dynamodb')
dynamoDB = new dynamoDB()
const logger = require('@stacksavings/utils').log()
const parameters = {
TableName: "Test"
}
const promise = dynamoDB.deleteTable(parameters)
promise.then(data => {
logger.info(data)
}, err => {
logger.error(err)
})
Salida:
{
code: true,
message: "Table <TABLE NAME> deleted!",
data: {
"TableDescription":{
"TableName":"Test",
"TableStatus":"DELETING",
"ProvisionedThroughput":{
"NumberOfDecreasesToday":0,
"ReadCapacityUnits":100,
"WriteCapacityUnits":100
},
"TableSizeBytes":0,
"ItemCount":0,
"TableArn":"arn:aws:dynamodb:us-east-1:281171835363:table/Test",
"TableId":"21f71a7a-dbc6-486a-b1ab-194f5944387c"
}
}
}
Creators
Stacksavings
- https://site.stacksavings.com
David Molina
- https://dmolina101.github.io
Hendrix Roa
- Site hendrixroa