【发布时间】:2016-04-01 15:08:21
【问题描述】:
尝试在 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() {}
}
【问题讨论】:
-
User不能是一个类,因为创建一个类是异步操作。它必须返回一个承诺,所以你必须打电话给User.create({...}).then...。 -
具体来说,在OP中的代码中给出,请您详细说明为什么
User不能是一个类? -
@Erich 他们说 typeorm 不能很好地与 MongoDB 配合使用,也许 Type goose 是一个不错的选择
标签: javascript node.js mongoose typescript