Mongoose the Typescript way…?

后端 未结 14 557
遥遥无期
遥遥无期 2020-11-28 19:27

Trying to implement a Mongoose model in Typescript. Scouring the Google has revealed only a hybrid approach (combining JS and TS). How would one go about implementing the

14条回答
  •  感情败类
    2020-11-28 20:05

    Here's how guys at Microsoft do it. here

    import mongoose from "mongoose";
    
    export type UserDocument = mongoose.Document & {
        email: string;
        password: string;
        passwordResetToken: string;
        passwordResetExpires: Date;
    ...
    };
    
    const userSchema = new mongoose.Schema({
        email: { type: String, unique: true },
        password: String,
        passwordResetToken: String,
        passwordResetExpires: Date,
    ...
    }, { timestamps: true });
    
    export const User = mongoose.model("User", userSchema);
    

    I recommend to check this excellent starter project out when you add TypeScript to your Node project.

    https://github.com/microsoft/TypeScript-Node-Starter

提交回复
热议问题