You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mongoose supports applying ES6 classes to a schema using [`schema.loadClass()`](../api/schema.html#Schema.prototype.loadClass()) as an alternative to defining statics and methods in your schema.
87
+
When using TypeScript, there are a few important typing details to understand.
88
+
89
+
### Basic Usage
90
+
91
+
`loadClass()` copies static methods, instance methods, and ES getters/setters from the class onto the schema.
92
+
93
+
```ts
94
+
classMyClass {
95
+
myMethod() {
96
+
return42;
97
+
}
98
+
99
+
static myStatic() {
100
+
return42;
101
+
}
102
+
103
+
get myVirtual() {
104
+
return42;
105
+
}
106
+
}
107
+
108
+
const schema =newSchema({ property1: String });
109
+
schema.loadClass(MyClass);
110
+
```
111
+
112
+
Mongoose does not automatically update TypeScript types for class members. To get full type support, you must manually define types using Mongoose's [Model](../api/model.html) and [HydratedDocument](../typescript.html) generics.
113
+
114
+
```ts
115
+
// 1. Define an interface for the raw document data
116
+
interfaceRawDocType {
117
+
property1:string;
118
+
}
119
+
120
+
// 2. Define the Model type
121
+
// This includes the raw data, query helpers, instance methods, virtuals, and statics.
You can annotate `this` in methods to enable full safety, using the [Model](../api/model.html) and [HydratedDocument](../typescript.html) types you defined.
153
+
Note that this must be done for **each method individually**; it is not possible to set a `this` type for the entire class at once.
154
+
155
+
```ts
156
+
classMyClass {
157
+
// Instance method typed with correct `this` type
158
+
myMethod(this:MyCombinedDocument) {
159
+
returnthis.property1;
160
+
}
161
+
162
+
// Static method typed with correct `this` type
163
+
static myStatic(this:MyCombinedModel) {
164
+
return42;
165
+
}
166
+
}
167
+
```
168
+
169
+
### Getters / Setters Limitation
170
+
171
+
TypeScript currently does **not** allow `this` parameters on getters/setters:
172
+
173
+
```ts
174
+
classMyClass {
175
+
// error TS2784: 'this' parameters are not allowed in getters
176
+
get myVirtual(this:MyCombinedDocument) {
177
+
returnthis.property1;
178
+
}
179
+
}
180
+
```
181
+
182
+
This is a TypeScript limitation. See: [TypeScript issue #52923](https://github.com/microsoft/TypeScript/issues/52923)
183
+
184
+
As a workaround, you can cast `this` to the document type inside your getter:
`loadClass()` is useful for defining methods and statics in classes.
249
+
If you have a strong preference for classes, you can use `loadClass()`; however, we recommend defining `statics` and `methods` in schema options as described in the first section.
250
+
251
+
The major downside of `loadClass()` in TypeScript is that it requires manual TypeScript types.
252
+
If you want better type inference, you can use schema options [`methods`](../guide.html#methods) and [`statics`](../guide.html#statics).
Mongoose 6.x (released August 24, 2021) is currently only receiving security fixes and requested bug fixes as of August 24, 2023.
14
-
Please open a [bug report on GitHub](https://github.com/Automattic/mongoose/issues/new?assignees=&labels=&template=bug.yml) to request backporting a fix to Mongoose 6.
13
+
Mongoose 7.x was released on **February 27, 2023**and is now in **legacy support**.
14
+
It still receives important fixes when needed, but it’s no longer the primary development focus.
15
15
16
-
Mongoose 6.x end of life (EOL) is January 1, 2025.
17
-
Mongoose 6.x will no longer receive any updates, security or otherwise, after that date.
Mongoose 5.x (released January 17, 2018) is End-of-Life (EOL) since March 1, 2024. Mongoose 5.x will no longer receive any updates, security or otherwise.
21
+
Released on **August 24, 2021**, Mongoose 6.x now only receives **security patches** and **requested bug fixes**.
22
+
If you find an issue that needs to be backported, you can open a request on GitHub.
0 commit comments