How i can implement server processing node.js + express + mongoose + mongodb

How i can implement server processing node.js + express + mongoose + mongodb

Jaider2523Jaider2523 Posts: 5Questions: 1Answers: 0

Hi,

Currently I'm using a lot datatables, but yet my records are many, need paging from the server, but the information is very little( For javascript with node...there is an example, but with mysql....). Can someone help me with an example.

I tried to mongoose-datatable, the only half decent plugin, but support and documentation in short, does not work for searches and filtering. I need help with extreme urgency.

Thanks.

Answers

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    If you are going to write server side code (which I don't recommend unless you have 1000's of records), then you need to write the code to handle all the background stuff that datatables does for you normally.

    Your ajax page must read the request variables that are passed from the dataTables to your ajax source. You want to look at the start and length request variables.

    Start is the record you want to start returning at, length is the number of records. You must format your response in json, no html.

  • Jaider2523Jaider2523 Posts: 5Questions: 1Answers: 0

    Hi,

    Thank you for your quick answer, yes, now I have more than 1000 records. I need to synchronize with datatables.net mongoose, as I said I've tried, but not working. All examples are for PHP, but none of these new technologies, and create my own datatable is too long. I need to solve this problem, any help is welcome.

    Thanks

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    I don't use PHP either. I write my own ajax page, you should do the same. Your ajax page needs to return json formatted data. That is the key to dataTables, it works using the industry standard json.

    I'm assuming that you already know who to open your database, make a record set query to your database, and dump the output directly to your browser response.

    Start by just creating a query to return the first 100 records, for instance.
    Make the connection, create the SQL with a limit of 100 records, then iterate over the 100 records formatting the data into a json data. You don't even need dataTables for this as you are only trying to get a valid json response.

    Once you have that, then you want to examine the start var and length var. They are passed in the ajax request of dataTables to your ajax page. Once you have those, you create the sql with the starting record of "start" and a limit (or length) of "length". Again datatTables needs the JSON data, you can use any language (php, c#, vb, etc.) to create the json data.

  • allanallan Posts: 63,230Questions: 1Answers: 10,417 Site admin

    The documentation required for creating your own server-side processing script is available in the manual.

    Allan

  • Jaider2523Jaider2523 Posts: 5Questions: 1Answers: 0

    Hi,

    Thanks glenderson and allan, for you answer.

    glenderson, yes I'm using json, my application is server-side JavaScript and the backend (Jade, Javascript, Express, Node.js, Mongoose, MongoDB...).

    My main idea is to do this: https://www.datatables.net/examples/data_sources/server_side.html
    But in backend javascript, your recommendation is very good, and mongo node, skip and limit used to perform the task that you say, but not that way I can set pagination to display the information. We must also use the search in real time as in the example with PHP, in all registers.

    Allan, your project is well documented and I congratulate you. My question is how can set (setting values) these options from my backend. Because from my API Rest, just sent a json, but I need pagination due to big size. Or exist other way, as indicated glenderson from the frontend.

    Thanks for your help.

  • allanallan Posts: 63,230Questions: 1Answers: 10,417 Site admin

    I'm afraid I don't understand your question. The request that DataTables sends will contain the page length and start point (per the documentation I linked to above). You'd server-side service needs to handle that data and return the JSON required.

    If you need to modify the parameters being sent to the server use ajax.data.

    Allan

  • Jaider2523Jaider2523 Posts: 5Questions: 1Answers: 0
    edited January 2016

    HI,

    Sorry for my bad explanation.

    I have a REST API, have large queries with MongoDB data base. I need to synchronize the paging of the table with my query. Eg. paging: db.users.find().skip(10).limit(10).

    My API REST

    My route without paging (for the moment). Express:

      app.get('/equipments', passportConf.isAuthenticated, function (req, res) {
        equipment.find({}, function (err, eqps) {
          if (err) {
            return (err, null);
          }
          res.json(eqps);    // big JSON, 
        });
      });
    

    My Javascript FRONTEND APP:

    $.getJSON( '/equipments', function( queryResult ) {
      var table = $('#datatable').DataTable( {
        responsive: true,
        autoWidth: false,
        data: queryResult,
        columns: [
            { data: 'tag',"defaultContent": "",width: '25%', responsivePriority: 1, targets: 0 },
            { data: 'description',"defaultContent": "", width: '30%', targets: 1},
            { data: 'created' ,"defaultContent": "", width: '25%', targets: 2,'render': function ( data, type, row ) {
                    return moment(data).format('MMMM Do YYYY');
            }},
            ]
          } );
    });
    

    the problem is that as much information table very slow load. My idea is to send small parts of the JSON file and synchronize with paging and searching directly with the database (Server-Process).

    I tried to https://github.com/eherve/mongoose-datatable, but conditions do not work, and the creator does not respond in time.

    Thanks

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,230Questions: 1Answers: 10,417 Site admin

    Your code above doesn't use server-side processing (serverSide). It sounds like you want to use server-side processing.

    I haven't published a NodeJS server-side processing script for DataTables, but if you want to create one yourself, use the documentation that is available for server-side processing.

    Allan

  • Jaider2523Jaider2523 Posts: 5Questions: 1Answers: 0

    Hi Allan,

    Thanks for your answer, I will study the documentation to set my table.

    Thanks.

  • renatoliibkerenatoliibke Posts: 1Questions: 0Answers: 0

    Hi Guys!

    I'm new on Nodejs but already worked with datatables ajax method with php.

    I found this module for node.js on npm.

    https://www.npmjs.com/package/datatables-query

    maybe help u.

    I did a restful api with node-restful module, and now i'm trying to paginate de results for datatables too.

This discussion has been closed.