Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const AWS = require('aws-sdk')
- AWS.config.update({ region: 'us-east-1' })
- const { DynamoDB } = AWS
- const dynamoDB = new DynamoDB.DocumentClient()
- module.exports.query = function (attributeName, attributeValue, tableName) {
- const params = {
- ExpressionAttributeNames: {
- [`#${attributeName}`]: attributeName
- },
- ExpressionAttributeValues: {
- [`:${attributeName}`]: attributeValue
- },
- // FilterExpression: `#${attributeName} = :${attributeName}`,
- KeyConditionExpression: `#${attributeName} = :${attributeName}`,
- TableName: tableName
- }
- return dynamoDB.query(params).promise()
- }
- module.exports.getItem = function (attributeName, attributeValue, tableName) {
- const params = {
- TableName: tableName,
- Key: { [attributeName]: attributeValue }
- }
- return dynamoDB.get(params).promise()
- }
- module.exports.putItem = function (item, tableName) {
- console.log('table', process.env.DYNAMODB_TABLE)
- const params = {
- TableName: tableName,
- Item: item
- }
- return dynamoDB.put(params).promise()
- }
- module.exports.updateItem = function (keyAttribute, keyValue, attrsToUpdate, attrValues, tableName) {
- const expressionAttributeNames = attrsToUpdate.map((attrToUpdate) => {
- return { [`#${attrToUpdate}`]: attrToUpdate }
- }).reduce((accum, item) => ({ ...accum, ...item }), {})
- const expressionAttributeValues = attrsToUpdate.map((attrToUpdate, index) => {
- return { [`:${attrToUpdate}`]: attrValues[index] }
- }).reduce((accum, item) => ({ ...accum, ...item }), {})
- console.log('attrNames', expressionAttributeNames)
- console.log('attrValues', expressionAttributeValues)
- const updateExpression = attrsToUpdate.map((attrToUpdate) => `#${attrToUpdate} = :${attrToUpdate}`).join(', ')
- const params = {
- TableName: tableName,
- Key: {
- [keyAttribute]: keyValue
- },
- UpdateExpression: 'set ' + updateExpression,
- ExpressionAttributeNames: expressionAttributeNames,
- ExpressionAttributeValues: expressionAttributeValues
- }
- return dynamoDB.update(params).promise()
- }
- module.exports.deleteItem = function (attributeName, attributeValue, tableName) {
- const params = {
- TableName: tableName,
- Key: {
- [attributeName]: attributeValue
- }
- }
- return dynamoDB.delete(params).promise()
- }
- module.exports.scan = function (keys, values, tableName, filterExpression) {
- const expressionAttributeNames = keys.map((attrToUpdate) => {
- return { [`#${attrToUpdate}`]: attrToUpdate }
- }).reduce((accum, item) => ({ ...accum, ...item }), {})
- const expressionAttributeValues = keys.map((attrToUpdate, index) => {
- return { [`:${attrToUpdate}`]: values[index] }
- }).reduce((accum, item) => ({ ...accum, ...item }), {})
- console.log('attrNames', expressionAttributeNames)
- console.log('attrValues', expressionAttributeValues)
- let _filterExpression = keys.map((attrToUpdate) => `#${attrToUpdate} = :${attrToUpdate}`).join(' AND ')
- _filterExpression = filterExpression ? `${_filterExpression} AND ${filterExpression}` : _filterExpression
- const params = {
- ExpressionAttributeNames: expressionAttributeNames,
- ExpressionAttributeValues: expressionAttributeValues,
- TableName: tableName,
- FilterExpression: _filterExpression
- }
- return dynamoDB.scan(params).promise()
- }
- module.exports.appendToList = function (attributeName, attributeValue, keyToUpdate, jsonVals = [], tableName) {
- const params = {
- TableName: tableName,
- Key: {
- [attributeName]: attributeValue
- },
- UpdateExpression: 'SET #c = list_append(#c, :vals)',
- ExpressionAttributeNames: {
- '#c': keyToUpdate
- },
- ExpressionAttributeValues: {
- ':vals': jsonVals
- },
- ReturnValues: 'UPDATED_NEW'
- }
- return dynamoDB.update(params).promise()
- }
- module.exports.updateItemInList = function (attributeName, attributeValue, keyToUpdate, listItemIndex, value, tableName) {
- const params = {
- TableName: tableName,
- Key: {
- [attributeName]: attributeValue
- },
- UpdateExpression: `SET #item[${listItemIndex}] = :value`,
- ExpressionAttributeNames: {
- '#item': keyToUpdate
- },
- ExpressionAttributeValues: {
- ':value': value
- },
- ReturnValues: 'UPDATED_NEW'
- }
- return dynamoDB.update(params).promise()
- }
- module.exports.deleteItemFromList = function (attributeName, attributeValue, keyToUpdate, listItemIndex, tableName) {
- console.log('EXPRESSION', `REMOVE #item[${listItemIndex}]`)
- const params = {
- TableName: tableName,
- ExpressionAttributeNames: {
- '#item': keyToUpdate
- },
- Key: {
- [attributeName]: attributeValue
- },
- UpdateExpression: `REMOVE #item[${listItemIndex}]`,
- ReturnValues: 'ALL_NEW'
- }
- return dynamoDB.update(params).promise()
- }
Add Comment
Please, Sign In to add comment