Skip to content

Commit

Permalink
Remove String manipulations of Date objects
Browse files Browse the repository at this point in the history
This commit contains all previous work after rebase went badly
RE: Pull Request requested by @kjdelisle regarding #149 following my
proposed solution "A" at #149 (comment).
As mentioned in the post linked above, this change allows the
underlying mysqljs/mysql module to handle Date object serialization
and removes the forced conversion of Dates to UTC Strings.
An opt-out fallback to forced coercion to UTC is included,
which was modeled after #265 from @darknos .

Old, squashed commits:
d0ea1d9
Legacy UTC date processing fallback credit: @darknos

a59dad7
Remove orphaned string functions

e8fdbdc
Incorporate @darknos expanded check for zero dates

abd4e0a
Remove DATE manipulations in from/toColumnValue
  • Loading branch information
bbito authored and Kevin Delisle committed Apr 28, 2017
1 parent ea33f55 commit d0a88ef
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ function generateOptions(settings) {
options[p] = s[p];
}
}
// Legacy UTC Date Processing fallback - SHOULD BE TRANSITIONAL
if (s.legacyUtcDateProcessing === undefined) {
s.legacyUtcDateProcessing = true;
}
if (s.legacyUtcDateProcessing) {
options.timezone = 'Z';
}
}
return options;
}
Expand Down Expand Up @@ -307,19 +314,6 @@ MySQL.prototype.updateOrCreate = function(model, data, options, cb) {
this._modifyOrCreate(model, data, options, fields, cb);
};

function dateToMysql(val) {
return val.getUTCFullYear() + '-' +
fillZeros(val.getUTCMonth() + 1) + '-' +
fillZeros(val.getUTCDate()) + ' ' +
fillZeros(val.getUTCHours()) + ':' +
fillZeros(val.getUTCMinutes()) + ':' +
fillZeros(val.getUTCSeconds());

function fillZeros(v) {
return v < 10 ? '0' + v : v;
}
}

MySQL.prototype.getInsertedId = function(model, info) {
var insertedId = info && typeof info.insertId === 'number' ?
info.insertId : undefined;
Expand Down Expand Up @@ -356,7 +350,7 @@ MySQL.prototype.toColumnValue = function(prop, val) {
if (!val.toUTCString) {
val = new Date(val);
}
return dateToMysql(val);
return val;
}
if (prop.type === Boolean) {
return !!val;
Expand Down Expand Up @@ -417,8 +411,6 @@ MySQL.prototype.fromColumnValue = function(prop, val) {
// those separate.
if (val == '0000-00-00 00:00:00') {
val = null;
} else {
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
}
break;
case 'Boolean':
Expand Down

0 comments on commit d0a88ef

Please sign in to comment.