Skip to content

Commit

Permalink
improve select function calls example even more
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Sep 28, 2023
1 parent d8bc552 commit 3a9d03f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
30 changes: 20 additions & 10 deletions site/docs/examples/SELECT/0060-function-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@ const result = await db.selectFrom('person')
// functions.
fn.count<number>('pet.id').as('pet_count'),
// You can call any function by calling \`fn\` directly.
// The arguments are treated as column references by
// default. If you want to pass in values, use the
// \`val\` function.
fn<string>('concat', [val('Ms. '), 'first_name', 'last_name']).as('full_name_with_title'),
// You can call any function by calling \`fn\`
// directly. The arguments are treated as column
// references by default. If you want to pass in
// values, use the \`val\` function.
fn<string>('concat', [
val('Ms. '),
'first_name',
val(' '),
'last_name'
]).as('full_name_with_title'),
// You can call any aggregate function using the
// \`fn.agg\` function.
fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names'),
// And once again, you can use the \`sql\` template tag.
// The template tag substitutions are treated as values
// by default. If you want to reference columns, you can
// use the \`ref\` function.
sql<string>\`concat(\${ref('first_name')}, \${ref('last_name')})\`.as('full_name')
// And once again, you can use the \`sql\`
// template tag. The template tag substitutions
// are treated as values by default. If you want
// to reference columns, you can use the \`ref\`
// function.
sql<string>\`concat(
\${ref('first_name')},
' ',
\${ref('last_name')}
)\`.as('full_name')
])
.groupBy('person.id')
.having((eb) => eb.fn.count('pet.id'), '>', 10)
Expand Down
36 changes: 23 additions & 13 deletions src/query-builder/function-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,31 @@ import { Selectable } from '../util/column-type.js'
* // functions.
* fn.count<number>('pet.id').as('pet_count'),
*
* // You can call any function by calling `fn` directly.
* // The arguments are treated as column references by
* // default. If you want to pass in values, use the
* // `val` function.
* fn<string>('concat', [val('Ms. '), 'first_name', 'last_name']).as('full_name_with_title'),
* // You can call any function by calling `fn`
* // directly. The arguments are treated as column
* // references by default. If you want to pass in
* // values, use the `val` function.
* fn<string>('concat', [
* val('Ms. '),
* 'first_name',
* val(' '),
* 'last_name'
* ]).as('full_name_with_title'),
*
* // You can call any aggregate function using the
* // `fn.agg` function.
* fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names'),
*
* // And once again, you can use the `sql` template tag.
* // The template tag substitutions are treated as values
* // by default. If you want to reference columns, you can
* // use the `ref` function.
* sql<string>`concat(${ref('first_name')}, ${ref('last_name')})`.as('full_name')
* // And once again, you can use the `sql`
* // template tag. The template tag substitutions
* // are treated as values by default. If you want
* // to reference columns, you can use the `ref`
* // function.
* sql<string>`concat(
* ${ref('first_name')},
* ' ',
* ${ref('last_name')}
* )`.as('full_name')
* ])
* .groupBy('person.id')
* .having((eb) => eb.fn.count('pet.id'), '>', 10)
Expand All @@ -72,13 +82,13 @@ import { Selectable } from '../util/column-type.js'
* select
* "person"."id",
* count("pet"."id") as "pet_count",
* concat($1, "first_name", "last_name") as "full_name_with_title",
* concat($1, "first_name", $2, "last_name") as "full_name_with_title",
* array_agg("pet"."name") as "pet_names",
* concat("first_name", "last_name") as "full_name"
* concat("first_name", ' ', "last_name") as "full_name"
* from "person"
* inner join "pet" on "pet"."owner_id" = "person"."id"
* group by "person"."id"
* having count("pet"."id") > $2
* having count("pet"."id") > $3
* ```
*/
export interface FunctionModule<DB, TB extends keyof DB> {
Expand Down

1 comment on commit 3a9d03f

@vercel
Copy link

@vercel vercel bot commented on 3a9d03f Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kysely – ./

kysely-git-master-kysely-team.vercel.app
kysely.dev
kysely-kysely-team.vercel.app
www.kysely.dev

Please sign in to comment.