From 87ed84f014b36f1fdddcb9f6d934b047911a2dc1 Mon Sep 17 00:00:00 2001 From: DivineGordon Date: Sat, 1 Jun 2024 10:28:09 +0000 Subject: [PATCH] added example code for getting dynamo tables from aws cdk stack --- readme.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/readme.md b/readme.md index 333debf..7a82552 100644 --- a/readme.md +++ b/readme.md @@ -157,6 +157,57 @@ module.exports = async () => { }; ``` +Or read table definitions from a CloudFormation template gotten directly from an [AWS CDK Stack](https://docs.aws.amazon.com/cdk/v2/guide/home.html): + +```js +const {Template} = require("aws-cdk-lib/assertions"); +const {stackWithTables} = require("path/to/stack"); + +const dynamodb = require("aws-cdk-lib/aws-dynamodb"); +const os = require("os"); +const path = require("path"); + +/** + * @type {import('@shelf/jest-dynamodb/lib').Config}')} + */ + +async function jestSetupForDynamoLocal() { + + const template = Template.fromStack(stackWithTables); + const dynamoDBTablesTemplate = template.findResources( + dynamodb.CfnGlobalTable.CFN_RESOURCE_TYPE_NAME, + ); + + const tables = Object.values(dynamoDBTablesTemplate).map(Resource => { + const {Properties = {}} = Resource; + + /** + * errors on dynamo-local + */ + if ("TimeToLiveSpecification" in Properties) { + delete Properties.TimeToLiveSpecification; + } + + if ("StreamSpecification" in Properties) { + Properties.StreamSpecification.StreamEnabled = false; + delete Properties.StreamSpecification.StreamViewType; + } + + return Properties; + }); + + return { + tables, + port: 8000, + installerConfig: { + installPath: + process.env.DYNAMO_DB_LOCAL_INSTALL_PATH || + path.join(os.homedir() , "dynamodb_local", "dynamodb_local_latest") , + }, + }; +} +``` + ### 3.1 Configure DynamoDB client (from aws-sdk v2) ```js