最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Knexjs returning mysql timestamp, datetime columns as Javascript Date object - Stack Overflow

programmeradmin2浏览0评论

I am using knex and I insert data in the format YYYY-MM-DD HH:mm:ss e.g 2017-07-14 15:00:00. After saving, when the data is fetched, the datetime column values are returned as JavaScript Date objects.

I want to return those objects in the format YYYY-MM-DD HH:mm:ss but knex returns them in the format YYYY-MM-DDTHH:mm:ss.000Z e.g 2017-06-23T06:44:44.000Z.

I am returning them by iterating and converting them manually. I was wondering if there is another way to do it like in mysql driver or knex configuration.

This is my knex configuration:

 var connection = require('knex')({
            client: 'mysql',
            connection: {
                host: db.host,
                user: db.user,
                password: db.password,
                database: db.database,
                timezone: 'UTC'
            }
       });

I am using knex and I insert data in the format YYYY-MM-DD HH:mm:ss e.g 2017-07-14 15:00:00. After saving, when the data is fetched, the datetime column values are returned as JavaScript Date objects.

I want to return those objects in the format YYYY-MM-DD HH:mm:ss but knex returns them in the format YYYY-MM-DDTHH:mm:ss.000Z e.g 2017-06-23T06:44:44.000Z.

I am returning them by iterating and converting them manually. I was wondering if there is another way to do it like in mysql driver or knex configuration.

This is my knex configuration:

 var connection = require('knex')({
            client: 'mysql',
            connection: {
                host: db.host,
                user: db.user,
                password: db.password,
                database: db.database,
                timezone: 'UTC'
            }
       });
Share Improve this question edited Sep 28, 2023 at 8:26 icc97 12.8k9 gold badges83 silver badges96 bronze badges asked Jul 14, 2017 at 13:04 Shahzaib SheikhShahzaib Sheikh 6671 gold badge10 silver badges22 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 27

Change your connection object with this:

var connection = require('knex')({
        client: 'mysql',
        connection: {
            host: db.host,
            user: db.user,
            password: db.password,
            database: db.database,
            timezone: 'UTC',
            dateStrings: true
        }
   });

This is how mysql driver converts types read from database to javascript (https://github.com/mysqljs/mysql#type-casting)

You can override default conversion by adding typeCast connection option:

var moment = require('moment');
var connection = require('knex')({
        client: 'mysql',
        connection: {
            host: db.host,
            user: db.user,
            password: db.password,
            database: db.database,
            timezone: 'UTC',
            typeCast: function (field, next) {
              if (field.type == 'DATETIME') {
                return moment(field.string()).format('YYYY-MM-DD HH:mm:ss');
              }
              return next();
            }
        }
   });

I'm not sure if you need to add custom parsing for DATETIME or TIMESTAMP type though.

In my case, the connection was a string so I had to find the date OID and use pg.types.setTypeParser(DATE_OID, d => moment(d));

发布评论

评论列表(0)

  1. 暂无评论