diff --git a/readme.md b/readme.md index 2c8305f..ca27665 100644 --- a/readme.md +++ b/readme.md @@ -20,7 +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) +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 = { @@ -37,6 +39,23 @@ module.exports = { }; ``` +Or as an async function (particularly useful when resolving DynamoDB setup dynamically from `serverless.yml`): + +```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 + }; +}; +``` + ### 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())); }