Sanjeet Uppal
Sanjeet Uppal

Reputation: 39

Timestamp being sent in my route response, but my table in the database does not show the timestamp

exports.up = (knex, Promise) => knex.schema.createTable('cohorts', (table) => {
  table.increments('id')
  .primary();
  table.string('name', 'varchar(6)')
  .notNullable();
  table.string('type', 'varchar(3)')
  .notNullable();
  table.date('q1_start_date')
  .notNullable();
  table.date('q2_start_date')
  .notNullable();
  table.date('q3_start_date')
  .notNullable();
  table.date('q4_start_date')
  .notNullable();
  table.date('graduation_date')
  .notNullable();
  table.integer('campus_id')
  .notNullable()
  .references('id')
  .inTable('campuses')
  .onDelete('CASCADE');
  table.timestamps(true, true);
});

exports.down = (knex, Promise) => knex.schema.dropTable('cohorts');

I am using postgresql to make my database in which I use date in my migrations when I check my table in the database it is stored in the date format; however when writing my routes in bookshelf.js those dates come with the timestamp.

Here is my response with the timestamp

Upvotes: 1

Views: 113

Answers (1)

Mikael Lepistö
Mikael Lepistö

Reputation: 19728

when node-postgres driver reads date type of column from database, it automatically converts it to Date object of javascript, which has actually date and time.

You can configure pg driver to return dates in string format if you like to with this package https://github.com/brianc/node-pg-types

Type id of date type is:

mikaelle=# select typname, oid, typarray from pg_type where 
pg_type.typname = 'date' order by oid;
 typname | oid  | typarray 
---------+------+----------
 date    | 1082 |     1182
(1 row)

And code to set parsing for the type can be done like this:

var types = require('pg').types
types.setTypeParser(1082, function(val) {
  return val; // just pass the string
})

Upvotes: 1

Related Questions