Permalink
Browse files

(#683) - introduce attachmentsFilter

So that adapters get a function to filter apropriate attachments
and don't have to worry about the logick.

Rework get a little so that it follows node callback style (err, res)
  • Loading branch information...
neojski authored and daleharvey committed Apr 18, 2013
1 parent f478313 commit 35e547919c5cd74d6219f918dec844538aac96c4
Showing with 38 additions and 65 deletions.
  1. +9 −24 src/adapters/pouch.idb.js
  2. +5 −12 src/adapters/pouch.leveldb.js
  3. +9 −18 src/adapters/pouch.websql.js
  4. +15 −11 src/pouch.adapter.js
View
@@ -441,11 +441,12 @@ var IdbPouch = function(opts, callback) {
// First we look up the metadata in the ids database, then we fetch the
// current revision(s) from the by sequence store
api._get = function idb_get(id, opts, callback) {
- var result;
+ var doc;
var metadata;
+ var err;
var txn = idb.transaction([DOC_STORE, BY_SEQ_STORE, ATTACH_STORE], 'readonly');
txn.oncomplete = function() {
- call(callback, result, metadata);
+ call(callback, err, {doc: doc, metadata: metadata});
};
txn.objectStore(DOC_STORE).get(id.docId).onsuccess = function(e) {
@@ -456,11 +457,11 @@ var IdbPouch = function(opts, callback) {
// When we ask with opts.rev we expect the answer to be either
// doc (possibly with _deleted=true) or missing error
if (!metadata) {
- result = Pouch.Errors.MISSING_DOC;
+ err = Pouch.Errors.MISSING_DOC;
return;
}
if (isDeleted(metadata) && !opts.rev) {
- result = Pouch.error(Pouch.Errors.MISSING_DOC, "deleted");
+ err = Pouch.error(Pouch.Errors.MISSING_DOC, "deleted");
return;
}
@@ -469,27 +470,17 @@ var IdbPouch = function(opts, callback) {
var index = txn.objectStore(BY_SEQ_STORE).index('_doc_id_rev');
index.get(key).onsuccess = function(e) {
- var doc = e.target.result;
+ doc = e.target.result;
if(doc && doc._doc_id_rev) {
delete(doc._doc_id_rev);
}
if (!doc) {
- result = Pouch.Errors.MISSING_DOC;
+ err = Pouch.Errors.MISSING_DOC;
return;
}
- result = doc;
- if ((opts.attachment || opts.attachments) && doc._attachments) {
+ if (opts.attachments && doc._attachments) {
var attachments = doc._attachments;
- var keys = Object.keys(attachments);
- if (opts.attachment) {
- if (keys.indexOf(opts.attachment) > -1) {
- keys = [opts.attachment];
- } else {
- keys = [];
- }
- }
-
- keys.forEach(function(key) {
+ Object.keys(attachments).filter(opts.attachmentsFilter).forEach(function(key) {
api._getAttachment(attachments[key], {encode: opts.encode, txn: txn}, function(err, data) {
doc._attachments[key].data = data;
});
@@ -508,11 +499,6 @@ var IdbPouch = function(opts, callback) {
api._getAttachment = function(attachment, opts, callback) {
var result;
var txn = opts.txn;
-
- if (!txn) {
- return; // fail miserably. FIXME!
- }
-
var digest = attachment.digest;
var type = attachment.content_type;
@@ -539,7 +525,6 @@ var IdbPouch = function(opts, callback) {
call(callback, null, result);
}
};
- return;
};
api._allDocs = function idb_allDocs(opts, callback) {
@@ -184,25 +184,18 @@ var LevelPouch = function(opts, callback) {
doc._id = metadata.id;
doc._rev = rev;
- if ((opts.attachment || opts.attachments) && doc._attachments) {
+ if (opts.attachments && doc._attachments) {
var attachments = doc._attachments;
- var keys = Object.keys(attachments);
- if (opts.attachment) {
- if (keys.indexOf(opts.attachment) > -1) {
- keys = [opts.attachment];
- } else {
- keys = [];
- }
- }
+ var keys = Object.keys(attachments).filter(opts.attachmentsFilter);
var count = keys.length;
if (!count) {
- callback(doc, metadata);
+ callback(null, {doc: doc, metadata: metadata});
}
keys.forEach(function(key) {
api._getAttachment(attachments[key], {encode: opts.encode}, function(err, data) {
doc._attachments[key].data = data;
if (!--count) {
- callback(doc, metadata);
+ callback(null, {doc: doc, metadata: metadata});
}
});
});
@@ -213,7 +206,7 @@ var LevelPouch = function(opts, callback) {
doc._attachments[key].stub = true;
}
}
- callback(doc, metadata);
+ callback(null, {doc: doc, metadata: metadata});
}
});
});
@@ -408,18 +408,19 @@ var webSqlPouch = function(opts, callback) {
};
api._get = function(id, opts, callback) {
- var result;
+ var doc;
var metadata;
+ var err;
db.transaction(function(tx) {
var sql = 'SELECT * FROM ' + DOC_STORE + ' WHERE id=?';
tx.executeSql(sql, [id.docId], function(tx, results) {
if (!results.rows.length) {
- result = Pouch.Errors.MISSING_DOC;
+ err = Pouch.Errors.MISSING_DOC;
return;
}
metadata = JSON.parse(results.rows.item(0).json);
if (isDeleted(metadata) && !opts.rev) {
- result = Pouch.error(Pouch.Errors.MISSING_DOC, "deleted");
+ err = Pouch.error(Pouch.Errors.MISSING_DOC, "deleted");
return;
}
@@ -429,23 +430,14 @@ var webSqlPouch = function(opts, callback) {
var sql = 'SELECT * FROM ' + BY_SEQ_STORE + ' WHERE doc_id_rev=?';
tx.executeSql(sql, [key], function(tx, results) {
if (!results.rows.length) {
- result = Pouch.Errors.MISSING_DOC;
+ err = Pouch.Errors.MISSING_DOC;
return;
}
- var doc = JSON.parse(results.rows.item(0).json);
+ doc = JSON.parse(results.rows.item(0).json);
- if ((opts.attachment || opts.attachments) && doc._attachments) {
+ if (opts.attachments && doc._attachments) {
var attachments = doc._attachments;
- var keys = Object.keys(attachments);
- if (opts.attachment) {
- if (keys.indexOf(opts.attachment) > -1) {
- keys = [opts.attachment];
- } else {
- keys = [];
- }
- }
-
- keys.forEach(function(key) {
+ Object.keys(attachments).filter(opts.attachmentsFilter).forEach(function(key) {
api._getAttachment(attachments[key], {encode: opts.encode, txn: tx}, function(err, data) {
doc._attachments[key].data = data;
});
@@ -457,11 +449,10 @@ var webSqlPouch = function(opts, callback) {
}
}
}
- result = doc;
});
});
}, unknownError(callback), function () {
- call(callback, result, metadata);
+ call(callback, err, {doc: doc, metadata: metadata});
});
};
View
@@ -314,29 +314,33 @@ var PouchAdapter = function(opts, callback) {
}
return; // open_revs does not like other options
}
-
id = parseDocId(id);
if (id.attachmentId !== '') {
- return customApi._get(id, {attachment: id.attachmentId, encode: false}, function(result, metadata){
- if ('error' in result) {
- return call(callback, result);
+ return customApi._get(id, {
+ attachments: true,
+ attachmentsFilter: function(name){return name === id.attachmentId;},
+ encode: false
+ }, function(err, result){
+ if (err) {
+ return call(callback, err);
}
-
- if (result._attachments && result._attachments[id.attachmentId]) {
- return call(callback, null, result._attachments[id.attachmentId].data);
+ if (result.doc._attachments && result.doc._attachments[id.attachmentId]) {
+ return call(callback, null, result.doc._attachments[id.attachmentId].data);
} else {
return call(callback, Pouch.Errors.MISSING_DOC);
}
});
}
opts.encode = true;
- return customApi._get(id, opts, function(result, metadata) {
- if ('error' in result) {
- return call(callback, result);
+ opts.attachmentsFilter = function(){ return true; };
+ return customApi._get(id, opts, function(err, result) {
+ if (err) {
+ return call(callback, err);
}
- var doc = result;
+ var doc = result.doc;
+ var metadata = result.metadata;
if (opts.conflicts) {
var conflicts = Pouch.merge.collectConflicts(metadata);

0 comments on commit 35e5479

Please sign in to comment.