Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added example code for getting dynamo tables from AWS cdk stack #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down