dynamodb-toolbox icon indicating copy to clipboard operation
dynamodb-toolbox copied to clipboard

Question regarding transformers for stringified json

Open aaarichter opened this issue 5 months ago • 3 comments

For legacy reasons, we have stringified JSON objects in the dynamoDB tables I'm working on, instead of maps or other constructs When I saw https://www.dynamodbtoolbox.com/docs/schemas/transformers/usage I was pretty hyped, that it would allow me to use the JSON.parse and JSON.stringify directly in the schema.

But I noticed that 1. it only works with primitives (as written in the documentation), 2. it wouldn't allow a change of primitive return back a complex object.

@ThomasAribart Do you think something like this might be possible:

interface complexObject {
  field1: string
  filed2: ...
}

const transformer: Transformer<string, complexObject | undefined> = {
  parse: input => input ? <complexObject>JSON.parse(input): undefined,

  format: saved => JSON.stringify(saved)
}

const pokemonSchema = schema({
  id: string().key(),
  props: string().transform(transformer)
})

so that props will be of type complexObject when used in code, and string when written to DynamoDB

aaarichter avatar Sep 22 '24 16:09 aaarichter