Skip to content

[BUG] Entity constructor overrides attributes on projection/selective Get #44

@iak4v

Description

@iak4v

Summary:

Love this library for managing my dynamoDB table!

However I came across a bug, when I try to get a item using .get({..}, {attributes: [..]}) method, constructor overrides other attribute values which do not match the true value from the DB.

Code sample:

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 } });

const user1 = await UserManager.get({ email: "user1@example.com" }, { attributes: ["name"] });
console.log(user1.isAdmin) // ❌ returns false instead of true
console.log(user1.createdAt) // ❌ returns current date and not matches the updated date value from DB

On the other hand

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

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

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

Action performed:

If I try to get an item from db, the values in it changes based on the attributes array

Expected result:

Retrieved item should be same and match the true value from DB irrespective of attributes array I provide

Actual result:

Retrieved item differs and do not match the true value from DB based on attributes array I provide

Environment:

Operating System & version: Windows 11 x64-based processor
Node.js version: v22.18.0
NPM version: 10.9.3
Dynamode version: ^1.5.0

Other:

  • I have read through the Dynamode documentation before posting this issue
  • I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • I have searched the internet to ensure this issue hasn't been raised or answered before

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions