diff --git a/dist/index.js b/dist/index.js index e0751f2..933036f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -144,4 +144,4 @@ exports.default = Bookshelf => { }); }; -module.exports = exports['default']; \ No newline at end of file +module.exports = exports['default']; diff --git a/src/index.js b/src/index.js index 9aeabb0..c496337 100644 --- a/src/index.js +++ b/src/index.js @@ -102,7 +102,7 @@ export default Bookshelf => { .then(model => { // Parse JSON columns. Object.keys(attributes).forEach(attribute => { - if (this.constructor.jsonColumns.includes(attribute)) { + if (this.constructor.jsonColumns.includes(attribute) && model.attributes[attribute]) { model.attributes[attribute] = JSON.parse(model.attributes[attribute]); } }); diff --git a/test/mysql/index.js b/test/mysql/index.js index 4d8040f..69830d6 100644 --- a/test/mysql/index.js +++ b/test/mysql/index.js @@ -140,6 +140,16 @@ describe('with MySQL client', () => { sinon.restore(ModelPrototype); }); + it('should keep an empty string on update with `patch` option', async () => { + sinon.spy(ModelPrototype, 'save'); + + const model = await Model.forge().save(); + + await model.save({ foo: '' }, { patch: true }); + + model.get('foo').should.equal(''); + }); + it('should keep a JSON value when updating with `patch` option', async () => { const model = await Model.forge().save(); diff --git a/test/sqlite/index.js b/test/sqlite/index.js index 87cd489..cbd1f69 100644 --- a/test/sqlite/index.js +++ b/test/sqlite/index.js @@ -163,6 +163,16 @@ describe('with SQLite client', () => { sinon.restore(ModelPrototype); }); + it('should keep an empty string on update with `patch` option', async () => { + sinon.spy(ModelPrototype, 'save'); + + const model = await Model.forge().save(); + + await model.save({ foo: '' }, { patch: true }); + + model.get('foo').should.equal(''); + }); + it('should keep a JSON value when updating with `patch` option', async () => { const model = await Model.forge().save();