博客后台管理-koa mongoose

116 阅读1分钟

koa mongoose v5 操作mongodb数据库

官网本来是纯静态,需要加上博客功能。 需要Markdown 编辑器,+服务端,东西不多,问了下后端,很忙,那我来搞吧。

开搞

技术栈 前端还是vue, 后端 用koajs + mongodb

动手
前端插件: 网上对比了下,使用 mavon-editor markdown在线编辑器 权限控制:因为文档需要编辑,还是需要权限控制,谁都能编辑不就乱套了
表设置: tag:标签,分类,方便归类 article:需要 title,content, tag, etc

整体感觉还好,就是一套增删改查

贴一下核心伪代码

// 设计article 表
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ArticleSchema = new Schema({
  id: String,
  title: String,
  content: String, 
  publish: { type: Boolean, default: false },
  recommend: { type: Boolean, default: false, required: true }, // 设置为推荐
  tag: { type: String, default: '', required: true }, // 标签
  createTime: { type: Number, default: +new Date() }, // 创建时间
  updateTime: { type: Number, default: +new Date() }, // 更新时间
});

module.exports = mongoose.model('Arcitle', ArticleSchema);

// 获取全部数据
ArticleModel.findAll()

// 增加一项
ArticleModel.create({...})

// 更新一项
ArticleModel.updateOne({ _id: id }, {name: 'update'})

// 删除一项
ArticleModel.deleteOne({ _id: id })

安装mongodb

安装教程见 mongo