尝试在 Typescript 中实现 Mongoose 模型。搜了一下谷歌,只发现了一种混合方式(结合JS和TS)。在没有 JS 的情况下,如何以我相当幼稚的方法来实现 User 类?
希望能够没有包袱的 IUserModel。
import {IUser} from './user.ts';
import {Document, Schema, Model} from 'mongoose';
// mixing in a couple of interfaces
interface IUserDocument extends IUser, Document {}
// mongoose, why oh why '[String]'
// TODO: investigate out why mongoose needs its own data types
let userSchema: Schema = new Schema({
userName : String,
password : String,
firstName : String,
lastName : String,
email : String,
activated : Boolean,
roles : [String]
});
// interface we want to code to?
export interface IUserModel extends Model<IUserDocument> {/* any custom methods here */}
// stumped here
export class User {
constructor() {}
}
原文由 Tim McNamara 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是我的做法: