Dynamo-Easy Config

Authorization

The AWS SDK creates a Snapshot of the credentials when creating the underlying DynamoDB client, if you use short-lived credentials (for instance with Cognito Identity) you have to ensure the credentials are valid before making requests.

session-validity-ensurer.snippet.ts
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

updateDynamoEasyConfig({
  sessionValidityEnsurer: (): Promise<void> => {
    // do whatever you need to do to make sure the session is valid
    // and return an Promise<void> when done
    return Promise.resolve()
  },
})

Logging

To receive log statements from dynamo-easy you can provide a function

log-receiver.snippet.ts
import { LogInfo, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

updateDynamoEasyConfig({
  logReceiver: (logInfo: LogInfo) => {
    const msg = `[${logInfo.level}] ${logInfo.timestamp} ${logInfo.className} (${
      logInfo.modelConstructor
      }): ${logInfo.message}`
    console.debug(msg, logInfo.data)
  }
})

TableNameResolver

To use different table names per stage you can provide the tableNameResolver function.

This function will receive the default table name (either resolved using the model name or custom value when a tableName was provided in the @Model decorator) and needs to return the extended table name as string.

table-name-resolver.snippet.ts
import { TableNameResolver, updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'

const myTableNameResolver: TableNameResolver = (tableName: string) => {
  return `myPrefix-${tableName}`
}

updateDynamoEasyConfig({
  tableNameResolver: myTableNameResolver
})

DateMapper

If you want to use a different type for the @DateProperty decorator (eg. Moment or DayJS) you need to define a custom mapper and register it in dynamo-easy easy config.

date-mapper.snippet.ts
import { updateDynamoEasyConfig } from '@shiftcoders/dynamo-easy'
import { dateToNumberMapper } from '../models'

updateDynamoEasyConfig({
  dateMapper: dateToNumberMapper
})

Last updated