Skip to content

Commit

Permalink
Create _design/app CouchDB design document.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradley-holt committed Oct 4, 2012
1 parent cf74afd commit e4aaac2
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 0 deletions.
27 changes: 27 additions & 0 deletions _design/app/lists/collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function(head, req) {
registerType("hal", "application/hal+json");
provides("hal", function() {
start({
"headers": {
"Content-Type": "application/hal+json",
"Vary": "Accept"
}
});
var collections = {
"_links": {
"self": { "href": "/" },
"item": []
},
"title": "Collections"
};
var row;
while (row = getRow()) {
collections._links.item.push({
"href": "/" + row.key + "/",
"title": row.value
});
}
send(toJSON(collections));
send("\n");
});
}
25 changes: 25 additions & 0 deletions _design/app/lists/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function(head, req) {
registerType("hal", "application/hal+json");
provides("hal", function() {
start({
"headers": {
"Content-Type": "application/hal+json",
"Vary": "Accept"
}
});
var resource = {};
var row;
while (row = getRow()) {
if (0 == row.key[1]) {
resource = row.value;
resource._links.item = [];
} else if (1 == row.key[1] && resource._links) {
resource._links.item.push({
"href": "/" + row.key[0] + "/" + row.id
});
}
}
send(toJSON(resource));
send("\n");
});
}
55 changes: 55 additions & 0 deletions _design/app/rewrites.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[
{
"from": "/",
"to": "/_list/collections/collections",
"method": "GET"
},
{
"from": "/",
"to": "/_update/collection",
"method": "POST"
},
{
"from": "/:collection/",
"to": "/_list/items/items",
"method": "GET",
"query": {
"reduce": false,
"startkey": [":collection", 0],
"endkey": [":collection", 1]
}
},
{
"from": "/:collection",
"to": "/_update/collection/:collection",
"method": "PUT"
},
{
"from": "/:collection",
"to": "/_update/collection/:collection",
"method": "DELETE"
},
{
"from": "/:collection/",
"to": "/_update/item",
"method": "POST",
"query": {
"collection": ":collection"
}
},
{
"from": "/:collection/:item",
"to": "/_show/item/:item",
"method": "GET"
},
{
"from": "/:collection/:item",
"to": "/_update/item/:item",
"method": "PUT"
},
{
"from": "/:collection/:item",
"to": "/_update/item/:item",
"method": "DELETE"
}
]
12 changes: 12 additions & 0 deletions _design/app/shows/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function(doc, req) {
registerType("hal", "application/hal+json");
provides("hal", function() {
return {
"headers": {
"Content-Type": "application/hal+json",
"Vary": "Accept"
},
"body": toJSON(doc.resource) + "\n"
};
});
}
50 changes: 50 additions & 0 deletions _design/app/updates/collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function(doc, req) {
if ("DELETE" == req.method) {
doc._deleted = true;
return [doc, {}];
}
switch (req.headers["Content-Type"]) {
case "application/hal+json":
if (doc) {
updatedDoc = doc;
} else {
var updatedDoc = {};
}
updatedDoc.resource = JSON.parse(req.body);
if (!updatedDoc._id) {
if (req.id) {
updatedDoc._id = req.id;
} else if (req.uuid) {
updatedDoc._id = req.uuid;
}
}
updatedDoc.is_collection = true;
updatedDoc.resource._links = {
"self": { "href": "/" + updatedDoc._id + "/" },
"up": { "href": "/" }
};
return [
updatedDoc,
{
"headers": {
"Location": updatedDoc.resource._links.self.href,
"Content-Type": "application/hal+json",
"Vary": "Accept"
},
"body": toJSON(updatedDoc.resource) + "\n"
}
];
default:
return [
null,
{
"code": 415,
"headers": {
"Content-Type": "text/plain;charset=utf-8",
"Vary": "Accept"
},
"body": toJSON({"error":"unsupported_media_type","reason":"The media type sent is not supported."}) + "\n"
}
];
}
}
52 changes: 52 additions & 0 deletions _design/app/updates/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function(doc, req) {
if ("DELETE" == req.method) {
doc._deleted = true;
return [doc, {}];
}
switch (req.headers["Content-Type"]) {
case "application/hal+json":
if (doc) {
updatedDoc = doc;
} else {
var updatedDoc = {};
}
updatedDoc.resource = JSON.parse(req.body);
if (!updatedDoc._id) {
if (req.id) {
updatedDoc._id = req.id;
} else if (req.uuid) {
updatedDoc._id = req.uuid;
}
}
if (req.query.collection) {
updatedDoc.collection = req.query.collection;
}
updatedDoc.resource._links = {
"self": { "href": "/" + updatedDoc.collection + "/" + updatedDoc._id },
"collection": { "href": "/" + updatedDoc.collection + "/" }
};
return [
updatedDoc,
{
"headers": {
"Location": updatedDoc.resource._links.self.href,
"Content-Type": "application/hal+json",
"Vary": "Accept"
},
"body": toJSON(updatedDoc.resource) + "\n"
}
];
default:
return [
null,
{
"code": 415,
"headers": {
"Content-Type": "text/plain;charset=utf-8",
"Vary": "Accept"
},
"body": toJSON({"error":"unsupported_media_type","reason":"The media type sent is not supported."}) + "\n"
}
];
}
}
5 changes: 5 additions & 0 deletions _design/app/views/collections/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function(doc) {
if (doc.is_collection) {
emit(doc._id, doc.resource.title);
}
}
8 changes: 8 additions & 0 deletions _design/app/views/items/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function(doc) {
if (doc.is_collection) {
emit([doc._id, 0], doc.resource);
}
if (doc.collection) {
emit([doc.collection, 1], doc.resource);
}
}
1 change: 1 addition & 0 deletions _design/app/views/items/reduce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_count

0 comments on commit e4aaac2

Please sign in to comment.