Связка NEST + Sequelize
Пытаюсь сделать сериализацию, исключить поле password из вывода.
Делаю по документации.
модель
import { Exclude } from "class-transformer";
import { AutoIncrement, Column, DataType, Model, PrimaryKey, Table, Unique } from "sequelize-typescript"
interface UserCreateionAttr {
email: string;
password: string;
}
@Table({ tableName: "users" })
export class User extends Model<User, UserCreateionAttr> {
@Unique
@PrimaryKey
@AutoIncrement
@Column({ type: DataType.INTEGER })
id: number;
@Unique
@Column({
type: DataType.STRING,
allowNull: false
})
email: string;
@Exclude({ toPlainOnly: true })
@Column({
type: DataType.STRING,
allowNull: false
})
password: string;
}
сервис
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { CreateUserDto } from './dto/create-user.dto';
import { User } from './users.model';
@Injectable()
export class UsersService {
constructor(
@InjectModel(User) private userRepository: typeof User,
) { }
async createUser(dto: CreateUserDto): Promise<User> {
return await this.userRepository.create(dto);
}
async getUserByEmail(email: string): Promise<User> {
return await this.userRepository.findOne({ where: { email } });
}
}
контроллер
import { Body, ClassSerializerInterceptor, Controller, Get, Post, UseInterceptors } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { User } from './users.model';
import { UsersService } from './users.service';
@Controller('users')
export class UsersController {
constructor(
private userService: UsersService,
) { }
@UseInterceptors(ClassSerializerInterceptor)
@Post()
async createUser(@Body() dto: CreateUserDto): Promise<User>{
const user: User = await this.userService.createUser(dto);
return user;
}
@UseInterceptors(ClassSerializerInterceptor)
@Get()
getUser(@Body() user: {email: string}) {
return this.userService.getUserByEmail(user.email);
}
}
Сервер при запросе возвращает
{
"dataValues": {
"id": 1,
"email": "maria",
"password": "guess",
"createdAt": "2022-06-30T09:38:05.255Z",
"updatedAt": "2022-06-30T09:38:05.255Z"
},
"_previousDataValues": {
"id": 1,
"email": "maria",
"password": "guess",
"createdAt": "2022-06-30T09:38:05.255Z",
"updatedAt": "2022-06-30T09:38:05.255Z"
},
"uniqno": 1,
"_changed": [],
"_options": {
"isNewRecord": false,
"_schema": null,
"_schemaDelimiter": "",
"raw": true,
"attributes": [
"id",
"email",
"password",
"createdAt",
"updatedAt"
]
},
"isNewRecord": false
}
вместо ожидаемого
{
"id": 1,
"email": "maria",
"createdAt": "2022-06-30T09:38:05.255Z",
"updatedAt": "2022-06-30T09:38:05.255Z"
}
Что я сделал не так?