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

6.4 #11947

Merged
merged 153 commits into from
Jun 17, 2022
Merged

6.4 #11947

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
153 commits
Select commit Hold shift + click to select a range
2582959
initial
Uzlopak Feb 9, 2022
4b1e8e8
fix linting issues
Uzlopak Feb 9, 2022
d330145
fix linter error
Uzlopak Feb 9, 2022
f0ab5c8
Arithmetic Expression Operators
Uzlopak Feb 9, 2022
d0f466f
Text Expression Operators
Uzlopak Feb 9, 2022
fdada6b
+ Trigonometry Expression Operators
Uzlopak Feb 9, 2022
0291e9b
add comment
Uzlopak Feb 9, 2022
c667b50
add version info
Uzlopak Feb 9, 2022
519c314
only add and subtract can handle dates
Uzlopak Feb 9, 2022
742c951
add DateExpressionOperators
Uzlopak Feb 10, 2022
e2d1e6a
add boolean and comparison
Uzlopak Feb 10, 2022
29a8837
add ConditionalExpressionOperator
Uzlopak Feb 10, 2022
a8f26b4
add ArrayExpression
Uzlopak Feb 10, 2022
a17370e
some minor changes
Uzlopak Feb 10, 2022
ebcad04
add StringExpressionOperators
Uzlopak Feb 10, 2022
40acd28
Merge branch 'master' into types-expression
Uzlopak Feb 16, 2022
9afcb1c
Added support for cleanAndClose connections from memory
Apr 16, 2022
dc8c589
Made sure to check for undefined, removed strict check
OmkarHirve Apr 16, 2022
7025c0a
Added unit test, method definition and fixed lint issues
Apr 18, 2022
623efdf
Added support for throwing error if openUri is called on cleaned conn…
Apr 23, 2022
c28abdd
Merge branch 'Automattic:master' into master
OmkarHirve Apr 23, 2022
dbcf0dc
restored stub after use
Apr 23, 2022
8fa8fec
Created a 'destroy' method to clear conns
Apr 29, 2022
946d97f
Merge branch 'Automattic:master' into master
OmkarHirve Apr 29, 2022
c925d3b
Update test/connection.test.js
OmkarHirve May 3, 2022
bc45714
Update test/connection.test.js
OmkarHirve May 3, 2022
bbe3bb7
Update lib/connection.js
OmkarHirve May 3, 2022
c665c4e
Update lib/connection.js
OmkarHirve May 3, 2022
aa5f934
Update lib/connection.js
OmkarHirve May 3, 2022
227c15f
Update lib/connection.js
OmkarHirve May 3, 2022
16df6f6
Apply suggestions from code review
Uzlopak May 3, 2022
cf76140
chore: update mongodb driver
AbdelrahmanHafez May 11, 2022
7b89ea9
Applied suggested changes and fixed unit test failures
May 13, 2022
7e937b5
Merge branch 'Automattic:master' into master
OmkarHirve May 13, 2022
36329d4
on is no longer a reserved word
IslandRhythms May 13, 2022
b3242b7
fixed model compilation failure
IslandRhythms May 13, 2022
5b7348c
lint fix
IslandRhythms May 13, 2022
0044913
added test case
IslandRhythms May 13, 2022
3d17528
Merge branch '6.4' into gh-11580
vkarpov15 May 16, 2022
2dc8de8
Merge pull request #11804 from Automattic/gh-11580
vkarpov15 May 16, 2022
2c9b417
`completeOneLean()` done
IslandRhythms May 16, 2022
b8dbcd8
removed logs
IslandRhythms May 16, 2022
ea4483a
Merge pull request #11796 from Automattic/AbdelrahmanHafez-patch-2
vkarpov15 May 17, 2022
6f8deed
Merge pull request #11674 from OmkarHirve/master
vkarpov15 May 17, 2022
f6be5e0
test: remove unnecessary log statements re: #11674
vkarpov15 May 17, 2022
6cbbaef
`_completeManyLean()` done, I think
IslandRhythms May 17, 2022
73cc742
reworking solution
IslandRhythms May 20, 2022
da0d878
independent `completeManyLean()`
IslandRhythms May 23, 2022
9babf9c
lint fix
IslandRhythms May 23, 2022
ef3237b
Merge branch 'master' into 6.4
vkarpov15 May 28, 2022
fb71984
feat(document): add `$assertPopulated()` for working with manually po…
vkarpov15 May 28, 2022
70ba07a
Apply suggestions from code review
AbdelrahmanHafez May 28, 2022
6b08472
fix JSDoc type for $assertPopulated
AbdelrahmanHafez May 28, 2022
2cff001
Merge pull request #11843 from Automattic/vkarpov15/gh11758
vkarpov15 May 28, 2022
2e19312
Merge branch 'master' into gh-10423
vkarpov15 May 29, 2022
06f879e
Merge branch 'main' into types-expression
Uzlopak May 30, 2022
15dcd8e
remove duplicate Expression.test.ts
Uzlopak May 30, 2022
8f3f784
add more
Uzlopak May 30, 2022
be4a808
add Set Expression Operators
Uzlopak May 30, 2022
7cef3c5
add Window Expressions
Uzlopak May 30, 2022
e8e6842
add type expressions
Uzlopak May 31, 2022
08270f4
add DataSizeOperator
Uzlopak May 31, 2022
1ef01c0
add last operation set
Uzlopak May 31, 2022
238bb90
add some tests
Uzlopak May 31, 2022
f57e007
add tests, fix typings
Uzlopak May 31, 2022
510412f
add more tests, fix more
Uzlopak May 31, 2022
4635fcd
fix more
Uzlopak May 31, 2022
c94d2bb
fix linting
Uzlopak May 31, 2022
16deac9
auto format .eslintrc.json
Uzlopak Jun 2, 2022
365f39f
fix .eslint.json and linting errors
Uzlopak Jun 2, 2022
e3dca30
Merge branch 'master' into types-expression
Uzlopak Jun 2, 2022
640adca
test(base): add test for immutableCreatedAt
AbdelrahmanHafez Jun 2, 2022
31926c9
implement immutableCreatedAt
AbdelrahmanHafez Jun 2, 2022
29969f0
add type for immutableCreatedAt
AbdelrahmanHafez Jun 2, 2022
1dc2e4f
change option from immutableCreatedAt => timestamps.createdAt.immutable
AbdelrahmanHafez Jun 2, 2022
403766a
fix failing TS test
AbdelrahmanHafez Jun 2, 2022
3b71125
Merge branch 'master' into master-into-6.4
AbdelrahmanHafez Jun 2, 2022
d082047
refining the logic to requests made
IslandRhythms Jun 2, 2022
2dbe288
Add TS utilities to infer schema type automatically.
mohammad0-0ahmad Mar 24, 2022
03e34db
Import inferschematype in index.d.ts file & refactor Schema class.
mohammad0-0ahmad Mar 24, 2022
ec6eb3d
Refactor model FN type & Model interface type.
mohammad0-0ahmad Mar 24, 2022
9321fd5
Make schema type obtainable from schema its constructor parameter "de…
mohammad0-0ahmad Mar 24, 2022
2145afa
Supporting custom typeKey to infer schema type.
mohammad0-0ahmad Mar 24, 2022
34ae74c
Add statics property in schema options to supporting auto typed model…
mohammad0-0ahmad Mar 24, 2022
967bd65
Add methods property in schema options to supporting auto typed docum…
mohammad0-0ahmad Mar 24, 2022
e75d4b6
Improve create FN type to show actual schema type instead of any
mohammad0-0ahmad Mar 24, 2022
ff2bf3e
Improve ApplyBasicQueryCasting type to improve find FN suggestions
mohammad0-0ahmad Mar 24, 2022
1bf940e
Refact create FN returned type
mohammad0-0ahmad Mar 24, 2022
4a5d712
Refact Model new FN & some related tests.
mohammad0-0ahmad Mar 26, 2022
295bad2
Fix typo from the previous PR
mohammad0-0ahmad Mar 26, 2022
cd215a7
Fix typo
mohammad0-0ahmad Mar 26, 2022
a7967d9
Improve Model.insertMany FN type
mohammad0-0ahmad Mar 27, 2022
e9d4491
Add query property in schema options to support auto typed query help…
mohammad0-0ahmad Mar 28, 2022
526ff6a
Rearrang Schema class generics
mohammad0-0ahmad Mar 28, 2022
411350b
Rearrange SchemaOptions generics.
mohammad0-0ahmad Mar 28, 2022
b942ce8
refactor query type in SchemaOptions & related tests.
mohammad0-0ahmad Mar 29, 2022
6bcb7a4
refactor statics & methods properties in SchemaOptions to get better …
mohammad0-0ahmad Mar 29, 2022
56af40b
Pass DocType to statics, methods & query props in SchemaOptions to ge…
mohammad0-0ahmad Mar 29, 2022
9cdec80
Refactor statics & methods props in SchemaOptions.
mohammad0-0ahmad Mar 29, 2022
b3e9668
Add ability to infer path of mixed array type.
mohammad0-0ahmad Apr 4, 2022
a42bbde
Refactor the previous PR.
mohammad0-0ahmad Apr 4, 2022
302de56
Refactor some function names & comment lines.
mohammad0-0ahmad Apr 5, 2022
96571b1
Refactor test in query.test.js file.
mohammad0-0ahmad Apr 5, 2022
897452b
Resolve Mixed type as any type.
mohammad0-0ahmad Apr 19, 2022
503c0d1
Refactor doc.populate FN to make it accept any Model type.
mohammad0-0ahmad Apr 22, 2022
aa6497d
Refactor Model new FN
mohammad0-0ahmad Apr 28, 2022
a3b32ac
Refactor model FN to optimize TS server performance.
mohammad0-0ahmad Apr 29, 2022
569d4eb
create MergeBOntoA And implement instead of UnpackedIntersection in s…
mohammad0-0ahmad Apr 29, 2022
0fe4928
Add some details about auto typed schema.
mohammad0-0ahmad Apr 29, 2022
f4f17d7
Add some details about auto typed statics functions
mohammad0-0ahmad Apr 29, 2022
6ef2f6c
Add some details about auto typed query helpers.
mohammad0-0ahmad Apr 29, 2022
6a77f09
Add details about auto typed statics, query-helpers and instance meth…
mohammad0-0ahmad Apr 30, 2022
f21dfbe
Refactor docs.
mohammad0-0ahmad Apr 30, 2022
7093195
Undo auto docs formatting changes.
mohammad0-0ahmad May 1, 2022
555db64
Refactor docs & some comments in tests files.
mohammad0-0ahmad May 3, 2022
0131b7e
fix typo
mohammad0-0ahmad May 17, 2022
47469d6
Cover decimal128
mohammad0-0ahmad Jun 2, 2022
e0d299f
patch autotyping to handle properly population and
Uzlopak Jun 2, 2022
e137a6d
Fix linting
mohammad0-0ahmad Jun 3, 2022
4c0ecbe
reworked `completeManyLean()` to be in line with requested change
IslandRhythms Jun 3, 2022
d11c511
reworked `_completeOneLean()` to be in line with requested changes
IslandRhythms Jun 3, 2022
1c7696e
should work now
IslandRhythms Jun 3, 2022
8c5263d
Merge branch '6.4' of github.com:Automattic/mongoose into 6.4
vkarpov15 Jun 3, 2022
f5dc4f8
Merge pull request #11814 from Automattic/gh-10423
vkarpov15 Jun 3, 2022
0a4bcc5
feat(mongoose): add `setDriver()` function to allow overwriting `driv…
vkarpov15 Jun 5, 2022
2e51510
Merge branch 'master' into vkarpov15/driver-fixes
vkarpov15 Jun 5, 2022
2475c61
Merge branch 'master' into 6.4
vkarpov15 Jun 5, 2022
5a4160e
Merge pull request #11889 from Automattic/master-into-6.4
vkarpov15 Jun 5, 2022
32d5933
Merge pull request #11888 from Automattic/feat/globalCreatedAtImmutable
vkarpov15 Jun 5, 2022
6547fe2
Merge branch '6.4' into auto-typed-schema
vkarpov15 Jun 5, 2022
f48eee2
Merge pull request #11563 from mohammad0-0ahmad-forks/auto-typed-schema
vkarpov15 Jun 5, 2022
0a53c79
refactor: use explanatory variable
AbdelrahmanHafez Jun 5, 2022
6df8c31
fix: make `doValidate()` on document array elements run validation on…
vkarpov15 Jun 6, 2022
0f85563
chore: upgrade mongodb driver to v4.7.0
AbdelrahmanHafez Jun 7, 2022
c714d52
Merge pull request #11909 from Automattic/upgrade-mongodb
AbdelrahmanHafez Jun 7, 2022
c9dad15
Merge branch 'master' into 6.4
vkarpov15 Jun 13, 2022
69dc0ee
Merge branch 'master' into 6.4
vkarpov15 Jun 13, 2022
db866bd
address code review comments
vkarpov15 Jun 13, 2022
379c459
fix: add test coverage for `doValidate()` on array subdocument path
vkarpov15 Jun 13, 2022
40626d1
Merge pull request #11902 from Automattic/gh-11770
vkarpov15 Jun 13, 2022
01c1d87
feat: add `skipMiddlewareFunction()` and `overwriteMiddlewareResult()…
vkarpov15 Jun 13, 2022
969fd1d
test: improve tests for overwriteMiddlewareResult() and skipMiddlewar…
vkarpov15 Jun 15, 2022
2228df7
Update lib/index.js
vkarpov15 Jun 16, 2022
a9b7d4c
Update lib/connection.js
vkarpov15 Jun 16, 2022
13f39c2
Merge branch '6.4' into vkarpov15/driver-fixes
vkarpov15 Jun 16, 2022
26a61fb
Merge branch 'vkarpov15/driver-fixes' of github.com:Automattic/mongoo…
vkarpov15 Jun 16, 2022
567631a
test: add test coverage for drivers re: #11900
vkarpov15 Jun 16, 2022
6db2e4a
Merge pull request #11900 from Automattic/vkarpov15/driver-fixes
vkarpov15 Jun 16, 2022
635e87c
Merge branch '6.4' into vkarpov15/gh-11426
vkarpov15 Jun 16, 2022
ebc0b88
fix: upgrade kareem to fix #6611 test
vkarpov15 Jun 16, 2022
96edf20
Merge pull request #11927 from Automattic/vkarpov15/gh-11426
vkarpov15 Jun 17, 2022
47c5f78
Merge pull request #11370 from Uzlopak/types-expression
vkarpov15 Jun 17, 2022
40fef4a
Merge branch 'master' into 6.4
vkarpov15 Jun 17, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 61 additions & 34 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,70 @@
],
"ignorePatterns": [
"docs",
"tools",
"dist",
"test/files/*"
"website.js",
"test/files/*",
"benchmarks"
],
"overrides": [{
"files": [
"**/*.{ts,tsx}"
],
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/triple-slash-reference": "off",
"spaced-comment": ["error", "always", {
"markers": ["/"]
}],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/indent": ["error", 2, {
"SwitchCase": 1
}],
"@typescript-eslint/prefer-optional-chain": "error",
"@typescript-eslint/brace-style": "error",
"@typescript-eslint/no-dupe-class-members": "error",
"@typescript-eslint/no-redeclare": "error",
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/object-curly-spacing": ["error", "always"],
"@typescript-eslint/semi": "error",
"@typescript-eslint/space-before-function-paren": ["error", "never"],
"@typescript-eslint/space-infix-ops": "off"
"overrides": [
{
"files": [
"**/*.{ts,tsx}"
],
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/triple-slash-reference": "off",
"spaced-comment": [
"error",
"always",
{
"block": {
"markers": [
"!"
],
"balanced": true
},
"markers": [
"/"
]
}
],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/indent": [
"error",
2,
{
"SwitchCase": 1
}
],
"@typescript-eslint/prefer-optional-chain": "error",
"@typescript-eslint/brace-style": "error",
"@typescript-eslint/no-dupe-class-members": "error",
"@typescript-eslint/no-redeclare": "error",
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/object-curly-spacing": [
"error",
"always"
],
"@typescript-eslint/semi": "error",
"@typescript-eslint/space-before-function-paren": [
"error",
"never"
],
"@typescript-eslint/space-infix-ops": "off"
}
}
}],
],
"plugins": [
"mocha-no-only"
],
Expand Down
48 changes: 44 additions & 4 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,18 @@ We may also define our own custom document instance methods.

```javascript
// define a schema
const animalSchema = new Schema({ name: String, type: String });
const animalSchema = new Schema({ name: String, type: String },
{
// Assign a function to the "methods" object of our animalSchema through schema options.
// By following this approach, there is no need to create a separate TS type to define the type of the instance functions.
methods:{
findSimilarTypes(cb){
return mongoose.model('Animal').find({ type: this.type }, cb);
}
}
});

// assign a function to the "methods" object of our animalSchema
// Or, assign a function to the "methods" object of our animalSchema
animalSchema.methods.findSimilarTypes = function(cb) {
return mongoose.model('Animal').find({ type: this.type }, cb);
};
Expand All @@ -169,14 +178,28 @@ dog.findSimilarTypes((err, dogs) => {

<h3 id="statics"><a href="#statics">Statics</a></h3>

You can also add static functions to your model. There are two equivalent
You can also add static functions to your model. There are three equivalent
ways to add a static:

- Add a function property to the second argument of the schema-constructor (`statics`)
- Add a function property to `schema.statics`
- Call the [`Schema#static()` function](/docs/api.html#schema_Schema-static)

```javascript
// Assign a function to the "statics" object of our animalSchema

// define a schema
const animalSchema = new Schema({ name: String, type: String },
{
// Assign a function to the "statics" object of our animalSchema through schema options.
// By following this approach, there is no need to create a separate TS type to define the type of the statics functions.
statics:{
findByName(name){
return this.find({ name: new RegExp(name, 'i') });
}
}
});

// Or, Assign a function to the "statics" object of our animalSchema
animalSchema.statics.findByName = function(name) {
return this.find({ name: new RegExp(name, 'i') });
};
Expand All @@ -197,6 +220,20 @@ but for mongoose queries. Query helper methods let you extend mongoose's
[chainable query builder API](./queries.html).

```javascript

// define a schema
const animalSchema = new Schema({ name: String, type: String },
{
// Assign a function to the "query" object of our animalSchema through schema options.
// By following this approach, there is no need to create a separate TS type to define the type of the query functions.
query:{
byName(name){
return this.where({ name: new RegExp(name, 'i') })
}
}
});

// Or, Assign a function to the "query" object of our animalSchema
animalSchema.query.byName = function(name) {
return this.where({ name: new RegExp(name, 'i') })
};
Expand Down Expand Up @@ -409,6 +446,7 @@ Valid options:
- [read](#read)
- [writeConcern](#writeConcern)
- [shardKey](#shardKey)
- [statics](#statics)
- [strict](#strict)
- [strictQuery](#strictQuery)
- [toJSON](#toJSON)
Expand All @@ -423,6 +461,8 @@ Valid options:
- [skipVersioning](#skipVersioning)
- [timestamps](#timestamps)
- [storeSubdocValidationError](#storeSubdocValidationError)
- [methods](#methods)
- [query](#query-helpers)

<h3 id="autoIndex"><a href="#autoIndex">option: autoIndex</a></h3>

Expand Down
33 changes: 32 additions & 1 deletion docs/typescript/query-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ var Project = mongoose.model('Project', ProjectSchema);
Project.find().where('stars').gt(1000).byName('mongoose');
```

In TypeScript, Mongoose's `Model` takes 3 generic parameters:
In TypeScript, Mongoose does support manually typed and automatically typed Query Helpers.

1- Manually typed:
Mongoose's `Model` takes 3 generic parameters:

1. The `DocType`
2. a `TQueryHelpers` type
Expand Down Expand Up @@ -58,4 +61,32 @@ async function run(): Promise<void> {
// Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })`
await ProjectModel.find().where('stars').gt(1000).byName('mongoose');
}
```

2- Automatically typed:
Mongoose does support auto typed Query Helpers that it are supplied in schema options.
Query Helpers functions can be defined as following:

```typescript
import { Schema, model } from 'mongoose';

const schema = new Schema({
name: { type: String, required: true },
stars: { type: Number, required: true }
}, {
query: {
byName(name) {
return this.find({ name: name });
}
}
});

const ProjectModel = model(
'Project',
schema
);

// Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })`
await ProjectModel.find().where('stars').gt(1000).byName('mongoose');
}
```
49 changes: 42 additions & 7 deletions docs/typescript/schemas.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Schemas in TypeScript

Mongoose [schemas](/docs/guide.html) are how you tell Mongoose what your documents look like.
Mongoose schemas are separate from TypeScript interfaces, so you need to define both a _document interface_ and a _schema_.
Mongoose schemas are separate from TypeScript interfaces, so you need to define both a _document interface_ and a _schema_ until V6.3.1.
Mongoose supports auto typed schemas so you don't need to define additional typescript interface anymore but you are still able to do so.
Mongoose provides a `InferSchemaType`, which infers the type of the auto typed schema document when needed.

`Until mongoose V6.3.1:`

```typescript
import { Schema } from 'mongoose';
Expand All @@ -21,6 +25,37 @@ const schema = new Schema<User>({
});
```

`another approach:`

```typescript
import { Schema, InferSchemaType } from 'mongoose';

// Document interface
// No need to define TS interface any more.
// interface User {
// name: string;
// email: string;
// avatar?: string;
// }

// Schema
const schema = new Schema({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});

type User = InferSchemaType<typeof schema>;
// InferSchemaType will determine the type as follows:
// type User = {
// name: string;
// email: string;
// avatar?: string;
// }


```

By default, Mongoose does **not** check if your document interface lines up with your schema.
For example, the above code won't throw an error if `email` is optional in the document interface, but `required` in `schema`.

Expand Down Expand Up @@ -51,7 +86,7 @@ Mongoose wraps `DocType` in a Mongoose document for cases like the `this` parame
For example:

```typescript
schema.pre('save', function(): void {
schema.pre('save', function (): void {
console.log(this.name); // TypeScript knows that `this` is a `mongoose.Document & User` by default
});
```
Expand Down Expand Up @@ -102,7 +137,7 @@ interface User {
const schema = new Schema<User, Model<User>>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
avatar: String,
});
```

Expand All @@ -121,13 +156,13 @@ interface BlogPost {
}

interface User {
tags: Types.Array<string>,
blogPosts: Types.DocumentArray<BlogPost>
tags: Types.Array<string>;
blogPosts: Types.DocumentArray<BlogPost>;
}

const schema = new Schema<User, Model<User>>({
tags: [String],
blogPosts: [{ title: String }]
blogPosts: [{ title: String }],
});
```

Expand All @@ -139,4 +174,4 @@ If you use `Types.DocumentArray` in the above case, you'll be able to `push()` a
const user = new User({ blogPosts: [] });

user.blogPosts.push({ title: 'test' }); // Would not work if you did `blogPosts: BlogPost[]`
```
```
21 changes: 21 additions & 0 deletions docs/typescript/statics-and-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ const User = model<IUser, UserModel>('User', schema);
const answer: number = User.myStaticMethod(); // 42
```

Mongoose does support auto typed static functions now that it is supplied in schema options.
Statics functions can be defined as following:

```typescript
import { Schema, model } from 'mongoose';

const schema = new Schema(
{ name: String },
{
statics: {
myStaticMethod() {
return 42;
},
},
}
);

const User = model('User', schema);

const answer = User.myStaticMethod(); // 42
```
## Both Methods and Statics

Below is how you can define a model that has both methods and statics.
Expand Down