Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing empty strings #50

Merged
merged 7 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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