You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't change default parser for Postgresql using setTypeParser after version 4.32.3 #9074
pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yetstaletype: bugDEPRECATED: replace with the "bug" issue type
changed the title [-]Can't change default parser for Postgresql using setTypeParser after version 4.32.4[/-][+]Can't change default parser for Postgresql using setTypeParser after version 4.32.3[/+]on Feb 20, 2018
Edit: The old pg.types.setTypeParser method still works for other DataTypes, like DATEONLY (1082). I'm a bit confused on what is going on in the code...
Reacted by mike-seekwell, rene-enriquez-ct, Jonathan Keebler and Andrii Oriekhov
I've also been digging though the code and problem seems to have been introduced with #9025 and #9026.
The way the parser is stored for future usage in the connectionManager (via a function that will call dataType.parse method when needed instead of storing its current reference) makes adding custom parser logic quite obscure if you don't clone the dataType object, as a subsequent modification of Sequelize.DataTypes.postgres.*.parse value in another sequelize instance will impact all instances.
My current workaround to solve the problem without having to use the hack provided by @kingjerod (which is a valid solution but it impacts all sequelize instances) is this:
Nevertheless, as a getTypeParser(oid) method is already there, I would say the more elegant way to provide the functionality is via a new setTypeParser(oid, parser) method.
Edit: I ended up using an afterConnect hook to configure it because
@psotres Your fix did the trick for me. But wow, what a frustrating red-herring with all the articles pointing to the setTypeParser(oid, parser) methods, but having DECIMAL (or NUMERIC in postgres' case) not work the same or be overwritten during instantiation.
@psotres This seemed to rear its head again under v5.x. The final call to
this.connectionManager.refreshTypeParser(types);
would fail -- it seems like the duped type { ...this.Sequelize.DataTypes.postgres.DECIMAL } wasn't pulling all the properties -- .DECIMAL is a Function with additional properties hung on it, such as key, types, parse etc...
Also under v5 there doesn't seem to be a this.Sequelize in scope when afterConnect runs setParsers() -- there is a this.sequelize (note the lower case), but it doesn't have the .DataTypes property. I instead used the Sequelize from my upper scoped initial import of the library.
I couldn't seem to clone it in a one-liner and keep those properties, so I wrote a cloning function to grab some things both manually and via Object.getOwnPropertyNames()
This is starting to feel pretty hacky, but tests are passing. 🤷♂
constSequelize=require('sequelize');// other sequelize setup codehooks: {afterConnect: [// sequelize defaults some numeric fields to strings to avoid overflows in parsing,// but our numeric/int values remain small enough that it's (probably) a safe override// so we adjust to to use appropriate parsers:// https://github.com/sequelize/sequelize/issues/9074#issuecomment-423271834functionsetParsers(){constcloneType=function(t,parseFn){constclone={};for(constnameofObject.getOwnPropertyNames(t)){clone[name]=t[name];}(clone.key=t.key),(clone.types=t.types),(clone.parse=parseFn);returnclone;};constdTypes={DECIMAL: cloneType(Sequelize.DataTypes.postgres.DECIMAL,parseFloat),INTEGER: cloneType(Sequelize.DataTypes.postgres.INTEGER,v=>parseInt(v,10)),BIGINT: cloneType(Sequelize.DataTypes.postgres.BIGINT,v=>parseInt(v,10)),};this.connectionManager.refreshTypeParser(dTypes);},],},
Happy to use a different approach if a better one exists!
May be important to mention that I am using TypeScript.
Hopefully this helps!
Reacted by Guillaume Duliscouët, David Ilizarov, Richard Simko, Nabil Zhafri, EyePulp, Lucky, fabrykowski, Dan Angell, sany-alume, Danil Valov and 3 more
@Leithal3 Thanks for the more succinct implementation! I'll admit I'm not entirely sure how this would look as standard ES6, as my familiarity with TypeScript is pretty limited =)
If you're feeling generous, I'd love a translated version, but regardless I appreciate your effort - thanks again.
@Leithal3 That works, thanks so much. That's pretty much identical to where I ended up. Clearly the best solution is for us to start using TS more often. =)
The workaround mentioned in this thread are going to break again in v7, but we have worked on a stable solution for this:
Starting with the next v7 alpha release, Data Types are a lot more powerful. Take a look at our updated documentation to see how you can customize an existing Data Type. This feature request should also cover this use case without needing to use internal APIs.
This bug report has not been verified by maintainers yet, and 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, consider commenting with more information to help us verify it.
pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yetstaletype: bugDEPRECATED: replace with the "bug" issue type
Activity
[-]Can't change default parser for Postgresql using setTypeParser after version 4.32.4[/-][+]Can't change default parser for Postgresql using setTypeParser after version 4.32.3[/+]Vovka19 commentedon Feb 20, 2018
Thank you!
tfazekas commentedon Mar 7, 2018
Hi olmo,
Same issue here.
Just upgraded to 4.32.4
Did you find any solution?
kingjerod commentedon May 23, 2018
After digging through the code, I was able to get it to work with this
Wonder how long this hack will last...
Edit: The old
pg.types.setTypeParsermethod still works for other DataTypes, like DATEONLY (1082). I'm a bit confused on what is going on in the code...stale commentedon Aug 21, 2018
psotres commentedon Sep 20, 2018
I've also been digging though the code and problem seems to have been introduced with #9025 and #9026.
The way the parser is stored for future usage in the connectionManager (via a function that will call
dataType.parsemethod when needed instead of storing its current reference) makes adding custom parser logic quite obscure if you don't clone thedataTypeobject, as a subsequent modification ofSequelize.DataTypes.postgres.*.parsevalue in another sequelize instance will impact all instances.My current workaround to solve the problem without having to use the hack provided by @kingjerod (which is a valid solution but it impacts all sequelize instances) is this:
Nevertheless, as a
getTypeParser(oid)method is already there, I would say the more elegant way to provide the functionality is via a newsetTypeParser(oid, parser)method.Edit: I ended up using an
afterConnecthook to configure it becausesequelize/lib/dialects/postgres/connection-manager.js
Line 181 in ee9ec57
Note: it uses once to avoid a refresh every time db is reconnected, but it can be easily done without the dependency.
EyePulp commentedon Mar 12, 2019
@psotres Your fix did the trick for me. But wow, what a frustrating red-herring with all the articles pointing to the
setTypeParser(oid, parser)methods, but having DECIMAL (or NUMERIC in postgres' case) not work the same or be overwritten during instantiation.Thanks again for figuring this out.
EyePulp commentedon Aug 26, 2019
@psotres This seemed to rear its head again under v5.x. The final call to
would fail -- it seems like the duped type
{ ...this.Sequelize.DataTypes.postgres.DECIMAL }wasn't pulling all the properties --.DECIMALis a Function with additional properties hung on it, such askey,types,parseetc...Also under v5 there doesn't seem to be a
this.Sequelizein scope whenafterConnectrunssetParsers()-- there is athis.sequelize(note the lower case), but it doesn't have the.DataTypesproperty. I instead used theSequelizefrom my upper scoped initial import of the library.I couldn't seem to clone it in a one-liner and keep those properties, so I wrote a cloning function to grab some things both manually and via
Object.getOwnPropertyNames()This is starting to feel pretty hacky, but tests are passing. 🤷♂
Happy to use a different approach if a better one exists!
papb commentedon Aug 26, 2019
Related: #8019
@EyePulp Can you take a look on the suggested workaround I gave there? Let me know, thanks
1 remaining item
guillaumeduliscouet commentedon Apr 7, 2020
For anyone facing the same problem as @k98kumar, refer to https://sequelize.org/master/manual/hooks.html. It seems like @EyePulp is referring to Permanent Hooks, they can be added this way for example:
Anyway, @EyePulp's workaround works for me, thanks
Leithal3 commentedon May 6, 2020
@EyePulp @psotres
I would like to start by thanking you, your help was invaluable.
Having a look it seems we can get the workaround to look a little cleaner by extending the class instead of trying to clone the entire object.
Defining our CustomDecimal as follows alllows us to override the parse function and keep all of the other information associated.
This enables us to make the hook itself a bit cleaner.
May be important to mention that I am using TypeScript.
Hopefully this helps!
EyePulp commentedon Jun 18, 2020
@Leithal3 Thanks for the more succinct implementation! I'll admit I'm not entirely sure how this would look as standard ES6, as my familiarity with TypeScript is pretty limited =)
If you're feeling generous, I'd love a translated version, but regardless I appreciate your effort - thanks again.
Leithal3 commentedon Jun 19, 2020
@EyePulp - I'm happy to give it a go - but have no way to verify right now.
Removing Typescript related code gives the following:
Good luck!
EyePulp commentedon Jun 19, 2020
@Leithal3 That works, thanks so much. That's pretty much identical to where I ended up. Clearly the best solution is for us to start using TS more often. =)
jobelenus commentedon Jan 8, 2021
I am on node12 and seq6.3.5. I have all my INTEGERs (even FKs) and BIGINT fields displaying as strings in JSON/get({plain}).
I tried @Leithal3 approach and confirmed that I was parsing ints with my custom parser type. However, the values were still strings in my JSON output.
In order to get them to be integers in a JSON representation, I had to add getters and return the parseInt value from there.
In short, this hook approach did not work for me.
ephys commentedon Dec 21, 2022
The workaround mentioned in this thread are going to break again in v7, but we have worked on a stable solution for this:
Starting with the next v7 alpha release, Data Types are a lot more powerful. Take a look at our updated documentation to see how you can customize an existing Data Type. This feature request should also cover this use case without needing to use internal APIs.
github-actions commentedon Jan 5, 2023
This bug report has not been verified by maintainers yet, and 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, consider commenting with more information to help us verify it.
jvalerodev commentedon May 31, 2023
I get the following error: "Base constructors must all have the same return type."
Could you tell me how can I fix it?
Chewbye commentedon Oct 16, 2023
This workaround worked with version 6.33, thank you
nikolaydyankov commentedon Mar 18, 2024
Thank you for the fix, it works. The only thing is that TS complains: