feat(core): add em.upsert()
method
#3525
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We can use
em.upsert()
create or update the entity, based on whether it is already present in the database. This method performs aninsert on conflict merge
query ensuring the database is in sync, returning a managed entity instance. The method accepts eitherentityName
together with the entitydata
, or just entity instance.The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where
Author.email
is a unique property:Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the
data
.If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit
flush
will be required for those changes to be persisted.Closes #3515