npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stackspot/cdk-component-dynamodb-typescript

v0.3.3

Published

A CDK construct for Typescript that can be used to create DynamoDB Table, give permissions for lambdas functions and generate commons commands.

Downloads

2

Readme

StackSpot DynamoDB Table construct library

A CDK construct for Typescript that can be used to create DynamoDB Table, give permissions for lambdas functions and generate commons commands.

The class StackSpotDynamoDBTable in the construct extends the original CDK DynamoDB Table, we're extending it to facilitate the integration between StackSpot OpenAPI contract first API Gateway + Lambda construct library.

Prerequisites

Optional (recommended) development tools

How to build library package

  • To build the library package clone the repository and run npm scripts
git clone [email protected]:Stack-Spot/crystal-cdk-dynamodb.git
cd crystal-cdk-dynamodb
npm install
npm run build
npm run package
  • The library will be built and package dynamodb@<version>.jsii.tgz will be generated in dist/js folder

Quick start

The example bellow will create a simple CDK App using TypeScript and will create a DynamoDB Table.

1. Init a CDK app using the cli

mkdir hello-world-app
cd hello-world-app
cdk init --language=typescript

2. Add dependencies

npm install @aws-cdk/aws-dynamodb @stackspot/cdk-component-dynamodb-typescript

3. Using the construct

Open the file lib/hello-world-app-stack.ts that was generated by the CDK CLI like bellow:

import * as cdk from '@aws-cdk/core';
import { StackSpotDynamoDBTable } from '@stackspot/cdk-component-dynamodb-typescript';
import { AttributeType } from '@aws-cdk/aws-dynamodb';

export class HelloWorldAppStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new StackSpotDynamoDBTable(this, 'HelloWorldTable', {
      tableConfiguration: {
        partitionKey: { name: 'pk', type: AttributeType.STRING },
      },
    });
  }
}

4. Execute CDK bootstrap to prepare stack

cdk bootstrap --profile <your-aws-profile>

If you have permissions problems related to S3 public access block configuration permissions on bootstrap you could add the option --public-access-block-configuration false to the bootstrap command as shown below:

cdk bootstrap --profile <your-aws-profile> --public-access-block-configuration false

5. Creating GSI

To create any GSI, add gsis property array:

import * as cdk from '@aws-cdk/core';
import { StackSpotDynamoDBTable } from '@stackspot/cdk-component-dynamodb-typescript';
import { AttributeType } from '@aws-cdk/aws-dynamodb';

export class HelloWorldAppStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new StackSpotDynamoDBTable(this, 'HelloWorldTable', {
      tableConfiguration: {
        partitionKey: { name: 'pk', type: AttributeType.STRING },
        sortKey: { name: 'sk', type: AttributeType.STRING },
      },
      gsis: [
        {
          indexName: 'my-gsi-index',
          partitionKey: { name: 'sk', type: AttributeType.STRING },
        },
      ],
    });
  }
}

6. Build and deploy the project to create the table

Run npm run build and cdk deploy.

Check in AWS Console the DynamoDB table created.

Integrating with StackSpot OpenAPI Typescript Lambda construct

The best part of this construct is to facilitate the integration between the table and lambdas created by StackSpot OpenAPI contract first API Gateway + Lambda construct library.

Check the StackSpot OpenAPI contract first API Gateway + Lambda construct library docs to learn more.

Then you can integrate the constructs using lambdasConfiguration properties:

const lambdas = new StackSpotOpenApiServices(this, 'HelloWorldApi', {
  specPath: 'spec/hello-world.yaml',
});

new StackSpotDynamoDBTable(this, 'HelloWorldTable', {
  tableConfiguration: {
    partitionKey: { name: 'pk', type: AttributeType.STRING },
  },
  lambdasConfiguration: {
    lambdasById: lambdas.backendLambdas,
    tableNameEnvironmentVariable: 'MY_DYNAMO_TABLE_NAME',
  },
});

Now all the lambdas will have permissions to read and write to the DynamoDB table.

Set readOnly lambda permission

To set readOnly permission to your lambda use readOnly property with the operation-id name defined in your OpenAPI Spec:

const lambdas = new StackSpotOpenApiServices(this, 'HelloWorldApi', {
  specPath: 'spec/hello-world.yaml',
});

new StackSpotDynamoDBTable(this, 'HelloWorldTable', {
  tableConfiguration: {
    partitionKey: { name: 'pk', type: AttributeType.STRING },
  },
  lambdasConfiguration: {
    lambdasById: lambdas.backendLambdas,
    tableNameEnvironmentVariable: 'MY_DYNAMO_TABLE_NAME',
    readOnly: ['list-hello', 'get-hello'],
  },
});

Generate DynamoDB commons commands

Some commons commands (insert, queries, updates and deletes) are commons and required for all sort off applications, so this construct is generating some helper functions to help you with this task.

To let the construct generate the code, set the StackSpotDynamoDBTable like bellow:

new StackSpotDynamoDBTable(this, 'HelloWorldTable', {
  // other settings...
  generateRepository: true,
});

If you'd like to configure some options, add the repositoryGenerationConfiguration object, like:

new StackSpotDynamoDBTable(this, 'HelloWorldTable', {
  // other settings...
  generateRepository: true,
  repositoryGenerationConfiguration: {
    tracing: false,
    sourceDir: 'src',
  },
});
  • tracing - enable x-ray tracing - default: true
  • sourceDir - change the default source foldes: default: src

Useful commands

  • npm run build compile typescript to jsii
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • npm run package package library using jsii
  • npm run coverage run tests with coverage reports

References

CDK