Skip to content

Commit

Permalink
Merge pull request #50 from zhongzhi107/master
Browse files Browse the repository at this point in the history
Fix parsing empty strings
  • Loading branch information
Ricardo Gama authored Jul 18, 2017
2 parents 0ecde4c + f469c2f commit a6ebb51
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ exports.default = Bookshelf => {
});
};

module.exports = exports['default'];
module.exports = exports['default'];
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
});
Expand Down
10 changes: 10 additions & 0 deletions test/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

Expand Down

0 comments on commit a6ebb51

Please sign in to comment.