From 1a947e0ea0934bf9ee6741cc5cabb2fb4c10d684 Mon Sep 17 00:00:00 2001 From: Ricardo Gama Date: Mon, 19 Sep 2016 09:58:01 +0100 Subject: [PATCH] Fix stringifying null values with patch option --- src/index.js | 2 +- test/postgres/index.js | 13 +++++++++++++ test/sqlite/index.js | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3a8693a..81c57ed 100644 --- a/src/index.js +++ b/src/index.js @@ -83,7 +83,7 @@ export default Bookshelf => { // Stringify JSON columns. Object.keys(attributes).forEach(attribute => { - if (this.jsonColumns.includes(attribute)) { + if (this.jsonColumns.includes(attribute) && attributes[attribute]) { attributes[attribute] = JSON.stringify(attributes[attribute]); } }); diff --git a/test/postgres/index.js b/test/postgres/index.js index b04dd5f..57d1416 100644 --- a/test/postgres/index.js +++ b/test/postgres/index.js @@ -130,6 +130,19 @@ describe('with PostgreSQL client', () => { should(model.get('foo')).be.undefined(); }); + it('should not stringify null values on update with `patch` option', async () => { + sinon.spy(ModelPrototype, 'save'); + + const model = await Model.forge().save(); + + await model.save({ foo: null }, { patch: true }); + + ModelPrototype.save.callCount.should.equal(2); + ModelPrototype.save.secondCall.args[0].should.eql({ foo: null }); + + sinon.restore(ModelPrototype); + }); + 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 a311759..5ee1151 100644 --- a/test/sqlite/index.js +++ b/test/sqlite/index.js @@ -142,6 +142,19 @@ describe('with SQLite client', () => { should(fetched.get('foo')).be.null(); }); + it('should not stringify null values on update with `patch` option', async () => { + sinon.spy(ModelPrototype, 'save'); + + const model = await Model.forge().save(); + + await model.save({ foo: null }, { patch: true }); + + ModelPrototype.save.callCount.should.equal(2); + ModelPrototype.save.secondCall.args[0].should.eql({ foo: null }); + + sinon.restore(ModelPrototype); + }); + it('should keep a json value when updating with `patch` option', async () => { const model = await Model.forge().save();