From b6f9e92ef86369487bfc51261476c28ff1e276f3 Mon Sep 17 00:00:00 2001 From: Hage Yaapa Date: Tue, 17 Dec 2019 21:22:15 +0530 Subject: [PATCH] feat: add persistDefaultValues (#1813) Ignores value if it matches default value. --- lib/model.js | 11 +++++++++++ test/defaults.test.js | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/lib/model.js b/lib/model.js index 94820a2b..5875ba02 100644 --- a/lib/model.js +++ b/lib/model.js @@ -299,6 +299,10 @@ ModelBaseClass.prototype._initProperties = function(data, options) { } } + if (ignoresMatchedDefault(properties[p]) && properties[p].default === propVal) { + delete self.__data[p]; + } + // Set default value using a named function if (applyDefaultValues && propVal === undefined) { const defn = properties[p].defaultFn; @@ -362,6 +366,13 @@ ModelBaseClass.prototype._initProperties = function(data, options) { this.trigger('initialize'); }; +// Implementation of persistDefaultValues property +function ignoresMatchedDefault(property) { + if (property && property.persistDefaultValues === false) { + return true; + } +} + // Helper function for determing the applyDefaultOnWrites value of a property function appliesDefaultsOnWrites(property) { if (property && ('applyDefaultOnWrites' in property)) { diff --git a/test/defaults.test.js b/test/defaults.test.js index cc2b7ab3..4be327bb 100644 --- a/test/defaults.test.js +++ b/test/defaults.test.js @@ -130,4 +130,48 @@ describe('defaults', function() { found.qualities.length.should.equal(1); }); }); + + context('persistDefaultValues', function() { + it('removes property if value matches default', async () => { + const Apple = db.define('Apple', { + color: {type: String, default: 'red', persistDefaultValues: false}, + taste: {type: String, default: 'sweet'}, + }, {applyDefaultsOnReads: false}); + + const apple = await Apple.create({color: 'red', taste: 'sweet'}); + const found = await Apple.findById(apple.id); + should(found.color).be.undefined(); + found.taste.should.equal('sweet'); + }); + + it('removes property if value matches default in an object', async () => { + const Apple = db.define('Apple', { + name: {type: String}, + qualities: { + color: {type: String, default: 'red', persistDefaultValues: false}, + taste: {type: String, default: 'sweet'}, + }, + }, {applyDefaultsOnReads: false}); + + const apple = await Apple.create({name: 'Honeycrisp', qualities: {taste: 'sweet'}}); + const found = await Apple.findById(apple.id); + should(found.qualities.color).be.undefined(); + found.qualities.taste.should.equal('sweet'); + }); + + it('removes property if value matches default in an array', async () => { + const Apple = db.define('Apple', { + name: {type: String}, + qualities: [ + {color: {type: String, default: 'red', persistDefaultValues: false}}, + {taste: {type: String, default: 'sweet'}}, + ], + }, {applyDefaultsOnReads: false}); + + const apple = await Apple.create({name: 'Honeycrisp', qualities: [{taste: 'sweet'}]}); + const found = await Apple.findById(apple.id); + should(found.qualities[0].color).be.undefined(); + found.qualities.length.should.equal(1); + }); + }); });