-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Javascript BigInt support in Node 10+ #10468
Comments
See brianc/node-pg-types#78 for discussion about this behavior upstream in node-pg. Arguably, Sequelize should just inherit the defaults of the underlying driver. |
BigInt and other non-Number number types are now passed through as of #10492. But there's still room for Sequelize to do more with native support. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂 |
I like this idea, reopening |
Node 8 reaches end-of-life on December 31, 2019. So any support probably shouldn't worry about polyfills at this point. |
I'm also having the same behaviour. Easy to handle but it would be nice to get more native support |
Seems like its about time we solve this :-) |
I have been working around this by implementing a custom |
Given TypeScript, some casting due to bad typing from Sequelize, and some hackery -- this appears to implement BigInt support although not ideal. It expected setting import { Utils, DataType } from 'sequelize';
import { Sequelize } from 'sequelize-typescript';
// must use the auto binding method due to how sequelize handles these values internally
// invalid types for this value
// eslint-disable-next-line @typescript-eslint/no-explicit-any
class BIGINT_NATIVE_CLASS extends (Sequelize as any).DataTypes.ABSTRACT {
key = 'BIGINT';
static key = 'BIGINT';
toSql = () => 'BIGINT(20) UNSIGNED';
validate = (value: unknown) => {
return typeof value === 'bigint';
};
_stringify = (value: bigint) => String(value);
_sanitize = (value: string) => BigInt(value);
_bindParam = (value: bigint | number | string, options: object) => {
return options.bindParam(BigInt(value));
};
}
// Mandatory: add the new type to DataTypes. Optionally wrap it on `Utils.classToInvokable` to
// be able to use this datatype directly without having to call `new` on it.
const BIGINT_NATIVE = (Utils.classToInvokable(
BIGINT_NATIVE_CLASS,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) as any) as DataType;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Sequelize as any).DataTypes.BIGINT_NATIVE = BIGINT_NATIVE;
export { BIGINT_NATIVE }; Should be easy enough to convert to js as well...
|
Note: I am on Node 12 w/ Sequelize 6.3.5 and am seeing the INTEGER sequelize type (w/ PG drivers) return Strings as well. Edit: the model definition is iNTEGER, but the postgres table definition is |
What are you doing?
That integers greater than Number.MAX_SAFE_INTEGER need to be represented as Javascript strings is an issue that confuses a lot of people. Now that Node 10+ has a native BigInt type, it may be time to reassess Sequelize's approach.
It would be nice if Sequelize deserialized arbitrary precision integer types as JS BigInts, perhaps falling back to a polyfill like https://www.npmjs.com/package/big-integer.
This would be a breaking change, so an option to opt in to this behavior is probably warranted.
What do you expect to happen?
userID
should equal1n
.What is actually happening?
userID
equals"1"
;Dialect: any
Dialect version: any
Database version: any
Sequelize version: any
Tested with latest release: No (If yes, specify that version)
The text was updated successfully, but these errors were encountered: