Is it possible to 'protect' a property and exclude it from select statements

Highmastdon picture Highmastdon · May 1, 2017 · Viewed 9.4k times · Source

I'd like to protect certain properties on the data-layer level. For example I'd like to protect the password hash I store in the database for a user, so that it doesn't show up in arbitrary select-statements.

This way only when it's explicitly requested in a select property, property2 statement.

Answer

nomadoda picture nomadoda · Mar 14, 2019

I think a more accurate answer would be to set select: false on column options:

@Column({ select: false })
password: string;

And explicitly select the column like this:

const user = await getRepository(User)
    .createQueryBuilder()
    .addSelect('password')
    .getOne()