support condition objects in utility functions #6666#6685
support condition objects in utility functions #6666#6685janmeier merged 1 commit intosequelize:masterfrom
Conversation
test/unit/utils.test.js
Outdated
| var run = generator.handleSequelizeMethod.bind(generator); | ||
| var expectsql = Support.expectsql | ||
|
|
||
| if (Support.getTestDialect() !== 'mssql') { |
There was a problem hiding this comment.
No need to skip mssql - It doesn't need to be 100 percent correct syntax (in my mind), as long as it tests the functionality (that you can pass nested conditions)
test/unit/utils.test.js
Outdated
| bar: 'bar' | ||
| } | ||
| }, 'int'))), { | ||
| default: 'SUM(CAST((`foo` = \'foo\' OR `bar` = \'bar\') AS INT))', |
There was a problem hiding this comment.
Default should use [ and ] for escaping
| }); | ||
| }); | ||
|
|
||
| describe('Sequelize.fn', function() { |
There was a problem hiding this comment.
While an integration test might not be strictly needed, I think this is good to keep as an example of how the functionality can be used
|
You can test MSSQL through AppVeyor ;) |
b8bdf1a to
a5a4988
Compare
Current coverage is 81.54% (diff: 100%)
|
a5a4988 to
1c4200f
Compare
|
@janmeier @felixfbecker Thanks for feedback! I addressed some of the comments. I still do not have access to mssql (not even sure if it supports the syntax I am using in the tests). Let me try if I can use appveyor CI runner here to get the tests pass easily but maybe not. |
f18691b to
4b89420
Compare
|
I played with mssql and it does not seem to support this syntax. The closest alternative is |
4b89420 to
56c70e8
Compare
|
Don't forget to mention in the documentation then that this is not supported by MSSQL |
433ce5a to
f33c5ad
Compare
sequelize#6666 Makes it possible to pass condition objects (e.g. {foo: 'bar'}) to Sequelize.fn(), Sequelize.cast(). This makes it easy to use conditional counts in Sequelize queries, e.g. Sequelize.fn('sum', {name: 'Fred'}) which results in SUM(`name` = 'Fred') which can be used in attribute definition to count entities for which the condition matches.
f33c5ad to
3329fad
Compare
|
I modified changelog and added mention to docs. From my POV this change is now finished. Let me know if you find anything missing. |
|
Thanks 👍 |
|
@janmeier Thank you! Btw is there a chance to get this into v3 as well? Should I open a new MR against v3? Do you plan further 3.x releases? |
|
Definitely - Let me see if I can cherry-pick it really quick |
#6666 Makes it possible to pass condition objects (e.g. {foo: 'bar'}) to Sequelize.fn(), Sequelize.cast(). This makes it easy to use conditional counts in Sequelize queries, e.g. Sequelize.fn('sum', {name: 'Fred'}) which results in SUM(`name` = 'Fred') which can be used in attribute definition to count entities for which the condition matches.
|
75bd525, just going to wait for travis to do his thing before i push a new version |
|
|
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Futurein the changelog?NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open.
Description of change
#6666
Makes it possible to pass condition objects (e.g.
{foo: 'bar'}) toSequelize.fn(),Sequelize.cast().This makes it easy to use conditional counts in Sequelize queries, e.g.
Sequelize.fn('sum', {name: 'Fred'})which results inSUM(``name``= 'Fred')which can be used in attribute definition to count entities for which the condition matches.I did not get a chance to test with mssql as I do not have it available. If anyone can do that for me I would appreciate that.