mongoose-datatables-serverside

1.0.0 • Public • Published

mongoose-datatables-serverside

mongoose-datatables-serverside simplifies server-side DataTables integration with MongoDB using Mongoose. Easily construct and execute queries, enabling advanced data searching, sorting, and pagination for your web applications.

npm npm NPM

Table of Contents

Installation

You can install mongoose-datatables-serverside via npm:

npm install mongoose-datatables-serverside

Usage

Here's how you can use this package to create server-side DataTables queries for your Node.js application:

const express = require('express');
const mongoose = require('mongoose');
const moment = require('moment');
const mongooseDataTableQuery = require('mongoose-datatables-serverside');

// Create Express app
const app = express();

// Connect to MongoDB
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });

// Users DataTable
app.post('/api/users', async (req, res) => {
    try {
        // Define query options
        const queryOptions = req.body;
        const moreQueryOptions = {
            createdAt: {
                $gte: moment().subtract(30, 'days').startOf('day').toDate(), // Last 30 days
            }
        }

        // Execute the query for DataTables
        const data = await mongooseDataTableQuery(User, queryOptions, moreQueryOptions);

        // Respond with the query result as JSON
        res.json(data);
    } catch (error) {
        console.error(error);
        res.status(500).json({ error: error.message });
    }
});

// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});

Getting Started

  1. Install the package via npm as described in the Installation section.

  2. Create an Express.js application and connect to your MongoDB database.

  3. Define a route for handling DataTables queries. In the example, this is done for the /api/users endpoint.

  4. In the route handler, define query options based on the DataTables request parameters and execute the query using mongooseDataTableQuery.

  5. Respond with the query result as JSON to be consumed by the DataTables frontend.

Issues

If you encounter any issues or have questions about this package, please open an issue on GitHub.

Acknowledgments

  • This package was inspired by the need for simplified server-side DataTables integration with MongoDB using Mongoose.

Author

Package Sidebar

Install

npm i mongoose-datatables-serverside

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

8.37 kB

Total Files

3

Last publish

Collaborators

  • anasqiblawi