From 089c2f147b5200a9a28785ac5d9c1c6c1ad35d7f Mon Sep 17 00:00:00 2001 From: Roni Frantchi Date: Tue, 25 Jun 2019 15:49:40 +0300 Subject: [PATCH 1/2] feat: add promise setup support --- readme.md | 22 +++++++++++++++++++++- setup.js | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 2c8305f..9e6c3b3 100644 --- a/readme.md +++ b/readme.md @@ -20,7 +20,8 @@ module.exports = { ### 2. Create `jest-dynamodb-config.js` -See [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property) +See [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property) +You can set the table as an object: ```js module.exports = { @@ -37,6 +38,25 @@ module.exports = { }; ``` +Or a function (particularly useful when resolving DynamoDB setup from Serverless or other `async` method): + +```js +module.exports = async () => { + await serverless.init(); + const service = await serverless.variables.populateService(); + const tables = service.resources.Resources.filter(r => r.Type === 'AWS::DynamoDB::Table').map( + r => r.Properties + ); + + return { + tables, + port: 8000 + }; +}; +``` + +This is prticularly useful + ### 3. Configure DynamoDB client ```js diff --git a/setup.js b/setup.js index f1e6af4..550e80d 100644 --- a/setup.js +++ b/setup.js @@ -25,7 +25,8 @@ module.exports = async function() { }; async function createTables() { - const {tables} = require(resolve(cwd(), 'jest-dynamodb-config.js')); + const config = require(resolve(cwd(), 'jest-dynamodb-config.js')); + const {tables} = typeof config === 'function' ? await config() : config; return Promise.all(tables.map(table => dynamoDB.createTable(table).promise())); } From 6bd55c67028c49525014955f422d18a2086c1438 Mon Sep 17 00:00:00 2001 From: Vlad Holubiev Date: Tue, 25 Jun 2019 15:57:16 +0300 Subject: [PATCH 2/2] docs: clarify wording in readme --- readme.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 9e6c3b3..ca27665 100644 --- a/readme.md +++ b/readme.md @@ -20,8 +20,9 @@ module.exports = { ### 2. Create `jest-dynamodb-config.js` -See [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property) -You can set the table as an object: +See [Create Table API](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property). + +You can set up tables as an object: ```js module.exports = { @@ -38,7 +39,7 @@ module.exports = { }; ``` -Or a function (particularly useful when resolving DynamoDB setup from Serverless or other `async` method): +Or as an async function (particularly useful when resolving DynamoDB setup dynamically from `serverless.yml`): ```js module.exports = async () => { @@ -55,8 +56,6 @@ module.exports = async () => { }; ``` -This is prticularly useful - ### 3. Configure DynamoDB client ```js