Skip to content

support passing bind parameters directly in Sequelize.literal() #14297

Closed as not planned

Description

@ephys

Literals are things we see the community try to avoid at all costs. Unfortunately, The Sequelize team is not able to implement each and every feature request due to time constraints.

This goal of this RFC is to propose new literal-based APIs to improve their Developer Experience.

More RFCs to improve literals:

Binding values in literal

Right now, if you're using bind parameters in literals, you need to pass them as a separate option:

User.findAll({
  where: literal(`id = $id`),
  bind: { id: 123 },
})

This is fine most of the time, but it makes reusing part of a query complicated. You can't just do this:

function isId(id) {
  // where do I pass the value of the ID?
  return literal(`id = $id`);
}

User.findAll({
  where: isId(123),
})

Solution

We can solve this easily by making literal accept a second parameter bind:

function isId(id) {
  return literal(`id = $id`, { id });
}

User.findAll({
  where: isId(123),
});

In its current form '?id' is a global identifier for the whole query. We should also rewrite the literal to make sure the identifier is unique. So this:

User.findAll({
  where: or(isId(123), isId(456)),
});

results in this:

WHERE id = $id_1 OR id = $id_2

Activity

  1. added
    RFC
    on Mar 30, 2022
  2. self-assigned this
    on Mar 30, 2022
  3. ephys commented on Dec 3, 2022

    @ephys
    MemberAuthor

    I'm no longer pursuing, as I think #14299 would cover the same use cases better

  4. moved this to Done in Roadmapon Dec 3, 2022
  5. added
    type: feature
    and removed
    RFC
    on Dec 3, 2022
  6. changed the title [-][RFC] support passing bind parameters directly in `Sequelize.literal()`[/-] [+]support passing bind parameters directly in `Sequelize.literal()`[/+] on Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: feature

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions