Giter Site home page Giter Site logo

rodrigotorresweb / mongo-datatable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dycodedev/mongo-datatable

0.0 2.0 0.0 50 KB

NodeJS module for server-side processing using jquery datatables and mongodb native driver.

License: MIT License

JavaScript 86.40% CSS 0.81% HTML 12.79%

mongo-datatable's Introduction

Mongo DataTable

NodeJS module for server-side processing using jquery datatables and mongodb native driver.

Supports:

  • datatables >= 1.10
  • mongodb >= 2.0 (native driver)
  • MongoDB >= 2.4

Install

npm install mongo-datatable

Documentation

This module returns MongoDataTable constructor when loaded using require.

MongoDataTable(db)

This constructor takes one argument and must be instantiated using new keyword.

Argument:

  • db - An instance of Db from mongodb module.

MongoDataTable.prototype.get(collection, options, callback)

This method validates the options argument and checks the connection to database. If the options is invalid or there is no connection made to database, the callback will be called immediately with error. If everything is ok, the callback will be called with result.

Arguments:

  • collection (String) - A string represents name of a collection in your database.
  • options (Object) - An object identic to sent parameter by jquery datatables.
  • callback(error, result) (Function) - The result parameter is an object identic to returned data to jquery datatables.

Extra Options:

  • showAlertOnError (Boolean) - If this field is set to true and callback is called with error, the error message will be displayed to the user by the datatables. The default value is false.
  • customQuery (Object) - Add custom query. Suppose you have a user collection with each user has either admin or user role and you want to display only users with admin role. You can add something like { role: 'admin' } to this field. This query has higher precedence over constructed query.

Search Operation

  • If both individual column and global search value are not given, then the search query will be an empty object. Therefore this method will fetch all documents inside the collection.

  • If there is no individual column search value is given and global search value is given, then the global search value will be used as each column's search value. Then, the search query will be like { $or: [{ column_0: value }, ... , { column_n: value }] }.

  • If there is one or more individual column search value is given and the global search value is not given, then the search query will be like { column_0: value_0, ... , column_n: value_n }.

  • If both individual column and global search value are given, then the search query will be like { column_0: value_0, column_1: value_1, $or: [{ column_2 : value }, ... , { column_n: value }] }.

There's More: You can search data in a specific column using global search input element with column_name:value format. This will be useful if you want to search data in a specific column or field but you don't want to display search input element for that column.

Note that this will work only if you specify name in columns configuration. See columns.name configuration.

Usage

Examples below are using express >= 4.0

  • Using MongoClient
var express = require('express');
var mongodb = require('mongodb');
var MongoDataTable = require('mongo-datatable');
var MongoClient = mongodb.MongoClient;
var router = express.Router();

router.get('/data.json', function(req, res, next) {
  var options = req.query;
  options.showAlertOnError = true;
  options.customQuery = {
    role: 'user'
  };

  MongoClient.connect('mongodb://localhost/database', function(err, db) {
    new MongoDataTable(db).get('collection', options, function(err, result) {
      res.json(result);
    });
  });
});
...
  • Using Db and Server
var express = require('express');
var mongodb = require('mongodb');
var MongoDataTable = require('mongo-datatable');
var Db = mongodb.Db;
var Server = mongodb.Server;
var router = express.Router();

router.get('/data.json', function(req, res, next) {
  var options = req.query;
  var db = new Db('database', new Server('localhost', 27017));

  options.showAlertOnError = true;
  options.customQuery = {
    role: 'user'
  };

  db.open(function(error, db) {
    new MongoDataTable(db).get('collection', options, function(err, result) {
      res.json(result);
    });
  });
});
...

mongo-datatable's People

Contributors

alwint3r avatar bentinata avatar ratson avatar

Watchers

James Cloos avatar Rodrigo Torres avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.