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

Support json_agg(column_ref) #1316

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/query-builder/function-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SelectQueryBuilderExpression } from '../query-builder/select-query-buil
import { isString } from '../util/object-utils.js'
import { parseTable } from '../parser/table-parser.js'
import { Selectable } from '../util/column-type.js'
import { RawBuilder } from '../raw-builder/raw-builder.js'

/**
* Helpers for type safe SQL function calls.
Expand Down Expand Up @@ -657,6 +658,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
*
* This function is only available on PostgreSQL.
*
* You can use it either on a table:
*
* ```ts
* await db.selectFrom('person')
* .innerJoin('pet', 'pet.owner_id', 'person.id')
Expand All @@ -673,6 +676,44 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* inner join "pet" on "pet"."owner_id" = "person"."id"
* group by "person"."first_name"
* ```
*
* ... or on a column:
*
* ```ts
* await db.selectFrom('person')
* .innerJoin('pet', 'pet.owner_id', 'person.id')
* .select((eb) => ['first_name', eb.fn.jsonAgg('pet.names').as('pet_names')])
* .groupBy('person.first_name')
* .execute()
*
* await db.selectFrom('person')
* .select((eb) => [
* 'first_name',
* eb
* .selectFrom('pet')
* .select((eb) => [eb.fn.jsonAgg("pet.owner_id")])
* .whereRef('pet.owner_id', '=', 'person.id')
* .as('petnames'),
* ])
* .execute()
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* select "first_name", json_agg("pet"."names") AS "pet_names"
* from "person"
* inner join "pet" ON "pet"."owner_id" = "person"."id"
* group by "person"."first_name"
*
* select "first_name",
* (
* select json_agg("pet"."owner_id")
* from "pet"
* where "pet"."owner_id" = "person"."id"
* ) as "petnames"
* from "person"
* ```
*/
jsonAgg<T extends (TB & string) | Expression<unknown>>(
table: T,
Expand All @@ -685,6 +726,9 @@ export interface FunctionModule<DB, TB extends keyof DB> {
? O[]
: never
>
jsonAgg<RE extends StringReference<DB, TB>>(
column: RE,
): RawBuilder<ExtractTypeFromStringReference<DB, TB, RE>[] | null>

/**
* Creates a to_json function call.
Expand Down
33 changes: 33 additions & 0 deletions test/typings/test-d/postgres-json.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,39 @@ async function testPostgresJsonAgg(db: Kysely<Database>) {
status: string | null
}[]
}>(r4)

const r5 = await db
.selectFrom('person')
.innerJoin('pet', 'pet.owner_id', 'person.id')
.select((eb) => ['first_name', eb.fn.jsonAgg('owner_id').as('pet_names')])
.groupBy('person.first_name')
.execute()

expectType<
{
first_name: string
pet_names: number[] | null
}[]
>(r5)

const r6 = await db
.selectFrom('person')
.select((eb) => [
'first_name',
eb
.selectFrom('pet')
.select((eb) => [eb.fn.jsonAgg('pet.owner_id').as('pet_names')])
.whereRef('pet.owner_id', '=', 'person.id')
.as('pet_names'),
])
.execute()

expectType<
{
first_name: string
pet_names: number[] | null
}[]
>(r6)
}

async function testPostgresToJson(db: Kysely<Database>) {
Expand Down
Loading