crud-mongoose-simple
Simple List, Create, Read, Update and Delete requests for a given Mongoose model. Create Express Route easily.
Install
$ npm install crud-mongoose-simple
Plugin Static Functions
- Model.httpGet(req, res);
- Model.httpPost(req, res);
- Model.httpPut(req, res);
- Model.httpDelete(req, res);
- Model.countItems(req, res);
- Model.totalPages(req, res);
- Model.registerRouter(req, res);
Server Setup With Manual Route
var express = ;var router = express; var mongoose = ;var crud = ;mongoose; var personSchema = name: first: String last: String age : Number accupation : String likes : ; var personModel = mongoose; router // Get all items by filterrouter; // Create new Item router // Get Item by Id // Update an Item with a given Id ; // Delete and Item by Id
Server Setup With Auto Route
var express = ;var router = express; var mongoose = ;var crud = ;mongoose; var personSchema = name: String; var personModel = mongoose; personModel; /** * It get routes: * GET - http://localhost:3000/api/v1/{modelName}/list - Get all items by filter * POST - http://localhost:3000/api/v1/{modelName}/ - Create new Item * PUT - http://localhost:3000/api/v1/{modelName}/:id - Update an Item with a given Id * DELETE - http://localhost:3000/api/v1/{schemaName}/:id - Delete and Item by Id */
Server Custom Route with ApiQuery
var express = ;var router = express; var mongoose = ;var crud = ;mongoose; var personSchema = fristName: String lastName: String; var personModel = mongoose; router
Server Schema Query
var express = ;var router = express; var mongoose = ;var crud = ;mongoose; var personSchema = fristName: String lastName: String query : pageSize : 25 sort : '-firstName' select : 'firstName lastName' ; var personModel = mongoose; //items filter by Schema Qeuryrouter
Example Call From Client Side By jQuery:
List
Get List with query params working all mongoose query var query = where : {} skip: 10 limit: 20 ; querywhere= 'occupation': "$regex": "host" "$options": "i" 'name.last': 'Ghost' 'age': $gt: 17 $lt: 66 'likes': $in: 'vaporizing' 'talking' ; queryselect = 'name occupation'; querysort = '-occupation'; $;
List Pagination
var query = where : {} pageSize : 25 page : 1 ; queryselect = 'name occupation'; querysort = '-occupation'; $;
Create
var data = name : first : "Giga" last : "Chkhikvadze" age : 50 $;
Read
var id = '578d33f2d0920b0db20f8643'; $;
Edit
var id = '578d33f2d0920b0db20f8643'; var data = name : first : "Giga" last : "Chkhikvadze" age : 50 $;
Delete
var id = '578d33f2d0920b0db20f8643'; $;