All files dynamo.js

74.55% Statements 41/55
85.71% Branches 6/7
62.5% Functions 5/8
74.55% Lines 41/55

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 551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x
import dynameh from 'dynameh'
 
import _ from '@x-recon/fn'
 
 
export const getItem = async ({ dynamo, table, hashKey, sortKey = null }) => {
  const request = dynameh.requestBuilder.buildGetInput(table, hashKey, sortKey)
  const response = await dynamo.getItem(request).promise()
  return dynameh.responseUnwrapper.unwrapGetOutput(response)
}
 
export const putItem = async ({ dynamo, table, item }) => {
  const request = dynameh.requestBuilder.buildPutInput(table, item)
  await dynamo.putItem(request).promise()
}
 
export const query = async ({ dynamo, table, hashKey, sortKey, operation = '=' }) => {
  const request = dynameh.requestBuilder.buildQueryInput(table, hashKey, operation, sortKey)
  const response = await dynamo.query(request).promise()
  return dynameh.responseUnwrapper.unwrapQueryOutput(response)
}
 
export const scanByCallback = async ({ dynamo, table, onItems }) => {
  const scanInput = dynameh.requestBuilder.buildScanInput(table)
  await dynameh.scanHelper.scanByCallback(dynamo, scanInput, onItems)
}
 
export const deleteItemsByQuery = async ({ dynamo, table, hashKey, sortKey, operation = '=' }) => {
  
  const { partitionKeyField, sortKeyField } = table
 
  const makeKey = (item) => {
    if (sortKeyField) return [item[partitionKeyField], item[sortKeyField]]
    else item[partitionKeyField]
  }
 
  const deleteBatch = async (items) => {    
    const batchDeleteInput = dynameh.requestBuilder.buildBatchDeleteInput(table, items.map(makeKey))
    await dynameh.batchHelper.batchWriteAll(dynamo, batchDeleteInput)
  }
 
  const queryInput = dynameh.requestBuilder.buildQueryInput(table, hashKey, operation, sortKey)
  await dynameh.queryHelper.queryByCallback(dynamo, queryInput, deleteBatch)
 
}
 
const client = (dynamo, table = null) => ({
  getItem:            _.partob(getItem,             { dynamo, table }),
  putItem:            _.partob(putItem,             { dynamo, table }),
  query:              _.partob(query,               { dynamo, table }),
  scanByCallback:     _.partob(scanByCallback,      { dynamo, table }),
  deleteItemsByQuery: _.partob(deleteItemsByQuery,  { dynamo, table })
})
 
export default client