-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
How to properly define createdAt and updatedAt members when using timestamps? #13897
Comments
could you try making interface IDocument {
name: string;
createdAt?: Date;
updatedAt?: Date;
} |
Thanks for mentioning it. This is one of the choices that I considered although I didn't initially mention it in the opening text. The approach works but it is "semantically wrong" because in the document object returned by the database these properties are not really optional. Of course one could theorize that specifying In short, this approach is a "circumvention". I've edited my opening post to include this. |
The easiest workaround would be to use
Admittedly that does involve a bit of friction because you need to add that generic every time you instantiate a @hasezoey @AbdelrahmanHafez what do you think of adding |
i dont know why it was changed, but it should likely be OK. once there is a PR i will try to see if there are any issues with typegoose
i cant quite tell what the commit changed to remove this, because at least from what i can tell, it didnt use / remove |
I think using |
Thanks for the ideas. The It seems there's a some kind of a building block missing here but I can't really identify it. It should be possible to say that documents created in code have properties A, B and C while documents returned by find(...) etc. have A, B, C, D and E. I did try the following code which passes syntax check but I haven't tried running it so I don't know if it actually works or not. The idea behind it was to "separate" the type definition of documents that have not been saved from the type definition of documents returned by
|
types(models): make all properties to Model constructor optional
Prerequisites
Mongoose version
6.5.2
Node.js version
18.12.1
MongoDB version
6.0.5
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
11
Issue
I know this has been asked previously but reviewing the various other issues I could not find a suitable solution.
In the https://mongoosejs.com/docs/typescript.html the steps to create interfaces and schemas is pretty straightforward. But things seem to break down when
timestamps: true
is being used.A simplified example below:
Attempting to fix this at the interface level does not work:
No matter what I've tried (methods & overrides, InferSchemaType etc.) they all seem to fall flat on their face in either one of these problems:
new Document<IDocument>(...)
will not compile as the 'createdAt' and 'updatedAt' have not been givenconsole.log(hydratedDocument.createdAt)
does not compile because the type does not have the propertyThe following circumventions I know:
IDocument
part from thenew Document(...)
; this kind of defeats the purpose of type-safetySchemaTimestampConfigs
; this requires explicitly typing whatever is returned fromDocument.find()
intoDocumentDoc | null
. It works but it is a bit unwieldy, and it doesn't work at all if the timestamps are at sub-document levelcreatedAt
andupdatedAt
optional; this works but it is semantically wrong. See the answer below for my view on why.Are there any other options I should or could try? What is the "best" way that results in clean, readable and structurally solid code?
The text was updated successfully, but these errors were encountered: