sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.79k stars 280 forks source link

$$ Syntax not accepted #615

Closed piyushkantm closed 5 years ago

piyushkantm commented 5 years ago

Versions

I'm submitting a ...

[x] bug report [ ] feature request

Actual behavior: Whenever i am trying to use $$ syntax to add a where clause on joined table, it throws type error on the where clause.

Expected behavior: It should not throw error and actually add a where condition on the joined tables.

Steps to reproduce:

  1. Make a one-many Relationship.
  2. include with required: false
  3. add a where clause in main query using $$ syntax
  4. type error

http://docs.sequelizejs.com/manual/models-usage.html#top-level-where-with-eagerly-loaded-models

Related code:

A.findAll({
    where: {
        $or: [
            {"$B.userId$" : 100},
            {"$C.userId$" : 100}
        ]
    },
    include: [{
        model: B,
        required: false

    }, {
        model: C,
        required: false
    }]
}); 
RobinBuschmann commented 5 years ago

Hey @piyushkantm, thanks for bringing this up. The reason why it’s not accepted there is that the where-options are restricted to keyof YourModel. Since the $A.column$ cannot be typed and is more an exception for the rule, we decided to do it like this and offer a workaround instead: {['$B.userId$' as any]: 100} should do the job. What do you think about it?

Hope this helps!

piyushkantm commented 5 years ago

Hi @RobinBuschmann . Thanks for replying.

You're right. I was using any to avoid type errors... But don't have anything better in mind to overcome this use-case..

It'd be nice though to have a demo code for this on readme. It could help someone save some time :) Let me know if i can help in any way...

RobinBuschmann commented 5 years ago

@piyushkantm Thanks for offering help: Yeah, any pull request adding this to the readme is appreciated :)

piyushkantm commented 5 years ago

Yup, i would try to lend my hand over this weekend..

RobinBuschmann commented 5 years ago

Closing this due to inactivity