VulcanJS / vulcan-npm

The full-stack JavaScript App Framework
https://vulcan-docs.vercel.app
MIT License
31 stars 8 forks source link

Investigate using string ids instead of ObjectId in Mongoose connector #63

Closed eric-burel closed 2 years ago

eric-burel commented 3 years ago

Is your feature request related to a problem? Please describe.

Meteor uses string ids as a default: https://docs.meteor.com/api/collections.html

idGeneration String
The method of generating the _id fields of new documents in this collection. Possible values:

'STRING': random strings
'MONGO': random Mongo.ObjectID values

But Mongo uses Mongo.ObjectID.

So in Vulcan NPM:

This leads to issues with isomorphic methods like Users.owns

Describe the solution you'd like Use string ids as a default? Or converting?

Describe alternatives you've considered

Altering our mongoose connector?

We may want to alter the connector so it uses string ids. I think the best way is to do this on creation, but it could be safer to do it during find. Connectors are expected to return the same type whatever the technology, so we can expect Mongoose connector to return string _id.

The problem arise in custom resolvers, if user want to call Mongo methods, they may accidentally return an ObjectId. Maybe TypeScript can catch this already?

=> this might be necessary anyway at least for "find" and "findOne", for typing consistency, connectors should return a VulcanDocument so a string _id

Handling the possibility of the _id to be an ObjectId in all functions?

Simplest approach might be to introduce a compareIds method that uses useEqual under the hookd, and use it whenever needed + improve VulcanDocument typing so it can be either string or ObjectId (but beware of not introducing dependency to mongo).

This is a bit annoying client-side though, where _id is guaranteed to be a string.

Configuring mongo to use strings as a default, at top level?

The problem is that it alters mongoose default behaviour. The good part is that it will guarantee string ids everywhere even for custom calls.