"Upsert" a document
Problem
You need to create a document in a collection within the current database, but if it already exists the document needs to be updated. This is traditionally called an "upsert" operation.
Solution
Fauna doesn’t provide an Upsert
function, but you can create one:
Once the UDF has been created, you can then "upsert" a document:
ObjectV(ref: RefV(id = "20211021", collection = RefV(id = "posts", collection = RefV(id = "collections"))),ts: LongV(1634841128670000),data: ObjectV(title: StringV(My October Post),body: StringV(Lorem ipsum...),author: StringV(me)))
map[data:map[author:me body:Lorem ipsum... title:My October Post] ref:{20211021 0xc000109d10 0xc000109d10 <nil>} ts:1634841132390000]
{ref: ref(id = "20211021", collection = ref(id = "posts", collection = ref(id = "collections"))), ts: 1634841145290000, data: {title: "My October Post", body: "Lorem ipsum...", author: "me"}}
{
ref: Ref(Collection("posts"), "20211021"),
ts: 1634841148190000,
data: { title: 'My October post', body: 'Lorem ipsum...', author: 'me' }
}
{'ref': Ref(id=20211021, collection=Ref(id=posts, collection=Ref(id=collections))), 'ts': 1634841150480000, 'data': {'title': 'My October post', 'body': 'Lorem ipsum...', 'author': 'me'}}
{
ref: Ref(Collection("posts"), "20211021"),
ts: 1634841198150000,
data: { title: 'My October post', body: 'Lorem ipsum...', author: 'me' }
}
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!