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(util-dynamodb): [WIP] DocumentClient with basic put operation #2031

Closed
wants to merge 14 commits into from

Conversation

trivikr
Copy link
Member

@trivikr trivikr commented Feb 12, 2021

Issue

Attempt to fix: #1223

Description

[WIP] DocumentClient with basic put operation

Testing

Tested with the following code:

With DocumentClient

Code
const { DocumentClient } = require("../aws-sdk-js-v3/packages/util-dynamodb");
const {
  DynamoDB,
  waitForTableExists,
} = require("../aws-sdk-js-v3/clients/client-dynamodb");

(async () => {
  const region = "us-west-2";
  const client = new DynamoDB({ 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.createTable({ ...params, TableName });
  await waitForTableExists({ client }, { TableName });

  const docClient = new DocumentClient({ region });
  await docClient.put({
    TableName,
    Item: { id: "1", content: "inserted through await call" },
  });

  console.log(await client.getItem({ TableName, Key: { id: { S: "1" } } }));

  docClient.put(
    {
      TableName,
      Item: { id: "2", content: "inserted through callback" },
    },
    (err, data) => {
      console.log(err);
      client
        .getItem({ TableName, Key: { id: { S: "2" } } })
        .then((data) => console.log(data))
        .catch((err) => console.log(err));

      client.deleteTable({ TableName });
    }
  );
})();
Output
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: '3SRG017G8Q3074S2FIV8EIUVVFVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  Item: { content: { S: 'inserted through await call' }, id: { S: '1' } }
}
null
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'LFBQENN54KF5M8LGVBGUKSVR2JVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  Item: { content: { S: 'inserted through callback' }, id: { S: '2' } }
}

With DynamoDBClient

Code
const {
  PutItemDocumentCommand,
} = require("../aws-sdk-js-v3/packages/util-dynamodb");
const {
  DynamoDBClient,
  CreateTableCommand,
  DeleteTableCommand,
  GetItemCommand,
  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 });

  await client.send(
    new PutItemDocumentCommand({
      TableName,
      Item: { id: "1", content: "inserted through await call" },
    })
  );

  console.log(
    await client.send(
      new GetItemCommand({ TableName, Key: { id: { S: "1" } } })
    )
  );

  client.send(
    new PutItemDocumentCommand({
      TableName,
      Item: { id: "2", content: "inserted through callback" },
    }),
    (err, data) => {
      console.log(err);
      client
        .send(new GetItemCommand({ TableName, Key: { id: { S: "2" } } }))
        .then((data) => console.log(data))
        .catch((err) => console.log(err));

      client.send(new DeleteTableCommand({ TableName }));
    }
  );
})();
Output
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: '7IOO78ES6CQR744U79CITTJV2VVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  Item: { content: { S: 'inserted through await call' }, id: { S: '1' } }
}
null
{
  '$metadata': {
    httpStatusCode: 200,
    requestId: 'NHBFORD136J3493UTA7FOMSVLRVV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ConsumedCapacity: undefined,
  Item: { content: { S: 'inserted through callback' }, id: { S: '2' } }
}

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

@kyeotic
Copy link

kyeotic commented Feb 12, 2021

Thank you!!! This is what we need.

My only concern is the selection of DynamoDB instead of DynamoDBClient. Correct me if I am wrong (which seems likely, the docs confuse the hell out of me) but using DynamoDB will break tree-shaking and pull in all command types and methods into the output.

Since you are only using send() anyway you should be able to use DynamoDBClient, which wont pull in all the methods on DynamoDB, and wont restrict your ability to provide the methods you want.

* | Array | L |
* | Object | M |
* | Buffer, File, Blob, ArrayBuffer, DataView, and JavaScript typed arrays | B |
*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Set is also marshalled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, we have marshall/unmarshall support for as this code reuses existing utilities.
The documentation was copied from DocumentClient in v2, and will need re-edit while polishing.

@trivikr
Copy link
Member Author

trivikr commented Feb 12, 2021

My only concern is the selection of DynamoDB instead of DynamoDBClient. Correct me if I am wrong (which seems likely, the docs confuse the hell out of me) but using DynamoDB will break tree-shaking and pull in all command types and methods into the output.

Did you mean the usage in the test code? You can use DynamoDBClient instead of DynamoDB?
This experimental implementation is not modular though. We're not planning to publish modular implementation of Document Client as of now, as that would not be straightforward.

@kyeotic
Copy link

kyeotic commented Feb 12, 2021

@trivikr What do you mean by "test code"? This looks like the actual implementation to me.

@trivikr
Copy link
Member Author

trivikr commented Feb 12, 2021

What do you mean by "test code"? This looks like the actual implementation to me.

By test code I meant the code used under testing in this implementation (under PR description).

@aws-sdk-js-automation
Copy link

AWS CodeBuild CI Report

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

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

Copy link

@cadam11 cadam11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excited to have this power in v3 sdk - thanks, @trivikr !

@rati-dzidziguri
Copy link

I liked the philosophy behind V3 and modular architecture. Having this package kind of goes against it. If one has a Lambda that does not require any writing or put item operations in a microservice world, it will still include unnecessary code when using this implementation of the DocumentClient. While the philosophy behind V3 is fully aligned with a distributed application model, backporting things in this way might be a little bit step backward instead.

@trivikr
Copy link
Member Author

trivikr commented Feb 21, 2021

Superceded by #2062

@trivikr trivikr closed this Feb 21, 2021
@github-actions
Copy link

github-actions bot commented Mar 8, 2021

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 8, 2021
@trivikr trivikr deleted the dynamodb-documentclient-experiment branch March 15, 2021 04:02
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.

DynamoDB documentClient support
7 participants