微信小程序+nodeJs+express+mongodb踩坑记录 2、封装crud方法

  • 前后端在工作中相互为搭档,又相互鄙视(手动狗头)。

  • 后台总觉得前端代码简单,切切图就好了,不用写什么逻辑,切图仔。由于安全性考虑,很多复杂的逻辑都是后台来完成。这个时候我们前端就会反击:后台只会crud,一个sql玩一天,查询出结果还要几十秒,渣渣。

  • 当然,这只是一个玩笑,这里就不深究了。今天主要记录一下封装的 crud 公共方法。
    这里只是封装了几个逻辑比较简单的 增 删 改 查:

创建:routes/common.js

const { formatDate } = require('./../utils')
    /**
     * @name: 新增方法
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
const saveMsg = (Model, res, params) => {
        new Model(params).save((err) => {
            const datas = err ? { isSuccess: false, message: '保存失败', err } : { isSuccess: true, message: '保存成功' }
            res.send(datas);
        })
    }
    /**
     * @name: 通过id删除
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
const deleteMsg = (Model, req, res) => {
        Model.findByIdAndRemove({ _id: req.body._id }, (err) => {
            const datas = err ? { isSuccess: false, message: '删除失败', err } : { isSuccess: true, message: '删除成功' }
            res.send(datas);
        })
    }
    /**
     * @name: 通过id修改
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
const editMsg = (Model, req, res) => {
        const { _id } = req.body
        const params = req.body
        params.updateDate = formatDate()
        delete params._id
        Model.findByIdAndUpdate({ _id }, params, (err) => {
            const datas = err ? { isSuccess: false, message: '修改失败', err } : { isSuccess: true, message: '修改成功' }
            res.send(datas);
        })
    }
    /**
     * @name: 不带参数的分页查询
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
const queryMsg = (Model, req, res) => {
    const { pageSize, pageNo } = req.body
    let size = pageSize || 10
    let num = pageNo - 1 || 0
    Model.find({}, (err, doc) => {
        if (err) {
            res.send({ isSuccess: false, message: '查询失败' });
        } else {
            const total = doc.length
            Model.find().sort({ _id: -1 }).skip(num * size).limit(size).exec((errs, docs) => {
                if (errs) {
                    res.send({ isSuccess: false, message: '查询失败' });
                } else {
                    res.send({ isSuccess: true, data: docs, total });
                }
            })
        }
    })
}
module.exports = {
    saveMsg,
    deleteMsg,
    editMsg,
    queryMsg
}

使用 : 通用方法,分别传入引入的Model 和 req , res即可

const expres = require('express')
const router = expres.Router()
const Product = require('./../model/product')
const { saveMsg, deleteMsg, editMsg, queryMsg } = require('./common')
    /**
     * @name: 增加商品
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
router.post('/addProduct', (req, res, next) => {
        const { types, cate, title, description, price, details, images, isSale, salePrice, isHot, isOn, isTui } = req.body
        Product.find({ title, types }).exec((err, doc) => {
            if (err) {
                res.send({ isSuccess: false, message: '添加失败' });
            } else {
                if (doc.length > 0) {
                    res.send({ isSuccess: false, message: '请不要重复添加商品' });
                } else {
                    saveMsg(Product, res, { types, cate, title, description, price, details, images, isSale, salePrice, isHot, isOn, isTui })
                }
            }
        })
    })
    /**
     * @name: 删除商品
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
router.post('/deleteProduct', (req, res, next) => {
        deleteMsg(Product, req, res)
    })
    /**
     * @name: 编辑商品
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
router.post('/editProduct', (req, res, next) => {
        editMsg(Product, req, res)
    })
    /**
     * @name: 查询商品
     * @param {type} 
     * @Author: luoyong/471826078@qq.com
     */
router.get('/queryProduct', (req, res, next) => {
    queryMsg(Product, req, res)
})

module.exports = router

参杂其他业务的接口需要另外写,但是也能减少很多粘贴复制的时间。

青浅个人博客

欢迎评论、点赞、留言

下一篇:微信小程序+nodeJs+express+mongodb踩坑记录 3、图片上传

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值