Git Product home page Git Product logo

mongoose-datatables's Introduction

mongoose-datatables

Server side table request.

Build Status

Installation

$ npm install mongoose-datatables

Usage

Configure the plugin in your model

var mongoose = require('mongoose')
var dataTables = require('mongoose-datatables')
var Schema = mongoose.Schema

var UserSchema = new Schema({
  first_name: String,
  last_name: String,
  username: String
})

UserSchema.plugin(dataTables)

Use plugin in your route

app.post('/table', (req, res) {
  User.dataTables({
    limit: req.body.length,
    skip: req.body.start,
    search: {
      value: req.body.search.value,
      fields: ['username']
    },
    sort: {
      username: 1
    }
  }).then(function (table) {
    res.json(table); // table.total, table.data
  })
});

API

plugin([options])

  • options.totalKey (String) - Default total
  • options.dataKey (String) - Default data
  • options.formatters (Object) - Specifies multiple formatters that can be used in the query

dataTables([options], [callback])

  • options.limit (Number) - Specifies mongo limit.
  • options.skip (Number) - Specifies mongo skip.
  • options.find (Object) - Specifies selection criteria.
  • options.select (Object) - Specifies the fields to return.
  • options.sort (Object) - Specifies the order in which the query returns matching documents.
  • options.order, options.columns (Array), (Array) - Specifies the order in which the query returns matching documents and replace the sort option
  • options.search (Object) - Search.
  • options.populate (Object) - Specifies models to populate.
  • options.formatter (String|Function) - Specifies formatter to use after the query.

Formaters

At query level

User.dataTables({
  limit: 20,
  formatter: function(user) {
    return {
      name: user.first_name + ' ' + user.last_name
    }
  }
})

At schema level

UserSchema.plugin(dataTables, {
  formatters: {
    toPublic : function (user) {
      return {
        name: user.first_name + ' ' + user.last_name
      }
    }
  }
});

Use by doing this at query level

User.dataTables({
  limit: 20,
  formatter: 'toPublic',
})

Note: if you use formatters you get an array of objects on data, instead of the model instance.

License

MIT

mongoose-datatables's People

Contributors

archr avatar siedrix avatar umarmw 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.