Coming from years of experience with ORM’s in the javaee world, where it is almost an abomination even to think of directly updating a database table/colllection without at least 3 layers of abstraction in between, I had found Meteor (node, javascript, whatever, I’m not pointing fingers) very liberating when I first met it.
As we now progress from the magical to more structured programming with components, imports, classes, schemas and whatnot, I keep needing to revisit how I am manipulating my data.
On the one hand, we can do
Things.update(thingId, {
$set: { foo: "bar" },
$unset: { baz: "" },
$inc: { qux: 1 },
$push: { quux: "corge" }
})
which is very straightforward, does not require us to grab the document and deal with updates to the other fields in the time we fire off our own update statement.
On the other hand we can do (perhaps Astronomy is our only viable choice here to manage this properly, but the code below is just pseudo, not astronomy)
Thing = Things.findOne(thingId) // Thing is an instance of a class
Thing.set("foo", "bar")
Thing.save() // a method that the ORM provides us where it does validations followed by a collection update
This second approach is harder to manage, feels like more boilerplate code, but it has one great advantage of providing you with a complete snapshot of the document, thus allowing you to do better validation and other orm-wise important data manipulation/calculation.
But there is a small problem with this, where the data on the client and server may be different (we may have trusted data on the server, or some server-only secret data) so there may be some subtleties involved.
Anyway both approaches of course have more pros and cons than the few I mentioned here.
So what do you guys prefer?
Mind you this is not a simple schema (collection2) vs astronomy question! Whatever you use for the implementation, I wonder what your strategy is.
Now I know this is not an either/or topic, but please vote for the one you use the most, even though you occasionally prefer the other (but I tried to provide the gray area options anyway):
- Pull up the doc, update props of the doc, overwrite it back as a whole
- Update props directly (set, push, inc etc)
- I use both depending on usecase (please comment below to elaborate)
- I’m torn in between and would like some best practice to emerge
0 voters
Of course I’d also be very happy if you can also elaborate by your comments.
PS: There may be an argument that there should be other options provided in the poll, like event sourcing / cqrs but let’s add that option if more than a few people object to its exlusion.
PPS: Some may argue even collection2 takes the orm-like approach with autoform, because especially arrays are hard to deal with, without knowing the full document - at least the array itself as a whole, but that’s a specific case, so does not demonstrate its overall strategy.