Skip to content

Commit 345492e

Browse files
author
Sakib Hasan
authored
1 parent 70f0aca commit 345492e

File tree

3 files changed

+22
-355
lines changed

3 files changed

+22
-355
lines changed

README.md

+13-40
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Edit `datasources.json` to add any other additional properties that you require.
103103
<td>Enable this option to deal with big numbers (BIGINT and DECIMAL columns) in the database. Default is false.</td>
104104
</tr>
105105
<tr>
106-
<td>timezone</td>
106+
<td>timeZone</td>
107107
<td>String</td>
108108
<td>The timezone used to store local dates. Default is ‘local’.</td>
109109
</tr>
@@ -280,45 +280,6 @@ Example:
280280
}
281281
```
282282

283-
### Date types
284-
285-
For TIMESTAMP and DATE types, use the `dateType` option to specify custom type. By default it is DATETIME.
286-
287-
Example:
288-
289-
```javascript
290-
{ startTime :
291-
{ type: Date,
292-
dataType: 'timestamp'
293-
}
294-
}
295-
```
296-
297-
**Note:** When quering a `DATE` type, please be aware that values sent to the server via REST API call will be converted to a Date object using the server timezone. Then, only `YYYY-MM-DD` part of the date will be used for the SQL query.
298-
299-
For example, if the client and the server is in GMT+2 and GMT -2 timezone respectively. Performing the following operation at `02:00 on 2016/11/22` from the client side:
300-
301-
```javascript
302-
var products = Product.find({where:{expired:new Date(2016,11,22)}});
303-
```
304-
305-
will result in the REST URL to look like: `/api/Products/?filter={"where":{"expired":"2016-12-21T22:00:00Z"}}` and the SQL will be like this:
306-
307-
```SQL
308-
SELECT * FROM Product WHERE expired = '2016-12-21'
309-
```
310-
which is not correct.
311-
312-
**Solution:** The workaround to avoid such edge case boundaries with timezones is to use the `DATE` type field as a **_string_** type in the LoopBack model definition.
313-
314-
```javascript
315-
{ birthday :
316-
{ type: String,
317-
dataType: 'date'
318-
}
319-
}
320-
```
321-
322283
### Other types
323284

324285
Convert String / DataSource.Text / DataSource.JSON to the following MySQL types:
@@ -351,6 +312,18 @@ Example: 
351312
}
352313
```
353314

315+
Convert JSON Date types to  datetime or timestamp
316+
317+
Example: 
318+
319+
```javascript
320+
{ startTime :
321+
{ type: Date,
322+
dataType: 'timestamp'
323+
}
324+
}
325+
```
326+
354327
### Enum
355328

356329
Enums are special. Create an Enum using Enum factory:

lib/mysql.js

+9-51
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ function generateOptions(settings) {
135135
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
136136
supportBigNumbers: s.supportBigNumbers,
137137
connectionLimit: s.connectionLimit,
138-
//prevent mysqljs from converting DATE, DATETIME and TIMESTAMP types
139-
//to javascript Date object
140-
dateStrings: true,
141138
};
142139

143140
// Don't configure the DB if the pool can be used for multiple DBs
@@ -310,40 +307,17 @@ MySQL.prototype.updateOrCreate = function(model, data, options, cb) {
310307
this._modifyOrCreate(model, data, options, fields, cb);
311308
};
312309

313-
function dateToMysql(dt, tz) {
314-
if (!tz || tz == 'local') {
315-
return dt.getFullYear() + '-' +
316-
fillZeros(dt.getMonth() + 1) + '-' +
317-
fillZeros(dt.getDate()) + ' ' +
318-
fillZeros(dt.getHours()) + ':' +
319-
fillZeros(dt.getMinutes()) + ':' +
320-
fillZeros(dt.getSeconds());
321-
} else {
322-
tz = convertTimezone(tz);
323-
324-
if (tz !== false && tz !== 0) dt.setTime(dt.getTime() + (tz * 60000));
325-
326-
return dt.getUTCFullYear() + '-' +
327-
fillZeros(dt.getUTCMonth() + 1) + '-' +
328-
fillZeros(dt.getUTCDate()) + ' ' +
329-
fillZeros(dt.getUTCHours()) + ':' +
330-
fillZeros(dt.getUTCMinutes()) + ':' +
331-
fillZeros(dt.getUTCSeconds());
332-
}
310+
function dateToMysql(val) {
311+
return val.getUTCFullYear() + '-' +
312+
fillZeros(val.getUTCMonth() + 1) + '-' +
313+
fillZeros(val.getUTCDate()) + ' ' +
314+
fillZeros(val.getUTCHours()) + ':' +
315+
fillZeros(val.getUTCMinutes()) + ':' +
316+
fillZeros(val.getUTCSeconds());
333317

334318
function fillZeros(v) {
335319
return v < 10 ? '0' + v : v;
336320
}
337-
338-
function convertTimezone(tz) {
339-
if (tz === 'Z') {
340-
return 0;
341-
}
342-
343-
var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
344-
if (m) return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
345-
return false;
346-
}
347321
}
348322

349323
MySQL.prototype.getInsertedId = function(model, info) {
@@ -382,13 +356,6 @@ MySQL.prototype.toColumnValue = function(prop, val) {
382356
if (!val.toUTCString) {
383357
val = new Date(val);
384358
}
385-
386-
if (prop.dataType == 'date') {
387-
return dateToMysql(val).substring(0, 10);
388-
} else if (prop.dataType == 'timestamp' || (prop.mysql && prop.mysql.dataType == 'timestamp')) {
389-
var tz = this.client.config.connectionConfig.timezone;
390-
return dateToMysql(val, tz);
391-
}
392359
return dateToMysql(val);
393360
}
394361
if (prop.type === Boolean) {
@@ -448,19 +415,10 @@ MySQL.prototype.fromColumnValue = function(prop, val) {
448415
// MySQL allows, unless NO_ZERO_DATE is set, dummy date/time entries
449416
// new Date() will return Invalid Date for those, so we need to handle
450417
// those separate.
451-
if (!val || val == '0000-00-00 00:00:00' || val == '0000-00-00') {
418+
if (val == '0000-00-00 00:00:00') {
452419
val = null;
453420
} else {
454-
var dateString = val;
455-
var tz = this.client.config.connectionConfig.timezone;
456-
if (prop.dataType == 'date') {
457-
dateString += ' 00:00:00';
458-
}
459-
//if datatype is timestamp and zimezone is not local - convert to proper timezone
460-
if (tz !== 'local' && (prop.dataType == 'timestamp' || (prop.mysql && prop.mysql.dataType == 'timestamp'))) {
461-
dateString += ' ' + tz;
462-
}
463-
return new Date(dateString);
421+
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
464422
}
465423
break;
466424
case 'Boolean':

0 commit comments

Comments
 (0)