diff --git a/index.js b/index.js index 94f4490..3fa8798 100644 --- a/index.js +++ b/index.js @@ -3,9 +3,9 @@ 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" @@ -13,13 +13,13 @@ 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; diff --git a/test/test.js b/test/test.js index e80994a..0618d71 100644 --- a/test/test.js +++ b/test/test.js @@ -140,19 +140,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); + }); }); });