Skip to main content

EntityFormatter

Given a saved item, validates that it respects the schema of the Entity and formats it:

import { EntityFormatter } from 'dynamodb-toolbox/entity/actions/format'

const formattedPikachu =
PokemonEntity.build(EntityFormatter).format(savedPikachu)
info

This action is mostly a wrapper around the schema Formatter action.

Note that:

  • Additional and hidden fields are omitted, but inputs are not mutated
  • The formatting throws an error if the saved item is invalid
  • Transformations (i.e. savedAs and transforms) are applied in reverse

Methods

format(...)

(savedItem: unknown, options?: FormatItemOptions) => FormattedItem<ENTITY>

Formats a saved item:

const formattedItem = PokemonEntity.build(EntityFormatter).format(savedItem)

You can provide formatting options as a second argument. Available options:

OptionTypeDefaultDescription
partialbooleanfalseAllow every attribute (flat or deep) to be optional while formatting.
attributesPath<Entity>[]-To specify a list of attributes to format (other attributes are omitted).

See the PathParser action for more details on how to write attribute paths.
Examples
const saved = {
pokemonId: 'pikachu1',
name: 'Pikachu'
}

// 🙌 Typed as `DeepPartial<Pokemon>`
const formatted = PokemonEntity.build(
EntityFormatter
).format(saved, { partial: true })

You can use the FormattedItem type to explicitly type an object as a formatting output object:

import type { FormattedItem } from 'dynamodb-toolbox/entity'

const formattedItem: FormattedItem<
typeof PokemonEntity,
// 👇 Optional options
{ partial: false; attributes: 'name' | 'level' }
// ❌ Throws a type error
> = { invalid: 'output' }