Merge vs Upsert

#1

Based on the documentation on Persistable it says that:

merge creates object if it does not and merges if it does
upsert creates object if it does not and updates if it does

So it seems like that the only difference is update vs merge.

Could you explain the difference between those two actions?
Should we use one vs the other?

0 Likes

#2

The best explanation will be the difference between the documentation for the ‘merge’ function and the ‘upsert’ function on Persistable

0 Likes

#3

I’ve also been wondering for a while what was the difference between merge and upsert.

Answer is that merge preserves fields of the instance that is being updated if they are not specified, while upsert completely replaces the object.

So if you have an object {id: "1", name: "testName", value: "testValue"} :

MyType.make({id: "1", name: "newName"}).merge()

Will modify the object so that MyType.get("1") returns {id: "1", name: "newName", value: "testValue"}.

MyType.make({id: "1", name: "anotherNewName"}).upsert()

Will modify the object so that MyType.get("1") returns {id: "1", name: "anotherName"} (note that value is gone).

5 Likes

#4

Thanks! What about update?

0 Likes

#5

update():

Updates an instance of a C3 type. If the operation fails an exception will be thrown. This will fail if the instance does not already exist.

vs upsert():

Creates an instance of a C3 type if it doesn’t exist or updates it if it does. If the operation fails an exception will be thrown.

update throws an error if an object with the given id doesn’t exist, while upsert will create the object instead

0 Likes

Difference Between TypeName.make() and TypeName.create