diff --git a/lib/CRUDCollection.js b/lib/CRUDCollection.js index f5571f9..fedb3d5 100644 --- a/lib/CRUDCollection.js +++ b/lib/CRUDCollection.js @@ -18,12 +18,14 @@ var CRUDCollection = function(options){ delete options.updateSchema; // if there's no update(), we ignore any updateSchema } - var outputList = function(req, res, list, listOptions){ - var collection = res.collection(list, listOptions); + var outputList = function (req, res, list, listOptions) { + var key = (listOptions) ? listOptions.key : null; + var objectKey = (key && listOptions.listAsKeyedObject) ? key : null; + var collection = res.collection(list, objectKey); if (req.app.autoLink){ collection = collection.linkEach('self', function(item, name){ - if (!!listOptions){ - return req.uri.child(item[listOptions.key]); // allow 'listOptions' to provide another key to link on. + if (key){ + return req.uri.child(item[key].toString()); // allow 'listOptions' to provide another key to link on. } else { return req.uri.child(name); } diff --git a/test/CRUDCollection.js b/test/CRUDCollection.js index 8df94be..0f7b42f 100644 --- a/test/CRUDCollection.js +++ b/test/CRUDCollection.js @@ -7,6 +7,7 @@ var hottap = require('hottap').hottap; var request = require('request'); var jobNumber = process.env.TRAVIS_JOB_NUMBER || '0.0'; var port = 8000 + parseInt(jobNumber.split(".")[1], 10); +var hyperjson = require('hyperjson-connect'); describe("CRUDCollection", function(){ it ("sets fetch on wildcard if fetch is defined", function(){ @@ -167,6 +168,85 @@ describe("CRUDCollection", function(){ }; module.handler.GET(req, res); }); + + it ("creates object by key", function (done) { + var module = new CRUDCollection({ + list : function(req, res, cb){ + cb(null, [{"id": "0", value: "zero"}, {"id": "1", value: "one"}, {"id": "2", value: "two"}], {key : "id", listAsKeyedObject: true}); + } + }); + var req = { + app : { + autoLink : true + }, + uri : urlgrey('http://localhost:8080/'), + headers: {} + }; + var res = { + setHeader: function () {}, + end: function (body) { + + body.toString().should.equal('{"_items":{"0":{"id":"0","value":"zero","_links":{"self":{"href":"http://localhost:8080/0"}}},"1":{"id":"1","value":"one","_links":{"self":{"href":"http://localhost:8080/1"}}},"2":{"id":"2","value":"two","_links":{"self":{"href":"http://localhost:8080/2"}}}}}') + console.log(body); + done(); + } + }; + hyperjson()(req, res, function () {}); + module.handler.GET(req, res); + }); + + it ("only use the key to create links", function (done) { + var module = new CRUDCollection({ + list : function(req, res, cb){ + cb(null, [{"id": "0", value: "zero"}, {"id": "1", value: "one"}, {"id": "2", value: "two"}], {key : "id"}); + } + }); + var req = { + app : { + autoLink : true + }, + uri : urlgrey('http://localhost:8080/'), + headers: {} + }; + var res = { + setHeader: function () {}, + end: function (body) { + + body.toString().should.equal('{"_items":[{"id":"0","value":"zero","_links":{"self":{"href":"http://localhost:8080/0"}}},{"id":"1","value":"one","_links":{"self":{"href":"http://localhost:8080/1"}}},{"id":"2","value":"two","_links":{"self":{"href":"http://localhost:8080/2"}}}]}') + console.log(body); + done(); + } + }; + hyperjson()(req, res, function () {}); + module.handler.GET(req, res); + }); + + it ("should create links when values for key are falsy (ex: 0)", function (done) { + var module = new CRUDCollection({ + list : function(req, res, cb){ + cb(null, [{"id": 0, value: "zero"}], {key : "id"}); + } + }); + var req = { + app : { + autoLink : true + }, + uri : urlgrey('http://localhost:8080/'), + headers: {} + }; + var res = { + setHeader: function () {}, + end: function (body) { + + body.toString().should.equal('{"_items":[{"id":0,"value":"zero","_links":{"self":{"href":"http://localhost:8080/0"}}}]}') + console.log(body); + done(); + } + }; + hyperjson()(req, res, function () {}); + module.handler.GET(req, res); + }); + it ("returns a 500 if the callback gets an error", function(done){ var module = new CRUDCollection({ list : function(req, res, cb){ cb("some error!"); }