I want to serialize a controller response by the nestjs serialization technique. I didn't find any approach and my solution is as follows:
export type UserRoleType = "admin" | "editor" | "ghost";
@Entity()
export class User {
@PrimaryGeneratedColumn() id: number;
@Column('text')
username: string;
@Column('text')
password: string;
@Column({
type: "enum",
enum: ["admin", "editor", "ghost"],
default: "ghost"
})
roles: UserRoleType;
@Column({ nullable: true })
profileId: number;
}
import { Exclude } from 'class-transformer';
export class UserResponse {
id: number;
username: string;
@Exclude()
roles: string;
@Exclude()
password: string;
@Exclude()
profileId: number;
constructor(partial: Partial<UserResponse>) {
Object.assign(this, partial);
}
}
import { Exclude, Type } from 'class-transformer';
import { User } from 'src/_entities/user.entity';
import { UserResponse } from './user.response';
export class UsersResponse {
@Type(() => UserResponse)
users: User[]
constructor() { }
}
@Controller('user')
export class UsersController {
constructor(
private readonly userService: UserService
) {
}
@UseInterceptors(ClassSerializerInterceptor)
@Get('all')
async findAll(
): Promise<UsersResponse> {
let users = await this.userService.findAll().catch(e => { throw new NotAcceptableException(e) })
let rsp =new UsersResponse()
rsp.users = users
return rsp
}
It works, but I must explicitly assign the db query result to the response users member. Is there a better way? Thanks a lot
Here the actual Response and wanted result, for a better explanation.
{
"users": [
{
"id": 1,
"username": "a"
},
{
"id": 2,
"username": "bbbbbb"
}
]
}
{
{
"id": 1,
"username": "a"
},
{
"id": 2,
"username": "bbbbbb"
}
}
I would recommend to directly put the @Exclude
decorators on your entity class User
instead of duplicating the properties in UserResponse
. The following answer assumes you have done so.
If you have a look at the code of the ClassSerializerInterceptor
, you can see that it automatically handles arrays:
return isArray
? (response as PlainLiteralObject[]).map(item =>
this.transformToPlain(item, options),
)
: this.transformToPlain(response, options);
However, it will only transform them, if you directly return the array, so return users
instead of return {users: users}
:
@UseInterceptors(ClassSerializerInterceptor)
@Get('all')
async findAll(): Promise<User> {
return this.userService.findAll()
}
If you need the nested response, then your way is a good solution.
Alternatively, you can call class-transformer's serialize
directly instead of using the ClassSerializerInterceptor
. It also handles arrays automatically:
import { serialize } from 'class-transformer';
@Get('all')
async findAll(): Promise<UsersResponse> {
const users: User[] = await this.userService.findAll();
return {users: serialize(users)};
}
Wow, what easy, if i know! Perfect, this solves my problem. Also your recommendation for the User Entity with the class-transformer @Exclue() decorator.
And i know that i do not need a custom UsersResponse class in this use case. This solution was that what i was looking for, but i overjump this quite easy way
Thank you so much for your superfast answer and the problem solution.
Greetings to Berlin from Rostock :)
Here my final approach:
@UseInterceptors(ClassSerializerInterceptor)
@Get('all')
async findAll(
): Promise<User> {
return await this.userService.findAll().catch(e => { throw new NotAcceptableException(e) })
}
import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn, OneToMany } from 'typeorm';
import { Profile } from './profile.entity';
import { Photo } from './photo.entity';
import { Album } from './album.entity';
import { Exclude } from 'class-transformer';
export type UserRoleType = "admin" | "editor" | "ghost";
@Entity()
export class User {
@PrimaryGeneratedColumn() id: number;
@Column('text')
username: string;
@Exclude()
@Column('text')
password: string;
@Column({
type: "enum",
enum: ["admin", "editor", "ghost"],
default: "ghost"
})
roles: UserRoleType;
@Exclude()
@Column({ nullable: true })
profileId: number;
@OneToMany(type => Photo, photo => photo.user)
photos: Photo[];
@OneToMany(type => Album, albums => albums.user)
albums: Album[];
@OneToOne(type => Profile, profile => profile.user)
@JoinColumn()
profile: Profile;
}
[
{
"id": 1,
"username": "a",
"roles": "admin"
},
{
"id": 2,
"username": "bbbbbb",
"roles": "ghost"
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With