DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?

Trending

  • Fraud Detection Using Artificial Intelligence and Machine Learning
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Debugging With Confidence in the Age of Observability-First Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. Create DynamoDB Tables with Node.js

Create DynamoDB Tables with Node.js

In this post, we look at how to create tables in DynamoDB using Node.js.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Jun. 26, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
16.5K Views

Join the DZone community and get the full member experience.

Join For Free

Before getting started, we need to have local DynamoDB installed since we want to avoid any costs for DynamoDB usage. There was a previous post on local DynamoDB.

If you use Docker, you can find a local DynamoDB image, or you can create one on you own as described here.

Using local DynamoDB and Node.js is extremely handy for debugging. Local DynamoDB provides us with a web user interface on http://localhost:8000/shell. The local DynamoDB shell is a JavaScript shell, therefore the actions for Node.js can be issued straight to the DynamoDB shell.

The actions would be the same as described in the corresponding Java tutorial.

The first step is to create a table with a hash key. In this case, the email of the user would be the hash key.

var createUsers = function(callback) {

    var dynamodb = new AWS.DynamoDB();

    var params = {
        TableName : "Users",
        KeySchema: [       
        { AttributeName: "email", KeyType: "HASH"}
    ],
    AttributeDefinitions: [       
        { AttributeName: "email", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 5, 
        WriteCapacityUnits: 5
   }
};

dynamodb.createTable(params, callback);
};


The next table will be called Logins. Logins should keep track each time the user logged in. To do so apart from using a hash key, we will also use a range key for the date it occurred.

var createLogins = function(callback) {

    var dynamodb = new AWS.DynamoDB();

    var params = {
        TableName : "Logins",
        KeySchema: [       
            { AttributeName: "email", KeyType: "HASH"},
            { AttributeName: "timestamp", KeyType: "RANGE"}
            ],
        AttributeDefinitions: [       
            { AttributeName: "email", AttributeType: "S" },
            { AttributeName: "timestamp", AttributeType: "N" }
            ],
        ProvisionedThroughput: {       
            ReadCapacityUnits: 5, 
            WriteCapacityUnits: 5
           }
        };
dynamodb.createTable(params, callback);
};


The next table is Supervisors. The hash key of Supervisor would be his name. A supervisor will work for a company. The company will be our global secondary index. Since the companies own more than one factories the field factory would be the range key.

var createSupervisors = function(callback) {

    var dynamodb = new AWS.DynamoDB();

    var params = {
        TableName : "Supervisors",
        KeySchema: [       
            { AttributeName: "name", KeyType: "HASH"}
            ],
        AttributeDefinitions: [       
            { AttributeName: "name", AttributeType: "S" },
            { AttributeName: "company", AttributeType: "S" },
            { AttributeName: "factory", AttributeType: "S" }    
        ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 5, 
        WriteCapacityUnits: 5
        },
    GlobalSecondaryIndexes: [{
        IndexName: "FactoryIndex",
        KeySchema: [
            {
                AttributeName: "company",
                KeyType: "HASH"
                },
            {
                AttributeName: "factory",
                KeyType: "RANGE"
            }
        ],
        Projection: {
            ProjectionType: "ALL"
            },
        ProvisionedThroughput: {
            ReadCapacityUnits: 1,
            WriteCapacityUnits: 1
            }
        }]
    };
dynamodb.createTable(params, callback);
};


The next table would be the table Companies. The hash key would be the parent company and the range key the subsidiary company. Each company has a CEO. The CEO would be the range key for the local secondary index.

var createCompanies = function(callback) {

    var dynamodb = new AWS.DynamoDB();

    var params = {
        TableName : "Companies",
        KeySchema: [       
            { AttributeName: "name", KeyType: "HASH"},
            { AttributeName: "subsidiary", KeyType: "RANGE"}
            ],
        AttributeDefinitions: [       
            { AttributeName: "name", AttributeType: "S" },
            { AttributeName: "subsidiary", AttributeType: "S" },
            { AttributeName: "ceo", AttributeType: "S" }    
            ],
        ProvisionedThroughput: {       
            ReadCapacityUnits: 5, 
            WriteCapacityUnits: 5
           },
        LocalSecondaryIndexes: [{
            IndexName: "CeoIndex",
            KeySchema: [
                {
                    AttributeName: "name",
                    KeyType: "HASH"
                    },
                {
                    AttributeName: "ceo",
                    KeyType: "RANGE"
                    }
                ],
            Projection: {
            ProjectionType: "ALL"
            }
        }]
    };
dynamodb.createTable(params, callback);
};

You can find the source code on GitHub.

Database

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!