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

how to hide password when get the model #1712

Open
1 task
susatyo441 opened this issue Mar 8, 2024 · 2 comments
Open
1 task

how to hide password when get the model #1712

susatyo441 opened this issue Mar 8, 2024 · 2 comments

Comments

@susatyo441
Copy link

Issue

How can I hide the password? is there any way to not select the password using typescript-sequelize?

Versions

  • sequelize: ^6.37.1
  • sequelize-typescript: 2.1.6
  • typescript: ^5.1.3

Issue type

  • bug report
  • [ x] feature request

Actual behavior

Expected behavior

Steps to reproduce

Related code

// src/models/user.model.ts

import { Table, Column, Model } from 'sequelize-typescript';
import { Exclude } from 'class-transformer';
import { IsNotEmpty } from 'class-validator';
import { IsEmail } from 'class-validator';

@Table
export class User extends Model {
  @Column
  @IsNotEmpty()
  name: string;

  @Column
  @IsEmail()
  email: string;

  @Column
  @Exclude({ toPlainOnly: true })
  password: string;

  @Column({
    type: 'ENUM',
    values: ['l', 'p'],
  })
  gender: string;
}
@knguyen0125
Copy link

I override toJSON

  toJSON() {
    const json = super.toJSON();
    // Make sure that the password is not included in the response
    delete json.password;
    return json;
  }

@NenadJovicic
Copy link

You can also use scopes for this

@Table
@DefaultScope(() => {
  exclude: ['password']
})
export class User extends Model {...}

And then, for cases where you need it, you can create a different @Scope()
you can find more here https://github.com/sequelize/sequelize-typescript?tab=readme-ov-file#scopes

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

No branches or pull requests

3 participants