2.0.5 • Published 10 years ago
crud-mongoose-simple v2.0.5
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-simplePlugin 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 = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var crud = require('crud-mongoose-simple');
mongoose.plugin(crud);
var personSchema = new mongoose.Schema({
name: {
first: String,
last: String
},
age : Number,
accupation : String,
likes : []
});
var personModel = mongoose.model('Person', personSchema);
router.route('/person/list').get(personModel.httpGet()) // Get all items by filter
router.route('/person/').post(personModel.httpPost()); // Create new Item
router.route('/person/:id')
.get(personModel.httpGet()) // Get Item by Id
.put(personModel.httpPut()) // Update an Item with a given Id
.delete(personModel.httpDelete()); // Delete and Item by IdServer Setup With Auto Route
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var crud = require('crud-mongoose-simple');
mongoose.plugin(crud);
var personSchema = new mongoose.Schema({
name: String
});
var personModel = mongoose.model('Person', personSchema);
personModel.registerRouter(router, '/api/v1/');
/**
* 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 = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var crud = require('crud-mongoose-simple');
mongoose.plugin(crud);
var personSchema = new Schema({
fristName: String,
lastName: String
});
var personModel = mongoose.model('Person', personSchema);
router.route('/person/listbyuser').get(function(req, res, next){
var query = {
where : {
userId : '578d33f2d0920b0db20f8643'
},
pageSize : 25,
sort : '-firstName',
select : 'firstName lastName',
populate : ['user']
};
req.apiQuery = query;
next();
}, personModel.httpGet())Server Schema Query
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var crud = require('crud-mongoose-simple');
mongoose.plugin(crud);
var personSchema = new Schema({
fristName: String,
lastName: String
},{
query : {
pageSize : 25,
sort : '-firstName',
select : 'firstName lastName'
}
});
var personModel = mongoose.model('Person', personSchema);
//items filter by Schema Qeury
router.route('/person/list').get(personModel.httpGet())##Example Call From Client Side By jQuery:
List
Get List with query params. (working all mongoose query)
var query = { where : {}, skip: 10, limit: 20 };
query.where= {
'occupation': { "$regex": "host", "$options": "i" },
'name.last': 'Ghost',
'age': { $gt: 17, $lt: 66 },
'likes': { $in: ['vaporizing', 'talking'] }
};
query.select = 'name occupation';
query.sort = '-occupation';
$.get('http://localhost:3000/api/person/list', query, function(result, status){
console.log(result);
});##List Pagination
var query = { where : {}, pageSize : 25, page : 1 };
query.select = 'name occupation';
query.sort = '-occupation';
$.get('http://localhost:3000/api/person/list', query, function(result, status){
console.log(result);
});##Create
var data = {
name : {first : "Giga",
last : "Chkhikvadze" },
age : 50
}
$.post('http://localhost:3000/api/person/', data, function(result){
console.log(result);
});##Read
var id = '578d33f2d0920b0db20f8643';
$.get('http://localhost:3000/api/person/' + id, function(result, status){
console.log(result);
});##Edit
var id = '578d33f2d0920b0db20f8643';
var data = {
name : {first : "Giga",
last : "Chkhikvadze" },
age : 50
}
$.ajax({
url: 'http://localhost:3000/api/person/' + id,
type: 'PUT',
success: function(result) {
console.log(result);
}
});##Delete
var id = '578d33f2d0920b0db20f8643';
$.ajax({
url: 'http://localhost:3000/api/person/' + id,
type: 'DELETE',
success: function(result) {
console.log(result);
}
});2.0.5
10 years ago
2.0.4
10 years ago
2.0.3
10 years ago
2.0.2
10 years ago
2.0.1
10 years ago
2.0.0
10 years ago
1.9.9
10 years ago
1.9.8
10 years ago
1.9.7
10 years ago
1.9.6
10 years ago
1.9.5
10 years ago
1.9.4
10 years ago
1.9.3
10 years ago
1.9.1
10 years ago
1.9.0
10 years ago
1.8.0
10 years ago
1.7.0
10 years ago
1.5.0
10 years ago
1.4.0
10 years ago
1.3.0
10 years ago
1.2.0
10 years ago
1.1.0
10 years ago
1.0.0
10 years ago