Skip to content

Commit

Permalink
update site examples to use updated expression builder
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Jul 9, 2023
1 parent 4a95f6b commit 7b83f8f
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 38 deletions.
6 changes: 3 additions & 3 deletions site/docs/examples/SELECT/0050-complex-selections.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const complexSelections = `import { sql } from 'kysely'
const persons = await db.selectFrom('person')
.select(({ selectFrom, or, cmpr }) => [
.select(({ eb, selectFrom, or }) => [
// Select a correlated subquery
selectFrom('pet')
.whereRef('person.id', '=', 'pet.owner_id')
Expand All @@ -13,8 +13,8 @@ const persons = await db.selectFrom('person')
// Build and select an expression using
// the expression builder
or([
cmpr('first_name', '=', 'Jennifer'),
cmpr('first_name', '=', 'Arnold')
eb('first_name', '=', 'Jennifer'),
eb('first_name', '=', 'Arnold')
]).as('is_jennifer_or_arnold'),
// Select a raw sql expression
Expand Down
11 changes: 11 additions & 0 deletions site/docs/examples/UPDATE/0020-complex-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const complexValues = `const result = await db
.updateTable('person')
.set((eb) => ({
age: eb('age', '+', 1),
first_name: eb.selectFrom('pet').select('name').limit(1),
last_name: 'updated',
}))
.where('id', '=', '1')
.executeTakeFirst()
console.log(result.numUpdatedRows)`
32 changes: 32 additions & 0 deletions site/docs/examples/UPDATE/0020-complex-values.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: 'Complex values'
---

# Complex values

As always, you can provide a callback to the `set` method to get access
to an expression builder:

import {
Playground,
exampleSetup,
} from '../../../src/components/Playground'

import {
complexValues
} from './0020-complex-values'

<div style={{ marginBottom: '1em' }}>
<Playground code={complexValues} setupCode={exampleSetup} />
</div>

:::info More examples
The API documentation is packed with examples. The API docs are hosted [here](https://kysely-org.github.io/kysely/)
but you can access the same documentation by hovering over functions/methods/classes in your IDE. The examples are always
just one hover away!

For example, check out these sections:
- [set method](https://kysely-org.github.io/kysely/classes/UpdateQueryBuilder.html#set)
- [returning method](https://kysely-org.github.io/kysely/classes/UpdateQueryBuilder.html#returning)
- [updateTable method](https://kysely-org.github.io/kysely/classes/Kysely.html#updateTable)
:::
18 changes: 9 additions & 9 deletions site/docs/examples/WHERE/0030-or-where.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export const orWhere = `const persons = await db
.selectFrom('person')
.selectAll()
.where(({ or, and, cmpr }) => or([
and([
cmpr('first_name', '=', 'Jennifer'),
cmpr('last_name', '=', 'Aniston')
]),
and([
cmpr('first_name', '=', 'Sylvester'),
cmpr('last_name', '=', 'Stallone')
])
// 1. Using the \`or\` method on the expression builder:
.where((eb) => eb.or([
eb('first_name', '=', 'Jennifer'),
eb('first_name', '=', 'Sylvester')
]))
// 2. Chaining expressions using the \`or\` method on the
// created expressions:
.where((eb) =>
eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
)
.execute()`
4 changes: 3 additions & 1 deletion site/docs/examples/WHERE/0030-or-where.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ title: 'OR where'

# OR where

To combine conditions using `OR`, you can use the expression builder:
To combine conditions using `OR`, you can use the expression builder.
There are two ways to create `OR` expressions. Both are shown in this
example:

import {
Playground,
Expand Down
6 changes: 3 additions & 3 deletions site/docs/examples/WHERE/0050-complex-where-clause.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const maxAge = 60
const persons = await db
.selectFrom('person')
.selectAll('person')
.where(({ cmpr, or, and, not, exists, selectFrom }) => and([
.where(({ eb, or, and, not, exists, selectFrom }) => and([
or([
cmpr('first_name', '=', firstName),
cmpr('age', '<', maxAge)
eb('first_name', '=', firstName),
eb('age', '<', maxAge)
]),
not(exists(
selectFrom('pet')
Expand Down
2 changes: 1 addition & 1 deletion src/expression/expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
*
* ```ts
* db.selectFrom('person')
* .where(({ cmpr, ref }) => cmpr(
* .where(({ eb, ref }) => eb(
* ref('address', '->').key('state').key('abbr'),
* '=',
* 'CA'
Expand Down
2 changes: 1 addition & 1 deletion src/query-builder/json-path-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class TraversedJSONPathBuilder<S, O>
* const result = await db
* .selectFrom('person')
* .select(eb =>
* eb.cmpr('first_name', '=', 'Jennifer').as('is_jennifer')
* eb('first_name', '=', 'Jennifer').as('is_jennifer')
* )
* .executeTakeFirstOrThrow()
*
Expand Down
6 changes: 3 additions & 3 deletions src/query-builder/select-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export class SelectQueryBuilder<DB, TB extends keyof DB, O>
* import { sql } from 'kysely'
*
* const persons = await db.selectFrom('person')
* .select(({ selectFrom, or, cmpr }) => [
* .select(({ eb, selectFrom, or }) => [
* // Select a correlated subquery
* selectFrom('pet')
* .whereRef('person.id', '=', 'pet.owner_id')
Expand All @@ -274,8 +274,8 @@ export class SelectQueryBuilder<DB, TB extends keyof DB, O>
* // Build and select an expression using
* // the expression builder
* or([
* cmpr('first_name', '=', 'Jennifer'),
* cmpr('first_name', '=', 'Arnold')
* eb('first_name', '=', 'Jennifer'),
* eb('first_name', '=', 'Arnold')
* ]).as('is_jennifer_or_arnold'),
*
* // Select a raw sql expression
Expand Down
33 changes: 32 additions & 1 deletion src/query-builder/update-query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,38 @@ export class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O>
* update "person" set "first_name" = $1, "last_name" = $2 where "id" = $3
* ```
*
* On PostgreSQL you ca chain `returning` to the query to get
* <!-- siteExample("update", "Complex values", 20) -->
*
* As always, you can provide a callback to the `set` method to get access
* to an expression builder:
*
* ```ts
* const result = await db
* .updateTable('person')
* .set((eb) => ({
* age: eb('age', '+', 1),
* first_name: eb.selectFrom('pet').select('name').limit(1),
* last_name: 'updated',
* }))
* .where('id', '=', '1')
* .executeTakeFirst()
*
* console.log(result.numUpdatedRows)
* ```
*
* The generated SQL (PostgreSQL):
*
* ```sql
* update "person"
* set
* "first_name" = (select "name" from "pet" limit $1),
* "age" = "age" + $2,
* "last_name" = $3
* where
* "id" = $4
* ```
*
* On PostgreSQL you can chain `returning` to the query to get
* the updated rows' columns (or any other expression) as the
* return value:
*
Expand Down
34 changes: 18 additions & 16 deletions src/query-builder/where-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,24 @@ export interface WhereInterface<DB, TB extends keyof DB> {
*
* <!-- siteExample("where", "OR where", 30) -->
*
* To combine conditions using `OR`, you can use the expression builder:
* To combine conditions using `OR`, you can use the expression builder.
* There are two ways to create `OR` expressions. Both are shown in this
* example:
*
* ```ts
* const persons = await db
* .selectFrom('person')
* .selectAll()
* .where(({ or, and, cmpr }) => or([
* and([
* cmpr('first_name', '=', 'Jennifer'),
* cmpr('last_name', '=', 'Aniston')
* ]),
* and([
* cmpr('first_name', '=', 'Sylvester'),
* cmpr('last_name', '=', 'Stallone')
* ])
* // 1. Using the `or` method on the expression builder:
* .where((eb) => eb.or([
* eb('first_name', '=', 'Jennifer'),
* eb('first_name', '=', 'Sylvester')
* ]))
* // 2. Chaining expressions using the `or` method on the
* // created expressions:
* .where((eb) =>
* eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
* )
* .execute()
* ```
*
Expand All @@ -88,9 +90,9 @@ export interface WhereInterface<DB, TB extends keyof DB> {
* select *
* from "person"
* where (
* ("first_name" = $1 AND "last_name" = $2)
* OR
* ("first_name" = $3 AND "last_name" = $4)
* ("first_name" = $1 or "first_name" = $2)
* and
* ("last_name" = $3 or "last_name" = $4)
* )
* ```
*
Expand Down Expand Up @@ -180,10 +182,10 @@ export interface WhereInterface<DB, TB extends keyof DB> {
* const persons = await db
* .selectFrom('person')
* .selectAll('person')
* .where(({ cmpr, or, and, not, exists, selectFrom }) => and([
* .where(({ eb, or, and, not, exists, selectFrom }) => and([
* or([
* cmpr('first_name', '=', firstName),
* cmpr('age', '<', maxAge)
* eb('first_name', '=', firstName),
* eb('age', '<', maxAge)
* ]),
* not(exists(
* selectFrom('pet')
Expand Down

1 comment on commit 7b83f8f

@vercel
Copy link

@vercel vercel bot commented on 7b83f8f Jul 9, 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 – ./

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

Please sign in to comment.