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

feat(lib-dynamodb): [PoC] DynamoDB DocumentClient with get/put/delete operations #2062

Closed
wants to merge 28 commits into from

Conversation

trivikr
Copy link
Member

@trivikr trivikr commented Feb 21, 2021

Issue

PoC for fixing #1223

Description

DynamoDBDocumentClient with get/put/delete operations.

The modular DynamoDBDocumentClient can be created as follows:

  const client = new DynamoDBClient({});
  const ddbDocClient = DynamoDBDocumentClient.from(client);
  await ddbDocClient.send(
    new PutCommand({
      TableName,
      Item: { id: "1", content: "content from DynamoDBDocumentClient" },
    })
  );

The v2 backward-compatible DocumentClient can be created as follows:

  const client = new DynamoDBClient({});
  const ddbDoc = DynamoDBDocument.from(client);
  await ddbDoc.put({
    TableName,
    Item: { id: "2", content: "content from DynamoDBDocument" },
  });

Note: This is a PoC for the output of CodeGen. In the final PR, the operations are going to be CodeGen.

Testing

get/put/delete operations
const {
  DynamoDBDocument,
  DynamoDBDocumentClient,
  DeleteCommand,
  GetCommand,
  PutCommand,
} = require("../aws-sdk-js-v3/lib/lib-dynamodb");
const {
  DynamoDBClient,
  CreateTableCommand,
  DeleteTableCommand,
  waitForTableExists,
} = require("../aws-sdk-js-v3/clients/client-dynamodb");

(async () => {
  const region = "us-west-2";
  const client = new DynamoDBClient({ region });

  const TableName = `test-${Math.ceil(Math.random() * 10 ** 10)}`;

  const params = {
    TableName,
    AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
    KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
    BillingMode: "PAY_PER_REQUEST",
  };

  await client.send(new CreateTableCommand({ ...params }));
  await waitForTableExists({ client }, { TableName });

  const ddbDocClient = DynamoDBDocumentClient.from(client);
  await ddbDocClient.send(
    new PutCommand({
      TableName,
      Item: { id: "1", content: "content from DynamoDBDocumentClient" },
    })
  );
  console.log(
    await ddbDocClient.send(new GetCommand({ TableName, Key: { id: "1" } }))
  );
  await ddbDocClient.send(new DeleteCommand({ TableName, Key: { id: "1" } }));

  await client.send(new DeleteTableCommand({ TableName }));
})();
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'NNA8HRNS5U5TE6N0SPSKJJE4HVVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  Item: { content: 'content from DynamoDBDocumentClient', id: '1' }
}
batchGet/batchWrite operations
import {
  BatchGetCommand,
  BatchWriteCommand,
  DynamoDBDocumentClient,
} from "../aws-sdk-js-v3/lib/lib-dynamodb";
import { DynamoDBClient } from "../aws-sdk-js-v3/clients/client-dynamodb";

(async () => {
  const region = "us-west-2";
  const client = new DynamoDBClient({ region });

  const TableName = `test-${Math.ceil(Math.random() * 10 ** 10)}`;

  const params = {
    TableName,
    AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
    KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
    BillingMode: "PAY_PER_REQUEST",
  };

  await client.send(new CreateTableCommand({ ...params }));
  await waitForTableExists({ client }, { TableName });

  const translateConfig = {
    marshallOptions: { removeUndefinedValues: true },
  };
  const ddbDocClient = DynamoDBDocumentClient.from(
    client,
    translateConfig
  );

  console.log(
    await ddbDocClient.send(
      new BatchWriteCommand({
        RequestItems: {
          [TableName]: [
            {
              PutRequest: {
                Item: {
                  id: "1",
                  content: "content #1 from DynamoDBDocumentClient",
                  undefinedValue: undefined,
                },
              },
            },
            {
              PutRequest: {
                Item: {
                  id: "2",
                  content: "content #2 from DynamoDBDocumentClient",
                  undefinedValue: undefined,
                },
              },
            },
          ],
        },
      })
    )
  );

  console.log(
    await ddbDocClient.send(
      new BatchGetCommand({
        RequestItems: {
          [TableName]: {
            Keys: [{ id: "1" }, { id: "2" }],
          },
        },
      })
    )
  );

  console.log(
    await ddbDocClient.send(
      new BatchWriteCommand({
        RequestItems: {
          [TableName]: [
            { DeleteRequest: { Key: { id: "1" } } },
            { DeleteRequest: { Key: { id: "2" } } },
          ],
        },
      })
    )
  );

  await client.send(new DeleteTableCommand({ TableName }));
})();
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: '49RVPOKS2LCQNNF841GOU01QN7VV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  ItemCollectionMetrics: undefined,
  UnprocessedItems: {}
}
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: '210DH49P6Q9GAJ6N5GIB6VSGMNVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  Responses: { 'test-1763877349': [ [Object], [Object] ] },
  UnprocessedKeys: {},
  UnprocessedItems: {},
  'test-1763877349': [
    { content: 'content #1 from DynamoDBDocumentClient', id: '1' },
    { content: 'content #2 from DynamoDBDocumentClient', id: '2' }
  ]
}
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'NEOL1HV2DAEIV3ATDGMA1OSOENVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  ItemCollectionMetrics: undefined,
  UnprocessedItems: {}
}

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@trivikr trivikr marked this pull request as draft February 21, 2021 18:39
@trivikr trivikr changed the title feat(lib-dynamodb): add PutCommand which accepts JS Native types feat(lib-dynamodb): [PoC] DynamoDB DocumentClient with get/put/delete operations Feb 21, 2021
Base automatically changed from master to main February 22, 2021 19:46
@codecov-io
Copy link

codecov-io commented Feb 22, 2021

Codecov Report

❗ No coverage uploaded for pull request base (main@bb9a9a2). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2062   +/-   ##
=======================================
  Coverage        ?   79.45%           
=======================================
  Files           ?      366           
  Lines           ?    15324           
  Branches        ?     3310           
=======================================
  Hits            ?    12176           
  Misses          ?     3148           
  Partials        ?        0           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bb9a9a2...0640943. Read the comment docs.

@aws-sdk-js-automation
Copy link

AWS CodeBuild CI Report

  • CodeBuild project: sdk-staging-test
  • Commit ID: c88e21a
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@trivikr
Copy link
Member Author

trivikr commented Mar 2, 2021

Succeeded by #2097

@trivikr trivikr closed this Mar 2, 2021
@trivikr trivikr deleted the lib-dynamodb-design-example branch March 15, 2021 04:01
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants