Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Sequelize.literal on VIRTUAL data type #1706

Open
1 of 2 tasks
eduwr opened this issue Jan 30, 2024 · 0 comments
Open
1 of 2 tasks

Support Sequelize.literal on VIRTUAL data type #1706

eduwr opened this issue Jan 30, 2024 · 0 comments

Comments

@eduwr
Copy link

eduwr commented Jan 30, 2024

Issue

It would be nice to support Sequelize.literal on virtual subqueries, because it's a really common use-case.

Versions

  • sequelize: 6.5.1
  • sequelize-typescript: 2.1.0
  • typescript: 4.7.2

Issue type

  • bug report
  • feature request

Actual behavior

I'm trying to perform a subquery on a virtual field similar to the one in the following comment:

sequelize/sequelize#4942 (comment)

it happens that sequelize-typescript doesn't expect a Sequelize literal on virtual constructor and when I implemented the code I found this error:

Type '[Literal, string]' is not assignable to type 'string'

Expected behavior

No Typescript error

Steps to reproduce

Implement a model with 1-to-N relation and include a virtual field with a subquery using a Sequelize.literal like this:

@Column({
    type: DataType.VIRTUAL(DataType.INTEGER, [
      [
        Sequelize.literal(
          '(SELECT COUNT("Subthings"."id") FROM "Subthings" WHERE "Subthings"."parentId" = "Thing"."id")'
        ),
        'thingCount'
      ]
    ])
  })
  thingCount: number

Related code

@Column({
    type: DataType.VIRTUAL(DataType.INTEGER, [
      //@ts-expect-error - sequelize-typescript doesn't support Sequelize.literal type
      [
        Sequelize.literal(
          '(SELECT COUNT("Subthings"."id") FROM "Subthings" WHERE "Subthings"."parentId" = "Thing"."id")'
        ),
        'thingCount'
      ]
    ])
  })
  thingCount: number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant