Đang cố gắng triển khai mô hình Mongoose trong Typescript. Google chỉ tiết lộ một cách tiếp cận kết hợp (kết hợp JS và TS). Làm thế nào để triển khai lớp Người dùng, theo cách tiếp cận khá ngây thơ của tôi, nếu không có JS?
Muốn có thể IUserModel mà không cần hành lý.
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
không thể là một lớp vì việc tạo một lớp là một hoạt động không đồng bộ. Nó phải trả lại một lời hứa nên bạn phải gọiUser.create({...}).then...
.