Essayer d'implémenter un modèle Mongoose dans Typescript. La fouille de Google n'a révélé qu'une approche hybride (combinant JS et TS). Comment implémenter la classe User, sur mon approche plutôt naïve, sans le JS?
Vous voulez pouvoir IUserModel sans bagages.
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
ne peut pas être une classe car en créer une est une opération asynchrone. Il doit retourner une promesse donc vous devez appelerUser.create({...}).then...
.