mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 04:32:24 +08:00
Added _limit param. Example usage: _start=2&_limit=3 alternative to _start=2&_end=5.
This commit is contained in:
@ -7,7 +7,7 @@ var utils = require('./utils')
|
||||
|
||||
low.mixin(require('underscore-db'))
|
||||
low.mixin(require('underscore.inflections'))
|
||||
low.mixin({ createId: utils.createId })
|
||||
low.mixin({createId: utils.createId})
|
||||
|
||||
module.exports = function(source) {
|
||||
// Create router
|
||||
@ -15,7 +15,7 @@ module.exports = function(source) {
|
||||
|
||||
// Add middlewares
|
||||
router.use(bodyParser.json({limit: '10mb'}))
|
||||
router.use(bodyParser.urlencoded({ extended: false }))
|
||||
router.use(bodyParser.urlencoded({extended: false}))
|
||||
router.use(methodOverride())
|
||||
|
||||
// Create database
|
||||
@ -58,11 +58,12 @@ module.exports = function(source) {
|
||||
var _end = req.query._end
|
||||
var _sort = req.query._sort
|
||||
var _order = req.query._order
|
||||
|
||||
var _limit = req.query._limit
|
||||
delete req.query._start
|
||||
delete req.query._end
|
||||
delete req.query._sort
|
||||
delete req.query._order
|
||||
delete req.query._limit
|
||||
|
||||
if (req.query.q) {
|
||||
|
||||
@ -82,7 +83,7 @@ module.exports = function(source) {
|
||||
|
||||
// Add :parentId filter in case URL is like /:parent/:parentId/:resource
|
||||
if (req.params.parent) {
|
||||
filters[req.params.parent.slice(0, - 1) + 'Id'] = +req.params.parentId
|
||||
filters[req.params.parent.slice(0, -1) + 'Id'] = +req.params.parentId
|
||||
}
|
||||
|
||||
// Add query parameters filters
|
||||
@ -104,7 +105,7 @@ module.exports = function(source) {
|
||||
}
|
||||
|
||||
// Sort
|
||||
if(_sort) {
|
||||
if (_sort) {
|
||||
_order = _order || 'ASC'
|
||||
|
||||
array = _.sortBy(array, function(element) {
|
||||
@ -117,13 +118,13 @@ module.exports = function(source) {
|
||||
}
|
||||
|
||||
// Slice result
|
||||
_start = _start || 0
|
||||
res.setHeader('X-Total-Count', array.length)
|
||||
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||
if (_end) {
|
||||
res.setHeader('X-Total-Count', array.length)
|
||||
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||
|
||||
_start = _start || 0
|
||||
|
||||
array = array.slice(_start, _end)
|
||||
} else if (_limit) {
|
||||
array = utils.limitArray(array, _start, _limit)
|
||||
}
|
||||
|
||||
res.jsonp(array)
|
||||
|
13
src/utils.js
13
src/utils.js
@ -49,13 +49,13 @@ function getRemovable(db) {
|
||||
_(coll).each(function(doc) {
|
||||
_(doc).each(function(value, key) {
|
||||
if (/Id$/.test(key)) {
|
||||
var refName = _.pluralize(key.slice(0, - 2))
|
||||
var refName = _.pluralize(key.slice(0, -2))
|
||||
// Test if table exists
|
||||
if (db[refName]) {
|
||||
// Test if references is defined in table
|
||||
var ref = _.findWhere(db[refName], {id: value})
|
||||
if (_.isUndefined(ref)) {
|
||||
removable.push({ name: collName, id: doc.id })
|
||||
removable.push({name: collName, id: doc.id})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -66,8 +66,15 @@ function getRemovable(db) {
|
||||
return removable
|
||||
}
|
||||
|
||||
//Returns limited array
|
||||
function limitArray(array, start, limit) {
|
||||
var end = parseInt(start) + parseInt(limit)
|
||||
return array.slice(start, end)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
toNative: toNative,
|
||||
createId: createId,
|
||||
getRemovable: getRemovable
|
||||
getRemovable: getRemovable,
|
||||
limitArray: limitArray
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
var request = require('supertest')
|
||||
var assert = require('assert')
|
||||
var jsonServer = require('../src/')
|
||||
|
||||
var assert = require('assert')
|
||||
var jsonServer = require('../src/')
|
||||
var utils = require('../src/utils')
|
||||
describe('Server', function() {
|
||||
|
||||
var server
|
||||
@ -23,11 +23,17 @@ describe('Server', function() {
|
||||
]
|
||||
|
||||
db.comments = [
|
||||
{id: 1, published: true, postId: 1},
|
||||
{id: 1, published: true, postId: 1},
|
||||
{id: 2, published: false, postId: 1},
|
||||
{id: 3, published: false, postId: 2},
|
||||
{id: 4, published: false, postId: 2},
|
||||
{id: 5, published: false, postId: 2},
|
||||
{id: 6, published: false, postId: 2},
|
||||
{id: 7, published: false, postId: 2},
|
||||
{id: 8, published: false, postId: 2},
|
||||
{id: 9, published: false, postId: 2},
|
||||
{id: 10, published: false, postId: 2},
|
||||
{id: 11, published: false, postId: 2}
|
||||
]
|
||||
|
||||
db.refs = [
|
||||
@ -89,11 +95,11 @@ describe('Server', function() {
|
||||
})
|
||||
|
||||
it('should return an empty array when nothing is matched', function(done) {
|
||||
request(server)
|
||||
.get('/tags?q=nope')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([])
|
||||
.expect(200, done)
|
||||
request(server)
|
||||
.get('/tags?q=nope')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([])
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
@ -110,29 +116,29 @@ describe('Server', function() {
|
||||
})
|
||||
|
||||
describe('GET /:resource?sort=', function() {
|
||||
it('should respond with json and sort on a field', function(done) {
|
||||
request(server)
|
||||
.get('/tags?_sort=body')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([db.tags[1], db.tags[0], db.tags[2]])
|
||||
.expect(200, done)
|
||||
})
|
||||
it('should respond with json and sort on a field', function(done) {
|
||||
request(server)
|
||||
.get('/tags?_sort=body')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([db.tags[1], db.tags[0], db.tags[2]])
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
it('should reverse sorting with _order=DESC', function(done) {
|
||||
request(server)
|
||||
.get('/tags?_sort=body&_order=DESC')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([db.tags[2], db.tags[0], db.tags[1]])
|
||||
.expect(200, done)
|
||||
})
|
||||
it('should reverse sorting with _order=DESC', function(done) {
|
||||
request(server)
|
||||
.get('/tags?_sort=body&_order=DESC')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([db.tags[2], db.tags[0], db.tags[1]])
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
it('should sort on numerical field', function(done) {
|
||||
request(server)
|
||||
.get('/posts?_sort=id&_order=DESC')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(db.posts.reverse())
|
||||
.expect(200, done)
|
||||
})
|
||||
it('should sort on numerical field', function(done) {
|
||||
request(server)
|
||||
.get('/posts?_sort=id&_order=DESC')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(db.posts.reverse())
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /:resource?_start=&_end=', function() {
|
||||
@ -147,6 +153,18 @@ describe('Server', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /:resource?_start=&_limit=', function() {
|
||||
it('should respond with a limited array', function(done) {
|
||||
request(server)
|
||||
.get('/comments?_start=5&_limit=3')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect('x-total-count', db.comments.length.toString())
|
||||
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||
.expect(utils.limitArray(db.comments, 5, 3))
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /:parent/:parentId/:resource', function() {
|
||||
it('should respond with json and corresponding nested resources', function(done) {
|
||||
request(server)
|
||||
@ -154,7 +172,8 @@ describe('Server', function() {
|
||||
.expect('Content-Type', /json/)
|
||||
.expect([
|
||||
db.comments[0],
|
||||
db.comments[1]
|
||||
db.comments[1],
|
||||
|
||||
])
|
||||
.expect(200, done)
|
||||
})
|
||||
@ -196,7 +215,7 @@ describe('Server', function() {
|
||||
.expect('Content-Type', /json/)
|
||||
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
|
||||
.expect(200)
|
||||
.end(function(err, res){
|
||||
.end(function(err, res) {
|
||||
if (err) return done(err)
|
||||
assert.equal(db.posts.length, 3)
|
||||
done()
|
||||
@ -210,7 +229,7 @@ describe('Server', function() {
|
||||
.send({url: 'http://foo.com', postId: '1'})
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end(function(err, res){
|
||||
.end(function(err, res) {
|
||||
if (err) return done(err)
|
||||
assert.equal(db.refs.length, 2)
|
||||
done()
|
||||
@ -226,7 +245,7 @@ describe('Server', function() {
|
||||
.expect('Content-Type', /json/)
|
||||
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
||||
.expect(200)
|
||||
.end(function(err, res){
|
||||
.end(function(err, res) {
|
||||
if (err) return done(err)
|
||||
// assert it was created in database too
|
||||
assert.deepEqual(db.posts[0], {id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
||||
@ -252,7 +271,7 @@ describe('Server', function() {
|
||||
.expect('Content-Type', /json/)
|
||||
.expect({id: 1, body: 'bar'})
|
||||
.expect(200)
|
||||
.end(function(err, res){
|
||||
.end(function(err, res) {
|
||||
if (err) return done(err)
|
||||
// assert it was created in database too
|
||||
assert.deepEqual(db.posts[0], {id: 1, body: 'bar'})
|
||||
@ -276,10 +295,10 @@ describe('Server', function() {
|
||||
.del('/posts/1')
|
||||
.expect({})
|
||||
.expect(200)
|
||||
.end(function(err, res){
|
||||
.end(function(err, res) {
|
||||
if (err) return done(err)
|
||||
assert.equal(db.posts.length, 1)
|
||||
assert.equal(db.comments.length, 3)
|
||||
assert.equal(db.comments.length, 9)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
@ -1,5 +1,5 @@
|
||||
var assert = require('assert')
|
||||
var utils = require('../src/utils')
|
||||
var utils = require('../src/utils')
|
||||
|
||||
describe('utils', function() {
|
||||
|
||||
@ -20,8 +20,8 @@ describe('utils', function() {
|
||||
}
|
||||
|
||||
var expected = [
|
||||
{ name: 'comments', id: 2 },
|
||||
{ name: 'comments', id: 3 }
|
||||
{name: 'comments', id: 2},
|
||||
{name: 'comments', id: 3}
|
||||
]
|
||||
|
||||
assert.deepEqual(utils.getRemovable(db), expected)
|
||||
@ -46,4 +46,28 @@ describe('utils', function() {
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('limitArray', function() {
|
||||
it('should return limited array', function() {
|
||||
var testArray = [
|
||||
{id: 2, postId: 2},
|
||||
{id: 3, postId: 4},
|
||||
{id: 4, postId: 6},
|
||||
{id: 5, postId: 8},
|
||||
{id: 6, postId: 9},
|
||||
{id: 7, postId: 10},
|
||||
{id: 8, postId: 11},
|
||||
{id: 9, postId: 12},
|
||||
{id: 10, postId: 13},
|
||||
{id: 11, postId: 14},
|
||||
{id: 12, postId: 15},
|
||||
{id: 13, postId: 16},
|
||||
{id: 14, postId: 17},
|
||||
{id: 15, postId: 18},
|
||||
{id: 16, postId: 19}
|
||||
]
|
||||
assert.deepEqual(utils.limitArray(testArray, 3, 3), testArray.slice(3, 6))
|
||||
assert.deepEqual(utils.limitArray(testArray, 5, 3), testArray.slice(5, 8))
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user