aws-utils
Purpose
Simple wrapper around aws-sdk to make it easier to use.
Compatibility
/!\ This module use async/await syntax, this is why you must have node 7.6+.
Supported and tested : >= 12.x
Version | Supported | Tested |
---|---|---|
16.x | yes | yes |
14.x | yes | yes |
12.x | yes | yes |
BREAKING CHANGES since version 2.0
Now dynamo tools returns an object and not directly an array of items.
{
Items:[ { ....} , { ... } ],
LastEvaluatedKey: {....}
}
Installation
$ npm install @voodoo.io/aws-utils --save
Usage
Dynamo tools
Configure dynamo tools
const awsUtils = require('@voodoo.io/aws-utils').dynamo;
const aws = require('aws-sdk');
aws.config.update({region: 'eu-west-1'});
const dynamoCli = new aws.DynamoDB.DocumentClient();
const dynamoTools = new awsUtils(dynamoCli)
Query
const res = await dynamoTools.queryHashKey('myTable', 'key', 'value');
const res = await dynamoTools.query('myTable', { 'conditions': [{'key': 'value', 'operator': 'value', 'value': [] /** or ''**/}]})
Scan
const res = await dynamoTools.scan('myTable');
Put item
const res = await dynamoTools.putItem('myTable', {'key': 'value'});
Put items
const res = await dynamoTools.putItems('myTable', [{'key': 'value'}]);
Put transactional items
const res = await dynamoTools.putTransactionItems([{'key': 'value'}]);
Update item
await dynamoTools.update('myTable', {'key': {'primaryKey': 'value', 'sortKey': 'value'},'add': {['column1']: 'value'}, 'set': {'column2': 'value'}});
delete item
const res = await dynamoTools.deleteItem('myTable', {'key': 'value'});
queryHashKey(dynamoTable, hashKeyName, hashKeyValue, [exclusiveStartKey], [limit])
-
dynamoTable
: table's name -
hashKeyName
: hashkey's name -
hashKeyValue
: hashkey's value -
exclusiveStartKey
: (optional) start search at a specific key -
limit
: (optional) don't return more items than the limit
query(dynamoTable, customParams)
-
dynamoTable
: table's name -
customParams
: object with the conditions of the query
putItem(dynamoTable, item)
-
dynamoTable
: table's name -
item
: item to insert
putItems(dynamoTable, items)
-
dynamoTable
: table's name -
item
: an array of items to insert
scan(dynamoTable, [hashKeyName], [hashKeyValue], [exclusiveStartKey], [limit])
-
dynamoTable
: table's name -
hashKeyName
: (optional) hashkey's name -
hashKeyValue
: (optional) hashkey's value -
exclusiveStartKey
: (optional) start search at a specific key -
limit
: (optional) don't return more items than the limit
If no hashkey is provided it returns the full table.
updateItem(dynamoTable, params)
-
dynamoTable
: table's name -
params
: parameters with columns to update
deleteItem(dynamoTable, key)
-
dynamoTable
: table's name -
key
: hashkey to delete
Secret Manager tools
Configure secret manager tools
const awsUtils = require('@voodoo.io/aws-utils').secretManager;
const aws = require('aws-sdk');
aws.config.update({region: 'eu-west-1'});
const cli = new aws.SecretsManager({});
const secretManagerTools = new awsUtils(cli);
getSecretValue
const res = await secretManagerTools.getSecretValue('secret');
getSecretValue(secret)
-
secret
: secret's id
S3 tools
Configure S3 tools
const awsUtils = require('@voodoo.io/aws-utils').s3;
const aws = require('aws-sdk');
aws.config.update({region: 'eu-west-1'});
const cli = new aws.S3();
const s3Tools = new awsUtils(cli);
getObject
const res = await s3Tools.getObject('bucket', 'key');
getObject(bucket, key)
-
bucket
: bucket's name -
key
: path to the ressource (/path/file.json)
putJsonObject
const res = await s3Tools.putJsonObject('bucket', 'key', {"key": "value"});
putJsonObject(bucket, key, item)
-
bucket
: bucket's name -
key
: path to the ressource (/path/file.json) -
item
: json object to save on S3
emptyS3Directory
const res = await s3Tools.emptyS3Directory('bucket', 'dir/');
emptyS3Directory(bucket, dir)
-
bucket
: bucket's name -
dir
: directory to remove
Test
$ npm test
Coverage report can be found in coverage/.