kysely 0.26.0 → 0.26.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -81,22 +81,24 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
81
81
  *
82
82
  * <!-- siteExample("where", "OR where", 30) -->
83
83
  *
84
- * To combine conditions using `OR`, you can use the expression builder:
84
+ * To combine conditions using `OR`, you can use the expression builder.
85
+ * There are two ways to create `OR` expressions. Both are shown in this
86
+ * example:
85
87
  *
86
88
  * ```ts
87
89
  * const persons = await db
88
90
  * .selectFrom('person')
89
91
  * .selectAll()
90
- * .where(({ or, and, cmpr }) => or([
91
- * and([
92
- * cmpr('first_name', '=', 'Jennifer'),
93
- * cmpr('last_name', '=', 'Aniston')
94
- * ]),
95
- * and([
96
- * cmpr('first_name', '=', 'Sylvester'),
97
- * cmpr('last_name', '=', 'Stallone')
98
- * ])
92
+ * // 1. Using the `or` method on the expression builder:
93
+ * .where((eb) => eb.or([
94
+ * eb('first_name', '=', 'Jennifer'),
95
+ * eb('first_name', '=', 'Sylvester')
99
96
  * ]))
97
+ * // 2. Chaining expressions using the `or` method on the
98
+ * // created expressions:
99
+ * .where((eb) =>
100
+ * eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
101
+ * )
100
102
  * .execute()
101
103
  * ```
102
104
  *
@@ -106,9 +108,9 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
106
108
  * select *
107
109
  * from "person"
108
110
  * where (
109
- * ("first_name" = $1 AND "last_name" = $2)
110
- * OR
111
- * ("first_name" = $3 AND "last_name" = $4)
111
+ * ("first_name" = $1 or "first_name" = $2)
112
+ * and
113
+ * ("last_name" = $3 or "last_name" = $4)
112
114
  * )
113
115
  * ```
114
116
  *
@@ -117,8 +119,12 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
117
119
  * You can add expressions conditionally like this:
118
120
  *
119
121
  * ```ts
122
+ * import { Expression, SqlBool } from 'kysely'
123
+ *
120
124
  * const firstName: string | undefined = 'Jennifer'
121
125
  * const lastName: string | undefined = 'Aniston'
126
+ * const under18 = true
127
+ * const over60 = true
122
128
  *
123
129
  * let query = db
124
130
  * .selectFrom('person')
@@ -134,6 +140,23 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
134
140
  * query = query.where('last_name', '=', lastName)
135
141
  * }
136
142
  *
143
+ * if (under18 || over60) {
144
+ * // Conditional OR expressions can be added like this.
145
+ * query = query.where((eb) => {
146
+ * const ors: Expression<SqlBool>[] = []
147
+ *
148
+ * if (under18) {
149
+ * ors.push(eb('age', '<', 18))
150
+ * }
151
+ *
152
+ * if (over60) {
153
+ * ors.push(eb('age', '>', 60))
154
+ * }
155
+ *
156
+ * return eb.or(ors)
157
+ * })
158
+ * }
159
+ *
137
160
  * const persons = await query.execute()
138
161
  * ```
139
162
  *
@@ -198,10 +221,10 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
198
221
  * const persons = await db
199
222
  * .selectFrom('person')
200
223
  * .selectAll('person')
201
- * .where(({ cmpr, or, and, not, exists, selectFrom }) => and([
224
+ * .where(({ eb, or, and, not, exists, selectFrom }) => and([
202
225
  * or([
203
- * cmpr('first_name', '=', firstName),
204
- * cmpr('age', '<', maxAge)
226
+ * eb('first_name', '=', firstName),
227
+ * eb('age', '<', maxAge)
205
228
  * ]),
206
229
  * not(exists(
207
230
  * selectFrom('pet')
@@ -544,7 +567,38 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
544
567
  * update "person" set "first_name" = $1, "last_name" = $2 where "id" = $3
545
568
  * ```
546
569
  *
547
- * On PostgreSQL you ca chain `returning` to the query to get
570
+ * <!-- siteExample("update", "Complex values", 20) -->
571
+ *
572
+ * As always, you can provide a callback to the `set` method to get access
573
+ * to an expression builder:
574
+ *
575
+ * ```ts
576
+ * const result = await db
577
+ * .updateTable('person')
578
+ * .set((eb) => ({
579
+ * age: eb('age', '+', 1),
580
+ * first_name: eb.selectFrom('pet').select('name').limit(1),
581
+ * last_name: 'updated',
582
+ * }))
583
+ * .where('id', '=', '1')
584
+ * .executeTakeFirst()
585
+ *
586
+ * console.log(result.numUpdatedRows)
587
+ * ```
588
+ *
589
+ * The generated SQL (PostgreSQL):
590
+ *
591
+ * ```sql
592
+ * update "person"
593
+ * set
594
+ * "first_name" = (select "name" from "pet" limit $1),
595
+ * "age" = "age" + $2,
596
+ * "last_name" = $3
597
+ * where
598
+ * "id" = $4
599
+ * ```
600
+ *
601
+ * On PostgreSQL you can chain `returning` to the query to get
548
602
  * the updated rows' columns (or any other expression) as the
549
603
  * return value:
550
604
  *
@@ -726,7 +780,7 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
726
780
  * }
727
781
  * ```
728
782
  */
729
- $if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<DB, UT, TB, O2>): O2 extends UpdateResult ? UpdateQueryBuilder<DB, UT, TB, UpdateResult> : O2 extends O & infer E ? UpdateQueryBuilder<DB, UT, TB, O & Partial<E>> : UpdateQueryBuilder<DB, UT, TB, Partial<O2>>;
783
+ $if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<any, any, any, O2>): O2 extends UpdateResult ? UpdateQueryBuilder<DB, UT, TB, UpdateResult> : O2 extends O & infer E ? UpdateQueryBuilder<DB, UT, TB, O & Partial<E>> : UpdateQueryBuilder<DB, UT, TB, Partial<O2>>;
730
784
  /**
731
785
  * Change the output type of the query.
732
786
  *
@@ -59,22 +59,24 @@ export interface WhereInterface<DB, TB extends keyof DB> {
59
59
  *
60
60
  * <!-- siteExample("where", "OR where", 30) -->
61
61
  *
62
- * To combine conditions using `OR`, you can use the expression builder:
62
+ * To combine conditions using `OR`, you can use the expression builder.
63
+ * There are two ways to create `OR` expressions. Both are shown in this
64
+ * example:
63
65
  *
64
66
  * ```ts
65
67
  * const persons = await db
66
68
  * .selectFrom('person')
67
69
  * .selectAll()
68
- * .where(({ or, and, cmpr }) => or([
69
- * and([
70
- * cmpr('first_name', '=', 'Jennifer'),
71
- * cmpr('last_name', '=', 'Aniston')
72
- * ]),
73
- * and([
74
- * cmpr('first_name', '=', 'Sylvester'),
75
- * cmpr('last_name', '=', 'Stallone')
76
- * ])
70
+ * // 1. Using the `or` method on the expression builder:
71
+ * .where((eb) => eb.or([
72
+ * eb('first_name', '=', 'Jennifer'),
73
+ * eb('first_name', '=', 'Sylvester')
77
74
  * ]))
75
+ * // 2. Chaining expressions using the `or` method on the
76
+ * // created expressions:
77
+ * .where((eb) =>
78
+ * eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
79
+ * )
78
80
  * .execute()
79
81
  * ```
80
82
  *
@@ -84,9 +86,9 @@ export interface WhereInterface<DB, TB extends keyof DB> {
84
86
  * select *
85
87
  * from "person"
86
88
  * where (
87
- * ("first_name" = $1 AND "last_name" = $2)
88
- * OR
89
- * ("first_name" = $3 AND "last_name" = $4)
89
+ * ("first_name" = $1 or "first_name" = $2)
90
+ * and
91
+ * ("last_name" = $3 or "last_name" = $4)
90
92
  * )
91
93
  * ```
92
94
  *
@@ -95,8 +97,12 @@ export interface WhereInterface<DB, TB extends keyof DB> {
95
97
  * You can add expressions conditionally like this:
96
98
  *
97
99
  * ```ts
100
+ * import { Expression, SqlBool } from 'kysely'
101
+ *
98
102
  * const firstName: string | undefined = 'Jennifer'
99
103
  * const lastName: string | undefined = 'Aniston'
104
+ * const under18 = true
105
+ * const over60 = true
100
106
  *
101
107
  * let query = db
102
108
  * .selectFrom('person')
@@ -112,6 +118,23 @@ export interface WhereInterface<DB, TB extends keyof DB> {
112
118
  * query = query.where('last_name', '=', lastName)
113
119
  * }
114
120
  *
121
+ * if (under18 || over60) {
122
+ * // Conditional OR expressions can be added like this.
123
+ * query = query.where((eb) => {
124
+ * const ors: Expression<SqlBool>[] = []
125
+ *
126
+ * if (under18) {
127
+ * ors.push(eb('age', '<', 18))
128
+ * }
129
+ *
130
+ * if (over60) {
131
+ * ors.push(eb('age', '>', 60))
132
+ * }
133
+ *
134
+ * return eb.or(ors)
135
+ * })
136
+ * }
137
+ *
115
138
  * const persons = await query.execute()
116
139
  * ```
117
140
  *
@@ -176,10 +199,10 @@ export interface WhereInterface<DB, TB extends keyof DB> {
176
199
  * const persons = await db
177
200
  * .selectFrom('person')
178
201
  * .selectAll('person')
179
- * .where(({ cmpr, or, and, not, exists, selectFrom }) => and([
202
+ * .where(({ eb, or, and, not, exists, selectFrom }) => and([
180
203
  * or([
181
- * cmpr('first_name', '=', firstName),
182
- * cmpr('age', '<', maxAge)
204
+ * eb('first_name', '=', firstName),
205
+ * eb('age', '<', maxAge)
183
206
  * ]),
184
207
  * not(exists(
185
208
  * selectFrom('pet')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kysely",
3
- "version": "0.26.0",
3
+ "version": "0.26.1",
4
4
  "description": "Type safe SQL query builder",
5
5
  "repository": {
6
6
  "type": "git",