Statics and Methods in TypeScriptTypeScript中的静力学和方法

You can define instance methods and static functions on Mongoose models. 您可以在Mongoose模型上定义实例方法和静态函数。With a little extra configuration, you can also register methods and statics in TypeScript.通过一点额外的配置,您还可以在TypeScript中注册方法和静态。

Methods方法

To define an instance method in TypeScript, create a new interface representing your instance methods. 要在TypeScript中定义实例方法,请创建一个表示实例方法的新接口。You need to pass that interface as the 3rd generic parameter to the Schema constructor and as the 3rd generic parameter to Model as shown below.您需要将该接口作为第三个泛型参数传递给Schema构造函数,并作为第三个子参数传递给Model,如下所示。

import { Model, Schema, model } from 'mongoose';

interface IUser {
firstName: string;
lastName: string;
}

// Put all user instance methods in this interface:
interface IUserMethods {
fullName(): string;
}

// Create a new Model type that knows about IUserMethods...
type UserModel = Model<IUser, {}, IUserMethods>;

// And a schema that knows about IUserMethods
const schema = new Schema<IUser, UserModel, IUserMethods>({
firstName: { type: String, required: true },
lastName: { type: String, required: true }
});
schema.method('fullName', function fullName() {
return this.firstName + ' ' + this.lastName;
});

const User = model<IUser, UserModel>('User', schema);

const user = new User({ firstName: 'Jean-Luc', lastName: 'Picard' });
const fullName: string = user.fullName(); // 'Jean-Luc Picard'

Statics静态

Mongoose models do not have an explicit generic parameter for statics. Mongoose模型没有用于静态的显式通用参数。If your model has statics, we recommend creating an interface that extends Mongoose's Model interface as shown below.如果您的模型有静态,我们建议创建一个扩展Mongoose的Model接口的接口,如下所示。

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 now that it is supplied in schema options. Mongoose确实支持自动类型化的静态函数,因为它是在模式选项中提供的。Statics functions can be defined as following:Statics函数可以定义如下:

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.以下是如何定义一个既有方法又有静力学的模型。

import { Model, Schema, HydratedDocument, model } from 'mongoose';

interface IUser {
firstName: string;
lastName: string;
}

interface IUserMethods {
fullName(): string;
}

interface UserModel extends Model<IUser, {}, IUserMethods> {
createWithFullName(name: string): Promise<HydratedDocument<IUser, IUserMethods>>;
}

const schema = new Schema<IUser, UserModel, IUserMethods>({
firstName: { type: String, required: true },
lastName: { type: String, required: true }
});
schema.static('createWithFullName', function createWithFullName(name: string) {
const [firstName, lastName] = name.split(' ');
return this.create({ firstName, lastName });
});
schema.method('fullName', function fullName(): string {
return this.firstName + ' ' + this.lastName;
});

const User = model<IUser, UserModel>('User', schema);

User.createWithFullName('Jean-Luc Picard').then(doc => {
console.log(doc.firstName); // 'Jean-Luc'
doc.fullName(); // 'Jean-Luc Picard'
});