Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Empty id field gets passed when .select() method is used #199

Open
TissuePowder opened this issue May 29, 2022 · 6 comments
Open

Empty id field gets passed when .select() method is used #199

TissuePowder opened this issue May 29, 2022 · 6 comments

Comments

@TissuePowder
Copy link

As the title says, when .select(), .distinct() or such methods are used, an empty id field is included in the returned data.

Basemodel:

class BaseModel extends authorize(hashid(Model)) {
  static get hashIdMinLength() {
    return 5;
  }
}

Code:

const meals = await Meal.query().select('quantity');

Current output:

[
  {
    "quantity": 1,
    "id": ""
  },
  {
    "quantity": 1,
    "id": ""
  },
  {
    "quantity": 1,
    "id": ""
  }
]

Expected output:

[
  {
    "quantity": 1
  },
  {
    "quantity": 1
  },
  {
    "quantity": 1
  }
]

Using objection-hashid 3.0.2, postgresql 14.3

@JaneJeon
Copy link
Owner

Makes sense. Should require a simple change in https://github.com/JaneJeon/objection-hashid/blob/master/index.js#L55 to "gate" setting any properties so that it's only run if the property was present in the original object as well. Mind raising a quick PR?

@TissuePowder
Copy link
Author

Looking at the code, will this do?

if (this.constructor.hashIdField) {
    obj.hasOwnProperty(this.constructor.hashIdField) && set(obj, this.constructor.hashIdField, this.hashId)
}

this.constructor.hashedFields.forEach(field => {
    const encoded = this.constructor._hashIdInstance.encode(get(obj, field))
    obj.hasOwnProperty(field) && set(obj, field, encoded)
})

I am not sure if there's any optimization or pattern you would want to follow, or would want to recheck some other parts of code, so just do it yourself if you don't mind.

@JaneJeon
Copy link
Owner

Should work, yeah. Just make sure to test your specific use case as well (the select())

@TissuePowder
Copy link
Author

Checked some time after I commented that. Worked as expected. Though, as I said, if you want to modify it in your preferred (or any better) way, please do it and make the commit.

@JaneJeon
Copy link
Owner

Cool, mind publishing the PR?

@TissuePowder
Copy link
Author

Done. #200

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

No branches or pull requests

2 participants