nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
530 stars 118 forks source link

Timestamps createdAt and updatedAt missing from document type after update #2271

Closed copilotwwai closed 1 month ago

copilotwwai commented 1 month ago

Is there an existing issue for this?

Current behavior

I'm experiencing an issue where the createdAt and updatedAt fields are not accessible on a HydratedDocument type in my NestJS + Mongoose application, even after updating to the latest versions of @nestjs/mongoose and mongoose. The fields createdAt and updatedAt are still not recognized by TypeScript on the document type, despite the fact that the schema is defined with timestamps: true. Here is the error I get when trying to access createdAt:

Property 'createdAt' does not exist on type 'Document<unknown, {}, Package> & Package & { _id: ObjectId; } & { __v?: number; }'.

Minimum reproduction code

https://github.com/nestjs/mongoose

Steps to reproduce

1- I updated to the latest versions of @nestjs/mongoose and mongoose using:

npm install @nestjs/mongoose mongoose

2- Confirmed that timestamps: true is enabled in the schema. 3- Restarted the TypeScript server and cleared cache to ensure no cached types were used.

import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

@Schema({ timestamps: true })
export class Package {
  // Other fields go here
}

export type PackageDocument = HydratedDocument<Package>;
export const PackageSchema = SchemaFactory.createForClass(Package);

When I try to access createdAt or updatedAt:

const package: PackageDocument = await this.packageModel.findById(id);
console.log(package.createdAt); // Property 'createdAt' does not exist

Expected behavior

I believe this should work automatically as per the changes introduced in PR #2236, where the createdAt and updatedAt properties should be inferred based on the timestamps: true option in the schema. However, these fields are still missing from the document type in my TypeScript code.

Additional Information: No manual extension or declaration of createdAt and updatedAt should be required according to the latest update. I would appreciate any clarification on whether this issue persists and what the recommended workaround would be in the meantime.

Package version

10.0.10

mongoose version

8.7.0

NestJS version

10.2.6

Node.js version

18.18.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 month ago

Including timestamp: true doesn't change your TS schema - you need to declare these properties yourself, otherwise you'll get complication errors.

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.