最近在学习koa2 mongoose 写两表关联 但是总感觉自己写的特别冗余 有没有大佬来指点一二 谢谢了!

blog_schema.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;
const BlogSchema = new Schema({
    title: {
        required: true,
        unique: true,
        type: String
    },
    tags: [{
        type: Object
    }],
    category: {
        type: ObjectId,
        ref: 'Category'
    },
    content: {
        type: String, required: true
    },
    anthor: { type: String, required: true },
    createdAt: Date,
    updateAt: Date,
});

category_schema.js


const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
    name: {
        require: true,
        type: String,
    },
   
})

blog_controller.js

const Blog_col = require('../models/Blog');
const Category_col = require('../models/Category');

const add_blog = async (ctx, next) => {
    // 查询是否已有category
    let has_category = async (name) => {
        return await Category_col.findOne({ name: name });
    }
    // 添加category
    let add_category = async (name) => {
        return await Category_col.create({ name: name });
    }
    // 添加blog
    let add_blog = async (data) => {
        return await Blog_col.create(req);
    }
    let req = ctx.request.body; //获取请求参数
    
    let category_name = req.category || ''; 
    
    //此处判断是否有选择分类 有则添加分类后 获取分类id后将id赋值给req.category再进行添加文章 没有分类 则直接添加文章;
  
    if (!category_name) { // 没有输入分类 直接添加文章
        let result = await add_blog(req);
        if (result) {
            ctx.body = {
                code: 200,
                msg: "添加成功",
                data: result
            }
        } else {
            ctx.body = {
                code: 400,
                msg: "添加失败",
                data: result
            }
        }
    } else { //输入了分类 判断分类是否存在 存在则直接添加文章 不存在则添加分类后添加文章
        let category_id = await has_category(category_name)._id || '';
        if (!category_id) {
            category_id = await add_category(category_name)._id;
            req.category = category_id;
            let result = await add_blog(req);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: "添加成功",
                    data: result
                }
            } else {
                ctx.body = {
                    code: 400,
                    msg: "添加失败",
                    data: result
                }
            }
        } else {
            req.category = category_id;
            let result = await add_blog(req);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: "添加成功",
                    data: result
                }
            } else {
                ctx.body = {
                    code: 400,
                    msg: "添加失败",
                    data: result
                }
            }
        }
    }

}

这个只是自己练习写的 但是总是找不到思路 blog表关联了category表,是否category表也应该添加一项关联blog表 用于储存该分类所包含的文章id?

关联查询 通过populate 这个网上都能查到 但是就是设计表这些的思路 不太清晰 还有总感觉写代码的方式不太对 async 和await 的用法还是不太熟悉 可以来个大佬指点一下吗? 万分感谢!

阅读 1.8k
1 个回答
新手上路,请多包涵
复杂做法:分类表、内容表、关联表
简单做法:分类表、内容表(冗余分类)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题