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.
- package/dist/cjs/expression/expression-builder.d.ts +1 -1
- package/dist/cjs/query-builder/delete-query-builder.d.ts +40 -17
- package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/cjs/query-builder/json-path-builder.d.ts +1 -1
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +78 -32
- package/dist/cjs/query-builder/select-query-builder.d.ts +44 -21
- package/dist/cjs/query-builder/select-query-builder.js +3 -3
- package/dist/cjs/query-builder/update-query-builder.d.ts +72 -18
- package/dist/cjs/query-builder/where-interface.d.ts +39 -16
- package/dist/esm/expression/expression-builder.d.ts +1 -1
- package/dist/esm/query-builder/delete-query-builder.d.ts +40 -17
- package/dist/esm/query-builder/insert-query-builder.d.ts +1 -1
- package/dist/esm/query-builder/json-path-builder.d.ts +1 -1
- package/dist/esm/query-builder/on-conflict-builder.d.ts +78 -32
- package/dist/esm/query-builder/select-query-builder.d.ts +44 -21
- package/dist/esm/query-builder/select-query-builder.js +3 -3
- package/dist/esm/query-builder/update-query-builder.d.ts +72 -18
- package/dist/esm/query-builder/where-interface.d.ts +39 -16
- package/package.json +1 -1
|
@@ -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
|
-
* .
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
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
|
|
88
|
-
*
|
|
89
|
-
* ("
|
|
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(({
|
|
202
|
+
* .where(({ eb, or, and, not, exists, selectFrom }) => and([
|
|
180
203
|
* or([
|
|
181
|
-
*
|
|
182
|
-
*
|
|
204
|
+
* eb('first_name', '=', firstName),
|
|
205
|
+
* eb('age', '<', maxAge)
|
|
183
206
|
* ]),
|
|
184
207
|
* not(exists(
|
|
185
208
|
* selectFrom('pet')
|
|
@@ -266,7 +266,7 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
|
|
|
266
266
|
*
|
|
267
267
|
* ```ts
|
|
268
268
|
* db.selectFrom('person')
|
|
269
|
-
* .where(({
|
|
269
|
+
* .where(({ eb, ref }) => eb(
|
|
270
270
|
* ref('address', '->').key('state').key('abbr'),
|
|
271
271
|
* '=',
|
|
272
272
|
* 'CA'
|
|
@@ -80,22 +80,24 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
80
80
|
*
|
|
81
81
|
* <!-- siteExample("where", "OR where", 30) -->
|
|
82
82
|
*
|
|
83
|
-
* To combine conditions using `OR`, you can use the expression builder
|
|
83
|
+
* To combine conditions using `OR`, you can use the expression builder.
|
|
84
|
+
* There are two ways to create `OR` expressions. Both are shown in this
|
|
85
|
+
* example:
|
|
84
86
|
*
|
|
85
87
|
* ```ts
|
|
86
88
|
* const persons = await db
|
|
87
89
|
* .selectFrom('person')
|
|
88
90
|
* .selectAll()
|
|
89
|
-
* .
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* ]),
|
|
94
|
-
* and([
|
|
95
|
-
* cmpr('first_name', '=', 'Sylvester'),
|
|
96
|
-
* cmpr('last_name', '=', 'Stallone')
|
|
97
|
-
* ])
|
|
91
|
+
* // 1. Using the `or` method on the expression builder:
|
|
92
|
+
* .where((eb) => eb.or([
|
|
93
|
+
* eb('first_name', '=', 'Jennifer'),
|
|
94
|
+
* eb('first_name', '=', 'Sylvester')
|
|
98
95
|
* ]))
|
|
96
|
+
* // 2. Chaining expressions using the `or` method on the
|
|
97
|
+
* // created expressions:
|
|
98
|
+
* .where((eb) =>
|
|
99
|
+
* eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
|
|
100
|
+
* )
|
|
99
101
|
* .execute()
|
|
100
102
|
* ```
|
|
101
103
|
*
|
|
@@ -105,9 +107,9 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
105
107
|
* select *
|
|
106
108
|
* from "person"
|
|
107
109
|
* where (
|
|
108
|
-
* ("first_name" = $1
|
|
109
|
-
*
|
|
110
|
-
* ("
|
|
110
|
+
* ("first_name" = $1 or "first_name" = $2)
|
|
111
|
+
* and
|
|
112
|
+
* ("last_name" = $3 or "last_name" = $4)
|
|
111
113
|
* )
|
|
112
114
|
* ```
|
|
113
115
|
*
|
|
@@ -116,8 +118,12 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
116
118
|
* You can add expressions conditionally like this:
|
|
117
119
|
*
|
|
118
120
|
* ```ts
|
|
121
|
+
* import { Expression, SqlBool } from 'kysely'
|
|
122
|
+
*
|
|
119
123
|
* const firstName: string | undefined = 'Jennifer'
|
|
120
124
|
* const lastName: string | undefined = 'Aniston'
|
|
125
|
+
* const under18 = true
|
|
126
|
+
* const over60 = true
|
|
121
127
|
*
|
|
122
128
|
* let query = db
|
|
123
129
|
* .selectFrom('person')
|
|
@@ -133,6 +139,23 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
133
139
|
* query = query.where('last_name', '=', lastName)
|
|
134
140
|
* }
|
|
135
141
|
*
|
|
142
|
+
* if (under18 || over60) {
|
|
143
|
+
* // Conditional OR expressions can be added like this.
|
|
144
|
+
* query = query.where((eb) => {
|
|
145
|
+
* const ors: Expression<SqlBool>[] = []
|
|
146
|
+
*
|
|
147
|
+
* if (under18) {
|
|
148
|
+
* ors.push(eb('age', '<', 18))
|
|
149
|
+
* }
|
|
150
|
+
*
|
|
151
|
+
* if (over60) {
|
|
152
|
+
* ors.push(eb('age', '>', 60))
|
|
153
|
+
* }
|
|
154
|
+
*
|
|
155
|
+
* return eb.or(ors)
|
|
156
|
+
* })
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
136
159
|
* const persons = await query.execute()
|
|
137
160
|
* ```
|
|
138
161
|
*
|
|
@@ -197,10 +220,10 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
197
220
|
* const persons = await db
|
|
198
221
|
* .selectFrom('person')
|
|
199
222
|
* .selectAll('person')
|
|
200
|
-
* .where(({
|
|
223
|
+
* .where(({ eb, or, and, not, exists, selectFrom }) => and([
|
|
201
224
|
* or([
|
|
202
|
-
*
|
|
203
|
-
*
|
|
225
|
+
* eb('first_name', '=', firstName),
|
|
226
|
+
* eb('age', '<', maxAge)
|
|
204
227
|
* ]),
|
|
205
228
|
* not(exists(
|
|
206
229
|
* selectFrom('pet')
|
|
@@ -821,7 +844,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
821
844
|
* }
|
|
822
845
|
* ```
|
|
823
846
|
*/
|
|
824
|
-
$if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<
|
|
847
|
+
$if<O2>(condition: boolean, func: (qb: this) => DeleteQueryBuilder<any, any, O2>): O2 extends DeleteResult ? DeleteQueryBuilder<DB, TB, DeleteResult> : O2 extends O & infer E ? DeleteQueryBuilder<DB, TB, O & Partial<E>> : DeleteQueryBuilder<DB, TB, Partial<O2>>;
|
|
825
848
|
/**
|
|
826
849
|
* Change the output type of the query.
|
|
827
850
|
*
|
|
@@ -539,7 +539,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
539
539
|
* }
|
|
540
540
|
* ```
|
|
541
541
|
*/
|
|
542
|
-
$if<O2>(condition: boolean, func: (qb: this) => InsertQueryBuilder<
|
|
542
|
+
$if<O2>(condition: boolean, func: (qb: this) => InsertQueryBuilder<any, any, O2>): O2 extends InsertResult ? InsertQueryBuilder<DB, TB, InsertResult> : O2 extends O & infer E ? InsertQueryBuilder<DB, TB, O & Partial<E>> : InsertQueryBuilder<DB, TB, Partial<O2>>;
|
|
543
543
|
/**
|
|
544
544
|
* Change the output type of the query.
|
|
545
545
|
*
|
|
@@ -152,7 +152,7 @@ export declare class TraversedJSONPathBuilder<S, O> extends JSONPathBuilder<S, O
|
|
|
152
152
|
* const result = await db
|
|
153
153
|
* .selectFrom('person')
|
|
154
154
|
* .select(eb =>
|
|
155
|
-
* eb
|
|
155
|
+
* eb('first_name', '=', 'Jennifer').as('is_jennifer')
|
|
156
156
|
* )
|
|
157
157
|
* .executeTakeFirstOrThrow()
|
|
158
158
|
*
|
|
@@ -100,22 +100,24 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
100
100
|
*
|
|
101
101
|
* <!-- siteExample("where", "OR where", 30) -->
|
|
102
102
|
*
|
|
103
|
-
* To combine conditions using `OR`, you can use the expression builder
|
|
103
|
+
* To combine conditions using `OR`, you can use the expression builder.
|
|
104
|
+
* There are two ways to create `OR` expressions. Both are shown in this
|
|
105
|
+
* example:
|
|
104
106
|
*
|
|
105
107
|
* ```ts
|
|
106
108
|
* const persons = await db
|
|
107
109
|
* .selectFrom('person')
|
|
108
110
|
* .selectAll()
|
|
109
|
-
* .
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* ]),
|
|
114
|
-
* and([
|
|
115
|
-
* cmpr('first_name', '=', 'Sylvester'),
|
|
116
|
-
* cmpr('last_name', '=', 'Stallone')
|
|
117
|
-
* ])
|
|
111
|
+
* // 1. Using the `or` method on the expression builder:
|
|
112
|
+
* .where((eb) => eb.or([
|
|
113
|
+
* eb('first_name', '=', 'Jennifer'),
|
|
114
|
+
* eb('first_name', '=', 'Sylvester')
|
|
118
115
|
* ]))
|
|
116
|
+
* // 2. Chaining expressions using the `or` method on the
|
|
117
|
+
* // created expressions:
|
|
118
|
+
* .where((eb) =>
|
|
119
|
+
* eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
|
|
120
|
+
* )
|
|
119
121
|
* .execute()
|
|
120
122
|
* ```
|
|
121
123
|
*
|
|
@@ -125,9 +127,9 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
125
127
|
* select *
|
|
126
128
|
* from "person"
|
|
127
129
|
* where (
|
|
128
|
-
* ("first_name" = $1
|
|
129
|
-
*
|
|
130
|
-
* ("
|
|
130
|
+
* ("first_name" = $1 or "first_name" = $2)
|
|
131
|
+
* and
|
|
132
|
+
* ("last_name" = $3 or "last_name" = $4)
|
|
131
133
|
* )
|
|
132
134
|
* ```
|
|
133
135
|
*
|
|
@@ -136,8 +138,12 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
136
138
|
* You can add expressions conditionally like this:
|
|
137
139
|
*
|
|
138
140
|
* ```ts
|
|
141
|
+
* import { Expression, SqlBool } from 'kysely'
|
|
142
|
+
*
|
|
139
143
|
* const firstName: string | undefined = 'Jennifer'
|
|
140
144
|
* const lastName: string | undefined = 'Aniston'
|
|
145
|
+
* const under18 = true
|
|
146
|
+
* const over60 = true
|
|
141
147
|
*
|
|
142
148
|
* let query = db
|
|
143
149
|
* .selectFrom('person')
|
|
@@ -153,6 +159,23 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
153
159
|
* query = query.where('last_name', '=', lastName)
|
|
154
160
|
* }
|
|
155
161
|
*
|
|
162
|
+
* if (under18 || over60) {
|
|
163
|
+
* // Conditional OR expressions can be added like this.
|
|
164
|
+
* query = query.where((eb) => {
|
|
165
|
+
* const ors: Expression<SqlBool>[] = []
|
|
166
|
+
*
|
|
167
|
+
* if (under18) {
|
|
168
|
+
* ors.push(eb('age', '<', 18))
|
|
169
|
+
* }
|
|
170
|
+
*
|
|
171
|
+
* if (over60) {
|
|
172
|
+
* ors.push(eb('age', '>', 60))
|
|
173
|
+
* }
|
|
174
|
+
*
|
|
175
|
+
* return eb.or(ors)
|
|
176
|
+
* })
|
|
177
|
+
* }
|
|
178
|
+
*
|
|
156
179
|
* const persons = await query.execute()
|
|
157
180
|
* ```
|
|
158
181
|
*
|
|
@@ -217,10 +240,10 @@ export declare class OnConflictBuilder<DB, TB extends keyof DB> implements Where
|
|
|
217
240
|
* const persons = await db
|
|
218
241
|
* .selectFrom('person')
|
|
219
242
|
* .selectAll('person')
|
|
220
|
-
* .where(({
|
|
243
|
+
* .where(({ eb, or, and, not, exists, selectFrom }) => and([
|
|
221
244
|
* or([
|
|
222
|
-
*
|
|
223
|
-
*
|
|
245
|
+
* eb('first_name', '=', firstName),
|
|
246
|
+
* eb('age', '<', maxAge)
|
|
224
247
|
* ]),
|
|
225
248
|
* not(exists(
|
|
226
249
|
* selectFrom('pet')
|
|
@@ -468,22 +491,24 @@ export declare class OnConflictUpdateBuilder<DB, TB extends keyof DB> implements
|
|
|
468
491
|
*
|
|
469
492
|
* <!-- siteExample("where", "OR where", 30) -->
|
|
470
493
|
*
|
|
471
|
-
* To combine conditions using `OR`, you can use the expression builder
|
|
494
|
+
* To combine conditions using `OR`, you can use the expression builder.
|
|
495
|
+
* There are two ways to create `OR` expressions. Both are shown in this
|
|
496
|
+
* example:
|
|
472
497
|
*
|
|
473
498
|
* ```ts
|
|
474
499
|
* const persons = await db
|
|
475
500
|
* .selectFrom('person')
|
|
476
501
|
* .selectAll()
|
|
477
|
-
* .
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
* ]),
|
|
482
|
-
* and([
|
|
483
|
-
* cmpr('first_name', '=', 'Sylvester'),
|
|
484
|
-
* cmpr('last_name', '=', 'Stallone')
|
|
485
|
-
* ])
|
|
502
|
+
* // 1. Using the `or` method on the expression builder:
|
|
503
|
+
* .where((eb) => eb.or([
|
|
504
|
+
* eb('first_name', '=', 'Jennifer'),
|
|
505
|
+
* eb('first_name', '=', 'Sylvester')
|
|
486
506
|
* ]))
|
|
507
|
+
* // 2. Chaining expressions using the `or` method on the
|
|
508
|
+
* // created expressions:
|
|
509
|
+
* .where((eb) =>
|
|
510
|
+
* eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
|
|
511
|
+
* )
|
|
487
512
|
* .execute()
|
|
488
513
|
* ```
|
|
489
514
|
*
|
|
@@ -493,9 +518,9 @@ export declare class OnConflictUpdateBuilder<DB, TB extends keyof DB> implements
|
|
|
493
518
|
* select *
|
|
494
519
|
* from "person"
|
|
495
520
|
* where (
|
|
496
|
-
* ("first_name" = $1
|
|
497
|
-
*
|
|
498
|
-
* ("
|
|
521
|
+
* ("first_name" = $1 or "first_name" = $2)
|
|
522
|
+
* and
|
|
523
|
+
* ("last_name" = $3 or "last_name" = $4)
|
|
499
524
|
* )
|
|
500
525
|
* ```
|
|
501
526
|
*
|
|
@@ -504,8 +529,12 @@ export declare class OnConflictUpdateBuilder<DB, TB extends keyof DB> implements
|
|
|
504
529
|
* You can add expressions conditionally like this:
|
|
505
530
|
*
|
|
506
531
|
* ```ts
|
|
532
|
+
* import { Expression, SqlBool } from 'kysely'
|
|
533
|
+
*
|
|
507
534
|
* const firstName: string | undefined = 'Jennifer'
|
|
508
535
|
* const lastName: string | undefined = 'Aniston'
|
|
536
|
+
* const under18 = true
|
|
537
|
+
* const over60 = true
|
|
509
538
|
*
|
|
510
539
|
* let query = db
|
|
511
540
|
* .selectFrom('person')
|
|
@@ -521,6 +550,23 @@ export declare class OnConflictUpdateBuilder<DB, TB extends keyof DB> implements
|
|
|
521
550
|
* query = query.where('last_name', '=', lastName)
|
|
522
551
|
* }
|
|
523
552
|
*
|
|
553
|
+
* if (under18 || over60) {
|
|
554
|
+
* // Conditional OR expressions can be added like this.
|
|
555
|
+
* query = query.where((eb) => {
|
|
556
|
+
* const ors: Expression<SqlBool>[] = []
|
|
557
|
+
*
|
|
558
|
+
* if (under18) {
|
|
559
|
+
* ors.push(eb('age', '<', 18))
|
|
560
|
+
* }
|
|
561
|
+
*
|
|
562
|
+
* if (over60) {
|
|
563
|
+
* ors.push(eb('age', '>', 60))
|
|
564
|
+
* }
|
|
565
|
+
*
|
|
566
|
+
* return eb.or(ors)
|
|
567
|
+
* })
|
|
568
|
+
* }
|
|
569
|
+
*
|
|
524
570
|
* const persons = await query.execute()
|
|
525
571
|
* ```
|
|
526
572
|
*
|
|
@@ -585,10 +631,10 @@ export declare class OnConflictUpdateBuilder<DB, TB extends keyof DB> implements
|
|
|
585
631
|
* const persons = await db
|
|
586
632
|
* .selectFrom('person')
|
|
587
633
|
* .selectAll('person')
|
|
588
|
-
* .where(({
|
|
634
|
+
* .where(({ eb, or, and, not, exists, selectFrom }) => and([
|
|
589
635
|
* or([
|
|
590
|
-
*
|
|
591
|
-
*
|
|
636
|
+
* eb('first_name', '=', firstName),
|
|
637
|
+
* eb('age', '<', maxAge)
|
|
592
638
|
* ]),
|
|
593
639
|
* not(exists(
|
|
594
640
|
* selectFrom('pet')
|
|
@@ -98,22 +98,24 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
98
98
|
*
|
|
99
99
|
* <!-- siteExample("where", "OR where", 30) -->
|
|
100
100
|
*
|
|
101
|
-
* To combine conditions using `OR`, you can use the expression builder
|
|
101
|
+
* To combine conditions using `OR`, you can use the expression builder.
|
|
102
|
+
* There are two ways to create `OR` expressions. Both are shown in this
|
|
103
|
+
* example:
|
|
102
104
|
*
|
|
103
105
|
* ```ts
|
|
104
106
|
* const persons = await db
|
|
105
107
|
* .selectFrom('person')
|
|
106
108
|
* .selectAll()
|
|
107
|
-
* .
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* ]),
|
|
112
|
-
* and([
|
|
113
|
-
* cmpr('first_name', '=', 'Sylvester'),
|
|
114
|
-
* cmpr('last_name', '=', 'Stallone')
|
|
115
|
-
* ])
|
|
109
|
+
* // 1. Using the `or` method on the expression builder:
|
|
110
|
+
* .where((eb) => eb.or([
|
|
111
|
+
* eb('first_name', '=', 'Jennifer'),
|
|
112
|
+
* eb('first_name', '=', 'Sylvester')
|
|
116
113
|
* ]))
|
|
114
|
+
* // 2. Chaining expressions using the `or` method on the
|
|
115
|
+
* // created expressions:
|
|
116
|
+
* .where((eb) =>
|
|
117
|
+
* eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone')
|
|
118
|
+
* )
|
|
117
119
|
* .execute()
|
|
118
120
|
* ```
|
|
119
121
|
*
|
|
@@ -123,9 +125,9 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
123
125
|
* select *
|
|
124
126
|
* from "person"
|
|
125
127
|
* where (
|
|
126
|
-
* ("first_name" = $1
|
|
127
|
-
*
|
|
128
|
-
* ("
|
|
128
|
+
* ("first_name" = $1 or "first_name" = $2)
|
|
129
|
+
* and
|
|
130
|
+
* ("last_name" = $3 or "last_name" = $4)
|
|
129
131
|
* )
|
|
130
132
|
* ```
|
|
131
133
|
*
|
|
@@ -134,8 +136,12 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
134
136
|
* You can add expressions conditionally like this:
|
|
135
137
|
*
|
|
136
138
|
* ```ts
|
|
139
|
+
* import { Expression, SqlBool } from 'kysely'
|
|
140
|
+
*
|
|
137
141
|
* const firstName: string | undefined = 'Jennifer'
|
|
138
142
|
* const lastName: string | undefined = 'Aniston'
|
|
143
|
+
* const under18 = true
|
|
144
|
+
* const over60 = true
|
|
139
145
|
*
|
|
140
146
|
* let query = db
|
|
141
147
|
* .selectFrom('person')
|
|
@@ -151,6 +157,23 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
151
157
|
* query = query.where('last_name', '=', lastName)
|
|
152
158
|
* }
|
|
153
159
|
*
|
|
160
|
+
* if (under18 || over60) {
|
|
161
|
+
* // Conditional OR expressions can be added like this.
|
|
162
|
+
* query = query.where((eb) => {
|
|
163
|
+
* const ors: Expression<SqlBool>[] = []
|
|
164
|
+
*
|
|
165
|
+
* if (under18) {
|
|
166
|
+
* ors.push(eb('age', '<', 18))
|
|
167
|
+
* }
|
|
168
|
+
*
|
|
169
|
+
* if (over60) {
|
|
170
|
+
* ors.push(eb('age', '>', 60))
|
|
171
|
+
* }
|
|
172
|
+
*
|
|
173
|
+
* return eb.or(ors)
|
|
174
|
+
* })
|
|
175
|
+
* }
|
|
176
|
+
*
|
|
154
177
|
* const persons = await query.execute()
|
|
155
178
|
* ```
|
|
156
179
|
*
|
|
@@ -215,10 +238,10 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
215
238
|
* const persons = await db
|
|
216
239
|
* .selectFrom('person')
|
|
217
240
|
* .selectAll('person')
|
|
218
|
-
* .where(({
|
|
241
|
+
* .where(({ eb, or, and, not, exists, selectFrom }) => and([
|
|
219
242
|
* or([
|
|
220
|
-
*
|
|
221
|
-
*
|
|
243
|
+
* eb('first_name', '=', firstName),
|
|
244
|
+
* eb('age', '<', maxAge)
|
|
222
245
|
* ]),
|
|
223
246
|
* not(exists(
|
|
224
247
|
* selectFrom('pet')
|
|
@@ -467,7 +490,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
467
490
|
* import { sql } from 'kysely'
|
|
468
491
|
*
|
|
469
492
|
* const persons = await db.selectFrom('person')
|
|
470
|
-
* .select(({ selectFrom, or
|
|
493
|
+
* .select(({ eb, selectFrom, or }) => [
|
|
471
494
|
* // Select a correlated subquery
|
|
472
495
|
* selectFrom('pet')
|
|
473
496
|
* .whereRef('person.id', '=', 'pet.owner_id')
|
|
@@ -479,8 +502,8 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
479
502
|
* // Build and select an expression using
|
|
480
503
|
* // the expression builder
|
|
481
504
|
* or([
|
|
482
|
-
*
|
|
483
|
-
*
|
|
505
|
+
* eb('first_name', '=', 'Jennifer'),
|
|
506
|
+
* eb('first_name', '=', 'Arnold')
|
|
484
507
|
* ]).as('is_jennifer_or_arnold'),
|
|
485
508
|
*
|
|
486
509
|
* // Select a raw sql expression
|
|
@@ -717,7 +740,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
717
740
|
* ```
|
|
718
741
|
*/
|
|
719
742
|
selectAll<T extends TB>(table: ReadonlyArray<T>): SelectQueryBuilder<DB, TB, O & AllSelection<DB, T>>;
|
|
720
|
-
selectAll<T extends TB>(table: T): SelectQueryBuilder<DB, TB, Selectable<DB[T]>>;
|
|
743
|
+
selectAll<T extends TB>(table: T): SelectQueryBuilder<DB, TB, O & Selectable<DB[T]>>;
|
|
721
744
|
selectAll(): SelectQueryBuilder<DB, TB, O & AllSelection<DB, TB>>;
|
|
722
745
|
/**
|
|
723
746
|
* Joins another table to the query using an inner join.
|
|
@@ -1414,7 +1437,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1414
1437
|
* )
|
|
1415
1438
|
* ```
|
|
1416
1439
|
*/
|
|
1417
|
-
$if<O2>(condition: boolean, func: (qb: this) => SelectQueryBuilder<
|
|
1440
|
+
$if<O2>(condition: boolean, func: (qb: this) => SelectQueryBuilder<any, any, O & O2>): SelectQueryBuilder<DB, TB, O & Partial<O2>>;
|
|
1418
1441
|
/**
|
|
1419
1442
|
* Change the output type of the query.
|
|
1420
1443
|
*
|
|
@@ -157,7 +157,7 @@ export class SelectQueryBuilder {
|
|
|
157
157
|
* import { sql } from 'kysely'
|
|
158
158
|
*
|
|
159
159
|
* const persons = await db.selectFrom('person')
|
|
160
|
-
* .select(({ selectFrom, or
|
|
160
|
+
* .select(({ eb, selectFrom, or }) => [
|
|
161
161
|
* // Select a correlated subquery
|
|
162
162
|
* selectFrom('pet')
|
|
163
163
|
* .whereRef('person.id', '=', 'pet.owner_id')
|
|
@@ -169,8 +169,8 @@ export class SelectQueryBuilder {
|
|
|
169
169
|
* // Build and select an expression using
|
|
170
170
|
* // the expression builder
|
|
171
171
|
* or([
|
|
172
|
-
*
|
|
173
|
-
*
|
|
172
|
+
* eb('first_name', '=', 'Jennifer'),
|
|
173
|
+
* eb('first_name', '=', 'Arnold')
|
|
174
174
|
* ]).as('is_jennifer_or_arnold'),
|
|
175
175
|
*
|
|
176
176
|
* // Select a raw sql expression
|