Skip to content

Commit

Permalink
Merge pull request #432 from cyjake/fix-jsonMerge-null
Browse files Browse the repository at this point in the history
fix: should still be able to jsonMerge() even if currently null
  • Loading branch information
coinkits authored Feb 7, 2025
2 parents 8f97cc5 + 2111e61 commit fb347cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ class Bone {
const data = { ...values };
for (const [name, value] of Object.entries(values)) {
if (value != null && typeof value === 'object') {
data[name] = new Raw(`${method}(${name}, ${SqlString.escape(JSON.stringify(value))})`);
data[name] = new Raw(`${method}(COALESCE(${name}, '{}'), ${SqlString.escape(JSON.stringify(value))})`);
} else {
data[name] = value;
}
Expand Down
7 changes: 7 additions & 0 deletions test/integration/suite/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ describe('=> Basic', () => {
await Gen.remove({}, true);
});

it('bone.jsonMerge(name, value, options) should still update if currently NULL', async () => {
const gen = await Gen.create({ name: '章3️⃣疯' });
assert.equal(gen.name, '章3️⃣疯');
await gen.jsonMerge('extra', { a: 1 });
assert.equal(gen.extra.a, 1);
});

it('bone.jsonMerge(name, value, options) should work', async () => {
const gen = await Gen.create({ name: '章3️⃣疯' });
assert.equal(gen.name, '章3️⃣疯');
Expand Down

0 comments on commit fb347cd

Please sign in to comment.