Skip to content

Commit ff83b32

Browse files
committed
Handle string/hash based ids
1 parent 52fcdd2 commit ff83b32

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"lowdb": "^0.5.1",
1818
"method-override": "^2.1.2",
1919
"morgan": "^1.3.1",
20+
"node-uuid": "^1.4.2",
2021
"serve-static": "^1.6.1",
2122
"superagent": "^0.15.7",
2223
"underscore": "^1.5.2",

src/utils.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('underscore')
2+
var uuid = require('node-uuid')
23
var _inflections = require('underscore.inflections')
34
_.mixin(_inflections)
45

@@ -19,14 +20,22 @@ function toNative(value) {
1920
return value
2021
}
2122

22-
// Creates incremental id.
23+
// Return incremented id or uuid
2324
function createId(coll) {
2425
if (_.isEmpty(coll)) {
2526
return 1
2627
} else {
27-
return _.max(coll, function(doc) {
28+
var id = _.max(coll, function(doc) {
2829
return doc.id
29-
}).id + 1
30+
}).id
31+
32+
if (_.isFinite(id)) {
33+
// Increment integer id
34+
return ++id
35+
} else {
36+
// Generate string id
37+
return uuid()
38+
}
3039
}
3140
}
3241

test/index.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ describe('Server', function() {
2929
{id: 5, published: false, postId: 2},
3030
]
3131

32+
db.refs = [
33+
{id: 'abcd-1234', url: 'http://example.com', postId: 1}
34+
]
35+
3236
server = jsonServer(db)
3337
})
3438

@@ -166,19 +170,34 @@ describe('Server', function() {
166170

167171

168172
describe('POST /:resource', function() {
169-
it('should respond with json and create a resource', function(done) {
170-
request(server)
171-
.post('/posts')
172-
.send({body: 'foo', booleanValue: 'true', integerValue: '1'})
173-
.expect('Content-Type', /json/)
174-
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
175-
.expect(200)
176-
.end(function(err, res){
177-
if (err) return done(err)
178-
assert.equal(db.posts.length, 3)
179-
done()
180-
})
181-
})
173+
it('should respond with json, create a resource and increment id',
174+
function(done) {
175+
request(server)
176+
.post('/posts')
177+
.send({body: 'foo', booleanValue: 'true', integerValue: '1'})
178+
.expect('Content-Type', /json/)
179+
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
180+
.expect(200)
181+
.end(function(err, res){
182+
if (err) return done(err)
183+
assert.equal(db.posts.length, 3)
184+
done()
185+
})
186+
})
187+
188+
it('should respond with json, create a resource and generate string id',
189+
function(done) {
190+
request(server)
191+
.post('/refs')
192+
.send({url: 'http://foo.com', postId: '1'})
193+
.expect('Content-Type', /json/)
194+
.expect(200)
195+
.end(function(err, res){
196+
if (err) return done(err)
197+
assert.equal(db.refs.length, 2)
198+
done()
199+
})
200+
})
182201
})
183202

184203
describe('PUT /:resource/:id', function() {

test/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('utils', function() {
2424
{ name: 'comments', id: 3 }
2525
]
2626

27-
assert.deepEqual(expected, utils.getRemovable(db))
27+
assert.deepEqual(utils.getRemovable(db), expected)
2828

2929
})
3030
})

0 commit comments

Comments
 (0)