Guest User

Untitled

a guest
Oct 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. const AWS = require('aws-sdk')
  2.  
  3. AWS.config.update({ region: 'us-east-1' })
  4. const { DynamoDB } = AWS
  5. const dynamoDB = new DynamoDB.DocumentClient()
  6.  
  7. module.exports.query = function (attributeName, attributeValue, tableName) {
  8. const params = {
  9. ExpressionAttributeNames: {
  10. [`#${attributeName}`]: attributeName
  11. },
  12. ExpressionAttributeValues: {
  13. [`:${attributeName}`]: attributeValue
  14. },
  15. // FilterExpression: `#${attributeName} = :${attributeName}`,
  16. KeyConditionExpression: `#${attributeName} = :${attributeName}`,
  17. TableName: tableName
  18. }
  19. return dynamoDB.query(params).promise()
  20. }
  21.  
  22. module.exports.getItem = function (attributeName, attributeValue, tableName) {
  23. const params = {
  24. TableName: tableName,
  25. Key: { [attributeName]: attributeValue }
  26. }
  27.  
  28. return dynamoDB.get(params).promise()
  29. }
  30.  
  31. module.exports.putItem = function (item, tableName) {
  32. console.log('table', process.env.DYNAMODB_TABLE)
  33. const params = {
  34. TableName: tableName,
  35. Item: item
  36. }
  37. return dynamoDB.put(params).promise()
  38. }
  39.  
  40. module.exports.updateItem = function (keyAttribute, keyValue, attrsToUpdate, attrValues, tableName) {
  41. const expressionAttributeNames = attrsToUpdate.map((attrToUpdate) => {
  42. return { [`#${attrToUpdate}`]: attrToUpdate }
  43. }).reduce((accum, item) => ({ ...accum, ...item }), {})
  44.  
  45. const expressionAttributeValues = attrsToUpdate.map((attrToUpdate, index) => {
  46. return { [`:${attrToUpdate}`]: attrValues[index] }
  47. }).reduce((accum, item) => ({ ...accum, ...item }), {})
  48. console.log('attrNames', expressionAttributeNames)
  49. console.log('attrValues', expressionAttributeValues)
  50.  
  51. const updateExpression = attrsToUpdate.map((attrToUpdate) => `#${attrToUpdate} = :${attrToUpdate}`).join(', ')
  52. const params = {
  53. TableName: tableName,
  54. Key: {
  55. [keyAttribute]: keyValue
  56. },
  57. UpdateExpression: 'set ' + updateExpression,
  58. ExpressionAttributeNames: expressionAttributeNames,
  59. ExpressionAttributeValues: expressionAttributeValues
  60. }
  61. return dynamoDB.update(params).promise()
  62. }
  63.  
  64. module.exports.deleteItem = function (attributeName, attributeValue, tableName) {
  65. const params = {
  66. TableName: tableName,
  67. Key: {
  68. [attributeName]: attributeValue
  69. }
  70. }
  71. return dynamoDB.delete(params).promise()
  72. }
  73.  
  74. module.exports.scan = function (keys, values, tableName, filterExpression) {
  75. const expressionAttributeNames = keys.map((attrToUpdate) => {
  76. return { [`#${attrToUpdate}`]: attrToUpdate }
  77. }).reduce((accum, item) => ({ ...accum, ...item }), {})
  78.  
  79. const expressionAttributeValues = keys.map((attrToUpdate, index) => {
  80. return { [`:${attrToUpdate}`]: values[index] }
  81. }).reduce((accum, item) => ({ ...accum, ...item }), {})
  82. console.log('attrNames', expressionAttributeNames)
  83. console.log('attrValues', expressionAttributeValues)
  84.  
  85. let _filterExpression = keys.map((attrToUpdate) => `#${attrToUpdate} = :${attrToUpdate}`).join(' AND ')
  86. _filterExpression = filterExpression ? `${_filterExpression} AND ${filterExpression}` : _filterExpression
  87.  
  88. const params = {
  89. ExpressionAttributeNames: expressionAttributeNames,
  90. ExpressionAttributeValues: expressionAttributeValues,
  91. TableName: tableName,
  92. FilterExpression: _filterExpression
  93. }
  94.  
  95. return dynamoDB.scan(params).promise()
  96. }
  97.  
  98. module.exports.appendToList = function (attributeName, attributeValue, keyToUpdate, jsonVals = [], tableName) {
  99. const params = {
  100. TableName: tableName,
  101. Key: {
  102. [attributeName]: attributeValue
  103. },
  104. UpdateExpression: 'SET #c = list_append(#c, :vals)',
  105. ExpressionAttributeNames: {
  106. '#c': keyToUpdate
  107. },
  108. ExpressionAttributeValues: {
  109. ':vals': jsonVals
  110. },
  111. ReturnValues: 'UPDATED_NEW'
  112. }
  113.  
  114. return dynamoDB.update(params).promise()
  115. }
  116.  
  117. module.exports.updateItemInList = function (attributeName, attributeValue, keyToUpdate, listItemIndex, value, tableName) {
  118. const params = {
  119. TableName: tableName,
  120. Key: {
  121. [attributeName]: attributeValue
  122. },
  123. UpdateExpression: `SET #item[${listItemIndex}] = :value`,
  124. ExpressionAttributeNames: {
  125. '#item': keyToUpdate
  126. },
  127. ExpressionAttributeValues: {
  128. ':value': value
  129. },
  130. ReturnValues: 'UPDATED_NEW'
  131. }
  132.  
  133. return dynamoDB.update(params).promise()
  134. }
  135.  
  136. module.exports.deleteItemFromList = function (attributeName, attributeValue, keyToUpdate, listItemIndex, tableName) {
  137. console.log('EXPRESSION', `REMOVE #item[${listItemIndex}]`)
  138. const params = {
  139. TableName: tableName,
  140. ExpressionAttributeNames: {
  141. '#item': keyToUpdate
  142. },
  143. Key: {
  144. [attributeName]: attributeValue
  145. },
  146. UpdateExpression: `REMOVE #item[${listItemIndex}]`,
  147. ReturnValues: 'ALL_NEW'
  148. }
  149.  
  150. return dynamoDB.update(params).promise()
  151. }
Add Comment
Please, Sign In to add comment