Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

535
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!