Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const fs = require('fs')
const path = require('path')
const debug = require('debug')

jsonApi.setConfig({
const config = {
graphiql: true,
swagger: {
title: 'Example JSON:API Server',
Expand Down Expand Up @@ -43,7 +43,9 @@ jsonApi.setConfig({
meta: {
description: 'This block shows up in the root node of every payload'
}
})
}

jsonApi.setConfig(config)

jsonApi.authenticate((request, callback) => {
// If a "blockMe" header is provided, block access.
Expand Down Expand Up @@ -79,4 +81,5 @@ if (typeof describe === 'undefined') {
}
server.start = jsonApi.start
server.close = jsonApi.close
server.setupTestServer = opts => (jsonApi.setConfig({...config, ...opts}), jsonApi.start())
server.getExpressServer = jsonApi.getExpressServer
6 changes: 4 additions & 2 deletions lib/pagination.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const pagination = module.exports = { }

const jsonApi = require('./jsonApi.js')
const ourJoi = require('./ourJoi.js')
const url = require('url')

Expand All @@ -27,8 +28,9 @@ pagination.validatePaginationParams = request => {
}
const page = request.params.page

page.offset = parseInt(page.offset, 10) || 0
page.limit = parseInt(page.limit, 10) || 50
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions:

  • What does this solve?
  • Why not make this configurable rather than unlimited?
  • MemoryHandler is not used for many records in production. Are you changing the sequelize-handler as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a pr up for https://jira.uncharted.software/browse/DCE-2930, but after speaking with Rob it seems that the issue has more to do with the pagination defaults in the framework.

These defaults were causing us issues on requests not made with query in the client such as findAll, where it was only returning 50 when we were expecting the entire dataset which caused bugs intermittently. Making it configurable here will also only replicate the issue we have on the client where, from what I understand, we've had to periodically increase the limits we've set for pagination. Doing it this way lets the client explicitly specify when/how many to paginate by.

I think only MemoryHandler needs updating, from what I can tell sequelize already has checks in place:
https://github.com/jagql/store-sequelize/blob/master/lib/sqlHandler.js#L524

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This theoretically solves our paging issues. We should be able to get rid of our ember-data store.query override, and the server-side equivalents.

// Set your own pagination defaults through setConfig()
page.offset = parseInt(page.offset, 10) || (jsonApi._apiConfig.page && jsonApi._apiConfig.page.offset) || 0
page.limit = parseInt(page.limit, 10) || (jsonApi._apiConfig.page && jsonApi._apiConfig.page.limit) || 50
}

pagination.enforcePagination = (request, results) => results.slice(0, request.params.page.size)
Expand Down
46 changes: 46 additions & 0 deletions test/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,49 @@ describe('Testing jsonapi-server', () => {
jsonApiTestServer.close()
})
})

describe('Testing configurable pagination', () => {
after(() => {
jsonApiTestServer.close()
})

it('Setting page defaults will set offset and limit', done => {
jsonApiTestServer.setupTestServer({ page: { offset: 1, limit: 3 }})

const data = {
method: 'get',
url: 'http://localhost:16006/rest/tags'
}
helpers.request(data, (err, res, json) => {
assert.strictEqual(err, null)
json = helpers.validateJson(json)

assert.strictEqual(res.statusCode, 200, 'Expecting 200')
assert.strictEqual(json.meta.page.offset, 1, 'should be at offset 0')
assert.strictEqual(json.meta.page.limit, 3, 'should have a limit of 3 record')
assert.strictEqual(json.meta.page.total, 5, 'should have a total of 5 records')

done()
})
})

it('Setting no page defaults will use the jagql defaults', done => {
jsonApiTestServer.setupTestServer()

const data = {
method: 'get',
url: 'http://localhost:16006/rest/tags'
}
helpers.request(data, (err, res, json) => {
assert.strictEqual(err, null)
json = helpers.validateJson(json)

assert.strictEqual(res.statusCode, 200, 'Expecting 200')
assert.strictEqual(json.meta.page.offset, 0, 'should be at offset 0')
assert.strictEqual(json.meta.page.limit, 50, 'should have a limit of 50 record')
assert.strictEqual(json.meta.page.total, 5, 'should have a total of 5 records')

done()
})
})
})