Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Handling of ObjectId properties #183

Closed
henit opened this issue Jun 16, 2017 · 4 comments
Closed

Handling of ObjectId properties #183

henit opened this issue Jun 16, 2017 · 4 comments
Labels

Comments

@henit
Copy link

henit commented Jun 16, 2017

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
Copy link
Owner

  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
Copy link
Author

henit commented Jun 27, 2017

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 henit closed this as completed Jun 27, 2017
@henit
Copy link
Author

henit commented Jun 29, 2017

@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
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants