Storing Node.js date in Knex dateTime () / MySQL DATETIME field

I'm having trouble finding a date format that is common to Node.js, knex, and MySQL (via the Bookshelf).

I installed a table with the Knex schema builder:

knex.schema.createTableIfNotExists("examples", function (table) {
    ...
    table.dateTime("some_datetime");
})

In MySQL, a column with a type is created DATETIME.

I have a model of a bookshelf that represents this (leaving all the boilerplate material here), and I'm trying to use the built-in Date.now()as a value:

exampleModel.save({
    some_datetime: Date.now()
})

With debugging enabled in Knex, I see that the request is actually trying to insert an era timestamp with milliseconds ("..." for short):

{ ...
  bindings: [ 1485644012453, ... ],
  sql: 'update `examples` set `some_datetime` = ? where `id` = ?' }

But this is not true, since MySQL expects what you will useFROM_UNIXTIME in this case, so the final date in the database is, of course, good ol ' 0000-00-00 00:00:00.

, ?

  • , ?
  • , - , , Date.now()?
  • - ?

. , DATETIME Knex, Date.now() Node, DATETIME MySQL, .

, : , - , / . , .

+4
2

Javascript Date.now() . Mysql, knex , , ISO8061 Date() DATETIME.

Date "YYYY-mm-dd HH: ii: ss"

https://github.com/mysqljs/mysql

, Date.now() new Date() new Date().toISOString()

EDIT:

, mysql .toISOString(), docs https://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-overview.html

MariaDB [(none)]> select CAST('2017-01-30T16:49:19.278Z' AS DATETIME);
+----------------------------------------------+
| CAST('2017-01-30T16:49:19.278Z' AS DATETIME) |
+----------------------------------------------+
| 2017-01-30 16:49:19                          |
+----------------------------------------------+
1 row in set, 1 warning (0.00 sec)

, . ! .

+7

Date.now() now() Knex.js:

const knexfile = require('../../knexfile');
const knex = require('knex')(knexfile.development);
const date = knex.fn.now();

:

const knexfile = require('../../knexfile');
const knex = require('knex')(knexfile.development);
knex.raw('CURRENT_TIMESTAMP');
+2

Source: https://habr.com/ru/post/1668149/


All Articles