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

Allow raw expressions in query builder aggregate methods #2257

Merged
merged 1 commit into from Oct 14, 2017
Merged

Allow raw expressions in query builder aggregate methods #2257

merged 1 commit into from Oct 14, 2017

Conversation

@noodle
Copy link
Contributor

@noodle noodle commented Oct 8, 2017

The aggregate methods include count, min, max, sum, avg, countDistinct, sumDistinct, and avgDistinct, all of which can now receive raw expressions, e.g.

knex('users').count(knex.raw("data->'active'"));

There seems to be some demand for this (#238, #1606, #935), and I think it's cleaner than the alternative of

knex('users').select(knex.raw("count(data->'active')"));
@noodle
Copy link
Contributor Author

@noodle noodle commented Oct 8, 2017

@elhigu
Copy link
Member

@elhigu elhigu commented Oct 9, 2017

I actually find the latter way cleaner. Anyways for the sake of consistency, that one should be able to put knex.raw pretty much anywhere this seems like a good fix.

@elhigu
Copy link
Member

@elhigu elhigu commented Oct 9, 2017

I'll check this PR a bit later.

@noodle
Copy link
Contributor Author

@noodle noodle commented Oct 10, 2017

Ok, thanks. And just a heads up that I recently amended this PR to fix the behavior for countDistinct, sumDistinct, and avgDistinct.

The aggregate methods include `count`, `min`, `max`, `sum`, `avg`,
`countDistinct`, `sumDistinct`, and `avgDistinct`, all of which
can now receive raw expressions, e.g.

```js
knex('users').count(knex.raw("data->'active'"));
```

There seems to be some demand for this, and I think it's cleaner than the alternative of

```js
knex('users').select(knex.raw("count(data->'active')"));
```
@elhigu
Copy link
Member

@elhigu elhigu commented Oct 14, 2017

This was perfect 👍 Thank you!

@elhigu elhigu merged commit b092a27 into knex:master Oct 14, 2017
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
elhigu added a commit to ivanfilhoz/knex that referenced this pull request Oct 16, 2017
The aggregate methods include `count`, `min`, `max`, `sum`, `avg`,
`countDistinct`, `sumDistinct`, and `avgDistinct`, all of which
can now receive raw expressions, e.g.

```js
knex('users').count(knex.raw("data->'active'"));
```

There seems to be some demand for this, and I think it's cleaner than the alternative of

```js
knex('users').select(knex.raw("count(data->'active')"));
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants