jLinux

Untitled

Dec 11th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const CONFIG         = require('./config');
  4. const EXPRESS        = require( 'express' );
  5. const JADE           = require( 'jade' );
  6. const FS             = require( 'fs' );
  7. const BODYPARSER     = require( 'body-parser' );
  8. const METHODOVERRIDE = require( 'method-override' );
  9. const ERRORHANDLER   = require( 'error-handler' );
  10. const HTTP           = require( 'http' );
  11. const PATH           = require( 'path' );
  12. const APP            = module.exports = EXPRESS();
  13. const SEQUELIZE = require( 'sequelize' );
  14.  
  15. APP.set( 'models', require('./models') );
  16. APP.set( 'port', CONFIG.app.port );
  17.  
  18. APP.use( METHODOVERRIDE() );
  19. APP.use( EXPRESS.static( PATH.join( __dirname, 'public' ) ) );
  20.  
  21. //APP.use( BODYPARSER() );
  22.  
  23. // Load the Server Side Processing model
  24. var SSP = APP.get('models').SSP;
  25.  
  26. // Page loading the HTML DataTable
  27. APP.get( '/', ( req, res ) => {
  28.     var pageContent = JADE.renderFile('templates/container.jade', {
  29.         sources: CONFIG.sources,
  30.         pageContent: JADE.renderFile('templates/datatable.jade')
  31.     });
  32.  
  33.     res.send( pageContent );
  34. });
  35.  
  36. // Start a new SSP result by creating an object with the draw integer
  37. function newSspResult( sspParams ) {
  38.     return new Promise(( resolve, reject ) => {
  39.         resolve(sspParams, {
  40.             draw: parseInt( sspParams.draw ),
  41.             data: []
  42.         });
  43.     })
  44. }
  45.  
  46. // Get the total rows in the table, append it to the SSP result object
  47. function totalRows( sspParams, sspResult ) {
  48.     console.log('sspResult',typeof sspResult);
  49.  
  50.     return new Promise( ( resolve, reject ) => {
  51.         SSP.findOne({
  52.             attributes: [[SEQUELIZE.fn('COUNT', SEQUELIZE.col('*')), 'total_rows']]
  53.         }).then(( result ) => {
  54.             sspResult.recordsTotal = result.dataValues.total_rows;
  55.  
  56.             resolve( sspParams, sspResult );
  57.         });
  58.     })
  59. }
  60.  
  61. function execSspQuery( sspParams, sspResult ){
  62.     var query = {};
  63.  
  64.     if(typeof sspParams.length !== 'undefined' && parseInt(sspParams.length) !== 0)
  65.         query.limit = parseInt(sspParams.length);
  66.  
  67.     if(typeof sspParams.start !== 'undefined')
  68.         query.offset = parseInt(sspParams.start);
  69.  
  70.     query.order = [];
  71.  
  72.     // Check if were supposed to be ordering anything..
  73.     if( typeof sspParams.order !== 'undefined' )
  74.         sspParams.order.forEach(( elem, key, arr ) => {
  75.             query.order.push([ SEQUELIZE.col(`dtCol_${elem.column}` ), elem.dir]);
  76.         });
  77.  
  78.     SSP.findAll( query ).then( ( result ) => {
  79.  
  80.         result.forEach( (element, index, arr) => {
  81.             var thisRow = [];
  82.             sspParams.columns.forEach( (cElement, cIndex, cArr) => {
  83.                 thisRow.push( element.dataValues[`dtCol_${cElement.data}`] )
  84.             });
  85.             sspResult.data.push( thisRow );
  86.         });
  87.  
  88.         resolve( sspResult );
  89.     });
  90. }
  91.  
  92. SSP.utils.APP = APP;
  93.  
  94. APP.get( '/ssp', ( req, res ) => {
  95.     // Inbound SSP Query
  96.     var sspParams = req.query;
  97.     var dbQuery = {};
  98.  
  99.     newSspResult( sspParams )
  100.         .then(totalRows)
  101.         .then(execSspQuery)
  102.         .then(( sspResult ) => {
  103.             console.log( 'sspResult',sspResult );
  104.             res.send( JSON.stringify( sspResult ) );
  105.         })
  106.         .catch((err) => {console.log(`ERROR: ${err}`)});
  107. });
  108.  
  109. HTTP.createServer( APP ).listen( APP.get( 'port' ), ( ) => {
  110.     console.log( `Express server listening on port ${APP.get( 'port' )}` );
  111. });
Advertisement
Add Comment
Please, Sign In to add comment