How soon will the upsert command be implemented in Meteor?
And, what is the best way to do the same thing in the mean time?
Something like this is what I'm doing at the moment:
if typeof ( item = Items.findOne({title:'Foo'}) ) == 'undefined'
item = Items.insert({title:'Foo'})
else
Items.update(item._id, {$set: {title:'Foo'}})
# do something with item
How soon will the upsert command be implemented in Meteor?
UPDATE: @Thomas4019 points out that upsert
is now supported:
v0.6.6
"Add upsert support. Collection.update now supports the {upsert: true} option. Additionally, add a Collection.upsert method which returns the newly inserted object id if applicable."
Source: History.md
Usage documentation: http://docs.meteor.com/#upsert
-- original answer follows --
There is a card on the Trello Meteor Roadmap which you can vote on to indicate its importance to you: https://trello.com/c/C91INHmj
It is currently in the "Later" list which means it will be a while before it is implemented unless it receives a lot of votes.
The other important thing to note is that since meteor is open-source, you could implement the necessary changes yourself and submit back.
What is the best way to do the same thing in the mean time?
There are several solutions but which is most appropriate for your use-case is impossible to tell without more knowledge of your design.
Use the code as is, add an unique index to the collection, and handle the duplicate key error if/when it arises
Change design to implement explicit optimistic concurrency.
The core of both of these solutions is the same, gracefully handle the error case. #1 is easier to implement. #2 allows for greater flexibility in how the optimistic concurrency is handled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With