Skip to content

Add support for newer JS Types (bigint, temporal, etc) #14296

Description

@ephys

With the arrival of new built-in JavaScript types like BigInt, and Temporal, we need to provide a way to tell Sequelize to use these types instead of the currently used string and Date.

Such a mechanism would allow users to easily migrate to these new types without creating a very large breaking change.

Proposed solution

My idea is to add a new option to the different DataTypes: jsType (e.g. jsType: 'string', jsType: 'bigint'). This option would tell Sequelize which built-in JS type should be used when retrieving an attribute from the database.

In the following example, id is declared as bigint in the database table, but will be represented as a string in JS.

class User extends Model {
  id: string;
}

User.init({
  id: DataTypes.BIGINT,
});

By setting the jsType option, we call make it be represented as a bigint in JS:

class User extends Model {
  id: bigint;
}

User.init({
  id: DataTypes.BIGINT({ jsType: `bigint` }),
});

side note: jsType: 'bigint' is likely to become the default setting for this data type, in a future major release.

This only impacts how Sequelize casts the values that come from the database. You can still set the value to a string yourself, this will continue functioning.

List of valid jsType values per Data type

if the data type is not listed, it doesn't accept this option.

  • DataTypes.BINARY: ArrayBuffer, Buffer
  • DataTypes.BIGINT: string (default), bigint
  • DataTypes.DECIMAL: string (default), unsafe-number (because decimal is an exact decimal, and number is an inexact decimal)
  • DataTypes.DATE: Date (default), string
  • DataTypes.DATEONLY: string (default), Temporal (= Temporal.PlainDate)
  • DataTypes.DATETIME: string (default), Temporal (= Temporal.PlainDateTime)
  • DataTypes.DATETIME.ZONED: string (default), Temporal (= Temporal.ZonedDateTime)
  • DataTypes.TIME: string (default), Temporal.PlainTime, Temporal.Duration
  • DataTypes.BLOB: ArrayBuffer, Buffer

This would resolve #8019

Activity

  1. added
    RFC
    on Mar 30, 2022
  2. self-assigned this
    on Mar 30, 2022
  3. changed the title [-][RFC] Support for Alternative JS Types[/-] [+][RFC] Add support for Alternative JS Types[/+] on Mar 30, 2022
  4. changed the title [-][RFC] Add support for Alternative JS Types[/-] [+][RFC] Add support for newer JS Types (bigint, temporal, etc)[/+] on Mar 30, 2022
  5. github-actions commented on Apr 14, 2022

    @github-actions
    Contributor

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

  6. QuentinFarizon commented on Oct 30, 2023

    @QuentinFarizon

    Hello @ephys I'd like to help by tackling this feature. Is it ready to work on ?

  7. changed the title [-][RFC] Add support for newer JS Types (bigint, temporal, etc)[/-] [+]Add support for newer JS Types (bigint, temporal, etc)[/+] on Mar 9, 2025
  8. removed
    RFC
    on Mar 9, 2025
  9. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions