# moncrud-leo ## using MongoDB with Mongoose and Express Create a MongoDB Model and give it CRUD (Create, Read, Update, Delete) controls on an API route. ### Configure moncrud ```js 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 ```js // 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 } } }); ``` ### Implementing DataTables AJAX request, pagination #### Feature is emplemented at root level, off by default ```html First NameLast NameDate Created
``` ```js // Client-side JavaScript // You must include jquery.js, datatables.js $(document).ready(function () { $(".demo-table").DataTable({ processing: true, serverSide: true, ajax: { url : '/api/users/dt', type: 'post' }, columns: [ { data: 'firstname' }, { data: 'lastname' }, { data: 'DateCreated' } ] }); }); ``` ```js // Server-side script // this initates a model, just like you see above mc.model('users', { firstname: String, lastname: String, email: String, password: String, }).crud({ // enable features (V0.1.* only supports dataTable) features: { //add the settings for a feature that you want enabled. //currently, this only has a datatables implementation . //there will be more customization in future versions. dataTables: { // now, let's specify to sort dateCreated by date in reverse. // By default, the sort is 1, or Acending // Dates are a bit different, so they need to be sorted Descening, thus -1 sort: { dateCreated: -1, } } } }); ``` ### This is a basic layout of the documention for this handy utility, more will come later.