Merge
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
merge( object1, object2, [customResolver] )
Merge( object1, object2, [customResolver] )
Description
The Merge
function combines two or more objects into one, by
performing a shallow merge of the fields and values from both objects
into a new object.
The merge starts with a copy of object1
— called result
— and then
merges object2
into result
, evaluating each top-level field in turn.
If object2
is an array of objects to merge, each object within
object2
is merged into result
in turn.
During the merge, each top-level field of the object to be merged into
result
is processed by a resolver function, which determines which
value to use in result
when there is a common field. The default
resolver always decides to use the value from the object to be merged,
overriding the value in result
.
You can provide an optional customResolver
function that overrides the
default resolver, and is used to determine which value to use.
When the resolver returns a null
value, Merge
deletes that
field in result
.
Parameters
Parameter | Type | Definition and Requirements |
---|---|---|
|
The first object to merge with the second object. |
|
|
The second object, or array of objects, to merge with the first
object. If an array of objects is provided, each object in the array
is merged with |
|
|
Optional - A lambda function that replaces the default resolver and decides which value to use during the merge. The
where If |
Returns
An object that is the result of merging object1
with object2
(and
any additional objects that you may have provided).
Examples
-
The following query demonstrates a merge of two objects where there is no key conflict:
ObjectV(a: StringV(Apple),b: StringV(Banana),x: StringV(width),y: StringV(height))
map[a:Apple b:Banana x:width y:height]
{a: "Apple", b: "Banana", x: "width", y: "height"}
{ a: 'Apple', b: 'Banana', x: 'width', y: 'height' }
{'a': 'Apple', 'b': 'Banana', 'x': 'width', 'y': 'height'}
{ a: 'Apple', b: 'Banana', x: 'width', y: 'height' }
-
The following query demonstrates a merge when there is a key conflict and no
resolver
function has been provided:ObjectV(f: StringV(Fauna))
map[f:Fauna]
{f: "Fauna"}
{ f: 'Fauna' }
{'f': 'Fauna'}
{ f: 'Fauna' }
-
The following query demonstrates that the default resolver performs a shallow merge:
ObjectV(data: ObjectV(name: StringV(Fauna),extra: StringV(two)))
map[data:map[extra:two name:Fauna]]
{data: {name: "Fauna", extra: "two"}}
{ data: { name: 'Fauna', extra: 'two' } }
{'data': {'name': 'Fauna', 'extra': 'two'}}
{ data: { name: 'Fauna', extra: 'two' } }
This happens because only the
data
field is evaluated, since it is the only top-level field in each object to be merged, and the default resolver simply uses the value fordata
inobject2
for the result. -
The following query demonstrates a merge when there is a key conflict and a custom
resolver
function has been provided:ObjectV(c: StringV(Compare),d: StringV(Delta))
map[c:Compare d:Delta]
{c: "Compare", d: "Delta"}
{ c: 'Compare', d: 'Delta' }
{'c': 'Compare', 'd': 'Delta'}
{ c: 'Compare', d: 'Delta' }
-
The following query demonstrates a merge when an array of objects is provided:
ObjectV(t: StringV(turkey),a: StringV(Apple),b: StringV(Banana),c: StringV(Correlate),d: StringV(disparity))
map[a:Apple b:Banana c:Correlate d:disparity t:turkey]
{t: "turkey", a: "Apple", b: "Banana", c: "Correlate", d: "disparity"}
{ t: 'turkey', a: 'Apple', b: 'Banana', c: 'Correlate', d: 'disparity' }
{'t': 'turkey', 'a': 'Apple', 'b': 'Banana', 'c': 'Correlate', 'd': 'disparity'}
{ t: 'turkey', a: 'Apple', b: 'Banana', c: 'Correlate', d: 'disparity' }
-
The following query demonstrates a merge with a document:
ObjectV(ref: RefV(id = "122", collection = RefV(id = "Letters", collection = RefV(id = "collections"))),ts: LongV(1603756505540000),data: ObjectV(letter: StringV(V),extra: StringV(22nd)),x: LongV(10))
map[data:map[extra:22nd letter:V] ref:{122 0xc00013a2a0 0xc00013a2a0 <nil>} ts:1603747233950000 x:10]
{ref: ref(id = "122", collection = ref(id = "Letters", collection = ref(id = "collections"))), ts: 1566580631370000, data: {letter: "V", extra: "22nd"}, x: 10}
{ ref: Ref(Collection("Letters"), "122"), ts: 1566580631370000, data: { letter: 'V', extra: '22nd' }, x: 10 }
{'ref': Ref(id=122, collection=Ref(id=Letters, collection=Ref(id=collections))), 'ts': 1566580631370000, 'data': {'letter': 'V', 'extra': '22nd'}, 'x': 10}
{ ref: Ref(Collection("Letters"), "122"), ts: 1624310468410000, data: { letter: 'V', extra: '22nd' }, x: 10 }
-
The following query demonstrates a merge with the
data
field of a document:ObjectV(letter: StringV(V),extra: StringV(22nd),x: LongV(10))
map[extra:22nd letter:V x:10]
{letter: "V", extra: "22nd", x: 10}
{ letter: 'V', extra: '22nd', x: 10 }
{'letter': 'V', 'extra': '22nd', 'x': 10}
{ letter: 'V', extra: '22nd', x: 10 }
-
The following query demonstrates a merge situation that provides no benefit:
ObjectV()
map[]
{}
{}
{}
{}
The
customResolver
always chooses the value fromobject1
. Since there are no fields inobject1
, the value returned fromcustomResolver
is alwaysnull
. Thenull
return value means that all of the fields inobject2
are removed fromobject1
, which results in an empty object.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!