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

189
Visualizações
Make objection model return match the TS interface

I'm working to implement an api using express/objection js and typescript.

I took a lot of inspiration from this repo: https://github.com/HappyZombies/brackette-alpha/tree/master/server/src

As the creator did, I want to have different interfaces for a same "component" ( eg: user ).

We can see here that he create different interfaces (here) and he uses them in his services as a returned promise (here) but there is no check that what he returned does match his interface. Eg: to match his promised interface here, he will only select the sql field that match his interface

  public async findAll(): Promise<IUserMiminimum[]> {
    let users;
    try {
      users = await this.usersModel.query().column('username', 'displayName');
    } catch (err) {
      this.logger.error(err);
      throw err;
    }
    return users;
  }

My questions are:

  • Is there a way to only select the fields you want from the model results based on the interface description ?
  • Else, is there a way to check the interface match the model results ?

Thanks for reading.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Objection castTo() might help with that

const users= await User.query()
  .column('username', 'displayName')
  .castTo<IUser[]>();
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