Skip to content
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

[help] InferSchemaType, accessing _id field #11967

Closed
1 task done
philiparvidsson opened this issue Jun 21, 2022 · 7 comments
Closed
1 task done

[help] InferSchemaType, accessing _id field #11967

philiparvidsson opened this issue Jun 21, 2022 · 7 comments
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@philiparvidsson
Copy link

philiparvidsson commented Jun 21, 2022

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.4.0

Node.js version

18.3

MongoDB version

Unsure

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

arch-5.18.5 something

Issue

Using InferSchemaType to create the schema interface type, how do I access the _id field? If I put it in the schema definition, I have to also set it explicitly when inserting new documents. If I don't put it there, InferSchemaType does not include it in the interface.

What do I do? I'd like to know the same for .save() and so on. Am I supposed to do a type union?

Code:

import { Document, InferSchemaType, Schema } from "mongoose"

const mySchema = new Schema({
  foo: { type: String, required: true }
})

type MySchemaType_Problematic = InferSchemaType<typeof mySchema>
// MySchemaType_Problematic is missing the _id field along with the save() method etc.

// Workaround:
type MySchemaType_Workaround = InferSchemaType<typeof mySchema> & Document
// MySchemaType_Workaround can now be used properly, with _id and save() etc.
@philiparvidsson philiparvidsson added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jun 21, 2022
@mohammad0-0ahmad
Copy link
Contributor

@philiparvidsson Thanks for providing this issue.
I would like to say that you can use type union until we can make better integration for _id path.
_id field is quite a special case that, it will take more efforts to improve its typing along entire mongoose.
I am aware of this issue and promise to provide better mechanism for InferSchemaType as soon as possible for all special paths like _id.
Best regards.

@vkarpov15
Copy link
Collaborator

@philiparvidsson can you please provide some code samples? That would be helpful for me to understand what you're trying to do.

@philiparvidsson
Copy link
Author

I added some example code, providing a brief example of the issue as well as the workaround I'm using.

@mohammad0-0ahmad
Copy link
Contributor

@philiparvidsson InferSchemaType meant to be a helper to infer schema defenition type.
So It will not return a document function but it will obtain paths type.
As I descriped _id property it is not added yet because there it a default path in the schema.
Do you need to use these types within mongoose code or you obtain the type of schema for another usecase?

Can you please provide more details to be able to help maybe?

@philiparvidsson
Copy link
Author

In this particular case, I'm passing a MySchemaType_Problematic to a factory function. If the factory cannot generate the needed type, it needs to log that it did not understand the data it received. So I tried to do: log.error(Factory was unable to create instance for {mySchemaType._id})

But it doesn't work since the type doesn't have an id. I guess I could cast it to any because I know for a fact that _id is there, but that defeats the purpose of having any type on it to begin with.

@philiparvidsson
Copy link
Author

Another thing that I suspsect is a bug: Setting the type of a field (in the schema) to mongoose.Schema.Types.ObjectId gives the wrong type in the inferred type. E.g.,

const mySchema = ...({ _id: mongoose.Schema.Types.ObjectId })
type MyType = InferSchemaType

const a: MyType = { _id: new mongoose.Types.ObjectId("...") }

Fails, because it is expecting to be assigned a mongoose.schema.Types.ObjectId (which is the "metatype" here, so there is no way I can use that type for assigning anything).

TL;DR: mongoose.Schema.Types.ObjectId should be inferred to mongoose.Types.ObjectId for fields

@vkarpov15 vkarpov15 modified the milestones: 6.4.1, 6.4.2, 6.4.3 Jun 25, 2022
@vkarpov15
Copy link
Collaborator

From OP, I'd recommend doing:

type MySchemaType_Workaround = HydratedDocument<InferSchemaType<typeof mySchema>>

For "TL;DR: mongoose.Schema.Types.ObjectId should be inferred to mongoose.Types.ObjectId for fields", this was fixed in 6.4.2 with #12007.

Those two issues are fixed, and there aren't enough code samples for this to be actionable. So I'm going to close this. Please re-open with a complete code sample that we can run to demonstrate the issue you're seeing, if you're still having issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants