-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Provide new dateStrings option: "DATE" #1481
Conversation
My apologies, I didn't mean to leave that big comment block in there! I'll clean that up in 12 hours. |
This hopes to be a low-impact method to satisfy users wishing to handle only DATE fields as strings while retaining the default behavior for TIMESTAMP and DATETIME fields and providing backwards compatibility with existing code. Another and perhaps more efficient approach would be to add yet another property to connection.config with boolean options, e.g. connection.config.dateOnlyStrings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests for the added functionality :) !
@@ -61,7 +61,7 @@ function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings, | |||
case Types.DATETIME2: | |||
case Types.NEWDATE: | |||
var dateString = parser.parseLengthCodedString(); | |||
if (dateStrings) { | |||
if (dateStrings === true || field.type === Types.DATE && dateStrings.toString().toLowerCase() === "date") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is backwards-compatible, because code today with dateStrings: 1
will no longer work after this change, since we allowed any truthy value before to mean date strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback, I'll look at supporting any truthy value - except dateStrings.toString().toLowerCase() === "date"
- for backward compatible behavior and also look to adding the requested tests.
I may be back to ask for help on setting up the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! You may want to rebase your changes to the current master as well, as I just saw the GitHub is saying the PR is not mergable due to conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dougwilson , I was hoping to find an existing test for the current dateStrings
connection option to either add to or use as a base, but I'm not finding it. Does such a test exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RE: Requested tests, @dougwilson could you take a quick look at the more exhaustive tests on my branch: https://github.com/bbito/node-mysql/blob/test-datestrings-options/test/integration/connection/test-datestrings-options.js and let me know if I should merge that into this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @bbito sorry I haven't had the time to revisit this PR just yet. As for additional tests, I'm all for it :) If you are making tests just as general additions to test the functionality that already exists, that is awesome! It would be nice to have it as a separate PR, ideally, but you can add it to this PR if you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it stands the new test file will fail without the rest of the PR, so I'll merge it for now and if the PR is rejected and you want the test for the current code just let me know and I'll strip out the dateStrings: 'date'
stuff and submit it as a separate PR.
I feel like I've got a good PR now and an additional improvement for ths, more backwards compatible code is that as in the pre-feature code, there is a simple evaluation for truthiness of
|
I can't tell what is in the PR anymore, as it seems like the GitHub changes tab is not working and showing a while lot of other changes: https://github.com/mysqljs/mysql/pull/1481/files |
Wow, I wouldn't doubt that I messed up the rebase (my first one!), but I think I would have noticed it was now showing 33 files changed instead of 2! I'll see if I can figure out what went haywire! |
All you need to do is create the connection with option you are trying to test in the test. THe current main test that is supposed to be testing all the type casting from the database to JavaScript is https://github.com/mysqljs/mysql/blob/c19c867d032a89263dc8717a84596275b3e4dfa5/test/integration/connection/test-type-casting.js |
Thanks @dougwilson , I'll look a that existing test. |
Weird. I have a comment I want to make, but not sure if it'll get lost or not. Can you do like a squash rebase on top of the current master, perhaps? The following commands may get you out of the hole:
|
Thanks for the git help - I'll try that now |
Sorry, I forgot you're a fork :) The first two should be the name of your remote instead of origin. By default, this is usually upstream:
|
Okay, commits squashed down to 1 and back to 2 files changed as hoped - thanks. |
Add 'date' as dateStrings value option. The connection option dateStrings: 'date' will only affect DATE fields whereas any truthy value (except 'date') retains original behavior, effecting DATE, DATETIME and TIMESTAMP fields. See: mysqljs#605 , loopbackio/loopback-connector-mysql#120 , loopbackio/loopback-connector-mysql#149
This test confirms that DATETIME and TIMESTAMP columns are still exhibiting the default behavior, accepting and returning javascript date objects while DATE columns will accept and return a string value.
Test added, please let me know if there are any additional tasks to sew up this PR. |
Hi @dougwilson , I have a new test that I haven't pushed yet (test-datestrings-options.js), that loops through various truthy, falsy and 'date'-ish values for |
This test loops through various truthy, falsy and 'date'-ish values for connection option dateStrings: and runs corresponding typecasting checks.
@dougwilson I stuck the test-datestrings-options.js test onto a branch: https://github.com/bbito/node-mysql/blob/test-datestrings-options/test/integration/connection/test-datestrings-options.js |
Add tests where JavaScript Date objects are supplied when dateStrings: truthy or 'date' and check that strings are returned as well as supplying strings when dateStrings: falsy and check that JavaScript Date objects are returned.
Latest commit in test-datestrings-options branch includes 'mis-matched' tests - that is supplying JavaScript Date objects to the module when |
I swear I though I replied here at some point in the last few days, but I guess I'm just making stuff up in my mind :) Anyway, I need to take a look. Seems every time I turn around, more stuff is added to this PR, so please bear with me in trying to look it over :) |
@@ -62,7 +62,11 @@ function typeCast(field, parser, timeZone, supportBigNumbers, bigNumberStrings, | |||
case Types.NEWDATE: | |||
var dateString = parser.parseLengthCodedString(); | |||
if (dateStrings) { | |||
return dateString; | |||
if (field.type === Types.DATE) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also include Types.NEWDATE
? I believe that is a date string with no time component as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to include it. When I first started poking into this module I found NEWDATE
in the constants: https://github.com/mysqljs/mysql/blob/master/lib/protocol/constants/types.js , but I couldn't find it in mysql docs. Today I find 2 references to it in current docs: http://dev.mysql.com/doc/connector-net/en/connector-net-ref-mysqlclient-mysqlcommandmembers.html
Newdate: Obsolete. Use Datetime or Date type.
http://dev.mysql.com/doc/connectors/en/connector-net-ref-mysqlclient-mysqlcommand.html
Newdate: Obsolete. Use Datetime or Date type.
So unless there is a decision for this module to remove support for this obsolete Member/Type and it is removed from the switch
block in this file, I think I should amend this PR to support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types.NEWDATE
support added with Oct. 7 2016 commit: 975b2ae
@dougwilson Is there anything more you want for this PR? |
Is there anything I could do to assist with this? Seems like the PR is just about done, but I'm definitely interested in this functionality and would be happy to help out. |
This is on my backlog, but is low priority since it doesn't add any functionality that is not already in this module; it simply adds some sugar to what you can already do using a custom typecast. |
@dougwilson in my scenario I don't think I have the ability to pass a custom typecast because I'm not using this module directly, but rather through Strongloop/Loopback: https://github.com/strongloop/loopback. |
Hi @bbito, you may also want to make a PR to that wrapper in parallel to this PR, because the typecast is a very essential feature. I add things to this module from the perspective of using this module; if you are using a wrapper and it's forcing you to alter the module it's wrapping just to use it, you may want to use a different wrapper. |
Hi @bbito thanks for all your hard work. I am working on merging this now, for inclusion in the next release. The API would be an array similar to |
Adding checks to allow true OR "DATE" as dateStrings values "DATE" should only effect DATE fields whereas true retains original behavior, effecting DATE, DATETIME and TIMESTAMP fields. See: #605 , loopbackio/loopback-connector-mysql#120 , loopbackio/loopback-connector-mysql#149