question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom datatype not working

See original GitHub issue

What 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:

  1. Defined models, and get everything wired up
  2. Run simple query
  3. 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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:30 (16 by maintainers)

github_iconTop GitHub Comments

19reactions
mcuelenaerecommented, Mar 8, 2020

The issue lays with classToInvokable, which doesn’t allow access to the actual constructor. The following code shows how we worked around that:

const DataTypes = require('sequelize/lib/data-types');
const { classToInvokable } = require('sequelize/lib/utils/classToInvokable');

// since DataTypes.ABSTRACT is wrapped with classToInvokable(), we can't get to
// the actual class directly, so use this indirection
const ABSTRACT = DataTypes.ABSTRACT.prototype.constructor;

class Custom extends ABSTRACT {
  static key = 'CUSTOM';

  toSql() {
    return 'BIGINT(20) UNSIGNED';
  }

  ...
}
DataTypes.CUSTOM = classToInvokable(Custom);
5reactions
github-actions[bot]commented, Nov 9, 2021

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. 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found