knex: If it's NULL, let it be NULL

I have NULL data in DATE column. But when fetching using bookshelf (using knex), it’s return Thu Jan 01 1970 07:00:00 GMT+0700 (SE Asia Standard Time) equal to new Date(null) or new Date(0).

After checking knex@0.10.0, I found parseType function:

// file: .\knex\lib\dialects\maria\index.js
// line: 117
function parseType(value, type) {
  switch (type) {
    case 'DATETIME':
    case 'TIMESTAMP':
      return new Date(value);
    case 'INTEGER':
      return parseInt(value, 10);
    default:
      return value;
  }
}

In regards to NULLABLE column type. IMHO, it’s should respect the NULL data.

But with parseType function above, if the data is NULL will produce:

  • start of javascript Date Object for DATETIME and TIMESTAMP
  • and NaN for Integer

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 26 (5 by maintainers)

Commits related to this issue

Most upvoted comments

IMO datetimes / timestamps should be returned as strings so that user can decide if he wants to convert them to JavaScript Date objects. However that change would probably break too many clients so probably better to leave that conversion there…

Anyways converting NULL really sounds like a bug. Some simple test case + PR to fix it would be nice.