javascript - How to use milliseconds in sequelize standard createdAt field? - Stack Overflow

In a Nodejs application I'm using the Sequelize ORM to write records to a mysql database. Every ta

In a Nodejs application I'm using the Sequelize ORM to write records to a mysql database. Every table has a createdAt field by default, but that only records datetime with seconds:

mysql> SELECT createdAt FROM ticks LIMIT 3;
+---------------------+
| createdAt           |
+---------------------+
| 2017-11-08 16:34:21 |
| 2017-11-08 16:34:15 |
| 2017-11-08 16:34:27 |
+---------------------+
3 rows in set (0.00 sec)

Since I'm running a very time sensitive service I would also like to record milliseconds. In the docs I found the data types, which includes:

Sequelize.DATE(6)  // DATETIME(6) for mysql 5.6.4+. Fractional seconds support with up to 6 digits of precision

I never explicitly write the createdAt field though (Sequelize does that automagically), so I'm not sure how I could make that write milliseconds.

Could anybody point me in the right direction to save records with the createdAt field using millisecond precision? All tips are wele!

In a Nodejs application I'm using the Sequelize ORM to write records to a mysql database. Every table has a createdAt field by default, but that only records datetime with seconds:

mysql> SELECT createdAt FROM ticks LIMIT 3;
+---------------------+
| createdAt           |
+---------------------+
| 2017-11-08 16:34:21 |
| 2017-11-08 16:34:15 |
| 2017-11-08 16:34:27 |
+---------------------+
3 rows in set (0.00 sec)

Since I'm running a very time sensitive service I would also like to record milliseconds. In the docs I found the data types, which includes:

Sequelize.DATE(6)  // DATETIME(6) for mysql 5.6.4+. Fractional seconds support with up to 6 digits of precision

I never explicitly write the createdAt field though (Sequelize does that automagically), so I'm not sure how I could make that write milliseconds.

Could anybody point me in the right direction to save records with the createdAt field using millisecond precision? All tips are wele!

Share asked Dec 11, 2017 at 12:08 kramer65kramer65 54.1k133 gold badges331 silver badges526 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I updated the model and created a migration to utilize milliseconds:

Model:

import Sequelize from 'sequelize'

class Ticks extends db.instance.Model {
  static init(sequelize) {
    return super.init({
      .
      .
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE(6)
      }
    })
  }
}

Migration:

export default {
  up: (queryInterface, Sequelize) => queryInterface
    .changeColumn('ticks', 'createdAt', {
      allowNull: false,
      type: Sequelize.DATE(6)
    }),
  down: (queryInterface, Sequelize) => queryInterface
    .changeColumn('ticks', 'createdAt', {
      allowNull: false,
      type: Sequelize.DATE
    })
}

(Using ES6 syntax)

And with those changes I was able to get the milliseconds working (although it seems to be only to 3 places, even though I specified 6)

You can define createdAt explicitly in your model, for example:

id: {
    type: Sequlieze.INTEGER(11).UNSIGNED,
    autoIncrement: true,
    primaryKey: true
},
...
...
createdAt: {
    type: schema.DATE(6),
    allowNull: true,
    defaultValue: Sequelize.fn('NOW')
}

Or rename it by:

created_ts: {
    type: schema.DATE(6),
    allowNull: true,
    defaultValue: Sequelize.fn('NOW')
}, {
    createdAt: created_ts
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745054258a4608567.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信