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
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
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