{"id":13512561,"url":"https://github.com/dabit3/dynamodb-documentclient-cheat-sheet","last_synced_at":"2025-11-17T15:22:19.664Z","repository":{"id":36679601,"uuid":"214827848","full_name":"dabit3/dynamodb-documentclient-cheat-sheet","owner":"dabit3","description":"DynamoDB JavaScript DocumentClient cheat sheet","archived":false,"fork":false,"pushed_at":"2021-12-24T19:38:19.000Z","size":32,"stargazers_count":452,"open_issues_count":1,"forks_count":56,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-10-09T09:58:58.132Z","etag":null,"topics":["aws","dynamodb","dynamodb-document-client","javascript"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dabit3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-13T13:45:27.000Z","updated_at":"2025-07-31T17:00:29.000Z","dependencies_parsed_at":"2022-07-15T20:17:19.586Z","dependency_job_id":null,"html_url":"https://github.com/dabit3/dynamodb-documentclient-cheat-sheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dabit3/dynamodb-documentclient-cheat-sheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fdynamodb-documentclient-cheat-sheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fdynamodb-documentclient-cheat-sheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fdynamodb-documentclient-cheat-sheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fdynamodb-documentclient-cheat-sheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dabit3","download_url":"https://codeload.github.com/dabit3/dynamodb-documentclient-cheat-sheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fdynamodb-documentclient-cheat-sheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":284908172,"owners_count":27082975,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-17T02:00:06.431Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","dynamodb","dynamodb-document-client","javascript"],"created_at":"2024-08-01T04:00:20.820Z","updated_at":"2025-11-17T15:22:19.648Z","avatar_url":"https://github.com/dabit3.png","language":null,"readme":"## DynamoDB JavaScript DocumentClient cheat sheet\n\nThe DynamoDB Document Client is the easiest and most preferred way to interact with a DynamoDB database from a Nodejs or JavaScript application.\n\nThis cheat sheet will help you get up and running quickly building applications with DynamoDB in a Nodejs or JavaScript environment.\n\nThis is meant to be a concise version of the full documentation located [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) to help you get productive as quickly as possible.\n\nFor more great DynamoDB resources, check out the [DynamoDB Guide](https://www.dynamodbguide.com/).\n\n## Getting started\n\n1. Make sure you have the `aws-sdk` JavaScript SDK installed or available in the environment. If you are using AWS Lambda, this should already be available.\n\n```sh\nnpm install aws-sdk\n```\n\n2. Import the SDK and create a new DynamoDB document client.\n\n```javascript\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n```\n\nThe `AWS.DynamoDB.DocumentClient()` constructor takes an optional hash of options. For instance, if you are wanting to set the location to a different region than the main AWS configuration, you could pass it in like this:\n\n```javascript\nconst docClient = new AWS.DynamoDB.DocumentClient({ region: 'us-east-2' })\n```\n\nCheck out the documentation for those options [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#constructor-property).\n\n## Database operations\n\nThe main things you will be doing are interacting with the database in one of the following ways:\n\n[put](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#put---creating-a-new-item--replacing-an-old-item-with-a-new-item) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property) - Creates a new item, or replaces an old item with a new item by delegating to AWS.DynamoDB.putItem().  \n[scan](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#scan---scanning-and-returning-all-of-the-items-in-the-database) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property) - Returns one or more items and item attributes by accessing every item in a table or a secondary index (limit of 1 MB of data).  \n[get](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#get---getting-a-single-item-by-primary-key) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property) - Returns a single item given the primary key of that item  \n[query](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#query---access-items-from-a-table-by-primary-key-or-a-secondary-index--gsi) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property) - Returns one or more items and item attributes by accessing every item in a table or a secondary index (maximum of 1 MB of data).  \n[delete](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#delete---delete-a-single-item) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#delete-property) - Deletes a single item in a table by primary key by delegating to AWS.DynamoDB.deleteItem().  \n[update](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#update---update-a-single-item) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#update-property) - Edits an existing item's attributes, or adds a new item to the table if it does not already exist by delegating to AWS.DynamoDB.updateItem().  \n[batchWrite](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#batchwrite---seed-a-table-with-data) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#batchWrite-property) - Puts or deletes multiple items in one or more tables by delegating to AWS.DynamoDB.batchWriteItem().\n\nThere are also other methods:\n\n[batchGet](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#batchGet-property) - Returns the attributes of one or more items from one or more tables by delegating to AWS.DynamoDB.batchGetItem().  \n[createSet](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#createSet-property) - Creates a set of elements inferring the type of set from the type of the first element.  \n[transactGet](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#transactGet-property) - Atomically retrieves multiple items from one or more tables (but not from indexes) in a single account and region.  \n[transactWrite](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#transactWrite-property) - Synchronous write operation that groups up to 10 action requests.\n\n## Usage\n\n### Put - Creating a new item / replacing an old item with a new item\n\n[full docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property)\n\n```javascript\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n\nvar params = {\n  TableName: 'ProductTable',\n  Item: {\n    id: '001',\n    price: 100.0,\n    inStock: true,\n    name: 'Yeezys',\n    sizes: [8, 8.5, 9, 10, 11, 12, 13],\n  },\n}\n\ndocClient.put(params, function (err, data) {\n  if (err) console.log(err)\n  else console.log(data)\n})\n\n// async function abstraction\nasync function createItem(itemData) {\n  var params = {\n    TableName: 'ProductTable',\n    Item: itemData,\n  }\n  try {\n    await docClient.put(params).promise()\n  } catch (err) {\n    return err\n  }\n}\n\n// usage\nexports.handler = async (event, context) =\u003e {\n  try {\n    const { data } = event.body\n    await createItem(data)\n    return { body: 'successfully created item' }\n  } catch (err) {\n    return { error: err }\n  }\n}\n```\n\n### scan - scanning and returning all of the items in the database\n\n[full docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property)\n\n```javascript\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n\nvar params = {\n  TableName: 'ProductTable',\n  FilterExpression: '#shoename = :shoename', // optional\n  ExpressionAttributeValues: { ':shoename': 'yeezys' }, // optional\n  ExpressionAttributeNames: { '#shoename': 'name' }, // optional\n}\n\ndocClient.scan(params, function (err, data) {\n  if (err) console.log(err)\n  else console.log(data)\n})\n\n// async function abstraction\nasync function listItems() {\n  var params = {\n    TableName: 'ProductTable',\n  }\n  try {\n    const data = await docClient.scan(params).promise()\n    return data\n  } catch (err) {\n    return err\n  }\n}\n\n// usage\nexports.handler = async (event, context) =\u003e {\n  try {\n    const data = await listItems()\n    return { body: JSON.stringify(data) }\n  } catch (err) {\n    return { error: err }\n  }\n}\n```\n\n### get - getting a single item by primary key\n\n[full docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property)\n\n```javascript\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n\nvar params = {\n  TableName: 'ProductTable',\n  Key: {\n    HashKey: 'hashkey',\n  },\n}\n\ndocClient.get(params, function (err, data) {\n  if (err) console.log(err)\n  else console.log(data)\n})\n\n// async function abstraction\nasync function getItem(id) {\n  var params = {\n    TableName: 'ProductTable',\n    Key: { id },\n  }\n  try {\n    const data = await docClient.get(params).promise()\n    return data\n  } catch (err) {\n    return err\n  }\n}\n\n// usage\nexports.handler = async (event, context) =\u003e {\n  try {\n    const data = await getItem(event.item.id)\n    return { body: JSON.stringify(data) }\n  } catch (err) {\n    return { error: err }\n  }\n}\n```\n\n### query - Access items from a table by primary key or a secondary index / GSI.\n\n[full docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property)\n\n```javascript\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n\nvar params = {\n  TableName: 'ProductTable',\n  IndexName: 'type-index',\n  KeyConditionExpression: '#typename = :typename', // this equals \"type = hat\"\n  ExpressionAttributeNames: { '#typename': 'type' },\n  ExpressionAttributeValues: { ':typename': 'hat' },\n}\n\ndocClient.query(params, function (err, data) {\n  if (err) console.log(err)\n  else console.log(data)\n})\n\n// async function abstraction\nasync function queryItems(type) {\n  var params = {\n    TableName: 'ProductTable',\n    IndexName: 'type-index',\n    ExpressionAttributeNames: { '#typename': 'type' },\n    KeyConditionExpression: '#typename = :typename',\n    ExpressionAttributeValues: { ':typename': type },\n  }\n  try {\n    const data = await docClient.query(params).promise()\n    return data\n  } catch (err) {\n    return err\n  }\n}\n\n// usage\nexports.handler = async (event, context) =\u003e {\n  try {\n    const data = await queryItems(event.item.type)\n    return { body: JSON.stringify(data) }\n  } catch (err) {\n    return { error: err }\n  }\n}\n```\n\n### delete - Delete a single item\n\n[full docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#delete-property)\n\n```javascript\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n\nvar params = {\n  TableName: 'ProductTable',\n  Key: {\n    id: 'my-item-id-to-delete',\n  },\n}\n\ndocClient.delete(params, function (err, data) {\n  if (err) console.log(err)\n  else console.log(data)\n})\n\n// async function abstraction\nasync function deleteItem(id) {\n  var params = {\n    TableName: 'ProductTable',\n    Key: { id },\n  }\n  try {\n    await docClient.delete(params).promise()\n  } catch (err) {\n    return err\n  }\n}\n\n// usage\nexports.handler = async (event, context) =\u003e {\n  try {\n    await deleteItem(event.item.id)\n    return { body: 'successfully deleted item' }\n  } catch (err) {\n    return { error: err }\n  }\n}\n```\n\n### update - Update a single item\n\n[full docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#update-property)\n\n```javascript\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n\nvar params = {\n  TableName: 'ProductTable',\n  Key: { id: 'my-item-id' },\n  UpdateExpression: 'set price = :newprice',\n  ExpressionAttributeValues: { ':newprice': 100 },\n}\n\ndocClient.update(params, function (err, data) {\n  if (err) console.log(err)\n  else console.log(data)\n})\n\n// async function abstraction\nasync function updateItem(id, price) {\n  var params = {\n    TableName: 'ProductTable',\n    Key: { id },\n    UpdateExpression: 'set price = :newprice',\n    ExpressionAttributeValues: { ':newprice': price },\n  }\n  try {\n    await docClient.update(params).promise()\n  } catch (err) {\n    return err\n  }\n}\n\n// usage\nexports.handler = async (event, context) =\u003e {\n  try {\n    const { id, price } = event.item\n    await updateItem(id, price)\n    return { body: 'successfully updated item' }\n  } catch (err) {\n    return { error: err }\n  }\n}\n```\n\n### batchWrite - Seed a table with data\n\n[full docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#batchWrite-property)\n\n```js\nconst AWS = require('aws-sdk')\nconst docClient = new AWS.DynamoDB.DocumentClient()\n\n// JSON data\nconst fetchedData = [\n  { language: 'JavaScript', isFun: true },\n  { language: 'Rust', isFun: true },\n]\n\n// format data for docClient\nconst seedData = fetchedData.map((item) =\u003e {\n  return {\n    PutRequest: {\n      Item: item,\n    },\n  }\n})\n\n/* We can only batch-write 25 items at a time,\n  so we'll store both the quotient, as well as what's left.\n  */\n\nlet quotient = Math.floor(seedData.length / 25)\nconst remainder = (seedData.length % 25)\n\n/* Upload in increments of 25 */\n\nlet batchMultiplier = 1\nwhile (quotient \u003e 0) {\n  for (let i = 0; i \u003c seedData.length - 1; i += 25) {\n    await docClient.batchWrite(\n      {\n        RequestItems: {\n          YOUR_TABLE_NAME: seedData.slice(i, 25 * batchMultiplier),\n        },\n      },\n      (err, data) =\u003e {\n        if (err) {\n          console.log('something went wrong...')\n        } else {\n          console.log('yay...uploaded!')\n        }\n      }\n    ).promise()\n    console.log({ quotient })\n    ++batchMultiplier\n    --quotient\n  }\n}\n\n/* Upload the remaining items (less than 25) */]\nif(remainder \u003e 0){\n  await docClient.batchWrite(\n    {\n      RequestItems: {\n        YOUR_TABLE_NAME: seedData.slice(seedData.length - remainder),\n      },\n    },\n    (err, data) =\u003e {\n      if (err) {\n        console.log('something went wrong...')\n      } else {\n        console.log('yay...the remainder uploaded!')\n      }\n    }\n  ).promise()\n}\n```\n","funding_links":[],"categories":["Written resources"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabit3%2Fdynamodb-documentclient-cheat-sheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdabit3%2Fdynamodb-documentclient-cheat-sheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabit3%2Fdynamodb-documentclient-cheat-sheet/lists"}