Browse Source

Fix #33: _size to return right number of records back.

pull/39/head
o1lab 7 years ago
parent
commit
cfa70c459c
  1. 3
      README.md
  2. 11
      lib/xsql.js
  3. 83
      tests/tests.js

3
README.md

@ -172,6 +172,9 @@ By default 20 records and max of 100 are returned per GET request on a table.
/api/payments?_p=2&_size=50
```
When _size is greater than 100 - number of records defaults to 100 (i.e maximum)
When _size is less than or equal to 0 - number of records defaults to 20 (i.e minimum)
## Order by / Sorting

11
lib/xsql.js

@ -193,15 +193,20 @@ class Xsql {
reqParams._index = 0;
reqParams._len = 20;
if ('_size' in reqParams && parseInt(reqParams._size) < 100) {
reqParams._len = parseInt(reqParams._size)
if('_size' in reqParams) {
if (parseInt(reqParams._size) > 0 && parseInt(reqParams._size) <= 100) {
reqParams._len = parseInt(reqParams._size)
} else if (parseInt(reqParams._size) > 100) {
reqParams._len = 100
}
}
if ('_p' in reqParams && parseInt(reqParams._p) > 0) {
reqParams._index = (parseInt(reqParams._p) - 1) * reqParams._len;
}
//console.log(reqParams._index, reqParams._len);
console.log(reqParams._index, reqParams._len);
return [reqParams._index, reqParams._len]

83
tests/tests.js

@ -241,6 +241,89 @@ describe('xmysql : tests', function () {
});
it('GET ' + apiPrefix + 'customers should PASS', function (done) {
//http get an url
agent.get(apiPrefix + 'customers') // api url
.expect(200) // 2xx for success and 4xx for failure
.end(function (err, res) {
// Handle /api/tables error
if (err) {
return done(err);
}
//validate response
res.body.length.should.be.equal(20);
return done();
});
});
it('GET ' + apiPrefix + 'customers?_size=100 should PASS', function (done) {
//http get an url
agent.get(apiPrefix + 'customers?_size=100') // api url
.expect(200) // 2xx for success and 4xx for failure
.end(function (err, res) {
// Handle /api/tables error
if (err) {
return done(err);
}
//validate response
res.body.length.should.be.equal(100);
return done();
});
});
it('GET ' + apiPrefix + 'customers?_size=1000 should PASS', function (done) {
//http get an url
agent.get(apiPrefix + 'customers?_size=1000') // api url
.expect(200) // 2xx for success and 4xx for failure
.end(function (err, res) {
// Handle /api/tables error
if (err) {
return done(err);
}
//validate response
res.body.length.should.be.equal(100);
return done();
});
});
it('GET ' + apiPrefix + 'customers?_size=-1 should PASS', function (done) {
//http get an url
agent.get(apiPrefix + 'customers?_size=-1') // api url
.expect(200) // 2xx for success and 4xx for failure
.end(function (err, res) {
// Handle /api/tables error
if (err) {
return done(err);
}
//validate response
res.body.length.should.be.equal(20);
return done();
});
});
it('GET ' + apiPrefix + 'payments?_p=2&_size=10 should PASS', function (done) {
//http get an url

Loading…
Cancel
Save