Skip to content

Commit

Permalink
improve select function calls example
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Sep 28, 2023
1 parent 15264e5 commit d8bc552
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
24 changes: 19 additions & 5 deletions site/docs/examples/SELECT/0060-function-calls.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
export const functionCalls = `const result = await db.selectFrom('person')
export const functionCalls = `import { sql } from 'kysely'
const result = await db.selectFrom('person')
.innerJoin('pet', 'pet.owner_id', 'person.id')
.select(({ fn }) => [
.select(({ fn, val, ref }) => [
'person.id',
// The \`fn\` module contains the most common
// functions.
fn.count<number>('pet.id').as('pet_count'),
// You can call any function using the
// \`agg\` method
fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names')
// 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 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')
])
.groupBy('person.id')
.having((eb) => eb.fn.count('pet.id'), '>', 10)
Expand Down
28 changes: 22 additions & 6 deletions src/query-builder/function-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,32 @@ import { Selectable } from '../util/column-type.js'
* This example uses the `fn` module to select some aggregates:
*
* ```ts
* import { sql } from 'kysely'
*
* const result = await db.selectFrom('person')
* .innerJoin('pet', 'pet.owner_id', 'person.id')
* .select(({ fn }) => [
* .select(({ fn, val, ref }) => [
* 'person.id',
*
* // The `fn` module contains the most common
* // functions.
* fn.count<number>('pet.id').as('pet_count'),
*
* // You can call any function using the
* // `agg` method
* fn.agg<string[]>('array_agg', ['pet.name']).as('pet_names')
* // 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 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')
* ])
* .groupBy('person.id')
* .having((eb) => eb.fn.count('pet.id'), '>', 10)
Expand All @@ -58,11 +72,13 @@ import { Selectable } from '../util/column-type.js'
* select
* "person"."id",
* count("pet"."id") as "pet_count",
* array_agg("pet"."name") as "pet_names"
* concat($1, "first_name", "last_name") as "full_name_with_title",
* array_agg("pet"."name") as "pet_names",
* 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") > $1
* having count("pet"."id") > $2
* ```
*/
export interface FunctionModule<DB, TB extends keyof DB> {
Expand Down

1 comment on commit d8bc552

@vercel
Copy link

@vercel vercel bot commented on d8bc552 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-kysely-team.vercel.app
www.kysely.dev
kysely-git-master-kysely-team.vercel.app
kysely.dev

Please sign in to comment.