美文网首页
使用mongoose驱动时的model约束

使用mongoose驱动时的model约束

作者: percykuang | 来源:发表于2020-07-04 23:24 被阅读0次

    db/ArticleSchema.ts

    import Mongoose from 'mongoose'
    import Article from '../model/Article'
    
    // Mongoose.Document有一些数据库的操作方法
    export interface IArticle extends Article, Mongoose.Document {}
    
    // 泛型IArticle,帮助进行编译时的类型推断
    const articleSchema = new Mongoose.Schema<IArticle>({
      // 运行时的类型
      title: String,
      tagList: [String],
      publishTime: Date,
      content: String
    }, { versionKey: false })
    
    // 泛型IArticle,帮助进行编译时的类型推断
    export default Mongoose.model<IArticle>('Article', articleSchema)
    

    db/index.ts

    import Mongoose from 'mongoose'
    import ArticleModel from './ArticleSchema'
    
    Mongoose.connect('mongodb://localhost:27017/blog', {
      useNewUrlParser: true
    }).then(() => console.log('连接数据库成功'))
    
    export { ArticleModel }
    

    代码提示

    1.png

    相关文章

      网友评论

          本文标题:使用mongoose驱动时的model约束

          本文链接:https://www.haomeiwen.com/subject/ufxmqktx.html