Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom AWS.Config instance via awsConfig option #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ module.exports = Dyno;

/**
* Creates a dyno client. You must provide a table name and the region where the
* table resides. Where applicable, dyno will use this table as the default in
* your requests. However you can override this when constructing any individual
* request.
* table resides. Region can be supplied with either the `region` option, or as
* a property on the `awsConfig` option. Where applicable, dyno will use this
* table as the default in your requests. However you can override this when
* constructing any individual request.
*
* If you do not explicitly pass credentials when creating a dyno client, the
* aws-sdk will look for credentials in a variety of places. See [the configuration guide](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html)
* for details.
*
* @param {object} options - configuration parameters
* @param {string} options.table - the name of the table to interact with by default
* @param {string} options.region - the region in which the default table resides
* @param {object} [options.awsConfig] - an instance of AWS.Config to use in lieu of setting individual options. See [AWS.Config docs for details](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html)
* @param {string} [options.region] - the region in which the default table resides
* @param {string} [options.endpoint] - the dynamodb endpoint url
* @param {object} [options.httpOptions] - httpOptions to provide the aws-sdk client. See [constructor docs for details](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#constructor-property).
* @param {string} [options.accessKeyId] - credentials for the client to utilize
Expand Down Expand Up @@ -60,20 +62,25 @@ function Dyno(options) {
*/

if (!options.table) throw new Error('table is required'); // Demand table be specified
if (!options.region) throw new Error('region is required');

var config = {
region: options.region,
endpoint: options.endpoint,
params: { TableName: options.table }, // Sets `TableName` in every request
httpOptions: options.httpOptions || { timeout: 5000 }, // Default appears to be 2 min
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey,
sessionToken: options.sessionToken,
logger: options.logger,
maxRetries: options.maxRetries
params: {
TableName: options.table // Sets `TableName` in every request
},
httpOptions: {
timeout: 5000 // Default appears to be 2 min
}
};

if (!options.awsConfig) {
if (!options.region) throw new Error('region is required');
Object.assign(config, options);
} else {
config = options.awsConfig;
config.params = config.params || {};
config.params.TableName = options.table;
}

var client = new AWS.DynamoDB(config);
var docClient = new AWS.DynamoDB.DocumentClient({ service: client });
var tableFreeClient = new AWS.DynamoDB(_(config).omit('params')); // no TableName in batch requests
Expand Down
13 changes: 12 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var AWS = require('aws-sdk');
var test = require('tape');
var Dyno = require('..');

Expand All @@ -8,8 +9,11 @@ test('[index] invalid config', function(assert) {

assert.throws(function() {
Dyno({ table: 'my-table' });
}, /region is required/, 'rejects config without region');
}, /region is required/, 'rejects config without region when awsConfig not set');

assert.doesNotThrow(function() {
Dyno({ table: 'my-table', awsConfig: new AWS.Config() });
}, 'accepts config without region when awsConfig is set');
assert.end();
});

Expand Down Expand Up @@ -158,6 +162,13 @@ test('[index] configuration', function(assert) {
assert.deepEqual(dyno.config.logger, config.logger, 'sets logger');
assert.equal(dyno.config.maxRetries, config.maxRetries, 'sets maxRetries');

var awsConfig = new AWS.Config();
var awsDyno = Dyno({
awsConfig: awsConfig,
table: 'my-table'
});
assert.strictEqual(awsDyno.config, awsConfig, 'uses awsConfig');

var multi = Dyno.multi(
{ table: 'read-table', region: 'us-east-1' },
{ table: 'write-table', region: 'us-east-1' }
Expand Down