Estoy usando el generador de consultas Knex con Postgres en mi aplicación. Estoy tratando de agregar campos created_at y updated_at en mi base de datos con datos con formato ISO8016 en hora UTC. Estoy tratando de que mis datos se vean así:
2017-04-20T16:33:56.774Z En mis migraciones Knex, he intentado usar el método .timestamps() creando tanto created_at como updated_at manualmente con el método .timestamp() y nombrándolos yo mismo.
Cuando sembré mi base de datos y configuré created_at y updated_at igual a moment().utc().toISOString() , pero se almacena en mi base de datos así:
2017-04-20 11:20:00.851-05Hay algo entre el código y la base de datos que cambia los datos y no sé si es Knex, la biblioteca de nodos de Postgres o el propio Postgres.
Postgres almacena la marca de tiempo en formato interno y cuando lo lee lo muestra en el formato que lo solicita.
knex_test=# update accounts set created_at = '2017-04-20T16:33:56.774Z'; UPDATE 47 knex_test=# select created_at from accounts where id = 3; created_at ---------------------------- 2017-04-20 19:33:56.774+03 (1 row) knex_test=# \d accounts Table "public.accounts" Column | Type | Modifiers ------------+--------------------------+------------------------------------------------------------- id | bigint | not null default nextval('test_table_one_id_seq'::regclass) last_name | character varying(255) | email | character varying(255) | logins | integer | default 1 about | text | created_at | timestamp with time zone | updated_at | timestamp with time zone | phone | character varying(255) | Indexes: "test_table_one_pkey" PRIMARY KEY, btree (id) "test_table_one_email_unique" UNIQUE CONSTRAINT, btree (email) "test_table_one_logins_index" btree (logins) knex_test=#Puede cambiar en qué zona horaria postgres devuelve las marcas de tiempo para su conexión con
knex_test=# SET timezone = 'UTC'; SET knex_test=# select created_at from accounts where id = 3; created_at ---------------------------- 2017-04-20 16:33:56.774+00 (1 row) knex_test=#Y así es como se hace con knex https://github.com/tgriesser/knex/issues/97
var knex = Knex.initialize({ client: 'pg', connection: { host : '127.0.0.1', user : 'your_database_user', password : 'your_database_password', database : 'myapp_test', }, pool: { afterCreate: function(connection, callback) { connection.query('SET timezone = timezone;', function(err) { callback(err, connection); }); } } });