All files / utils entityItemCount.js

100% Statements 8/8
100% Branches 2/2
100% Functions 5/5
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 10825x                     1x                                         1x                                           1x                                         40x           40x                     1x               25x              
const { GetCommand, UpdateCommand, DeleteCommand } = require('@aws-sdk/lib-dynamodb')
 
/**
 * A TransactWriteItems `Update` action adjusting an entity's item count by
 * `delta`. Auto-vivifies the row on first ADD, so nothing needs pre-seeding.
 *
 * @param {string} tableName
 * @param {string} entityId
 * @param {number} delta
 */
function buildItemCountUpdate(tableName, entityId, delta) {
  return {
    Update: {
      TableName: tableName,
      Key: { entityId },
      UpdateExpression: 'ADD #count :delta',
      ExpressionAttributeNames: { '#count': 'count' },
      ExpressionAttributeValues: { ':delta': delta }
    }
  }
}
 
/**
 * Non-transactional +/- adjustment, for best-effort call sites (item delete)
 * that don't already run inside a TransactWriteCommand.
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {string} tableName
 * @param {string} entityId
 * @param {number} delta
 */
async function adjustItemCount(docClient, tableName, entityId, delta) {
  await docClient.send(
    new UpdateCommand({
      TableName: tableName,
      Key: { entityId },
      UpdateExpression: 'ADD #count :delta',
      ExpressionAttributeNames: { '#count': 'count' },
      ExpressionAttributeValues: { ':delta': delta }
    })
  )
}
 
/**
 * Sets an entity's item count to an exact value — used after a bulk
 * operation (clear-all) where re-deriving the delta from a loop is more
 * error-prone than just asserting the known end state.
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {string} tableName
 * @param {string} entityId
 * @param {number} value
 */
async function setItemCount(docClient, tableName, entityId, value) {
  await docClient.send(
    new UpdateCommand({
      TableName: tableName,
      Key: { entityId },
      UpdateExpression: 'SET #count = :value',
      ExpressionAttributeNames: { '#count': 'count' },
      ExpressionAttributeValues: { ':value': value }
    })
  )
}
 
/**
 * Reads an entity's item count, defaulting to 0 if no row exists yet (e.g. a
 * brand-new entity that has never had an item created).
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {string} tableName
 * @param {string} entityId
 * @returns {Promise<number>}
 */
async function getItemCount(docClient, tableName, entityId) {
  const result = await docClient.send(
    new GetCommand({
      TableName: tableName,
      Key: { entityId }
    })
  )
  return result.Item?.count || 0
}
 
/**
 * Deletes an entity's item-count row entirely (entity deletion).
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {string} tableName
 * @param {string} entityId
 */
async function deleteItemCount(docClient, tableName, entityId) {
  await docClient.send(
    new DeleteCommand({
      TableName: tableName,
      Key: { entityId }
    })
  )
}
 
module.exports = {
  buildItemCountUpdate,
  adjustItemCount,
  setItemCount,
  getItemCount,
  deleteItemCount
}