Mongoose the Typescript way…?

后端 未结 14 555
遥遥无期
遥遥无期 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:01

    Another alternative if you want to detach your type definitions and the database implementation.

    import {IUser} from './user.ts';
    import * as mongoose from 'mongoose';
    
    type UserType = IUser & mongoose.Document;
    const User = mongoose.model('User', new mongoose.Schema({
        userName  : String,
        password  : String,
        /* etc */
    }));
    

    Inspiration from here: https://github.com/Appsilon/styleguide/wiki/mongoose-typescript-models

提交回复
热议问题