Map raw field names to attributes.#3995
Conversation
|
I think it would be better if it happened at a lower level than build so it could be reused for bulkCreate etc. |
|
Any suggestions where a better place for it would be? Maybe in the Also, not sure if you have any insight into the error the build system is throwing, I am not able to reproduce the error locally (and it seems unrelated to my changes): |
|
Probably in post query result parsing, let the caller pass a field to attribute map that the query result parser can use perhaps. It's something we need a generalized approach to. |
|
I attempted to generalize this a bit more and moved the aliasing into |
d78c926 to
bbdea2a
Compare
lib/dialects/abstract/query.js
Outdated
There was a problem hiding this comment.
result[field] = undefined is a bit quicker, can you check if it produces the same result?
There was a problem hiding this comment.
It still passes the tests, however, it leaves remnants in the dataValues object.
For example, this is from one of the tests, email_address was aliased to emailAddress.
dataValues: {
id: 1,
username: 'john',
email_address: undefined,
createdAt: Sun Jan 01 2012 02:10:10 GMT-0800 (PST),
updatedAt: Sun Jan 01 2012 02:10:10 GMT-0800 (PST),
emailAddress: 'john@gmail.com'
}There was a problem hiding this comment.
Ah yeah of course, we don't want that.
|
Looking good. Although i'm not sure about the |
|
Looks like the other parameter names for |
|
That's even less descriptive though :) |
|
I see, more descriptive! How about |
|
Maybe just |
|
That sounds good to me, updating it now. |
6ffd7ab to
027743f
Compare
lib/dialects/abstract/query.js
Outdated
There was a problem hiding this comment.
This should be written as
result.field !== undefinedThere was a problem hiding this comment.
@BridgeAR in causes de-opt? And it would be result[field]
There was a problem hiding this comment.
This depends on if any field name in result is somehow special or not. If it is e.g. Strange / field it'll deopt (there was an issue with someone using slashing in his field names :D ). Otherwise it should not but checking for undefined will be faster.
|
LGTM (besides one minor comment) even tough I think it would be nice to improve this at some point to only include the fields needed from the beginning on. But this is only internal stuff. |
|
@BridgeAR From the beggining? I'm not quite sure i understand? You mean parse and rewrite a raw query to alias the fields? |
|
Yes |
|
Just pushed the minor change, checking if the field existed in the result using |
|
@BridgeAR That does not sound feasible at all, don't think we'll ever do that ;p Rewriting raw queries would be a bit too magic i think. (Why is the input not my actual input?) |
lib/dialects/abstract/query.js
Outdated
There was a problem hiding this comment.
We should probably put this under the raw if so it isn't run on all selects, no need to encur the extra loop when it's not needed.
There was a problem hiding this comment.
Currently the fix also works for postgres returning: true on update. We'd have to put it behind this.options.raw || this.options.type === QueryTypes.BULKUPDATE.
There was a problem hiding this comment.
That sounds like a bug, update should not result in a handleSelectQuery call.
There was a problem hiding this comment.
I assumed it did this by design as handling a UPDATE ... RETURNING * is similar to a select. Here is the call to handleSelectQuery in postgres/query.js.
There was a problem hiding this comment.
Huh, yeah maybe you're right. I'm afraid we can't incur that perf overhead for all select queries since they are already perf sensitive and wouldnt take nicely to another nested loop, please do the large if :)
There was a problem hiding this comment.
Hmm, it looks like this.options.raw isn't set if a model is passed in query.js#L671. Is there a way to know if the query originated from user input on sequelize.query directly without using options.raw?
There was a problem hiding this comment.
Hmm no, no way to know, that is a bit of a problem since actually in this case you don't want a raw result.
There was a problem hiding this comment.
We could introduce a new option to toggle on or off the auto-mapping to model attributes behavior. Right now we assume we should map fields anytime the model option is set.
But it would require users writing raw queries to set on the new option in addition to setting the model.
82e9be2 to
8ed2b18
Compare
f89e722 to
932e654
Compare
|
@mickhansen Trying another approach to this problem. I replaced Let me know your thoughts! |
|
@sankethkatta I like this approach, it looks pretty clean to me. I'll let @mickhansen comment on this too before merging but LGTM :) |
|
@janmeier @mickhansen any update on this? |
|
@sankethkatta Mick is on vacation, I'll bug him about this when he's back ;) |
|
Thanks! 😃 |
|
ping! 🔔 |
|
Merge please? |
|
Hmm, nevermind the last comment i just deleted. |
|
I don't necessarily like that the current approach requires the user to set the Let me know if you can think of a better way! |
There was a problem hiding this comment.
Sorry for being so late to comment.
Ideally i'd like a generic mapper passed to query, or one generated in query and then used here.
The less the query generator and query handler needs to know about Model the better.
Refactor this specific code to use this.options.fieldMap and then in sequelize.query you can use options.fieldMap = mapToModel && options.model && etc.
There was a problem hiding this comment.
Refactored in latest commit.
e8358ad to
24dfa28
Compare
changelog.md
Outdated
There was a problem hiding this comment.
[FEATURE] > [ADDED]
perhaps: "Map raw fields back to attribute names when using returning ..."
|
Please add API docs to (please squash commits to a single descriptive commit when ready aswell) |
24dfa28 to
ad18baf
Compare
|
I addressed the comments and squashed the commits. |
Map raw field names to attributes.
|
Nope jsdoc works :) |
|
Thank you for your work! |
|
Awesome! Thank you for working through this with me! 😃 |
Add feature #3714.
This also allows the
returning: trueoption forupdatein postgres to work properly. Currently it generates outRETURNING *, which faces the same problem as described with raw queries in #3714.