From 6416e0bf24e304a2aff2521acc72b25c8bfc8d78 Mon Sep 17 00:00:00 2001 From: Kyle Smith Date: Wed, 18 Jan 2017 16:38:55 -0600 Subject: [PATCH] feat: support custom AWS.Config instead of requiring individual parameters resolves #133 --- index.js | 35 +++++++++++++++++++++-------------- test/index.test.js | 13 ++++++++++++- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 5af85ac..4ac39d0 100644 --- a/index.js +++ b/index.js @@ -6,9 +6,10 @@ 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) @@ -16,7 +17,8 @@ module.exports = Dyno; * * @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 @@ -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 diff --git a/test/index.test.js b/test/index.test.js index e4a559a..56a7942 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,3 +1,4 @@ +var AWS = require('aws-sdk'); var test = require('tape'); var Dyno = require('..'); @@ -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(); }); @@ -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' }