mongodb, mongoose를 활용한 CRUD api. node model을 생성한 후 각각의 CRUD api를 작성할 필요없이 사용가능하다.

mongodb, mongoose를 활용한 CRUD api. node model을 생성한 후 각각의 CRUD api를 작성할 필요없이 사용가능하다.
MongoDB를 express에서 사용할 때 Model,Router,Controller로 나누어 집행한다.. 아래의 예제는 CRUD를 위한 이에 대한 각각의 예제이다.
var mongoose = require("mongoose")const Schema = mongoose.Schemaconst models = {}const allpurposeSchema = new Schema({title: String,desc: String,formid: String,data: Schema.Types.Mixed,author: Schema.Types.Mixed,})models.Allpurpose = mongoose.model("Allpurpose", allpurposeSchema)module.exports = models
const crud = require("../controllers/reuseCRUD");//controller//route index에서 express 전송(app)module.exports = (app) => {const model = require("../models/model");app.use("/allpurpose", crud(model.Control));
var mongoose = require("mongoose");const Schema = mongoose.Schema;const models = {};const allpurposeSchema = new Schema({title: String,desc: String,formid: String,data: Schema.Types.Mixed,author: Schema.Types.Mixed,});models.Allpurpose = mongoose.model("Allpurpose", allpurposeSchema);module.exports = models;$$$$$|javascript|controller|재사용가능한 CRUD로 모든 모델에서 공통사용모든 모델을 한곳에서 처리함./model/models.js /router/reuseCRUD.js에서 접근처리const express = require("express");module.exports = (Collection) => {// ======// Create// ======const create = (req, res) => {const newEntry = req.body;Collection.create(newEntry, (e, newEntry) => {if (e) {console.log(e);res.sendStatus(500);} else {res.send(newEntry);}});};// =========// Read many// =========const readMany = (req, res) => {let query = res.locals.query || {};Collection.find(query, (e, result) => {if (e) {res.status(500).send(e);console.log(e.message);} else {res.send(result);}});};// ========// Read one// ========const readOne = (req, res, next) => {/* reqest.query 경우예: http://localhost:3001/reuse/control/:_id?ctrid=gggg예: http://localhost:3001/reuse/control/:1?ctrid=ggggreq.param에 숫자가 있는 경우 숫자개만 리턴 1=>1개*/let { _id } = req.params;if (Object.keys(req.query).length > 0) {var query = Collection.find(req.query);if (typeof parseInt(_id) === "number") query = query.limit(parseInt(_id));query.exec(function (err, someValue) {if (err) return next(err);res.send(someValue);});return null;}/* request.param 경우예: http://localhost:3001/reuse/control/:_idcontrol/:_id*/Collection.findById(_id, (e, result) => {if (e) {res.status(500).send(e);console.log(e.message);} else {//res.send(result);res.send(result);}});};// ======// Update// ======const update = (req, res) => {const keycode = Object.keys(req.query);const changedEntry = req.body;// ======// MultiUpdate: array로 batch update처리// url은 "/mutiupdate?id="과 같이 전송// 현재는 multiupdate시 querystring 1개만 가능!!!!!!!!!! 수정할것// ======if (req.params._id == "multiupdate") {let bulk = Collection.collection.initializeUnorderedBulkOp();changedEntry.forEach((k, i) => {bulk.find({[keycode]: k[keycode],}).upsert().update({ $set: k });});bulk.execute((e, result) => {if (e) {res.status(500).send(e);console.log(e.message);} else {res.send(result);}});return false;}//single update인 경우let query = { _id: req.params._id };if (keycode.length > 0) {//querystring이 있을 경우 query를 대체함//localhost:3001/menu/<req.param._id>?qrystring=valuequery = req.query;}Collection.update(query, { $set: changedEntry }, (e) => {if (e) res.sendStatus(500);else res.sendStatus(200);});};// ======// Remove// ======const remove = (req, res) => {let query = { _id: req.params._id };//querystring이 있을 경우 query를 대체함if (Object.keys(req.query).length > 0) {query = req.query;}Collection.deleteOne(query, (e) => {if (e) res.status(500).send(e);else res.sendStatus(200);});};// ======// Routes// ======let router = express.Router();router.get("/", readMany);router.get("/:_id", readOne);router.post("/", create);router.put("/:_id", update);router.delete("/:_id", remove);return router;};