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

Conversation

Projects
None yet
2 participants
@noodle
Contributor

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

This comment has been minimized.

Contributor

noodle commented Oct 8, 2017

@elhigu

This comment has been minimized.

Collaborator

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

This comment has been minimized.

Collaborator

elhigu commented Oct 9, 2017

I'll check this PR a bit later.

@noodle

This comment has been minimized.

Contributor

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.

Allow raw expressions in query builder aggregate methods
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

This comment has been minimized.

Collaborator

elhigu commented Oct 14, 2017

This was perfect 👍 Thank you!

@elhigu elhigu merged commit b092a27 into tgriesser:master Oct 14, 2017

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

elhigu added a commit to ivanslf/knex that referenced this pull request Oct 16, 2017

Allow raw expressions in query builder aggregate methods (tgriesser#2257
)

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