Skip to content

Commit fefe957

Browse files
committed
Add resourceConfig.generateId to disable automatic ID generation.
1 parent ad212e3 commit fefe957

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

documentation/resources.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@ url: jsonApi.Joi.string().uri().meta("readonly").description("This attribute can
9797
```
9898

9999
If you look through the example json:api resources in the `/example/resources` folder things should become clearer.
100+
101+
#### generateId
102+
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.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const jsonApi = require('../..')
4+
5+
const chainHandler = new jsonApi.ChainHandler()
6+
7+
let i = 2 // 1 is used by the example in resources/autoincrement.js
8+
chainHandler.beforeCreate = (request, newResource, callback) => {
9+
// Autoincrement the ID.
10+
// In practice this would actually be handled by the underlying database.
11+
newResource.id = (i++).toString()
12+
callback(null, request, newResource)
13+
}
14+
15+
module.exports = chainHandler.chain(new jsonApi.MemoryHandler())

example/resources/autoincrement.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const jsonApi = require('../../.')
4+
const autoincrementHandler = require('../handlers/autoincrementHandler.js')
5+
6+
jsonApi.define({
7+
namespace: 'json:api',
8+
resource: 'autoincrement',
9+
description: 'Demonstration of a resource with an auto-incrementing ID',
10+
handlers: autoincrementHandler,
11+
searchParams: { },
12+
generateId: false,
13+
attributes: {
14+
id: jsonApi.Joi.string(),
15+
name: jsonApi.Joi.string()
16+
.description('The name of the item')
17+
.example('Hello')
18+
},
19+
examples: [
20+
{
21+
id: '1',
22+
type: 'autoincrement',
23+
name: 'Foo'
24+
}
25+
]
26+
})

lib/routes/create.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ createRoute.register = () => {
3333
callback => {
3434
const theirs = request.params.data
3535
theirResource = _.assign(
36-
{
37-
id: uuid.v4(),
38-
type: request.params.type
39-
},
36+
{ type: request.params.type },
37+
(request.resourceConfig.generateId !== false) && { id: uuid.v4() },
4038
theirs.id && { id: theirs.id },
4139
theirs.attributes,
4240
{ meta: theirs.meta }

test/post-resource.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,53 @@ describe('Testing jsonapi-server', () => {
201201
done()
202202
})
203203
})
204+
describe('creates a resource with non-UUID ID', () => {
205+
let id
206+
207+
it('works', done => {
208+
const data = {
209+
method: 'post',
210+
url: 'http://localhost:16006/rest/autoincrement',
211+
headers: {
212+
'Content-Type': 'application/vnd.api+json'
213+
},
214+
body: JSON.stringify({
215+
'data': {
216+
'type': 'autoincrement',
217+
'attributes': {
218+
'name': 'bar'
219+
}
220+
}
221+
})
222+
}
223+
helpers.request(data, (err, res, json) => {
224+
assert.equal(err, null)
225+
json = helpers.validateJson(json)
226+
227+
assert.equal(json.data.id, '2')
228+
assert.equal(res.headers.location, `http://localhost:16006/rest/autoincrement/${json.data.id}`)
229+
assert.equal(res.statusCode, '201', 'Expecting 201')
230+
assert.equal(json.data.type, 'autoincrement', 'Should be a autoincrement resource')
231+
id = json.data.id
232+
233+
done()
234+
})
235+
})
236+
237+
it('new resource is retrievable', done => {
238+
const url = `http://localhost:16006/rest/autoincrement/${id}`
239+
helpers.request({
240+
method: 'GET',
241+
url
242+
}, (err, res, json) => {
243+
assert.equal(err, null)
244+
json = helpers.validateJson(json)
245+
assert.equal(res.statusCode, '200', 'Expecting 200 OK')
246+
assert.equal(json.included.length, 0, 'Should be no included resources')
247+
done()
248+
})
249+
})
250+
})
204251
})
205252
})
206253

0 commit comments

Comments
 (0)