tywalch / electrodb

A DynamoDB library to ease the use of modeling complex hierarchical relationships and implementing a Single Table Design while keeping your query code readable.
MIT License
1.03k stars 67 forks source link

Computed attributes ignore `field` property during update #281

Closed sam3d closed 1 year ago

sam3d commented 1 year ago

Describe the bug I'd expect an attribute to adhere to the field property at all times - whether or not it's a computed attribute. However, computed attributes seem to ignore the field property during an update operation when their update is triggered from another attribute being set.

ElectroDB Version 2.8.0

ElectroDB Playground Link

https://electrodb.fun/?#code/JYWwDg9gTgLgB...

Entity/Service Definitions

import { Entity } from "electrodb";

const tasks = new Entity(
  {
    model: {
      entity: "tasks",
      version: "1",
      service: "taskapp",
    },
    attributes: {
      id: { type: "string" },
      expiresAt: { type: "string", default: () => new Date().toISOString() },
      localFieldName: {
        type: "number",
        field: "TTL", // This should be used when updating
        watch: ["expiresAt"],
        set: (_, { expiresAt }) => Date.parse(expiresAt),
      },
    },
    indexes: {
      byId: {
        pk: { field: "PK", composite: ["id"] },
        sk: { field: "SK", composite: [] },
      },
    },
  },
  { table: "taskapp" },
);

tasks.create({ id: "test" }).go();

// Updating `expiredAt` triggers an update of `localFieldName`, which itself
// should be saved as `TTL` based on the `field` attribute, but it's saved as
// `localFieldName` to dynamodb
tasks.update({ id: "test" }).set({ expiresAt: new Date().toISOString() }).go();
tywalch commented 1 year ago

Thank you! I will make this a priority!

tywalch commented 1 year ago

This a great find 👍

tywalch commented 1 year ago

I have addressed this issue in version 2.8.2, let me know if that fixes your issue 👍

sam3d commented 1 year ago

Wow that was so fast 😱 That works perfectly for me now, thank you so much !!