Skip to content

question: Transform deeply nested objects #1316

Open
@SkelatorIndy

Description

@SkelatorIndy

I was trying to...
I'm attempting to use class transformer with Prisma and create formatted objects to return to my front end. I'm trying to fetch an exemption that includes a certain user, and then use entity classes to transform and remove properties so it conforms to a certain object structure.

The problem:
Class transformer seems to work on a depth of 1 object, but deeper transforms don't seem to be applied. I can fetch the exemption, and when transforming the nested user, it will remove all excluded properties, however it will not return an exposed name function, so I'm not sure if there's an additional property or decorator I need to define, or if it's a limitation of the library, but any assistance would be greatly appreciated.

Here's the current class structures:

Exemption

class ExemptionEntity implements Exemption {
    @Exclude()
    student_id: number;
    @Type(() => UserEntity)
    student: UserEntity;

    expires_on: Date | null;

    @Exclude()
    client_id: number;

    constructor(partial: Partial<ExemptionEntity>) {
        Object.assign(this, partial);
    }
}

User

class UserEntity implements User {
    id: number;

    @Exclude()
    first_name: string;
    @Exclude()
    last_name: string;
    @Expose()
    get name() {
        return {
            first: this.first_name,
            last: this.last_name,
            full: `${this.first_name} ${this.last_name}`,
        };
    }

    constructor(partial: Partial<UserEntity>) {
        Object.assign(this, partial);
    }
}

The resulting objects look like

{
    "expires_on": "2022-08-24T04:00:00.000Z",
    "student": {
        "id": 3,
        "unique_id": "00003"
    }
}

While it should look like

  {
      "expires_on":"2022-08-24T04:00:00.000Z",
      "student": {
          "id": 3,
          "unique_id": "00003",
          "name": {
              "first": "Test",
              "last": "User",
              "full": "Test User"
          }
      }
  }

If any more information is needed, just let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions