Skip to content

Commit

Permalink
fixes #132
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Aug 8, 2022
1 parent 8667b2e commit 8f054ae
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kysely",
"version": "0.21.0",
"version": "0.21.1",
"description": "Type safe SQL query builder",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/operation-node/operation-node-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import { SchemableIdentifierNode } from './schemable-identifier-node.js'
*
* return {
* ...node,
* identifier: snakeCase(node.identifier),
* name: snakeCase(node.name),
* }
* }
* }
Expand Down
12 changes: 10 additions & 2 deletions src/parser/group-by-parser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { GroupByItemNode } from '../operation-node/group-by-item-node.js'
import {
parseReferenceExpressionOrList,
ReferenceExpressionOrList,
ReferenceExpression,
} from './reference-parser.js'

export type GroupByExpression<DB, TB extends keyof DB, O> =
| ReferenceExpression<DB, TB>
| (keyof O & string)

export type GroupByExpressionOrList<DB, TB extends keyof DB, O> =
| ReadonlyArray<GroupByExpression<DB, TB, O>>
| GroupByExpression<DB, TB, O>

export function parseGroupBy(
orderBy: ReferenceExpressionOrList<any, any>
orderBy: GroupByExpressionOrList<any, any, any>
): GroupByItemNode[] {
return parseReferenceExpressionOrList(orderBy).map(GroupByItemNode.create)
}
6 changes: 2 additions & 4 deletions src/plugin/with-schema/with-schema-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AliasNode } from '../../operation-node/alias-node.js'
import { IdentifierNode } from '../../operation-node/identifier-node.js'
import { OperationNodeTransformer } from '../../operation-node/operation-node-transformer.js'
import { TableExpressionNode } from '../../operation-node/operation-node-utils.js'
import { OperationNode } from '../../operation-node/operation-node.js'
Expand Down Expand Up @@ -72,10 +73,7 @@ export class WithSchemaTransformer extends OperationNodeTransformer {

return {
...transformed,
schema: freeze({
kind: 'IdentifierNode',
name: this.#schema,
}),
schema: IdentifierNode.create(this.#schema),
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/query-builder/select-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ import {
WhereGrouper,
HavingGrouper,
} from '../parser/filter-parser.js'
import {
ReferenceExpression,
ReferenceExpressionOrList,
} from '../parser/reference-parser.js'
import { ReferenceExpression } from '../parser/reference-parser.js'
import { SelectQueryNode } from '../operation-node/select-query-node.js'
import { QueryNode } from '../operation-node/query-node.js'
import {
Expand All @@ -52,7 +49,11 @@ import { Compilable } from '../util/compilable.js'
import { QueryExecutor } from '../query-executor/query-executor.js'
import { QueryId } from '../util/query-id.js'
import { freeze } from '../util/object-utils.js'
import { parseGroupBy } from '../parser/group-by-parser.js'
import {
GroupByExpression,
GroupByExpressionOrList,
parseGroupBy,
} from '../parser/group-by-parser.js'
import { parseUnion, UnionExpression } from '../parser/union-parser.js'
import { KyselyPlugin } from '../plugin/kysely-plugin.js'
import { WhereInterface } from './where-interface.js'
Expand Down Expand Up @@ -1244,12 +1245,12 @@ export class SelectQueryBuilder<DB, TB extends keyof DB, O>
* ```
*/
groupBy(
groupBy: ReadonlyArray<ReferenceExpression<DB, TB>>
groupBy: ReadonlyArray<GroupByExpression<DB, TB, O>>
): SelectQueryBuilder<DB, TB, O>

groupBy(groupBy: ReferenceExpression<DB, TB>): SelectQueryBuilder<DB, TB, O>
groupBy(groupBy: GroupByExpression<DB, TB, O>): SelectQueryBuilder<DB, TB, O>

groupBy(groupBy: ReferenceExpressionOrList<DB, TB>): any {
groupBy(groupBy: GroupByExpressionOrList<DB, TB, O>): any {
return new SelectQueryBuilder({
...this.#props,
queryNode: SelectQueryNode.cloneWithGroupByItems(
Expand Down
37 changes: 37 additions & 0 deletions test/node/src/group-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,43 @@ for (const dialect of BUILT_IN_DIALECTS) {
])
})

it('group by selection', async () => {
const query = ctx.db
.selectFrom('person')
.select(['gender as g', sql`max(first_name)`.as('max_first_name')])
.groupBy('g')
.orderBy('g')

testSql(query, dialect, {
postgres: {
sql: 'select "gender" as "g", max(first_name) as "max_first_name" from "person" group by "g" order by "g"',
parameters: [],
},
mysql: {
sql: 'select `gender` as `g`, max(first_name) as `max_first_name` from `person` group by `g` order by `g`',
parameters: [],
},
sqlite: {
sql: 'select "gender" as "g", max(first_name) as "max_first_name" from "person" group by "g" order by "g"',
parameters: [],
},
})

const persons = await query.execute()

expect(persons).to.have.length(2)
expect(persons).to.containSubset([
{
max_first_name: 'Jennifer',
g: 'female',
},
{
max_first_name: 'Sylvester',
g: 'male',
},
])
})

it('group by two columns', async () => {
const query = ctx.db
.selectFrom('person')
Expand Down

0 comments on commit 8f054ae

Please sign in to comment.