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

mongo-datatable

v1.1.1

Published

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

Downloads

229

Readme

Mongo DataTable

Node.js module for server-side processing using jQuery datatables and MongoDB native driver.

Supports:

  • jQuery Datatables v1.10
  • mongodb native driver v2.0 and later
  • MongoDB database server v2.4 and later

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.
  • caseInsensitiveSearch (Boolean) - To enable case insensitive search, set this option value to true. It is case sensitive by default.

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

These examples assume that you are using Express v4

  • 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;

  /**
   * Using customQuery for specific needs such as
   * filtering data which has `role` property set to user
   */
  options.customQuery = {
    role: 'user'
  };

  /* uncomment the line below to enable case insensitive search */
  // options.caseInsensitiveSearch = true;

  MongoClient.connect('mongodb://localhost/database', function(err, db) {
    new MongoDataTable(db).get('collection', options, function(err, result) {
      if (err) {
        // handle the error
      }

      res.json(result);
    });
  });
});
...
  • With MongoDB native driver v3
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;

  /**
   * Using customQuery for specific needs such as
   * filtering data which has `role` property set to user
   */
  options.customQuery = {
    role: 'user'
  };

  // uncomment the line below to enable case insensitive search
  // options.caseInsensitiveSearch = true;


   /**
    * MongoDB native driver v3, MongoClient.connect no longer yields instance of Db.
    * It yields the instance of MongoClient instead.
    * To get Db instance, you can call `db` method of client with the database name as the argument
    */
  
  MongoClient.connect('mongodb://localhost/database', function(err, client) {
    var db = client.db('database');
    new MongoDataTable(db).get('collection', options, function(err, result) {
      if (err) {
        // handle the error
      }

      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;

  /**
   * Using customQuery for specific needs such as
   * filtering data which has `role` property set to user
   */
  options.customQuery = {
    role: 'user'
  };

  // uncomment the line below to enable case insensitive search
  // options.caseInsensitiveSearch = true;

  db.open(function(error, db) {
    new MongoDataTable(db).get('collection', options, function(err, result) {
      if (err) {
        // handle the error
      }

      res.json(result);
    });
  });
});
...