Custom datatype not working
See original GitHub issueWhat are you doing?
// Custom Type File
import Big from "big.js";
export default function sequelizeAdditions(Sequelize) {
const DataTypes = Sequelize.DataTypes;
/*
* Create new types
*/
class BIGJS extends DataTypes.DECIMAL {
public static parse(value) {
return new Big(value);
}
private _precision: number;
private _scale: number;
constructor(precision: number, scale: number) {
super();
this._precision = precision;
this._scale = scale;
}
public toSql() {
if (this._precision || this._scale) {
return `DECIMAL(${[this._precision, this._scale].filter(this.identity).join(",")})`;
}
return "DECIMAL";
}
// Optional, validator function
public validate(value, options) {
if (value instanceof Big) {
return true;
} else {
return false;
}
}
}
DataTypes.BIGJS = BIGJS;
// Mandatory, set key
DataTypes.BIGJS.prototype.key = DataTypes.BIGJS.key = "BIGJS";
// For convenience
// `classToInvokable` allows you to use the datatype without `new`
Sequelize.BIGJS = Sequelize.Utils.classToInvokable(DataTypes.BIGJS);
}
// Connection File
import { Sequelize } from "sequelize";
import BigJsCol from "./types/BigJsColumn";
BigJsCol(Sequelize);
const sequelize = new Sequelize(...);
// Model File
MyModel.init({
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
},
amount: new DataTypes.BIGJS(13, 4)
}, {
sequelize: Connection,
tableName: "MyModel",
});
// Simple Query
await MyModel.findAll();
To Reproduce Steps to reproduce the behavior:
- Defined models, and get everything wired up
- Run simple query
- Result for the field amount is not wrapped in a BigJs object
What do you expect to happen?
I would like the result coming back from the database to be wrapped
What is actually happening?
It is just returning a number 😦
Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any Dialect mysql2 version: 1.6.5 Database version: 5.7.26 Sequelize version: 5.9.3 Node Version: 12.x OS: Mac OS 10.14.5 If TypeScript related: TypeScript version: 3.5.3 Tested with latest release:
- No
- Yes, specify that version:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:30 (16 by maintainers)
Top Results From Across the Web
LWC Datatable with custom data type not rendering as supose
I found the problem, a big part of the issue was a typo in the CustomDatatable component. export default class richDatatable extends ...
Read more >Custom data type not getting assigned values for record creation
I wrote a custom data type by extending Abstract, like in the example listed for the following link: https://sequelize.org/v5/manual/data-types.
Read more >custom data type not showing up - Extending Umbraco
I have the following, issue, i created a new custom datatype with the Umbraco usercontrol wrapper. I just based it on a few...
Read more >Custom types - Introduction to MPI - CodinGame
This problem could be solved in a simpler way using derived datatypes. A datatype can be defined easily by specifying a sequence of...
Read more >Custom Datatype in sql table - Forum - One Identity
I have a custom datatype, which is not view-able through sync editor. ... -user-guide-for-connecting-sql-server-databases/native-database-connector-for-sql- ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The issue lays with
classToInvokable, which doesn’t allow access to the actual constructor. The following code shows how we worked around that:This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the “stale” label. 🙂