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

Attributes created with `any` type are not recommended into the `attributes` of queries or scans.

Open jdbu2002 opened this issue 11 months ago • 1 comments

Having the following schemas, entity and table:

const dateSchema = s
  .any()
  .castAs<Date>()
  .default(() => new Date())
  .transform({
    encode: (value) => value.toISOString(),
    decode: (value: string) => new Date(value),
  });

const schema = s.item({
  first_name: s.string(),
  last_name: s.string(),
  email: s.string(),
  gender: s.string(),
  ip_address: s.string(),
  company: s.string().optional(),
  birthdate: dateSchema
});

export const table = new Table({
  documentClient,
  name: "Users",
  partitionKey: {
    name: "id",
    type: "string",
  },
});

export const entity = new Entity({
  name: "User",
  table,
  schema,
  timestamps: false,
});

const queryPromise = table
  .build(QueryCommand)
  .entities(entity)
  .query({ partition: "ID" })
  .options({ attributes: ["birthdate"] }) // ⬅️ This does not recommend the "birthdate" property. But allows it.
  .send();

Image

Inspecting the generated paths, we get birthdate as a template string type:

type SchemaPaths = Paths<typeof schema>
   vvv
type SchemaPaths = "id" | "company" | "first_name" | "last_name" | "email" | "gender" | "ip_address" | "['id']" | "['company']" | `birthdate${string}` | `['birthdate']${string}` | "['first_name']" | "['last_name']" | "['email']" | "['gender']" | "['ip_address']"

I thinks this is because of any to be extendable for example birthday.month or birthdate[0].value.

jdbu2002 avatar May 20 '25 03:05 jdbu2002

Yes, that is because any can be maps with subpaths. Seen your PR and I'm OK with it 👍

ThomasAribart avatar May 20 '25 08:05 ThomasAribart