Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
get instance fields from a TypeORM entity class

I have an typeorm entity of the following form:

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    isActive: boolean;
}

Now I am looking to get the instance fields from this entity class into an array, like below:

['id', 'firstName', 'lastName', 'isActive']

How can I achieve that?

I tried the following but with no help:

class Me extends User{
    constructor() {
        super()     
    }
}

const me = new Me()
console.log(Object.keys(me)) // []
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Give initial values in your User class to the properties. They are null now, Object.keys wont list them. Try this:

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number = 0;

    @Column()
    firstName: string = "";

    @Column()
    lastName: string = "";

    @Column()
    isActive: boolean = false;
}

Alternative solution is when you fill the attrs manually:

const user = new User();
user.id = 0;
user.firstName = "";
user.lastName = "";
user.isActive = false;
about 4 years ago · Juan Pablo Isaza Relatório

0

You might be able to do something like this:

const userProps = getConnection()
  .getMetadata(User)
  .ownColumns
  .map(column => column.propertyName);

This means that you obtain metadata from an entity if you take the class function (AKA constructor) and use the .getMetadata method on the DB connection.

Reference:

Get all properties from a entity #1764

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda