You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{"cat.instance.name": 'Cosmonaut'}undefined/* The cat can't talk anymore. */
Question
Why flatten any non-Object-prototype objects? I.e. why not only objects that satisfy Object.getPrototypeOf(obj) === Object.prototype? Is there a use-case for flattening custom (prototyped) objects?
My use case
I was happily using this convenient library with MongoDB queries, to flatten this for example (oversimplified):
functionupdateExistingObject({ $set }: {$set: DbObject}){collection.updateOne({_id: ID},{$set: flatten($set)});}updateExistingObject({$set: {name: 'Cosmonaut',props: {related: newObjectID("catcosmonaut"),// the problem is thiscanMeow: true,// ...}}});
However, soon I realized that Mongo's ObjectID also gets flattened in a very weird way:
By not introducing a breaking change: add one more option like pureObjectsOnly. But this IMO is weird with the presence of the safe option.
flatten(cat,{pureObjectsOnly: true,});
Breaking change: make safe also not transforming non-"pure" objects, as well as arrays (this is enough for my case, as I never update arrays like this in MongoDB).
Request for comments
I would be happy to know any opinions on this and contribute. Thanks!
The text was updated successfully, but these errors were encountered:
But on a serious note: Came here to look for a solution for exactly @ZitRos 's problem. I was happily flattening objects before throwing them into mongo/mongoose updates until I stumbled upon this behaviour.
Hello! Thanks for the great and popular library.
This is a kind of library design-level question.
The code:
Expectation
Actual result
Question
Why flatten any non-Object-prototype objects? I.e. why not only objects that satisfy
Object.getPrototypeOf(obj) === Object.prototype
? Is there a use-case for flattening custom (prototyped) objects?My use case
I was happily using this convenient library with MongoDB queries, to flatten this for example (oversimplified):
However, soon I realized that Mongo's
ObjectID
also gets flattened in a very weird way:This breaks MongoDB's
updateOne
.Suggestions
pureObjectsOnly
. But this IMO is weird with the presence of thesafe
option.safe
also not transforming non-"pure" objects, as well as arrays (this is enough for my case, as I never update arrays like this in MongoDB).Request for comments
I would be happy to know any opinions on this and contribute. Thanks!
The text was updated successfully, but these errors were encountered: