npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crud-mongoose-simple

v2.0.5

Published

crud mongoose

Downloads

23

Readme

crud-mongoose-simple

Build Status

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 = 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 Id

Server 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);
    }
});