Skip to content
Closed
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
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
var utils = require('./pouch-utils');
var wrappers = require('pouchdb-wrappers');

function isUntransformable(doc) {
function isUntransformable(doc, config) {
var isLocal = typeof doc._id === 'string' && utils.isLocalId(doc._id);
return isLocal || doc._deleted;
return isLocal || doc._deleted ? !config.handleDeleted : false;
}

// api.filter provided for backwards compat with the old "filter-pouch"
exports.transform = exports.filter = function transform(config) {
var db = this;

var incoming = function (doc) {
if (!isUntransformable(doc) && config.incoming) {
if (!isUntransformable(doc, config) && config.incoming) {
return config.incoming(utils.clone(doc));
}
return doc;
};
var outgoing = function (doc) {
if (!isUntransformable(doc) && config.outgoing) {
if (!isUntransformable(doc, config) && config.outgoing) {
return config.outgoing(utils.clone(doc));
}
return doc;
Expand Down
38 changes: 31 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,43 @@ function tests(dbName, dbType) {
});

it('skips deleted docs', function () {
db.transform({
incoming: function (doc) {
doc.foo.baz = 'baz';
return doc;
}
var doc = {_id: 'foo', foo: {}};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
return db.get('foo');
}).then(function (doc) {
var transformCalledOnDelete = false;
db.transform({
incoming: function (doc) {
transformCalledOnDelete = true;
return doc;
}
});

return db.remove(doc).then(function () {
transformCalledOnDelete.should.equal(false);
});
});
});

it('transforms deleted docs if `handleDeleted: true` #18', function () {
var doc = {_id: 'foo', foo: {}};
return db.put(doc).then(function (res) {
doc._rev = res.rev;
return db.get('foo');
}).then(function (doc) {
doc.foo.baz.should.equal('baz');
return db.remove(doc);
var transformCalledOnDelete = false;
db.transform({
incoming: function (doc) {
transformCalledOnDelete = true;
return doc;
},
handleDeleted: true
});

return db.remove(doc).then(function () {
transformCalledOnDelete.should.equal(true);
});
});
});

Expand Down