How to handle MySQL Date and Time formats in Sequelize?

I’ve been playing with Sequelize for a while and one thing that I really didn't like about it is that how it deals with MySQL dates. It requires that fields of Date or Time types would be defined as default JavaScript date objects. This in my opinion causes some unnecessary problems as you need to do conversations between date strings in different formats...

For example we have two fields in MySQL:

`CreateDate` DATE NULL DEFAULT NULL
`CreateTime` TIME NULL DEFAULT NULL

And I need to create new entry and show it to the user

let now = new Date() // 2017-02-27T10:03:30.212Z

DB.SomeModel.create({
    CreateDate: now,
    CreateTime: now
}).then(function(created) {
    console.log(created.CreateDate) // 2017-02-27T10:03:30.212Z
    console.log(created.CreateTime) // 2017-02-27T10:03:30.212Z
})
  • The first thing you will notice is that all dates are in JS format, not like default MySQL dates 2017-02-27 12:03:30. Yes, on the next query you will get it, but for now sorry.
  • Second thing is that all those dates are in UTC. It's not bad, but in my case I needed it with exact time zone

To solve the first problem just add get methods to the fields. We will use Moment for date parsing.

CreateDate: {
    type: DataTypes.DATEONLY,
    get: function() {
        return moment.utc(this.getDataValue('CreateDate')).format('YYYY-MM-DD')
    }
},
CreateTime: {
    type: DataTypes.TIME,
    get: function() {
        let time = this.getDataValue('CreateTime')

        if (moment(time, moment.ISO_8601, true).isValid()) {
            return moment(this.getDataValue('CreateTime')).format('HH:mm:ss')
        } else {
            return time
        }
    }
}

To solve the second problem you will need to set timezone for both Sequalize and Moment

const timezone = 'Europe/Vilnius'

require('moment').tz.setDefault(timezone)

let sequelize = new Sequelize(
    'database',
    'username',
    'password',
    {
        timezone: timezone
    }
)

Read More / Comment

Making HTTP requests in Node.JS without any dependencies using Promise

Quite often programming on Node.JS I have to perform various types of HTTP requests. To do this, I simply use the library Request, but sometimes there are situations when you need to perform a query in a certain way and it becomes a problem. As a result, I decided to write a small module that solves this problem.

Read More / Comment

A Fix For Slow Sendmail [Linux, PHP]

Recently I have noticed that one of my PHP scripts that was sending emails via the mail() function were hanging for roughly one minute while waiting for Sendmail to work.

The problem was that Sendmail requires that the result of "hostname" be a fully qualified domain name to start cleanly.

To get the domain name type hostname command and then copy it to /etc/hosts like this:

127.0.0.1 kiril-macbook.localdomain kiril-macbook localdev localhost

Read More / Comment

Socket.IO client authentication using Laravel 5 session data

Recently I had a need for rapid notifications of the user about the specific changes in the MySQL database. So I decided to use Socket.IO which I already used before in similar situations. The only problem was that I had to solve how to check if the connected Socket.IO client was authenticated in my Laravel 5 application and get its id to show specific data

Complete code example on GitHub

Read More / Comment

Fixing some column editing problems in Laravel Migrations

I faced this problems while I was rewriting file section of my blog. At first, I meant to use files only for blog, but recently I decided to create portfolio section which will also use uploaded files so the easiest way to do this was to add type column and map with other tables by id.

Read More / Comment

Hello, %username%!

So, the blog is open! Yay .

This is my first experience of creating a multilingual blog (swithc icon is in the heder), before that there were some attempts to create the it in Lithuanian and Russian.

The source code can be found at GitHub. Blog is currently in development, so in near future there will be a lot of new features.

Read More / Comment

Fork me on GitHub