-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sure we're following Mongoose Typescript best practices #221
Comments
Yes, I think this pretty much captures it. Any time that we are pulling data out of MongoDB or initialize a model class (e.g
The part that I'm admittedly uncertain of is if the
By
I'm assuming that you're talking about this blurb:
I think we're safe here, because we don't explicitely specify document interfaces for our schemas. Instead, we use
Yes, I also haven't seen any nested paths in the system.
I find the Mongoose TS docs to be not great. They discuss how to be super hands-on with the type system (eg manually creating interfaces for each schema, passing those to |
There are a bunch of things I don't understand about how Typescript works with Mongoose, but we should read up the best practices and be sure we're implementing it correctly. Here are a few points of confusion:
HydratedDocument<>
generic? From what I can tell from the docs, HydratedDocument provides the document properties as defined in the Schema/document type, as well as all of the standard Mongoose properties/methods. So my guess is any time we are using a Mongoose method likeSomeDocument.save()
we'll need to be dealing with aHydratedDocument
type. If we're just returning the document without using any of it's methods we probably don't need to useHydratedDocument
, but it probably wouldn't hurt, either. We obviously use.save()
in lots of places, but I'm not sure what other Mongoose document methods we are using and where, but it would be worth auditing all of the model files to make sure we have fullHydratedDocument
coverage. Maybe we should just use it everywhere we create new documents or fetch them from the MongoDB, even if we're not using those methods?_id
property in a Mongoose Schema and instead rely on Mongoose to add one of typeObjectID
for us (e.g. like we do here here), doesmongoose.InferRawDocType<SomeSchema>
know about the_id
field, or do we have to explicitly set it in the schema? (b) Are we usingSchema.Types.ObjectId
in our schema definitions (as described in here) correctly? I know we use a lot of string_ids
, for both good reasons and bad, but I did a search through the code forSchema.Types.ObjectId
and only found one instance of it. Is it possible that we only ever explicitly set an id of typeObjectId
once in the whole code base?Image.objects
), we then callsave()
on theImage
rather than theImage.object
. If that is 100% the case this might not matter to us, but according to the docs, you need do some work to make sure that the returned, hydrated subdocument types have all the regular mongoose document properties and methods.The text was updated successfully, but these errors were encountered: