This is a simple filter sort paginate module designed to work with expressJS like frameworks.
var fsp = require('filter-sort-paginate'),
express = require('express'),
mysql = require('mysql'),
app = express()
;
app.use(function (req, res, next) {
req.mysql = ... //get mysql connection
});
app.get('/', fsp('table_name'))
The response will be an object that has the following structure:
{
items: [ { name: 'value' }, ... ],
total: 10, // total number of items produced by the query.
field_data: [ { /* column data from mysql */ ... ],
repository: 'table_name', // name of the current table.
fields: [ /* any currently specified fields */ ],
sorts: [ /* any currently specified sort fields */ ],
filters: [ /* any currently specified filters */ ],
limit: 100, // 100 is the default limit
page: 1, // page is one based
}
Given the following request URL
/?filter=test:is:something|this:gt:that&sort=+test|-that&page=2&limit=25
With the following set up.
app.get('/', fsp('table_name', [ 'test', { column: 'this', alias: 'me' }, 'that' ]));
The following response will be generated
{
items: [ { test: 'something', this: 'then', that: 'this' }, ... ],
total: 10, // total number of items produced by the query.
field_data: [ { /* column data from mysql */ ...} ],
repository: 'table_name', // name of the current table.
fields: [
{ column: 'test', alias: null },
{ column: 'this', alias: 'me' },
{ column: 'that', alias: null }
],
sorts: [
{ column: 'test', direction: '+' },
{ column: 'that', direction: '-' }
],
filters: [
{ column: 'test', action: 'is', value: 'something' },
{ column: 'this', action: 'gt', value: 'that' }
],
limit: 25, // 100 is the default limit
page: 2, // page is one based
}
- Fix exceptions in MySQL driver not being caught.
- Add concept of defaults to the options array.
These will be default filters, sorts, etc...