Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11563 from mohammad0-0ahmad-forks/auto-typed-schema
Feature: Infer schema type automatically.
- Loading branch information
Showing
24 changed files
with
985 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Statics in TypeScript | ||
|
||
Mongoose [models](/docs/models.html) do **not** have an explicit generic parameter for [statics](/docs/guide.html#statics). | ||
If your model has statics, we recommend creating an interface that [extends](https://www.typescriptlang.org/docs/handbook/interfaces.html) Mongoose's `Model` interface as shown below. | ||
|
||
```typescript | ||
import { Model, Schema, model } from 'mongoose'; | ||
|
||
interface IUser { | ||
name: string; | ||
} | ||
|
||
interface UserModel extends Model<IUser> { | ||
myStaticMethod(): number; | ||
} | ||
|
||
const schema = new Schema<IUser, UserModel>({ name: String }); | ||
schema.static('myStaticMethod', function myStaticMethod() { | ||
return 42; | ||
}); | ||
|
||
const User = model<IUser, UserModel>('User', schema); | ||
|
||
const answer: number = User.myStaticMethod(); // 42 | ||
``` | ||
|
||
Mongoose does support auto typed static functions that it are supplied in schema options. | ||
Static functions can be defined by: | ||
|
||
```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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.