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.
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 = await 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 🍻!