Skip to content

Conversation

@blazejkustra
Copy link
Owner

@blazejkustra blazejkustra commented Oct 20, 2025

Summary:

Fixes a bug where calling .get() with a specified attributes array incorrectly overrides other non-requested fields in the returned item with default constructor values, instead of preserving the true values from DynamoDB.

This causes retrieved entities to differ from their actual stored state when partial attribute projections are used.

Code sample:

Model

import { attribute, Dynamode, Entity, TableManager } from "dynamode";

Dynamode.ddb.local();

export class User extends Entity {
    @attribute.partitionKey.string()
    email: string;

    @attribute.string()
    name: string;

    @attribute.boolean()
    isAdmin: boolean;

    @attribute.date.string()
    createdAt: Date;

    constructor(props: { email: string, name: string, isAdmin?: boolean, createdAt?: Date }) {
        super();
        this.email = props.email;
        this.name = props.name;
        this.isAdmin = props.isAdmin || false;
        this.createdAt = props.createdAt || new Date();
    }
}

const UserTableManager = new TableManager(User, {
    tableName: "Users",
    partitionKey: "email",
    createdAt: "createdAt"
});

export const UserManager = UserTableManager.entityManager();

try { await UserTableManager.createTable() } catch { } // created already

General

await UserManager.create(new User({ email: "user1@example.com", name: "User 1" }));
await UserManager.update(
  { email: "user1@example.com" },
  { set: { createdAt: new Date("2000-01-01T12:00:00Z"), isAdmin: true } }
);

// ❌ Incorrect behavior
const user1 = await UserManager.get({ email: "user1@example.com" }, { attributes: ["name"] });
console.log(user1.isAdmin);   // should be undefined
console.log(user1.createdAt); // should be undefined

// ✅ Correct behavior
const user2 = await UserManager.get({ email: "user1@example.com" });
console.log(user2.isAdmin);   // returns true as expected
console.log(user2.createdAt); // returns correct DB value

GitHub linked issue:

#44

Type of change:

  • Bug fix
  • Feature implementation
  • Documentation improvement
  • Testing improvement
  • Something not listed here

Is this a breaking change?

  • Partly yes
  • No

Other:

  • I have searched through the GitHub pull requests to ensure this PR has not already been submitted
  • I have updated the Dynamode documentation (if required)
  • I have added/updated the Dynamode test cases (if required)
  • I agree that all changes made in this pull request may be distributed and are made available in accordance with the [Dynamode License](../LICENSE).

@blazejkustra blazejkustra changed the title Selected attributes Fix: Entity constructor overrides attributes on projection/selective Get Oct 20, 2025
@blazejkustra blazejkustra merged commit 4f0e7ea into main Oct 21, 2025
3 checks passed
@blazejkustra blazejkustra deleted the selected-attributes branch October 21, 2025 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants