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

mongoose-datatable

v4.3.0

Published

Server side dataTable request support for mongoose

Downloads

482

Readme

mongoose-dataTable

Version 3.0.0

Server side dataTable request support for mongoose.

Support mongoose version >= 3.8.0

Support mongoDB version >= 2.4

Support DataTable >= 1.10

New Functionnalities

  • Population of ref sub-document or ref sub-documents are done with $lookup in aggregation
  • Filter and sort are possible on populated sub-documents
  • add regex search on string fields

Migration

v1.x.x

var DataTable = require('mongoose-datatable');
var options = { debug: true, verbose: true };
DataTable.configure(options);
mongoose.plugin(Datatable.init);

...

model.dataTable(query, {}, function(err, data) {
  if (err) { return manageError(err); }
  console.log('send data...');
  res.send(data);
});

v2.x.x [typescript]

import Datatable from './datatable';
const nologger = { debug: () => { }, info: () => { }, warn: () => { }, error: () => { } } as any;
var options = { logger: nologger };
Datatable.configure(options);
mongoose.plugin(Datatable.init);

...

model.dataTable(query)
  .then((data: any) =>  res.send(data))
  .catch(err => manageError(err))

Install

npm install mongoose-datatable

Loading

import Datatable from './datatable';

Configuration

DataTable.configure(options);

Options

Configuration is not mandatory, the default options contains only the default handlers, for other types, an unknown type message is displayed on the console.

Logger

option key: logger If the options contains a logger [key: logger]: ILogger, the default logger console is override by it. It allows to use an application specific logger.

interface ILogger {
  debug: (...data: any) => void;
  info: (...data: any) => void;
  warn: (...data: any) => void;
  error: (...data: any) => void;
}

Condition handlers

option key: handlers The condition handlers is an object that contains handlers (functions) with for key, mongoose schema field type name. eg. String, Date, Boolean... It is possible to declare a default handler (key: default) that will be used if no handler found for a specific type. To not do any search the function should return null. These handlers are called when the module try to build the condition on a field. They can return a condition object for the field to match ( eg.: { $in: [...] } ) and have for arguments:

  • options: IOptions

    The options passed to the model database call.

  • query: IQuery

    The query passed to the model database call.

  • column: IColumn

    The jquery column data.

  • field: any

    The field against the condition is build. This is the mongoose FieldType.

  • search: ISearch

    The search data to build the condition.

  • global: boolean

    If true, the search condition is from jquery global search; if false, it's a specific search on thie field.

Default condition handlers
  • String

    • global
      Match anywere in the string in case insensitive mode.

    • specific
      If the search string is a regex (match: /^/./$/*) then the regex is used
      Else match anywere in the string in case insensitive mode.

  • Boolean

    • global
      No search done

    • specific
      Match true or false in case insensitive mode.

  • Date

    • global
      No search done

    • specific
      The date search is composed in three parts, the type of the match, the from value and the to value. The from and to value are String dates and the to value is only needed when the type is "<>" or "<=>". The type can be "=" (same as no type), ">", ">=", "<", "<=", "<>", "<=>" meaning respectively equals, greater than, greater or equal than, less than, less or equal than, between and between equals.

  • Number

    • global
      No search done

    • specific
      The number search is composed in three parts, the type of the match, the from value and the to value. The from and to value are number and the to value is only needed when the type is "<>" or "<=>". The type can be "=" (same as no type), ">", ">=", "<", "<=", "<>", "<=>" meaning respectively equals, greater than, greater or equal than, less than, less or equal than, between and between equals.

eg.

Initialization

const mongoose = require('mongoose');
mongoose.plugin(DataTable.init);

Fields

If a mongoose schema field is marked as not selectable with the option "select: false". Or if the option dataTableSelect is present and set to false: "dataTableSelect: false". Then the field will not be selected even if it was requested by the dataTable client.

Usage

The method datatable was added to all the schema as static method. The method has for parameters:

  • query

    The query parameters send by the dataTable client

  • options

    Options pass to the condition handlers. OPTIONAL parameter.

    • handlers

      Handlers can be given to override the overall behavior of condition builder handlers. The field options.handlers is an object with for keys either a field type (like String, Boolean,,,) or a field path (like username, name.firstName) and for values, functions like the condition handler functions.

    • conditions

      Conditions is an object as the mongoose find conditions. This conditions filter the dataTable data returned and the counts, it is applied as the first conjunction condition.

    • select

      Select is an object, a string or an array as the mongoose query select argument. The select is applied on the find query that return the displayed entities.

  • callback

    The callback called in case of error or when the data have been retrieved.

model.dataTable(query, options)
  .then((data: any) =>  res.send(data))
  .catch(err => manageError(err))

Chunk Search

- Not implemented yet

Change log]

  • v2.1.0
    • null search value are now intepreted as search for null value on field