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

moncrud-leo

v0.1.18

Published

MongoDB+Mongoose CRUD handler. Designed to help reduce the amount of code needded to add simple [find, find-one, add, update, remove] AJAX calls.

Downloads

15

Readme

moncrud-leo

using MongoDB with Mongoose and Express

moncrud-leo package helps creating a model with Ajax Create, Read, Update, Delete structure. Because in many scenarios, I needed same types of AJAX calls to manipulate and retrieve data, and I got tired of writing tons of code for it, I have descieded to credate a package for it.

It takes care of creating the model, creating all standard data calls, feeding data to DataTables with server-side search, sorting and pagination, and, of course, custom actions.

It is very easy to implement and set up this engine!

https://github.com/lbarsukov/moncrud-leo

Basics

Initialize moncrud-leo

var mc = require('moncrud-leo');

mc.init(app, {
	apidomain: 'api',
	connection: 'mongodb://localhost/collection'
}, function(data, next){
	var hasAccess = true;
	next(hasAccess); // true/false return

}); 

More Options and Details

var mc = require('moncrud-leo');
// this depends on express, give the initializer the [app] element
mc.init(app, {
	//settings
	// more details will be available later
	apidomain: 'api',
	connection: 'mongodb://localhost/collection'
}, function(data, next){
	var hasAccess = true;
	//authenticate	
	/*
	 * data gives you details about the request,
	 */
	next(hasAccess); // true/false return 
	// this will return a response to the query based on a boolean.
	// if user is qualified after this test, the query is executed.
	// otherwise client get's an error response.
}); 

Using moncrud

// require moncrud
var mc = require('moncrud-leo');
// create a model
var users = mc.model('users', {
	firstname: String,
	lastname: String,
	email: String,
	active: Boolean,
	username: String,
	password: String,
	roles: ['basic'],
	friends: [{ 
		type: mc.ObjectId,
		ref: 'users'		
	}]
	// etc. Use the standard model parameters you would use for Mongoose	
});
// After the model is created, you can initate CRUD
// basic CRUD initializer:
users.crud();
// users.crud() function initiates HTTP\post calls: '/api/users/' [find, find-one, add, update, remove]
// you can also customoze the controller
users.crud({
	populate: {
		'get': ['friends'],
		'get-all': []				
	},
	custom: {
		'add-friend':function(req,res,model){
			// execute you standard calls here for a custom controller			
		}		
	}
		
});

This produces URL's to work with the data

  1. /api/users/find-one
  • /api/users/find

  • /api/users/add

  • /api/users/update

  • /api/users/remove

All methods are 'post' methods, because they can all send data to use 'find-one', you feed parameters, such as '_id' as an AJAX data request jQuery action looks like this:

$.post('/api/users/find-one', { _id: 's98d7fs9d87f7d89d87fd8d8' }, function(data){
	// data response comes here //
});

This method becomes the the one you can use for any method. Of course, if you are adding the data, you would need to specify all the properties.

Client-side JavaScript

You must include jquery.js, datatables.js please go to https://www.datatables.net/examples/data_sources/server_side.html for more details.

The example on the provided link is focusing on php, so it's not very relevent. This controller sends data as objects, not arrays; thus the "columns" element will be required.

$(document).ready(function () {
	$(".demo-table").DataTable({
		processing: true, 
		serverSide: true, 
		// notice that the request url is set to [model]/dt
		// /dt is an internal DataTables action which will automatically take care of search
		// and sort of data coming into DataTables
		ajax: { url : '/api/users/dt', type: 'post' }, 
		columns: [
			{ data: 'firstname' }, 
			{ data: 'lastname' },
			{ data: 'email' },
			{ data: 'dateCreated' },
			{ data: 'dateUpdated' }
		]
	});
});
// Server-side script
// this initates a model, just like you see above
mc.model('users', {
	// this should be structured just like a standard 
	// Mongoose model. 
	firstname: String,
	lastname: String,
	email: String,
	password: String,
	// reference records
	roles: [{ type: mc.ObjectId, ref: 'roles' }],
	friends: [{ type: mc.ObjectId, ref: 'users' }]
}).crud({
	custom: {		
		'add-friend': function (req, res, user) {
			var u = req.body;
			user.model.update({ _id: u._id }, { $push: { friends: u.friend } }, function (err, data) {
				if (err) return mc.error(res, err, data);
				res.json(data);
			});
		}
	},
	populate: {
		'find-one': ['friends'],
	},
	features: {
		dataTables: {
			sort: {
				// to make sure sorting is done correctly for dates
				dateCreated: -1, 
				dateUpdated: -1
			},			
		}
	}
});

This is a basic layout of the documention for this handy utility, more will come later.