11import { DataTypes , ValidationErrorItem } from "@sequelize/core" ;
22
3+ /**
4+ * A better bigint type than the one Sequelize provides
5+ * It uses a native bigint where supported, otherwise it converts it to a string adding the suffix "n" to prevent
6+ * the driver parsing as a number
7+ */
38export class RealBigInt extends DataTypes . ABSTRACT < bigint > {
49 toSql ( ) {
5- return "BIGINT" ;
6- // this is actually kind of bad, as it will use BIGINT on sqlite too, potentially losing data
7- // however since sqlite is only used for testing i basically don't care
10+ if ( this . nativeBigIntSupport ( ) ) {
11+ return "BIGINT" ;
12+ } else {
13+ return "STRING" ;
14+ }
15+ }
16+
17+ nativeBigIntSupport ( ) {
18+ return this . _getDialect ( ) . supports . dataTypes . BIGINT ;
19+ }
20+
21+ override toBindableValue ( value : bigint ) : unknown {
22+ if ( this . nativeBigIntSupport ( ) ) {
23+ return value ;
24+ } else {
25+ return `${ value . toString ( ) } n` ;
26+ }
827 }
928
1029 override sanitize ( value : unknown ) : unknown {
@@ -29,7 +48,13 @@ export class RealBigInt extends DataTypes.ABSTRACT<bigint> {
2948
3049 override parseDatabaseValue ( value : unknown ) {
3150 if ( typeof value === "bigint" ) return value ;
32- if ( typeof value === "string" ) return BigInt ( value ) ;
51+ if ( typeof value === "string" ) {
52+ // stupid lol
53+ if ( value . endsWith ( "n" ) ) {
54+ return BigInt ( value . slice ( 0 , - 1 ) ) ;
55+ }
56+ return BigInt ( value ) ;
57+ }
3358 if ( typeof value === "number" ) return BigInt ( value ) ;
3459 if ( typeof value === "boolean" ) return BigInt ( value ) ;
3560
0 commit comments