Skip to content

Commit

Permalink
docs: use methods from expression builder callback instead of global …
Browse files Browse the repository at this point in the history
…instance (kysely-org#446)

Co-authored-by: Igal Klebanov <[email protected]>
  • Loading branch information
2 people authored and Gaspero committed Oct 2, 2023
1 parent 88e5355 commit c2e9a3f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/expression/expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
* eb.fn.count('pet.id').as('pet_count')
* ])
* .groupBy('person.id')
* .having(count('pet.id'), '>', 10)
* .having((eb) => eb.fn.count('pet.id'), '>', 10)
* .execute()
* ```
*
Expand Down
2 changes: 1 addition & 1 deletion src/expression/expression-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ExpressionWrapper<T> implements Expression<T> {
* ```ts
* const result = await db
* .selectFrom('person')
* .select(eb =>
* .select((eb) =>
* eb.cmpr('first_name', '=', 'Jennifer').as('is_jennifer')
* )
* .executeTakeFirstOrThrow()
Expand Down
8 changes: 3 additions & 5 deletions src/kysely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,14 @@ export class Kysely<DB>
* calls.
*
* ```ts
* const { count } = db.fn
*
* await db.selectFrom('person')
* .innerJoin('pet', 'pet.owner_id', 'person.id')
* .select([
* .select((eb) => [
* 'person.id',
* count('pet.id').as('pet_count')
* eb.fn.count('pet.id').as('pet_count')
* ])
* .groupBy('person.id')
* .having(count('pet.id'), '>', 10)
* .having((eb) => eb.fn.count('pet.id'), '>', 10)
* .execute()
* ```
*
Expand Down
6 changes: 3 additions & 3 deletions src/query-builder/aggregate-function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class AggregateFunctionBuilder<DB, TB extends keyof DB, O = unknown>
* const result = await db
* .selectFrom('person')
* .select(
* eb => eb.fn.count<number>('id').as('person_count')
* (eb) => eb.fn.count<number>('id').as('person_count')
* )
* .executeTakeFirstOrThrow()
*
Expand Down Expand Up @@ -326,7 +326,7 @@ export class AggregateFunctionBuilder<DB, TB extends keyof DB, O = unknown>
* const result = await db
* .selectFrom('person')
* .select(
* eb => eb.fn.avg<number>('age').over().as('average_age')
* (eb) => eb.fn.avg<number>('age').over().as('average_age')
* )
* .execute()
* ```
Expand All @@ -345,7 +345,7 @@ export class AggregateFunctionBuilder<DB, TB extends keyof DB, O = unknown>
* const result = await db
* .selectFrom('person')
* .select(
* eb => eb.fn.avg<number>('age').over(
* (eb) => eb.fn.avg<number>('age').over(
* ob => ob.partitionBy('last_name').orderBy('first_name', 'asc')
* ).as('average_age')
* )
Expand Down
68 changes: 17 additions & 51 deletions src/query-builder/function-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ### Examples
*
* ```ts
* const { avg } = db.fn
*
* db.selectFrom('toy')
* .select(avg('price').as('avg_price'))
* .select((eb) => eb.fn.avg('price').as('avg_price'))
* .execute()
* ```
*
Expand Down Expand Up @@ -170,10 +168,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* the first type argument:
*
* ```ts
* const { avg } = db.fn
*
* db.selectFrom('toy')
* .select(avg<number>('price').as('avg_price'))
* .select((eb) => eb.fn.avg<number>('price').as('avg_price'))
* .execute()
* ```
*
Expand All @@ -183,10 +179,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* function.
*
* ```ts
* const { avg } = db.fn
*
* db.selectFrom('toy')
* .select(avg<number | null>('price').as('avg_price'))
* .select((eb) => eb.fn.avg<number | null>('price').as('avg_price'))
* .execute()
* ```
*/
Expand Down Expand Up @@ -218,10 +212,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ### Examples
*
* ```ts
* const { coalesce } = db.fn
*
* db.selectFrom('participant')
* .select(coalesce('nickname', sql<string>`'<anonymous>'`).as('nickname'))
* .select((eb) => eb.fn.coalesce('nickname', sql<string>`'<anonymous>'`).as('nickname'))
* .where('room_id', '=', roomId)
* .execute()
* ```
Expand All @@ -247,10 +239,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* You can combine this function with other helpers in this module:
*
* ```ts
* const { avg, coalesce } = db.fn
*
* db.selectFrom('person')
* .select(coalesce(avg<number | null>('age'), sql<number>`0`).as('avg_age'))
* .select((eb) => eb.fn.coalesce(eb.fn.avg<number | null>('age'), sql<number>`0`).as('avg_age'))
* .where('first_name', '=', 'Jennifer')
* .execute()
* ```
Expand Down Expand Up @@ -284,10 +274,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ### Examples
*
* ```ts
* const { count } = db.fn
*
* db.selectFrom('toy')
* .select(count('id').as('num_toys'))
* .select((eb) => eb.fn.count('id').as('num_toys'))
* .execute()
* ```
*
Expand All @@ -308,10 +296,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* the type as the first type argument:
*
* ```ts
* const { count } = db.fn
*
* db.selectFrom('toy')
* .select(count<number>('id').as('num_toys'))
* .select((eb) => eb.fn.count<number>('id').as('num_toys'))
* .execute()
* ```
*
Expand Down Expand Up @@ -346,10 +332,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ### Examples
*
* ```ts
* const { countAll } = db.fn
*
* db.selectFrom('toy')
* .select(countAll().as('num_toys'))
* .select((eb) => eb.fn.countAll().as('num_toys'))
* .execute()
* ```
*
Expand All @@ -370,22 +354,18 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* the type as the first type argument:
*
* ```ts
* const { countAll } = db.fn
*
* db.selectFrom('toy')
* .select(countAll<number>().as('num_toys'))
* .select((eb) => eb.fn.countAll<number>().as('num_toys'))
* .execute()
* ```
*
* Some databases, such as PostgreSQL, support scoping the function to a specific
* table:
*
* ```ts
* const { countAll } = db.fn
*
* db.selectFrom('toy')
* .innerJoin('pet', 'pet.id', 'toy.pet_id')
* .select(countAll('toy').as('num_toys'))
* .select((eb) => eb.fn.countAll('toy').as('num_toys'))
* .execute()
* ```
*
Expand Down Expand Up @@ -431,10 +411,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ### Examples
*
* ```ts
* const { max } = db.fn
*
* db.selectFrom('toy')
* .select(max('price').as('max_price'))
* .select((eb) => eb.fn.max('price').as('max_price'))
* .execute()
* ```
*
Expand All @@ -458,10 +436,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* function.
*
* ```ts
* const { max } = db.fn
*
* db.selectFrom('toy')
* .select(max<number | null, 'price'>('price').as('max_price'))
* .select((eb) => eb.fn.max<number | null, 'price'>('price').as('max_price'))
* .execute()
* ```
*/
Expand Down Expand Up @@ -492,10 +468,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ### Examples
*
* ```ts
* const { min } = db.fn
*
* db.selectFrom('toy')
* .select(min('price').as('min_price'))
* .select((eb) => eb.fn.min('price').as('min_price'))
* .execute()
* ```
*
Expand All @@ -519,10 +493,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* function.
*
* ```ts
* const { min } = db.fn
*
* db.selectFrom('toy')
* .select(min<number | null, 'price'>('price').as('min_price'))
* .select((eb) => eb.fn.min<number | null, 'price'>('price').as('min_price'))
* .execute()
* ```
*/
Expand All @@ -549,10 +521,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ### Examples
*
* ```ts
* const { sum } = db.fn
*
* db.selectFrom('toy')
* .select(sum('price').as('total_price'))
* .select((eb) => eb.fn.sum('price').as('total_price'))
* .execute()
* ```
*
Expand Down Expand Up @@ -581,10 +551,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* the first type argument:
*
* ```ts
* const { sum } = db.fn
*
* db.selectFrom('toy')
* .select(sum<number>('price').as('total_price'))
* .select((eb) => eb.fn.sum<number>('price').as('total_price'))
* .execute()
* ```
*
Expand All @@ -594,10 +562,8 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* function.
*
* ```ts
* const { sum } = db.fn
*
* db.selectFrom('toy')
* .select(sum<number | null>('price').as('total_price'))
* .select((eb) => eb.fn.sum<number | null>('price').as('total_price'))
* .execute()
* ```
*/
Expand Down
4 changes: 2 additions & 2 deletions src/query-builder/over-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class OverBuilder<DB, TB extends keyof DB>
* const result = await db
* .selectFrom('person')
* .select(
* eb => eb.fn.avg<number>('age').over(
* (eb) => eb.fn.avg<number>('age').over(
* ob => ob.orderBy('first_name', 'asc').orderBy('last_name', 'asc')
* ).as('average_age')
* )
Expand Down Expand Up @@ -63,7 +63,7 @@ export class OverBuilder<DB, TB extends keyof DB>
* const result = await db
* .selectFrom('person')
* .select(
* eb => eb.fn.avg<number>('age').over(
* (eb) => eb.fn.avg<number>('age').over(
* ob => ob.partitionBy(['last_name', 'first_name'])
* ).as('average_age')
* )
Expand Down
2 changes: 1 addition & 1 deletion src/query-builder/returning-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface ReturningInterface<DB, TB extends keyof DB, O> {
* first_name: 'Jennifer',
* last_name: 'Aniston'
* })
* .returning(eb => [
* .returning((eb) => [
* 'id as id',
* sql<string>`concat(first_name, ' ', last_name)`.as('full_name'),
* eb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
Expand Down
4 changes: 1 addition & 3 deletions src/query-builder/select-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1734,14 +1734,12 @@ export class SelectQueryBuilder<DB, TB extends keyof DB, O>
* You can also call any other methods inside the callback:
*
* ```ts
* const { count } = db.fn
*
* db.selectFrom('person')
* .select('person.id')
* .$if(filterByFirstName, (qb) => qb.where('first_name', '=', firstName))
* .$if(filterByPetCount, (qb) => qb
* .innerJoin('pet', 'pet.owner_id', 'person.id')
* .having(count('pet.id'), '>', petCountLimit)
* .having((eb) => eb.fn.count('pet.id'), '>', petCountLimit)
* .groupBy('person.id')
* )
* ```
Expand Down

0 comments on commit c2e9a3f

Please sign in to comment.