Created
November 27, 2020 05:47
-
-
Save luciyer/3637e8802d6326b1aed144c5ac37efd5 to your computer and use it in GitHub Desktop.
Express + Mongoose Middleware: Standard Controller + Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express") | |
const mw = require("./middleware") | |
const app = express() | |
app | |
.use(express.json()) | |
.use(express.urlencoded({ extended: true })) | |
.use(mw.Book.router) | |
app.listen(process.env.PORT || 8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* middleware/controller.js */ | |
const router = require("express").Router() | |
/* Require or set up your mongoose models here */ | |
const models = require("./models") | |
module.exports = class { | |
constructor(modelName) { | |
if (!models[modelName]) | |
throw new Error(`Model "${modelName}" not found.`) | |
this.Model = models[modelName] | |
this.modelName = this.Model.modelName.toLowerCase() | |
this.routes = { | |
base: `/api/${this.modelName}`, | |
id: `/api/${this.modelName}/:id`, | |
query: `/api/${this.modelName}/query` | |
} | |
} | |
get dbMethods() { | |
const getDocs = () => { | |
return this.Model.find() | |
} | |
const queryDocs = (body) => { | |
return this.Model.find(body) | |
} | |
const queryUpdateDocs = (filter, update) => { | |
this.Model.findOneAndUpdate(filter, update, { new: true }).exec() | |
} | |
const createDoc = (body) => { | |
const doc = new this.Model(body) | |
return doc.save() | |
} | |
const readDoc = (id) => { | |
return this.Model.findById(id) | |
} | |
const updateDoc = (id, body) => { | |
return this.Model.findByIdAndUpdate(id, body, { new: true }).exec() | |
} | |
const appendChildDoc = (id, body) => { | |
return this.Model.findByIdAndUpdate(id, { $push: body }, { new: true }).exec() | |
} | |
const deleteDoc = (id) => { | |
return this.Model.findByIdAndDelete(id).exec() | |
} | |
return { | |
getDocs, | |
queryDocs, | |
queryUpdateDocs, | |
createDoc, | |
readDoc, | |
updateDoc, | |
appendChildDoc, | |
deleteDoc | |
} | |
} | |
get requestHandlers() { | |
let handleSuccess = (res, result) => { | |
return res.status(200).json(result) | |
} | |
let handleError = (res, error) => { | |
console.error(error.message) | |
return res.status(500).json({ status: status[500] }) | |
} | |
const retrieveDocuments = (req, res) => { | |
this.dbMethods.getDocs() | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
const queryDocuments = (req, res) => { | |
this.dbMethods.queryDocs(req.body) | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
const queryUpdateDocuments = (req, res) => { | |
this.dbMethods.queryUpdateDocs(req.filter, req.body) | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
const createDocument = (req, res) => { | |
this.dbMethods.createDoc(req.body) | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
const readDocument = (req, res) => { | |
this.dbMethods.readDoc(req.params.id) | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
const updateDocument = (req, res) => { | |
this.dbMethods.updateDoc(req.params.id, req.body) | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
const appendChildDocument = (req, res) => { | |
this.dbMethods.appendChildDoc(req.params.id, req.body) | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
const deleteDocument = (req, res) => { | |
this.dbMethods.deleteDoc(req.params.id) | |
.then(result => handleSuccess(res, result)) | |
.catch(error => handleError(res, error)) | |
} | |
return { | |
retrieveDocuments, | |
queryDocuments, | |
queryUpdateDocuments, | |
createDocument, | |
readDocument, | |
updateDocument, | |
appendChildDocument, | |
deleteDocument | |
} | |
} | |
get router() { | |
const ctrl = this.requestHandlers | |
router.route(this.routes.base) | |
.get(ctrl.retrieveDocuments) | |
.post(ctrl.createDocument) | |
router.route(this.routes.id) | |
.get(ctrl.readDocument) | |
.put(ctrl.updateDocument) | |
.patch(ctrl.appendChildDocument) | |
.delete(ctrl.deleteDocument) | |
router.route(this.routes.query) | |
.post(ctrl.queryDocuments) | |
return router | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* middleware/index.js */ | |
const StandardController = require("./controller") | |
module.exports = { | |
Book: new StandardController("Book") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment