Skip to content

Commit 6416e0b

Browse files
Kyle Smithknksmith57
authored andcommitted
feat: support custom AWS.Config instead of requiring individual parameters
resolves #133
1 parent e7139c9 commit 6416e0b

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

index.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ module.exports = Dyno;
66

77
/**
88
* Creates a dyno client. You must provide a table name and the region where the
9-
* table resides. Where applicable, dyno will use this table as the default in
10-
* your requests. However you can override this when constructing any individual
11-
* request.
9+
* table resides. Region can be supplied with either the `region` option, or as
10+
* a property on the `awsConfig` option. Where applicable, dyno will use this
11+
* table as the default in your requests. However you can override this when
12+
* constructing any individual request.
1213
*
1314
* If you do not explicitly pass credentials when creating a dyno client, the
1415
* 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)
1516
* for details.
1617
*
1718
* @param {object} options - configuration parameters
1819
* @param {string} options.table - the name of the table to interact with by default
19-
* @param {string} options.region - the region in which the default table resides
20+
* @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)
21+
* @param {string} [options.region] - the region in which the default table resides
2022
* @param {string} [options.endpoint] - the dynamodb endpoint url
2123
* @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).
2224
* @param {string} [options.accessKeyId] - credentials for the client to utilize
@@ -60,20 +62,25 @@ function Dyno(options) {
6062
*/
6163

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

6566
var config = {
66-
region: options.region,
67-
endpoint: options.endpoint,
68-
params: { TableName: options.table }, // Sets `TableName` in every request
69-
httpOptions: options.httpOptions || { timeout: 5000 }, // Default appears to be 2 min
70-
accessKeyId: options.accessKeyId,
71-
secretAccessKey: options.secretAccessKey,
72-
sessionToken: options.sessionToken,
73-
logger: options.logger,
74-
maxRetries: options.maxRetries
67+
params: {
68+
TableName: options.table // Sets `TableName` in every request
69+
},
70+
httpOptions: {
71+
timeout: 5000 // Default appears to be 2 min
72+
}
7573
};
7674

75+
if (!options.awsConfig) {
76+
if (!options.region) throw new Error('region is required');
77+
Object.assign(config, options);
78+
} else {
79+
config = options.awsConfig;
80+
config.params = config.params || {};
81+
config.params.TableName = options.table;
82+
}
83+
7784
var client = new AWS.DynamoDB(config);
7885
var docClient = new AWS.DynamoDB.DocumentClient({ service: client });
7986
var tableFreeClient = new AWS.DynamoDB(_(config).omit('params')); // no TableName in batch requests

test/index.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var AWS = require('aws-sdk');
12
var test = require('tape');
23
var Dyno = require('..');
34

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

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

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

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

165+
var awsConfig = new AWS.Config();
166+
var awsDyno = Dyno({
167+
awsConfig: awsConfig,
168+
table: 'my-table'
169+
});
170+
assert.strictEqual(awsDyno.config, awsConfig, 'uses awsConfig');
171+
161172
var multi = Dyno.multi(
162173
{ table: 'read-table', region: 'us-east-1' },
163174
{ table: 'write-table', region: 'us-east-1' }

0 commit comments

Comments
 (0)