Skip to content
Open
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
10 changes: 6 additions & 4 deletions lib/CRUDCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
80 changes: 80 additions & 0 deletions test/CRUDCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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!"); }
Expand Down