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: DynamoDB DocumentClient #55

Closed
wants to merge 67 commits into from
Closed

Conversation

trivikr
Copy link
Owner

@trivikr trivikr commented Mar 2, 2021

Issue

The cleaned PR will eventually fix aws#1223 upstream

Description

Add DynamoDB DocumentClient in AWS SDK for JavaScript v3

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" },
    })
  );

[WIP] 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" },
  });

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 added 30 commits March 2, 2021 18:17
Reason: bunch of issues with protocol tests
@trivikr trivikr force-pushed the codegen-documentclient-v2 branch from f17f42b to 37aa21c Compare March 2, 2021 18:17
@trivikr trivikr force-pushed the codegen-documentclient-v2 branch from 0bb0362 to 0c776b7 Compare March 3, 2021 02:05
@trivikr
Copy link
Owner Author

trivikr commented Mar 15, 2021

The final PR was posted upstream at aws#2097

@trivikr trivikr closed this Mar 15, 2021
@trivikr trivikr deleted the codegen-documentclient-v2 branch March 15, 2021 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DynamoDB documentClient support
1 participant