Converting BIGINT to DatePicker

Hi All,

We are facing an issue with converting the BIGINT field from our database to DataTypes.DATE format on the forest admin side.

Value from our DB: 1582194823
FA field in model:

expirationTime: {
      type: DataTypes.DATE,
      field: 'expiration_time',
      defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
    },

Expected behavior

Expectation: 2020-02-20

Actual behavior

Actual: 1970-01-19

Failure Logs

And on update a field we have an error:
Data truncated for column 'expiration_time' at row 1

Context

Please provide any relevant information about your setup.

  • Package Version: 0.0.1
  • Express Version: ~4.16.3
  • Sequelize Version: ~5.15.1
  • Database Dialect:
  • Database Version:
  • Project Name: tymeshiftfa
1 Like

Hi @Bojan_Antonijevic,

Welcome to Forest Admin Community :wave: !

I don’t think you specify DataTypes.DATE to cast it. It does not work like that as sequelize will try to save it like that in database 2019-01-02 so it cannot work.

I think you could try to use a TIMESTAMP:

expirationTime: {
      type: 'TIMESTAMP',
      field: 'expiration_time',
      defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},

If it does not work, a solution could be to create a smart field. So you would declare first your expirationTime like that:

expirationTime: {
      type: DataTypes.BIGINT,
      field: 'expiration_time',
      defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
    },

And then create a smart field like so:

collection('myModel', {
  fields: [{
    field: 'expirationTimeDate',
    type: 'Date',
    get(record) {
      return new Date(record.expirationTime);
    },
    set(record, expirationTimeDate) {
      // I don't know if expirationTimeDate is a date here or a string. I will consider it as a Date
      record.expirationTime = expirationTimeDate.getTime();
      return record;
    },
});

I hope that helps :pray:

Hey @vince ,
We appreciate your answer, thanks for helping us.

When we add a new field as in the example:

fields: [{ 
    field: 'expirationTimeDate', 
    type: 'Date',
  1. Should it be added in .forestadmin-scheme.json?
  2. And we could display this field in our UI layout?

If that is the case we have an issue because after adding this field scheme is not updated and we could not see it in the layout. What reasons should be to not updating the scheme? I guess this is the reason why this example didn’t solve the problem completely. We tried another way without extra fields.

We solved half of the problem, getting proper date now is working after modifying get(record) with this:

get(record) {
    record.expirationTime = new Date(record.expirationTime * 1000);
	return record.expirationTime;
}

When we try the same with the set(record) we could not do the similar:

set(record) {
	   console.log(record); // It's look like not getting here at all. Probably some syntax problem?
	   record.expirationTime = (new Date(record.expirationTime).getTime()/1000);
	   return record.expirationTime;
 },

still have an error:

Data truncated for column 'expiration_time' at row 1

Hi @Bojan_Antonijevic,

The .forestadmin-schema.json should contains a definition of your newly added field (After a server restart).
Looking at the smart field documentation, it seems that set should take more than one parameter if I’m correct.

Could you please share the complete configuration of your smart field so I could also try it myself ?

Thanks