moncrud-leo v0.1.18
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
- /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.
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago