Skip to content

Commit

Permalink
Fix stringifying null values with patch option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Gama committed Sep 19, 2016
1 parent 710f8a6 commit 0c5d78d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
});
Expand Down
13 changes: 13 additions & 0 deletions test/postgres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
13 changes: 13 additions & 0 deletions test/sqlite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 0c5d78d

Please # to comment.