vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

Handling of ObjectId properties #183

Closed henit closed 7 years ago

henit commented 7 years ago

Hi. I think Mongorito looks very nice, and consider using it. Two questions:

  1. In normal direct queries to a database (using the mongodb-driver), properties saved as ObjectId has to be cast to ObjectId. Like if I run category.find({ parentCategory: 'abcdef' }) it does not find any documents, I have to convert the id-string before passing it as a query condition. Is this the same with Mongorito, or is there a way to avoid this?

  2. And, is it with Mongorito possible to get all ObjectId properties on documents returned by find operations etc converted to strings so I don't have to do this manually? And the same for converting back before insert/update operations.

vadimdemedes commented 7 years ago
  1. Unfortunately, yes, you need to cast your value to ObjectId if you want to get the correct results. Previous iteration of Mongorito had automatic casting to ObjectId, but people were opening issues about it not being compatible with their custom ids (e.g. in Meteor).

  2. I imagine this could be possible using a custom middleware, but it would be a risky one, since it might affect Mongorito's internals.

henit commented 7 years ago

Thanks. In them name of functional programming, I could just wrap the props in a reusable convertion function before passing it to the db-functions. Should work in most cases so I don't have to repeat the casting back and forward all the time, since most of the project code works by having reference properties as strings.

henit commented 7 years ago

@vadimdemedes Btw, about the compatibility issue you mentioned.. Could that have been solved with an optional feature? Like enabling an option that triggers automatic casting, so people that need to cast manually can just skip enabling autocast, and the rest of us can avoid repeated casting syntaces all over our code...

vadimdemedes commented 7 years ago

I think having it as an external plugin would be better. I'll add it to my todo list.