dmfay / massive-js

A data mapper for Node.js and PostgreSQL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Update body (jsonb) and field at once

ritwickdey opened this issue · comments

Summary

db.tests.update(id, { firstName: 'Ritwick' }) // this only update Field.
db.tests.updateDoc(id, { someProps: 'foo' }) // this only update body (jsonb) Field.

But I can't do

db.tests.update(id, { firstName: 'Ritwick', body: { someProps: 'foo' } })

Have you tried? Did you receive an error message, did one or both fields retain their previous values, what happened? "I can't do" doesn't give me anything to go on. Please use the full issue template.

I can tell you that I've used update to modify mixed ordinary and JSON values going back to Massive v2, and that this testcase passes:

  it('updates mixed column types', function () {
    return db.normal_pk.update(1, {field1: 'zeta', json_field: {a: 'b', c: {d: 'e'}}}).then(res => {
      assert.equal(res.id, 1);
      assert.equal(res.field1, 'zeta');
      assert.deepEqual(res.json_field, {a: 'b', c: {d: 'e'}});
    });
  });

Hi @dmfay, Sorry for late reply.

It actually override the existing body object.
Is this possible to patch body object ?

Merging into existing JSON/JSONB values is only supported with updateDoc. Your other options at present are to make sure you have the full body beforehand and use update, or to create a script that merges your body changes with the || operator.