Skip to content

[DRAFT] use integer ( maybe autoincrement on SQL) ids instead of UUID #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
npm-debug.log
/.nyc_output/
/coverage/
.idea/
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- 2017-11-02 - v3.3.0
- 2017-11-02 - support generateId type
- 2017-10-21 - v3.2.2
- 2017-10-21 - Report coverage with Coveralls.
- 2017-10-21 - Modernise script.
Expand Down
3 changes: 3 additions & 0 deletions documentation/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ url: jsonApi.Joi.string().uri().meta("readonly").description("This attribute can
```

If you look through the example json:api resources in the `/example/resources` folder things should become clearer.

#### generateId
By default, the server autogenerates a UUID for resources which are created without specifying an ID. To disable this behavior (for example, if the database generates an ID by auto-incrementing), set `generateId` to `false`. If the resource's ID is not a UUID, it is also necessary to specify an `id` attribute with the correct type. See `/examples/resorces/autoincrement.js` for an example of such a resource.
15 changes: 15 additions & 0 deletions example/handlers/autoincrementHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const jsonApi = require('../..')

const chainHandler = new jsonApi.ChainHandler()

let i = 2 // 1 is used by the example in resources/autoincrement.js
chainHandler.beforeCreate = (request, newResource, callback) => {
// Autoincrement the ID.
// In practice this would actually be handled by the underlying database.
newResource.id = (i++).toString()
callback(null, request, newResource)
}

module.exports = chainHandler.chain(new jsonApi.MemoryHandler())
26 changes: 26 additions & 0 deletions example/resources/autoincrement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const jsonApi = require('../../.')
const autoincrementHandler = require('../handlers/autoincrementHandler.js')

jsonApi.define({
namespace: 'json:api',
resource: 'autoincrement',
description: 'Demonstration of a resource with an auto-incrementing ID',
handlers: autoincrementHandler,
searchParams: { },
generateId: false,
attributes: {
id: jsonApi.Joi.string(),
name: jsonApi.Joi.string()
.description('The name of the item')
.example('Hello')
},
examples: [
{
id: '1',
type: 'autoincrement',
name: 'Foo'
}
]
})
6 changes: 2 additions & 4 deletions lib/routes/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ createRoute.register = () => {
callback => {
const theirs = request.params.data
theirResource = _.assign(
{
id: uuid.v4(),
type: request.params.type
},
{ type: request.params.type },
(request.resourceConfig.generateId !== false) && { id: uuid.v4() },
theirs.id && { id: theirs.id },
theirs.attributes,
{ meta: theirs.meta }
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-server",
"version": "3.2.2",
"name": "jsonapi-graphql-server",
"version": "3.3.0",
"description": "A config driven NodeJS framework implementing json:api",
"keywords": [
"jsonapi",
Expand Down
47 changes: 47 additions & 0 deletions test/post-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,53 @@ describe('Testing jsonapi-server', () => {
done()
})
})
describe('creates a resource with non-UUID ID', () => {
let id

it('works', done => {
const data = {
method: 'post',
url: 'http://localhost:16006/rest/autoincrement',
headers: {
'Content-Type': 'application/vnd.api+json'
},
body: JSON.stringify({
'data': {
'type': 'autoincrement',
'attributes': {
'name': 'bar'
}
}
})
}
helpers.request(data, (err, res, json) => {
assert.equal(err, null)
json = helpers.validateJson(json)

assert.equal(json.data.id, '2')
assert.equal(res.headers.location, `http://localhost:16006/rest/autoincrement/${json.data.id}`)
assert.equal(res.statusCode, '201', 'Expecting 201')
assert.equal(json.data.type, 'autoincrement', 'Should be a autoincrement resource')
id = json.data.id

done()
})
})

it('new resource is retrievable', done => {
const url = `http://localhost:16006/rest/autoincrement/${id}`
helpers.request({
method: 'GET',
url
}, (err, res, json) => {
assert.equal(err, null)
json = helpers.validateJson(json)
assert.equal(res.statusCode, '200', 'Expecting 200 OK')
assert.equal(json.included.length, 0, 'Should be no included resources')
done()
})
})
})
})
})

Expand Down