Skip to content

Commit

Permalink
(#2) - Pouch will now create docs before attachments if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Mar 6, 2013
1 parent a780499 commit 19764f1
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,9 @@ app.put('/:db/:id/:attachment', function (req, res, next) {
? new Buffer(req.body)
: new Buffer(JSON.stringify(req.body));

// CouchDB will create the document for you before adding the attachment
// if the document doesn't already exist. Mimic that here.
req.db.get(req.params.id, function (err, doc) {
if (!err) {
rev = doc._rev;
req.db.putAttachment(name, rev, body, type, function (err, response) {
if (err) return res.send(409, err);
res.send(200, response);
});
} else {
req.db.put({ _id: req.params.id }, function (err, response) {
req.db.putAttachment(name, response.rev, body, type, function (err, response) {
if (err) return res.send(409, err);
res.send(200, response);
});
});
}
req.db.putAttachment(name, rev, body, type, function (err, response) {
if (err) return res.send(409, err);
res.send(200, response);
});

});
Expand Down

0 comments on commit 19764f1

Please sign in to comment.