All files / utils fieldValueIndexHelpers.js

100% Statements 30/30
92.85% Branches 13/14
100% Functions 4/4
100% Lines 24/24

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 8547x                                                 58x   58x   58x 62x               62x 12990x 12990x   57x 57x 5x 5x 4x       53x                                 152x 666x   133x 133x   133x 152x   133x 666x     133x     47x  
const { QueryCommand } = require('@aws-sdk/lib-dynamodb')
 
/**
 * Generic paginated exact-`pk` Query against FieldValueIndexTable (or any
 * table sharing its pk/sk shape), accumulating one id-shaped attribute into
 * a capped Set. Extracted once three call sites converged on the same
 * byte-for-byte pagination loop: textSearchIndex.js's
 * queryItemIdsForTextSearchToken, equalityIndexValue.js's
 * queryItemIdsForEqualityValue, and catalogSearchIndex.js's
 * queryEntityIdsForCatalogSearchToken.
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {string} tableName
 * @param {string} pk
 * @param {string} idAttribute name of the attribute to collect (e.g. 'itemId', 'entityId')
 * @param {{ maxResults?: number }} [options]
 * @returns {Promise<Set<string>>}
 */
async function queryIdsForExactPk(
  docClient,
  tableName,
  pk,
  idAttribute,
  { maxResults = Infinity } = {}
) {
  const ids = new Set()
  let ExclusiveStartKey
  const seenKeys = new Set()
 
  do {
    const result = await docClient.send(
      new QueryCommand({
        TableName: tableName,
        KeyConditionExpression: 'pk = :pk',
        ExpressionAttributeValues: { ':pk': pk },
        ExclusiveStartKey
      })
    )
    for (const item of result.Items || []) {
      ids.add(item[idAttribute])
      if (ids.size >= maxResults) return ids
    }
    ExclusiveStartKey = result.LastEvaluatedKey
    if (ExclusiveStartKey) {
      const keySignature = JSON.stringify(ExclusiveStartKey)
      if (seenKeys.has(keySignature)) break
      seenKeys.add(keySignature)
    }
  } while (ExclusiveStartKey)
 
  return ids
}
 
/**
 * Generic toRelease/toClaim diff between two lists of tagged tokens/values,
 * keyed by `keyFn`. Same Map-diff shape `diffTextSearchTokens` already used
 * internally; pulled out so a caller that needs a differently-keyed diff
 * (e.g. catalogSearchIndex.js, which deliberately drops the `lang`
 * dimension from its key — see its own doc comment for why) can reuse the
 * mechanics without being tied to textSearchIndex.js's own key shape.
 *
 * @param {Array<Record<string, unknown>>} oldList
 * @param {Array<Record<string, unknown>>} newList
 * @param {(item: Record<string, unknown>) => string} keyFn
 * @returns {{ toRelease: Array<Record<string, unknown>>, toClaim: Array<Record<string, unknown>> }}
 */
function diffByKey(oldList, newList, keyFn) {
  const oldMap = new Map(oldList.map((item) => [keyFn(item), item]))
  const newMap = new Map(newList.map((item) => [keyFn(item), item]))
 
  const toRelease = []
  const toClaim = []
 
  for (const [key, item] of oldMap) {
    if (!newMap.has(key)) toRelease.push(item)
  }
  for (const [key, item] of newMap) {
    if (!oldMap.has(key)) toClaim.push(item)
  }
 
  return { toRelease, toClaim }
}
 
module.exports = { queryIdsForExactPk, diffByKey }