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

537
Visualizações
How to update an entity with relations using QueryBuilder in TypeORM

I have UserEntity and AddressEntity, they are related as OneToOne, that's one user may have only one address. UserEntity has fields firstName, secondName, address. AddressEntity has fields country and city.

If I wanted to update UserEntity without doing it to its relations I would do this:

      await entityManager.getRepository(UserEntity)
                         .createQueryBuilder('users')
                         .update(UserEntity)
                         .set(updateUserObject)
                         .where('users.id = :userId', { userId })
                         .execute();

where updateUserObject is formed from a request body. That's to say, if I need to update firstName, the object would look like this: { firstName: 'Joe' }. Now what is unclear is how to use that builder if I have the following updateUserObject:

{
    firstName: "Bob",
    address: {
        "city": "Ottawa"
    }
}

The official documentation does not address such cases.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can achieve this using preload and save methods.

Update your UserEntity similar to below:

@Entity('user')
export class UserEntity {
  ...

  @OneToOne(
    () => AddressEntity,
    {
      // Make sure that when you delete or update a user, it will affect the
      // corresponding `AddressEntity`
      cascade: true,
      // Make sure when you use `preload`, `AddressEntity` of the user will also
      // return (This means whenever you use any kind of `find` operations on
      // `UserEntity`, it would load this entity as well)
      eager: true
    }
  )
  @JoinColumn()
  address: AddressEntity;
}

Now using entityManager, you can update all the fields that you want using the following way:

const partialUserEntity = {
    id: userId,
    firstName: "Bob",
    address: {
        "city": "Ottawa"
    }
};

const userRepository = await entityManager.getRepository(UserEntity);

// Here we load the current user entity value from the database and replace
// all the related values from `partialUserEntity`
const updatedUserEntity = userRepository.preload(partialUserEntity);

// Here we update (create if not exists) `updatedUserEntity` to the database
await userRepository.save(updatedUserEntity);

However, you need to make sure that your UserEntity has an AddressEntity associated always. Otherwise, you will have to generate an id for AddressEntity like below before you execute save method.

/* 
 * If `updatedUserEntity.address.id` is `undefined`
 */

// `generateIDForAddress` is a function which would return an `id`
const generatedIDForAddress = generateIDForAddress();
const partialUserEntity = {
    id: userId,
    firstName: "Bob",
    address: {
        "id": generatedIDForAddress,
        "city": "Ottawa"
    }
};

Please note that under the hood, typeorm will run UPDATE statements separately for UserEntity and AddressEntity. This is just an encapsulation of multiple join statements (when executing preload method) and update statements (when executing save method) such that the developer can easily implement this scenario.

Hope this helps you. Cheers 🍻!

over 4 years ago · Santiago Trujillo 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