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
网友评论