Skip to content

RFC: deprecate typeCast and introduce resultsMapper ( exact name TBD ) #1446

Open
@sidorares

Description

@sidorares

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:

  1. 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
  2. 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
  3. difficult to implement for execute(). typeCast in its current form expects user to know binary serialization format of results and data for execute() if often encoded differently from data for query()
  4. 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".

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions