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

Json keys no longer updating #1324

Closed
thejuan opened this issue May 8, 2019 · 9 comments
Closed

Json keys no longer updating #1324

thejuan opened this issue May 8, 2019 · 9 comments

Comments

@thejuan
Copy link
Contributor

thejuan commented May 8, 2019

The fix for #1089 is a breaking change for us and I'm trying to work out whether we have been misusing the system or whether there is a bug.

Our use case is that we have a data dump field on most tables that we normalise onto the model

modelClass.columnNameToPropertyName always returns null and so the paths are empty in the call to jsonb_set

Repo:

const { Model } = require("objection");
const _ = require("lodash");

class Example extends Model {
  static get tableName() {
    return "example";
  }

  // From Database
  $parseDatabaseJson(json) {
    json.availabilityRequirements = _.get(json, "data.availabilityRequirements");
    return json;
  }

  // On database insert
  $formatDatabaseJson(json) {
    if (_.has(json, "availabilityRequirements")) {
      json[`data:availabilityRequirements`] = json["availabilityRequirements"];
      delete json["availabilityRequirements"];
    }
    return json;
  }
}

describe("Problem", () => {
  it("should set inner properties", () => {
    expect(
      Example.query()
        .patch({ availabilityRequirements: "some" })
        .toString(),
    ).toEqual(`update "example" set "data" = jsonb_set("data", '{availabilityRequirements}', '"some"', true)`);
  });
});

@elhigu
Copy link
Contributor

elhigu commented May 8, 2019

Shouldn't that patch be: { "data:availabilityRequirements": "some" } AFAIK you are trying to set unknown column availabilityRequirements to be some with that code.

@thejuan
Copy link
Contributor Author

thejuan commented May 8, 2019

FormatDatabaseJson handles that

@elhigu
Copy link
Contributor

elhigu commented May 8, 2019

Could add output what your query now generates?

@thejuan
Copy link
Contributor Author

thejuan commented May 8, 2019

update "example" set "data" = jsonb_set("data", '{}', '"some"', true)

@thejuan
Copy link
Contributor Author

thejuan commented May 8, 2019

columnNameToPropertyName returns null for the prop name when generating the statement.

After formatDatabaseName is called why do we need to map a col to a prop name ?

@koskimas
Copy link
Collaborator

koskimas commented May 9, 2019

After formatDatabaseName is called why do we need to map a col to a prop name ?

See #1089

@koskimas
Copy link
Collaborator

koskimas commented May 9, 2019

I haven't considered that use case. It worked before by accident. $formatDatabaseJson isn't the best place to do that. Could you use beforeInsert and beforeUpdate instead?

@thejuan
Copy link
Contributor Author

thejuan commented May 9, 2019

Its a large change for us. The docs for https://vincit.github.io/objection.js/api/model/instance-methods.html#formatdatabasejson are pretty clear that this method is put it into database format.
Once it's in db format it doesn't make sense to be converting column names again.

As you said here #1089 (comment)
Objection touching the json field keys is a bug?

An easy fix for us is columnNameToPropertyName shouldn't return null ever, just return the unmodified key

@koskimas koskimas closed this as completed May 9, 2019
@thejuan
Copy link
Contributor Author

thejuan commented May 10, 2019

Can you elaborate on why this was closed? Why is $formatDatabaseJson the wrong place for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants