Skip to content

How to use/add custom operators? #13011

Closed

Description

@TrurlMcByte

Issue Description

Can't find way to create query with Postgresql jsonpath operators "@?" and "@@"

Additional context

for example something like:

    where: {
      body: {
        "@?": "$.** ? (@.tagName == \"a\" && (@.attr.href like_regex \"^/[^/]\") )"
      }
    }

where body is jsonb type field must produce

WHERE "body" @? '$.** ? (@.tagName == "a" && (@.attr.href like_regex "^/[^/]") )' 

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): PostgreSQL
  • I don't know.

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. Spekpannenkoek commented on Feb 8, 2021

    @Spekpannenkoek

    Don't know off the top of my head how to define a custom Sequelize.Op, but you can drop down to raw SQL with literal:

        where: {
          body: Sequelize.literal(
            `@? '$.** ? (@.tagName == "a" && (@.attr.href like_regex "^/[^/]") )'`
          )
        }
    

    Use placeholders if the queried value is dynamic:

        where: {
          body: Sequelize.literal(
            `@? '$.** ? (@.tagName == :MyTagName && (@.attr.href like_regex "^/[^/]") )'`
          )
        }
        replacements: {
          MyTagName: 'a'
        }
    

    This might contain a little mistake or two but it should be an okay starting-off point. If you search Google, you'll find some useful pointers on its usage.

  2. TrurlMcByte commented on Feb 9, 2021

    @TrurlMcByte
    Author

    this code loose field name in generated query. Like WHERE @? '$.** ? (@.tagName == "a" && (@.attr.href like_regex "^/[^/]") )'
    working only crazy construction

    body: Sequelize.literal(`body @? '$.** ? (@.tagName == "a" && (@.attr.href like_regex "^/[^/]") )'`)
    

    but this way not compatible with jsonb subkeys and query includes ( just "body" in this case must be "some_table_alias"."body" )

  3. github-actions commented on Oct 27, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  4. rusekr commented on Aug 11, 2022

    @rusekr

    Hello! Want to reopen this because lot of json postgresql operators absent in Sequelize. In particular it will be convinient to have some function to register own operators like '?' for searching in jsonb postgreslq arrays and do not use raw in business logic.

  5. added
    type: feature
    and removed on Aug 11, 2022
  6. ephys commented on Sep 17, 2022

    @ephys
    Member

    I think the best way to support custom operators is going to be by implementing #14299 & #14297. Once they are ready, a custom operator could be implemented like this:

    import { sql } from '@sequelize/core';
    
    function greaterThan(left, right) {
      // thanks to the sql tag, value will be escaped before inlining.
      return sql`${left} >= ${right}`;
    }
    
    User.findAll({
      where: greaterThan(
        sql.attribute('friendCount'),
        10,
      ),
    })

    The alternative would be to support hooking into QueryGenerator, which would be incredibly complicated to do & to use. I don't think we want to do that.

  7. GuillaumeCisco commented on Jun 29, 2023

    @GuillaumeCisco

    Is it possible to achieve this using v6?
    sql in only available in v7, and migrating from v6 to v7 is like recoding our entire project.
    This is a total no go.

  8. ephys commented on Jun 29, 2023

    @ephys
    Member

    Somewhat, but definitely not with the same ergonomics

    The best we have in Sequelize 6 is a mix of literal and sequelize.escape, but there is no good alternative to sql.attribute, nor can you easily reference a column in literal

    Best I can come up with in Sequelize 6:

    import { literal } from 'sequelize';
    
    function greaterThan(left, right) {
      return literal(`${left} >= ${right}`;
    }
    
    User.findAll({
      where: greaterThan(
        '"friend_count"',
        sequelize.escape(10),
      ),
    })
  9. GuillaumeCisco commented on Jun 29, 2023

    @GuillaumeCisco

    But how do you work with others classic operators?

  10. GuillaumeCisco commented on Jun 29, 2023

    @GuillaumeCisco

    Ok looks like it can be achieved by wrapping everything in an Op.and operator...

  11. GuillaumeCisco commented on Jun 29, 2023

    @GuillaumeCisco

    And by the way, escape is not present in v6...
    #13225

  12. ephys commented on Jun 29, 2023

    @ephys
    Member

    It's sequelize.escape, not queryInterface.escape

  13. GuillaumeCisco commented on Jun 29, 2023

    @GuillaumeCisco

    sequelize.escape does not exist either...

  14. reopened this on Jun 29, 2023
  15. ephys commented on Jun 29, 2023

    @ephys
    Member

    Woops, misclick from using github on mobile

    sequelize.escape:

    escape(value) {

    public escape(value: string | number | Date): string;

  16. GuillaumeCisco commented on Jun 30, 2023

    @GuillaumeCisco
    TS2305: Module '"sequelize"' has no exported member 'escape'.
    
    (0 , sequelize_1.escape) is not a function
    
  17. GuillaumeCisco commented on Jun 30, 2023

    @GuillaumeCisco

    escape works on a sequelize instance, not on the sequelize package

  18. ephys commented on Jun 30, 2023

    @ephys
    Member

    escape is an instance method, not a static one

  19. mysuf commented on Oct 28, 2024

    @mysuf

    It looks like atleast in v6, it can be done also this way:

    where(col('body'), "@?", "$.** ? (@.tagName == \"a\" && (@.attr.href like_regex \"^/[^/]\") )")

    While digging, I found out other issues. E.g. sequelize is not able to build simple MySQL query like: WHERE NOT (a <=> 2)
    NOT implementation probably can't handle right side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions