Skip to content

Commit d8bc552

Browse files
committed
improve select function calls example
1 parent 15264e5 commit d8bc552

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

site/docs/examples/SELECT/0060-function-calls.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
export const functionCalls = `const result = await db.selectFrom('person')
1+
export const functionCalls = `import { sql } from 'kysely'
2+
3+
const result = await db.selectFrom('person')
24
.innerJoin('pet', 'pet.owner_id', 'person.id')
3-
.select(({ fn }) => [
5+
.select(({ fn, val, ref }) => [
46
'person.id',
57
68
// The \`fn\` module contains the most common
79
// functions.
810
fn.count<number>('pet.id').as('pet_count'),
911
10-
// You can call any function using the
11-
// \`agg\` method
12-
fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names')
12+
// You can call any function by calling \`fn\` directly.
13+
// The arguments are treated as column references by
14+
// default. If you want to pass in values, use the
15+
// \`val\` function.
16+
fn<string>('concat', [val('Ms. '), 'first_name', 'last_name']).as('full_name_with_title'),
17+
18+
// You can call any aggregate function using the
19+
// \`fn.agg\` function.
20+
fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names'),
21+
22+
// And once again, you can use the \`sql\` template tag.
23+
// The template tag substitutions are treated as values
24+
// by default. If you want to reference columns, you can
25+
// use the \`ref\` function.
26+
sql<string>\`concat(\${ref('first_name')}, \${ref('last_name')})\`.as('full_name')
1327
])
1428
.groupBy('person.id')
1529
.having((eb) => eb.fn.count('pet.id'), '>', 10)

src/query-builder/function-module.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,32 @@ import { Selectable } from '../util/column-type.js'
3434
* This example uses the `fn` module to select some aggregates:
3535
*
3636
* ```ts
37+
* import { sql } from 'kysely'
38+
*
3739
* const result = await db.selectFrom('person')
3840
* .innerJoin('pet', 'pet.owner_id', 'person.id')
39-
* .select(({ fn }) => [
41+
* .select(({ fn, val, ref }) => [
4042
* 'person.id',
4143
*
4244
* // The `fn` module contains the most common
4345
* // functions.
4446
* fn.count<number>('pet.id').as('pet_count'),
4547
*
46-
* // You can call any function using the
47-
* // `agg` method
48-
* fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names')
48+
* // You can call any function by calling `fn` directly.
49+
* // The arguments are treated as column references by
50+
* // default. If you want to pass in values, use the
51+
* // `val` function.
52+
* fn<string>('concat', [val('Ms. '), 'first_name', 'last_name']).as('full_name_with_title'),
53+
*
54+
* // You can call any aggregate function using the
55+
* // `fn.agg` function.
56+
* fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names'),
57+
*
58+
* // And once again, you can use the `sql` template tag.
59+
* // The template tag substitutions are treated as values
60+
* // by default. If you want to reference columns, you can
61+
* // use the `ref` function.
62+
* sql<string>`concat(${ref('first_name')}, ${ref('last_name')})`.as('full_name')
4963
* ])
5064
* .groupBy('person.id')
5165
* .having((eb) => eb.fn.count('pet.id'), '>', 10)
@@ -58,11 +72,13 @@ import { Selectable } from '../util/column-type.js'
5872
* select
5973
* "person"."id",
6074
* count("pet"."id") as "pet_count",
61-
* array_agg("pet"."name") as "pet_names"
75+
* concat($1, "first_name", "last_name") as "full_name_with_title",
76+
* array_agg("pet"."name") as "pet_names",
77+
* concat("first_name", "last_name") as "full_name"
6278
* from "person"
6379
* inner join "pet" on "pet"."owner_id" = "person"."id"
6480
* group by "person"."id"
65-
* having count("pet"."id") > $1
81+
* having count("pet"."id") > $2
6682
* ```
6783
*/
6884
export interface FunctionModule<DB, TB extends keyof DB> {

0 commit comments

Comments
 (0)