Skip to content

Commit ca5156b

Browse files
committed
Merge pull request typicode#66 from binali-rustamov/master
Added _limit param.
2 parents 23b0faf + 34b95e2 commit ca5156b

File tree

4 files changed

+104
-53
lines changed

4 files changed

+104
-53
lines changed

src/router.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ var utils = require('./utils')
77

88
low.mixin(require('underscore-db'))
99
low.mixin(require('underscore.inflections'))
10-
low.mixin({ createId: utils.createId })
10+
low.mixin({createId: utils.createId})
1111

1212
module.exports = function(source) {
1313
// Create router
1414
var router = express.Router()
1515

1616
// Add middlewares
1717
router.use(bodyParser.json({limit: '10mb'}))
18-
router.use(bodyParser.urlencoded({ extended: false }))
18+
router.use(bodyParser.urlencoded({extended: false}))
1919
router.use(methodOverride())
2020

2121
// Create database
@@ -58,11 +58,12 @@ module.exports = function(source) {
5858
var _end = req.query._end
5959
var _sort = req.query._sort
6060
var _order = req.query._order
61-
61+
var _limit = req.query._limit
6262
delete req.query._start
6363
delete req.query._end
6464
delete req.query._sort
6565
delete req.query._order
66+
delete req.query._limit
6667

6768
if (req.query.q) {
6869

@@ -82,7 +83,7 @@ module.exports = function(source) {
8283

8384
// Add :parentId filter in case URL is like /:parent/:parentId/:resource
8485
if (req.params.parent) {
85-
filters[req.params.parent.slice(0, - 1) + 'Id'] = +req.params.parentId
86+
filters[req.params.parent.slice(0, -1) + 'Id'] = +req.params.parentId
8687
}
8788

8889
// Add query parameters filters
@@ -104,7 +105,7 @@ module.exports = function(source) {
104105
}
105106

106107
// Sort
107-
if(_sort) {
108+
if (_sort) {
108109
_order = _order || 'ASC'
109110

110111
array = _.sortBy(array, function(element) {
@@ -117,13 +118,13 @@ module.exports = function(source) {
117118
}
118119

119120
// Slice result
121+
_start = _start || 0
122+
res.setHeader('X-Total-Count', array.length)
123+
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
120124
if (_end) {
121-
res.setHeader('X-Total-Count', array.length)
122-
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
123-
124-
_start = _start || 0
125-
126125
array = array.slice(_start, _end)
126+
} else if (_limit) {
127+
array = utils.limitArray(array, _start, _limit)
127128
}
128129

129130
res.jsonp(array)

src/utils.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ function getRemovable(db) {
4949
_(coll).each(function(doc) {
5050
_(doc).each(function(value, key) {
5151
if (/Id$/.test(key)) {
52-
var refName = _.pluralize(key.slice(0, - 2))
52+
var refName = _.pluralize(key.slice(0, -2))
5353
// Test if table exists
5454
if (db[refName]) {
5555
// Test if references is defined in table
5656
var ref = _.findWhere(db[refName], {id: value})
5757
if (_.isUndefined(ref)) {
58-
removable.push({ name: collName, id: doc.id })
58+
removable.push({name: collName, id: doc.id})
5959
}
6060
}
6161
}
@@ -66,8 +66,15 @@ function getRemovable(db) {
6666
return removable
6767
}
6868

69+
//Returns limited array
70+
function limitArray(array, start, limit) {
71+
var end = parseInt(start) + parseInt(limit)
72+
return array.slice(start, end)
73+
}
74+
6975
module.exports = {
7076
toNative: toNative,
7177
createId: createId,
72-
getRemovable: getRemovable
78+
getRemovable: getRemovable,
79+
limitArray: limitArray
7380
}

test/index.js

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var request = require('supertest')
2-
var assert = require('assert')
3-
var jsonServer = require('../src/')
4-
2+
var assert = require('assert')
3+
var jsonServer = require('../src/')
4+
var utils = require('../src/utils')
55
describe('Server', function() {
66

77
var server
@@ -23,11 +23,17 @@ describe('Server', function() {
2323
]
2424

2525
db.comments = [
26-
{id: 1, published: true, postId: 1},
26+
{id: 1, published: true, postId: 1},
2727
{id: 2, published: false, postId: 1},
2828
{id: 3, published: false, postId: 2},
2929
{id: 4, published: false, postId: 2},
3030
{id: 5, published: false, postId: 2},
31+
{id: 6, published: false, postId: 2},
32+
{id: 7, published: false, postId: 2},
33+
{id: 8, published: false, postId: 2},
34+
{id: 9, published: false, postId: 2},
35+
{id: 10, published: false, postId: 2},
36+
{id: 11, published: false, postId: 2}
3137
]
3238

3339
db.refs = [
@@ -89,11 +95,11 @@ describe('Server', function() {
8995
})
9096

9197
it('should return an empty array when nothing is matched', function(done) {
92-
request(server)
93-
.get('/tags?q=nope')
94-
.expect('Content-Type', /json/)
95-
.expect([])
96-
.expect(200, done)
98+
request(server)
99+
.get('/tags?q=nope')
100+
.expect('Content-Type', /json/)
101+
.expect([])
102+
.expect(200, done)
97103
})
98104
})
99105

@@ -110,29 +116,29 @@ describe('Server', function() {
110116
})
111117

112118
describe('GET /:resource?sort=', function() {
113-
it('should respond with json and sort on a field', function(done) {
114-
request(server)
115-
.get('/tags?_sort=body')
116-
.expect('Content-Type', /json/)
117-
.expect([db.tags[1], db.tags[0], db.tags[2]])
118-
.expect(200, done)
119-
})
119+
it('should respond with json and sort on a field', function(done) {
120+
request(server)
121+
.get('/tags?_sort=body')
122+
.expect('Content-Type', /json/)
123+
.expect([db.tags[1], db.tags[0], db.tags[2]])
124+
.expect(200, done)
125+
})
120126

121-
it('should reverse sorting with _order=DESC', function(done) {
122-
request(server)
123-
.get('/tags?_sort=body&_order=DESC')
124-
.expect('Content-Type', /json/)
125-
.expect([db.tags[2], db.tags[0], db.tags[1]])
126-
.expect(200, done)
127-
})
127+
it('should reverse sorting with _order=DESC', function(done) {
128+
request(server)
129+
.get('/tags?_sort=body&_order=DESC')
130+
.expect('Content-Type', /json/)
131+
.expect([db.tags[2], db.tags[0], db.tags[1]])
132+
.expect(200, done)
133+
})
128134

129-
it('should sort on numerical field', function(done) {
130-
request(server)
131-
.get('/posts?_sort=id&_order=DESC')
132-
.expect('Content-Type', /json/)
133-
.expect(db.posts.reverse())
134-
.expect(200, done)
135-
})
135+
it('should sort on numerical field', function(done) {
136+
request(server)
137+
.get('/posts?_sort=id&_order=DESC')
138+
.expect('Content-Type', /json/)
139+
.expect(db.posts.reverse())
140+
.expect(200, done)
141+
})
136142
})
137143

138144
describe('GET /:resource?_start=&_end=', function() {
@@ -147,14 +153,27 @@ describe('Server', function() {
147153
})
148154
})
149155

156+
describe('GET /:resource?_start=&_limit=', function() {
157+
it('should respond with a limited array', function(done) {
158+
request(server)
159+
.get('/comments?_start=5&_limit=3')
160+
.expect('Content-Type', /json/)
161+
.expect('x-total-count', db.comments.length.toString())
162+
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
163+
.expect(utils.limitArray(db.comments, 5, 3))
164+
.expect(200, done)
165+
})
166+
})
167+
150168
describe('GET /:parent/:parentId/:resource', function() {
151169
it('should respond with json and corresponding nested resources', function(done) {
152170
request(server)
153171
.get('/posts/1/comments')
154172
.expect('Content-Type', /json/)
155173
.expect([
156174
db.comments[0],
157-
db.comments[1]
175+
db.comments[1],
176+
158177
])
159178
.expect(200, done)
160179
})
@@ -196,7 +215,7 @@ describe('Server', function() {
196215
.expect('Content-Type', /json/)
197216
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
198217
.expect(200)
199-
.end(function(err, res){
218+
.end(function(err, res) {
200219
if (err) return done(err)
201220
assert.equal(db.posts.length, 3)
202221
done()
@@ -210,7 +229,7 @@ describe('Server', function() {
210229
.send({url: 'http://foo.com', postId: '1'})
211230
.expect('Content-Type', /json/)
212231
.expect(200)
213-
.end(function(err, res){
232+
.end(function(err, res) {
214233
if (err) return done(err)
215234
assert.equal(db.refs.length, 2)
216235
done()
@@ -226,7 +245,7 @@ describe('Server', function() {
226245
.expect('Content-Type', /json/)
227246
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
228247
.expect(200)
229-
.end(function(err, res){
248+
.end(function(err, res) {
230249
if (err) return done(err)
231250
// assert it was created in database too
232251
assert.deepEqual(db.posts[0], {id: 1, body: 'bar', booleanValue: true, integerValue: 1})
@@ -252,7 +271,7 @@ describe('Server', function() {
252271
.expect('Content-Type', /json/)
253272
.expect({id: 1, body: 'bar'})
254273
.expect(200)
255-
.end(function(err, res){
274+
.end(function(err, res) {
256275
if (err) return done(err)
257276
// assert it was created in database too
258277
assert.deepEqual(db.posts[0], {id: 1, body: 'bar'})
@@ -276,10 +295,10 @@ describe('Server', function() {
276295
.del('/posts/1')
277296
.expect({})
278297
.expect(200)
279-
.end(function(err, res){
298+
.end(function(err, res) {
280299
if (err) return done(err)
281300
assert.equal(db.posts.length, 1)
282-
assert.equal(db.comments.length, 3)
301+
assert.equal(db.comments.length, 9)
283302
done()
284303
})
285304
})

test/utils.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var assert = require('assert')
2-
var utils = require('../src/utils')
2+
var utils = require('../src/utils')
33

44
describe('utils', function() {
55

@@ -20,8 +20,8 @@ describe('utils', function() {
2020
}
2121

2222
var expected = [
23-
{ name: 'comments', id: 2 },
24-
{ name: 'comments', id: 3 }
23+
{name: 'comments', id: 2},
24+
{name: 'comments', id: 3}
2525
]
2626

2727
assert.deepEqual(utils.getRemovable(db), expected)
@@ -46,4 +46,28 @@ describe('utils', function() {
4646
})
4747

4848
})
49+
50+
describe('limitArray', function() {
51+
it('should return limited array', function() {
52+
var testArray = [
53+
{id: 2, postId: 2},
54+
{id: 3, postId: 4},
55+
{id: 4, postId: 6},
56+
{id: 5, postId: 8},
57+
{id: 6, postId: 9},
58+
{id: 7, postId: 10},
59+
{id: 8, postId: 11},
60+
{id: 9, postId: 12},
61+
{id: 10, postId: 13},
62+
{id: 11, postId: 14},
63+
{id: 12, postId: 15},
64+
{id: 13, postId: 16},
65+
{id: 14, postId: 17},
66+
{id: 15, postId: 18},
67+
{id: 16, postId: 19}
68+
]
69+
assert.deepEqual(utils.limitArray(testArray, 3, 3), testArray.slice(3, 6))
70+
assert.deepEqual(utils.limitArray(testArray, 5, 3), testArray.slice(5, 8))
71+
})
72+
})
4973
})

0 commit comments

Comments
 (0)