Description
There are few issues with typeCast
currently, and most of them better solved by moving it to a different level where it sits in the driver, I think it applies both to mysql2
/ mysqljs/mysql
Problems I'd like to solve with the change:
- most important - typeCast sits too low and is too powerful. It's super easy to make connection go into completely undefined / unpredictable state by reading incorrect number of bytes
- performance: mysql2 generates row parser JS code in the run time and just the fact that you have custom typeCast function makes generated parser slower and more likely to bail out from optimisations
- difficult to implement for
execute()
.typeCast
in its current form expects user to know binary serialization format of results and data forexecute()
if often encoded differently from data forquery()
- one can't join 2 rows into one etc as
typeCast
is called before every column of a row is read
Most importantly: almost every use case I see is "map rows from default types into something that works best for our use case" and not "I wan't to change / extend parsing logic".
- typeCast not supported with .execute #649
- TypeCast seems doesnt work #948
- typeCast is up to 10x slower #889
- [Breaking change]: field.length is a string inside typeCast. #1426 (comment)
- Returning nested rows #709
Proposed solution
Introduce resultsMapper
config function (happy to hear better name suggestions). If set, it's applied to every row and result of the function is then used as returned row.
signature:
function resultsMapper(row: Row, fields: Array<Column>) => CustomRow
Deprecation strategy
- soft deprecate
typeCast
- note in the documentation - next step - decide if we want to remove typeCast completely - any use cases that can't be efficiently solved without having typeCast. If nothing, add deprecation message in the code
- finally, leave a stub throwing an error and pointing to documentation/migration guide
CC ( not sure if I'm tagging correct people, if you know who is better suited to comment please let me know )
sequelize - @sushantdhiman , @papb
knex - @tgriesser , @kibertoad
mysqljs/mysql @dougwilson