You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sequelize is automatically converting the date to include time zone adjustments. Because of this, inserts into the mssql DB are failing.
I would like to define a datatype to override the stringify method for the Date datatype. Doing so, following the sequelize documentation, I'm unable to override the method (https://sequelize.org/docs/v6/other-topics/extending-data-types/) is not working properly.
The text was updated successfully, but these errors were encountered:
import { DataTypes, Sequelize, Utils } from "sequelize";
const DATE = DataTypes.DATE.prototype.constructor;
export class MSSQLDATE extends DATE {
constructor(length: any) {
super(length);
}
// Method to convert to SQL representation
toSql() {
return "DATE";
}
// Method to stringify date for DB
stringify(date: Date, options: object) {
return this._applyTimezone(date, options).format('YYYY-MM-DD HH:mm:ss.SSS');
}
}
export function initCustomDatatypes() {
// Apply classToInvokable transformation
const invokableMSSQLDATE = Utils.classToInvokable(MSSQLDATE);
// Add key property
MSSQLDATE.prototype.key = 'MSSQLDATE';
//@ts-ignore
// Expose the class through Sequelize and DataTypes
Sequelize.MSSQLDATE = DataTypes.MSSQLDATE = invokableMSSQLDATE;
};
Make sure this code is executed before sequelize is initialized. I imported and called the initCustomDatatatypes function in a utilities database mixin. I imported and used the MSSQLDATE class as a datatype type in the sequelize model file.
How can we define a custom sequelize datatype?
Sequelize is automatically converting the date to include time zone adjustments. Because of this, inserts into the mssql DB are failing.
I would like to define a datatype to override the stringify method for the Date datatype. Doing so, following the sequelize documentation, I'm unable to override the method (https://sequelize.org/docs/v6/other-topics/extending-data-types/) is not working properly.
The text was updated successfully, but these errors were encountered: