Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added class transform options #811

Merged
merged 3 commits into from
Dec 7, 2022

Conversation

xTCry
Copy link
Contributor

@xTCry xTCry commented Dec 3, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message was generated by yarn commit
  • Tests for the changes have been added (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Tests
  • Release
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Added the possibility of optional transformation of classes for entities

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@xTCry
Copy link
Contributor Author

xTCry commented Dec 3, 2022

Example usage
This allows to get and update only the available fields by user role.

user.controller.ts

@Feature(User.name)
@Crud({
  model: { type: User },
  query: {
    join: {
      balances: { eager: true },
    },
    softDelete: true,
  },
  routes: {
    only: ['getOneBase', 'getManyBase', 'updateOneBase', 'replaceOneBase'],
    getOneBase: {
      decorators: [
        AllowedRoles(UserRole.ADMIN, UserRole.DEFAULT),
      ],
    },
    getManyBase: {
      decorators: [
        AllowedRoles(UserRole.ADMIN, UserRole.DEFAULT),
      ],
    },
    updateOneBase: {
      returnShallow: true,
      decorators: [AllowedRoles(UserRole.ADMIN)],
    },
    replaceOneBase: {
      returnShallow: true,
      decorators: [AllowedRoles(UserRole.ADMIN)],
    },
  },
  validation: {
    transform: true,
    transformOptions: {
      groups: [FOR_ALL],
    },
  },
})
@CrudAuth({
  property: 'user',
  groups: (user: User) => user.role,
})
@Controller('user')
export class UserController implements CrudController<User> {
  constructor(
    public readonly service: UserService,
  ) {}
}

user.entity.ts

import { Exclude, Expose, plainToClass } from 'class-transformer';
import {
  Column,
  CreateDateColumn,
  Entity,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { UserRole, FOR_ALL } from '@my-common';
import { DefaultTransform } from '@my-common/decorator';

import { UserBalance } from './user-balance.entity';

@Entity()
@Exclude()
export class User {
  @Expose()
  @PrimaryGeneratedColumn()
  public id: number;

  @Expose({ groups: [FOR_ALL, UserRole.ADMIN] })
  @Column({ type: 'character varying', length: 32, nullable: true })
  public username: string | null;

  @Expose({ groups: [FOR_ALL, UserRole.ADMIN] })
  @Column({ type: 'character varying', nullable: true })
  public fullname: string | null;

  @Expose()
  @Column({ type: 'enum', enum: UserRole, default: UserRole.DEFAULT })
  @DefaultTransform(UserRole.DEFAULT, { toClassOnly: true })
  public role: UserRole;

  @Expose({ groups: [FOR_ALL, UserRole.ADMIN] })
  @Column({ type: 'character varying', nullable: true })
  @DefaultTransform(null, { toClassOnly: true })
  public notes: string;

  @Expose({ groups: [FOR_ALL, UserRole.ADMIN] })
  @OneToMany(() => UserBalance, (balance) => balance.user)
  public balances: UserBalance[];

  @Expose()
  @CreateDateColumn()
  public createdAt: Date;

  @Expose()
  @UpdateDateColumn()
  public updatedAt: Date;

  constructor(input?: Partial<User>) {
    if (input) {
      Object.assign(this, plainToClass(User, input, { groups: [FOR_ALL] }));
    }
  }
}

@michaelyali michaelyali merged commit d6d3c4e into nestjsx:master Dec 7, 2022
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.

3 participants