I am storing data returned from an external api in our db. The data will consist of new and updated records. I need to insert the new documents and update the existing documents. Since the external documents won’t have a ref value, I need to match on the included _id
parameter.
In preparation for this I created a unique index on the collection.data.id field
. I’m hoping I can get a sanity check on my upsert query below. While it works, I find it to be incredibly verbose and it’s giving me PTSD flashes from old node style call back hell and react render props.
q.Map(
documents,
q.Lambda(
'doc',
q.If(
q.Exists(q.Match(q.Index(indexName), q.Select(['id'], q.Var('doc')))),
q.Update(
q.Select(
'ref',
q.Get(
q.Match(
q.Index(indexName),
q.Select(['id'], q.Var('doc'))
)
)
),
{ data: q.Var('doc') }),
q.Create(q.Collection('Collection'), { data: q.Var('doc') }),
),
),
),
I can tolerate the first and third expressions of the If
but the Update
is brutal. Is there a more concise way to accomplish the Update
?
I tried the same Match
used in the Exists
statement, but it threw an error saying it expected a reference. Please tell me there is a better way.
I come from Mongo and while I like the premise of Fauna, in practice I’m finding the interface very unapproachable. I’m trying very hard to not be set in my ways, but this took me almost 2 full working days to accomplish, thanks largely to this:
Here’s the equivalent operation in Momgo (minus the map):
db.Collection.findAndModify({
query: { id: doc.id },
update: doc,
upsert: true
});