• [Express] Extends CrudController with Mongoose model


    Each model controller:

    import { crudControllers } from '../../utils/crud'
    import { Item } from './item.model'
    
    export default crudControllers(Item)

    You are able to override default methods from crudController:

    export default {
      ...crudControllers(Item),
      removeOne() {...}
    }

    model:

    查看代码
     import mongoose from 'mongoose'
    
    const itemSchema = new mongoose.Schema(
      {
        name: {
          type: String,
          required: true,
          trim: true,
          maxlength: 50
        },
        status: {
          type: String,
          required: true,
          enum: ['active', 'complete', 'pastdue'],
          default: 'active'
        },
        notes: String,
        due: Date,
        createdBy: {
          type: mongoose.SchemaTypes.ObjectId,
          ref: 'user',
          required: true
        },
        list: {
          type: mongoose.SchemaTypes.ObjectId,
          ref: 'list',
          required: true
        }
      },
      { timestamps: true }
    )
    
    // 1: means sorting order
    // order matters, list first, then name
    itemSchema.index({ list: 1, name: 1 }, { unique: true })
    
    export const Item = mongoose.model('item', itemSchema)

    CrudController:

    export const getOne = model => async (req, res) => {
      try {
        const doc = model
          .findOne({ createdBy: req.user._id, _id: req.params.id })
          .lean()
          .exec()
    
        if (!doc) {
          return res.status(400).end()
        }
    
        res.status(200).json({ data: doc })
      } catch (e) {
        console.error(e)
        res.status(400).end()
      }
    }
    
    const getMany = model => async (req, res) => {
      try {
        const docs = model
          .find({ createdBy: req.user._id })
          .lean()
          .exec()
    
        res.status(200).json({ data: docs })
      } catch (e) {
        console.error(e)
        res.status(400).end()
      }
    }
    
    export const createOne = model => async (req, res) => {
      const createdBy = req.user._id
      try {
        const doc = await model.create({ ...req.body, createdBy })
        res.status(201).json({ data: doc })
      } catch (e) {
        console.error(e)
        res.status(400).end()
      }
    }
    
    export const updateOne = model => async (req, res) => {
      try {
        const updatedDoc = await model
          .findOneAndUpdate(
            {
              createdBy: req.user._id,
              _id: req.params.id
            },
            req.body,
            { new: true }
          )
          .lean()
          .exec()
    
        if (!updatedDoc) {
          return res.status(400).end()
        }
    
        res.status(200).json({ data: updatedDoc })
      } catch (e) {
        console.error(e)
        res.status(400).end()
      }
    }
    
    export const removeOne = model => async (req, res) => {
      try {
        const removed = await model.findOneAndRemove({
          createdBy: req.user._id,
          _id: req.params.id
        })
    
        if (!removed) {
          return res.status(400).end()
        }
    
        return res.status(200).json({ data: removed })
      } catch (e) {
        console.error(e)
        res.status(400).end()
      }
    }
    
    export const crudControllers = model => ({
      removeOne: removeOne(model),
      updateOne: updateOne(model),
      getMany: getMany(model),
      getOne: getOne(model),
      createOne: createOne(model)
    })
  • 相关阅读:
    神经网络梯度下降的推导
    在虚拟环境里安装TensorFlow-cpu完成Ng作业
    利用anaconda配置虚拟环境
    Java基础(三)面向对象(下)
    Java基础(二)面向对象(上)
    Java基础(一)
    《深入理解计算机系统》阅读笔记--程序的机器级表示(上)
    《深入理解计算机系统》阅读笔记--信息的表示和处理(下)
    《深入理解计算机系统》阅读笔记--信息的表示和处理(上)
    《深入理解计算机系统》阅读笔记--计算机系统漫游
  • 原文地址:https://www.cnblogs.com/Answer1215/p/16617498.html
Copyright © 2020-2023  润新知