Skip to content

Support specifying the type of a bind param / replacement #14410

@ephys

Description

@ephys

Issue Creation Checklist

Feature Description

Is your feature request related to a problem? Please describe.

Right now, Sequelize automatically determines the type of bind params & replacements based on their JS type. But a JS type can be mapped to more than one SQL type.

It guesses these types in https://github.com/sequelize/sequelize/blob/main/src/sql-string.js

This leads to things like:

// "permissions" and ":permissions" are both SQL arrays.
sequelize.query('SELECT * FROM users WHERE permissions @> :permissions', {
  replacements: {
    permissions: ['read', 'write']
  },
});

generating

SELECT * FROM users WHERE permissions @> 'read', 'write';

instead of

SELECT * FROM users WHERE permissions @> ARRAY['read', 'write'];

Describe the solution you'd like

It should be possible to specify the DataType of a given replacement or bind parameter.

// "permissions" and ":permissions" are both SQL arrays.
sequelize.query('SELECT * FROM users WHERE permissions @> :permissions', {
  parameterTypes: {
    permissions: DataTypes.ARRAY(DataTypes.STRING),
  },
  replacements: {
    permissions: ['read', 'write']
  },
});

Why should this be in Sequelize

To continue making raw queries first class citizen.

Describe alternatives/workarounds you've considered

Use replacementTypes and bindTypes instead of parameterTypes.

Other ideas to solve this feature welcome.

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.

Would you be willing to implement this feature by submitting a Pull Request?

  • Eventually, at some point.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions