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

Typescript schema doesn't have _id field. #14286

Closed
2 tasks done
abarriel opened this issue Jan 24, 2024 · 3 comments
Closed
2 tasks done

Typescript schema doesn't have _id field. #14286

abarriel opened this issue Jan 24, 2024 · 3 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@abarriel
Copy link
Contributor

abarriel commented Jan 24, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.0.4

Node.js version

16

MongoDB server version

6.0.6

Typescript version (if applicable)

5.2.2

Description

Hello,

I think the SchemaType of a schema should have an _id field by default

For e.g, we have createdAt, and updatedAt field in the type when we set the option { timestamps: true }.

Which is not the case when we set options: { _id: true }

Steps to Reproduce

import mongoose, { type InferSchemaType, Schema } from "mongoose";

const eventSchema = new mongoose.Schema({ url: String }, { timestamps: true });
const Event = mongoose.model("Event", eventSchema);

type EventDoc = InferSchemaType<typeof eventSchema>;

async function run() {
  const event = {} as EventDoc;

  // createdAt exists because timestamps: true, which is totally OK
  event.createdAt = new Date();

  //  WRONG: _id does not exist on type EventDoc, this should be set, since options._id is to true by default
  event._id; // ERROR
}

run();

Expected Behavior

No response

@IslandRhythms IslandRhythms added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue typescript Types or Types-test related issue / Pull Request and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 25, 2024
@IslandRhythms
Copy link
Collaborator

import mongoose, { type InferSchemaType, Schema } from "mongoose";

const eventSchema = new mongoose.Schema({ url: String }, { timestamps: true });
const Event = mongoose.model("Event", eventSchema);

type EventDoc = InferSchemaType<typeof eventSchema>;

async function run() {
  const event = {} as EventDoc;

  // createdAt exists because timestamps: true, which is totally OK
  event.createdAt = new Date();

  //  WRONG: _id does not exist on type EventDoc, this should be set, since options._id is to true by default
  event._id; // ERROR
}

run();

@vkarpov15 vkarpov15 added this to the 8.1.2 milestone Feb 5, 2024
@vkarpov15
Copy link
Collaborator

I agree this is a bit confusing, however currently the issue is that InferSchemaType doesn't return the full hydrated doc type. You still need to pass InferSchemaType through HydratedDocument<>. The easiest way to infer the full hydrated document type currently is the following:

type EventDoc = ReturnType<(typeof Event)['hydrate']>; //InferSchemaType<typeof eventSchema>;

@vkarpov15 vkarpov15 modified the milestones: 8.1.2, 8.1.3 Feb 8, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.1.3, 8.1.4 Feb 15, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.1, 8.2.2 Mar 4, 2024
@vkarpov15
Copy link
Collaborator

I think we'll have to make some docs changes to account for this, adding _id to the return value of InferSchemaType<> tends to cause circular dependency errors and other issues. I'll patch up the docs to make this more clear. There's a couple of places, like here, where we imply that InferSchemaType returns a hydrated document type.

@vkarpov15 vkarpov15 modified the milestones: 8.2.2, 8.2.3, 8.2.4 Mar 15, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.4, 8.2.5 Mar 28, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.3.1, 8.3.2, 8.3.3 Apr 5, 2024
vkarpov15 added a commit that referenced this issue Apr 27, 2024
vkarpov15 added a commit that referenced this issue Apr 29, 2024
docs: de-emphasize `InferSchemaType<>` in TypeScript docs in favor of automatic inference
@vkarpov15 vkarpov15 modified the milestones: 8.3.3, 8.3.4 Apr 29, 2024
@vkarpov15 vkarpov15 removed this from the 8.3.4 milestone May 5, 2024
@vkarpov15 vkarpov15 added this to the 8.3.3 milestone May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants