Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize.js TIMESTAMP not DATETIME

In my node.js app I have several models in which I want to define TIMESTAMP type columns, including the default timestamps created_at and updated_at.

According to sequelize.js' documentation, there is only a DATE data type. It creates DATETIME columns in MySQL.

Example:

var User = sequelize.define('User', { ... // columns last_login: {             type: DataTypes.DATE,             allowNull: false         }, ... }, { // options         timestamps: true }); 

Is it possible to generate TIMESTAMP columns instead?

like image 212
ılǝ Avatar asked Apr 15 '15 14:04

ılǝ


People also ask

How do I store a TIMESTAMP in Sequelize?

If you want to use the TIMESTAMP type, then you need to manually specify the attributes as TIMESTAMP types in your model as shown below: const User = sequelize. define( "User", { firstName: Sequelize. STRING, createdAt: { type: "TIMESTAMP", defaultValue: sequelize.

What is difference between TIMESTAMP and datetime in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

How do you get the current date in Sequelize?

const TODAY = new Date(); const SUM = await OrdersModel. sum('price', { where: { created: Sequelize. DATE(TODAY), }, }); console. log(SUM);


2 Answers

Just pass in 'TIMESTAMP' string to your type

module.exports = {   up: function (queryInterface, Sequelize) {     return queryInterface.createTable('users', {       id: {         type: Sequelize.INTEGER,         primaryKey: true,         autoIncrement: true       },         created_at: {         type: 'TIMESTAMP',         defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),         allowNull: false       },       updated_at: {         type: 'TIMESTAMP',         defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),         allowNull: false       }     });   } }; 
like image 80
alucic Avatar answered Sep 28 '22 02:09

alucic


According to the Sequelize Documentation, you can set a defaultValue of Sequelize.NOW to create a timestamp field. This has the effect but relies on Sequelize to actually populate the timestamp. It does not create a "CURRENT_TIMESTAMP' attribute on the table.

var Foo = sequelize.define('Foo', {     // default values for dates => current time     myDate: {           type: Sequelize.DATE,           defaultValue: Sequelize.NOW      } }); 

So, this does accomplish the end goal of having a timestamp field, but it is controlled through Sequelize and not through the actual database engine.

It also appears to work on databases that do not have a timestamp functionality, so that may be a benefit.

Reference URL: http://sequelize.readthedocs.org/en/latest/docs/models-definition/#definition

like image 34
Nate Stone Avatar answered Sep 28 '22 04:09

Nate Stone