forked from knex/documentation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
617 lines (584 loc) · 347 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="http://knexjs.org" />
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png" />
<link rel="icon" type="image/png" href="assets/favicons/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="assets/favicons/favicon-16x16.png" sizes="16x16" />
<link rel="manifest" href="assets/favicons/manifest.json" />
<link rel="mask-icon" href="assets/favicons/safari-pinned-tab.svg" color="#e16426" />
<meta name="theme-color" content="#ffffff" />
<link rel="stylesheet" href="build/bundle.css" />
<title>Knex.js - A SQL Query Builder for Javascript</title>
</head>
<body>
<div id="documentation"><div data-reactroot="" data-reactid="1" data-react-checksum="-1603197132"><div id="sidebar" class="interface" data-reactid="2"><a class="toc_title" href="#changelog" data-reactid="3"><!-- react-text: 4 -->Knex.js <!-- /react-text --><span class="version" data-reactid="5"><!-- react-text: 6 -->(<!-- /react-text --><!-- react-text: 7 -->0.12.9<!-- /react-text --><!-- react-text: 8 -->)<!-- /react-text --></span></a><ul class="toc_section" data-reactid="9"><li data-reactid="10"><!-- react-text: 11 -->» <!-- /react-text --><a href="https://github.com/tgriesser/knex" data-reactid="12">GitHub Repository</a></li><li data-reactid="13"><!-- react-text: 14 -->» <!-- /react-text --><a href="#support" data-reactid="15">Support</a></li><li data-reactid="16"><!-- react-text: 17 -->» <!-- /react-text --><a href="#faq" data-reactid="18">FAQ</a></li><li data-reactid="19"><!-- react-text: 20 -->» <!-- /react-text --><a href="#changelog" data-reactid="21">Change Log</a></li></ul><a class="toc_title" href="#Upgrading" data-reactid="22">Upgrading</a><ul class="toc_section" data-reactid="23"><li data-reactid="24"><!-- react-text: 25 -->– <!-- /react-text --><a href="#Upgrading-from0.11" data-reactid="26">0.11.x -> 0.12</a></li></ul><a class="toc_title" href="#Installation" data-reactid="27">Installation</a><ul class="toc_section" data-reactid="28"><li data-reactid="29"><!-- react-text: 30 -->– <!-- /react-text --><a href="#Installation-node" data-reactid="31">Node.js</a></li><li data-reactid="32"><!-- react-text: 33 -->– <!-- /react-text --><a href="#Installation-browser" data-reactid="34">Browser</a></li><li data-reactid="35"><b data-reactid="36"><a href="#Installation-client" data-reactid="37">Config Options:</a></b></li><li data-reactid="38"><!-- react-text: 39 --> – <!-- /react-text --><a href="#Installation-client" data-reactid="40">client</a></li><li data-reactid="41"><!-- react-text: 42 --> – <!-- /react-text --><a href="#Installation-debug" data-reactid="43">debug</a></li><li data-reactid="44"><!-- react-text: 45 --> – <!-- /react-text --><a href="#Installation-pooling" data-reactid="46">pooling</a></li><li data-reactid="47"><!-- react-text: 48 --> – <!-- /react-text --><a href="#Installation-acquireConnectionTimeout" data-reactid="49">acquireConnectionTimeout</a></li><li data-reactid="50"><!-- react-text: 51 --> – <!-- /react-text --><a href="#Installation-migrations" data-reactid="52">migrations</a></li></ul><a class="toc_title" href="#Builder" data-reactid="53">Query Builder</a><ul class="toc_section" data-reactid="54"><li data-reactid="55"><!-- react-text: 56 -->– <!-- /react-text --><a href="#Builder-knex" data-reactid="57"><b data-reactid="58">constructor</b></a></li><li data-reactid="59"><!-- react-text: 60 -->– <!-- /react-text --><a href="#Builder-timeout" data-reactid="61">timeout</a></li><li data-reactid="62"><!-- react-text: 63 -->– <!-- /react-text --><a href="#Builder-select" data-reactid="64">select</a></li><li data-reactid="65"><!-- react-text: 66 -->– <!-- /react-text --><a href="#Builder-as" data-reactid="67">as</a></li><li data-reactid="68"><!-- react-text: 69 -->– <!-- /react-text --><a href="#Builder-column" data-reactid="70">column</a></li><li data-reactid="71"><!-- react-text: 72 -->– <!-- /react-text --><a href="#Builder-from" data-reactid="73">from</a></li><li data-reactid="74"><!-- react-text: 75 -->– <!-- /react-text --><a href="#Builder-with" data-reactid="76">with</a></li><li data-reactid="77"><!-- react-text: 78 -->– <!-- /react-text --><a href="#Builder-withSchema" data-reactid="79">withSchema</a></li><li data-reactid="80"><b data-reactid="81"><a href="#Builder-wheres" data-reactid="82">Where Methods:</a></b></li><li data-reactid="83"><!-- react-text: 84 --> – <!-- /react-text --><a href="#Builder-where" data-reactid="85">where</a></li><li data-reactid="86"><!-- react-text: 87 --> – <!-- /react-text --><a href="#Builder-whereNot" data-reactid="88">whereNot</a></li><li data-reactid="89"><!-- react-text: 90 --> – <!-- /react-text --><a href="#Builder-whereIn" data-reactid="91">whereIn</a></li><li data-reactid="92"><!-- react-text: 93 --> – <!-- /react-text --><a href="#Builder-whereNotIn" data-reactid="94">whereNotIn</a></li><li data-reactid="95"><!-- react-text: 96 --> – <!-- /react-text --><a href="#Builder-whereNull" data-reactid="97">whereNull</a></li><li data-reactid="98"><!-- react-text: 99 --> – <!-- /react-text --><a href="#Builder-whereNotNull" data-reactid="100">whereNotNull</a></li><li data-reactid="101"><!-- react-text: 102 --> – <!-- /react-text --><a href="#Builder-whereExists" data-reactid="103">whereExists</a></li><li data-reactid="104"><!-- react-text: 105 --> – <!-- /react-text --><a href="#Builder-whereNotExists" data-reactid="106">whereNotExists</a></li><li data-reactid="107"><!-- react-text: 108 --> – <!-- /react-text --><a href="#Builder-whereBetween" data-reactid="109">whereBetween</a></li><li data-reactid="110"><!-- react-text: 111 --> – <!-- /react-text --><a href="#Builder-whereNotBetween" data-reactid="112">whereNotBetween</a></li><li data-reactid="113"><!-- react-text: 114 --> – <!-- /react-text --><a href="#Builder-whereRaw" data-reactid="115">whereRaw</a></li><li data-reactid="116"><b data-reactid="117"><a href="#Builder-join" data-reactid="118">Join Methods:</a></b></li><li data-reactid="119"><!-- react-text: 120 --> – <!-- /react-text --><a href="#Builder-innerJoin" data-reactid="121">innerJoin</a></li><li data-reactid="122"><!-- react-text: 123 --> – <!-- /react-text --><a href="#Builder-leftJoin" data-reactid="124">leftJoin</a></li><li data-reactid="125"><!-- react-text: 126 --> – <!-- /react-text --><a href="#Builder-leftOuterJoin" data-reactid="127">leftOuterJoin</a></li><li data-reactid="128"><!-- react-text: 129 --> – <!-- /react-text --><a href="#Builder-rightJoin" data-reactid="130">rightJoin</a></li><li data-reactid="131"><!-- react-text: 132 --> – <!-- /react-text --><a href="#Builder-rightOuterJoin" data-reactid="133">rightOuterJoin</a></li><li data-reactid="134"><!-- react-text: 135 --> – <!-- /react-text --><a href="#Builder-outerJoin" data-reactid="136">outerJoin</a></li><li data-reactid="137"><!-- react-text: 138 --> – <!-- /react-text --><a href="#Builder-fullOuterJoin" data-reactid="139">fullOuterJoin</a></li><li data-reactid="140"><!-- react-text: 141 --> – <!-- /react-text --><a href="#Builder-crossJoin" data-reactid="142">crossJoin</a></li><li data-reactid="143"><!-- react-text: 144 --> – <!-- /react-text --><a href="#Builder-joinRaw" data-reactid="145">joinRaw</a></li><li data-reactid="146"><b data-reactid="147"><a href="#Builder-havings" data-reactid="148">Having Methods:</a></b></li><li data-reactid="149"><!-- react-text: 150 --> – <!-- /react-text --><a href="#Builder-having" data-reactid="151">having</a></li><li data-reactid="152"><!-- react-text: 153 --> – <!-- /react-text --><a href="#Builder-havingIn" data-reactid="154">havingIn</a></li><li data-reactid="155"><!-- react-text: 156 --> – <!-- /react-text --><a href="#Builder-havingNotIn" data-reactid="157">havingNotIn</a></li><li data-reactid="158"><!-- react-text: 159 --> – <!-- /react-text --><a href="#Builder-havingNull" data-reactid="160">havingNull</a></li><li data-reactid="161"><!-- react-text: 162 --> – <!-- /react-text --><a href="#Builder-havingNotNull" data-reactid="163">havingNotNull</a></li><li data-reactid="164"><!-- react-text: 165 --> – <!-- /react-text --><a href="#Builder-havingExists" data-reactid="166">havingExists</a></li><li data-reactid="167"><!-- react-text: 168 --> – <!-- /react-text --><a href="#Builder-havingNotExists" data-reactid="169">havingNotExists</a></li><li data-reactid="170"><!-- react-text: 171 --> – <!-- /react-text --><a href="#Builder-havingBetween" data-reactid="172">havingBetween</a></li><li data-reactid="173"><!-- react-text: 174 --> – <!-- /react-text --><a href="#Builder-havingNotBetween" data-reactid="175">havingNotBetween</a></li><li data-reactid="176"><!-- react-text: 177 --> – <!-- /react-text --><a href="#Builder-havingRaw" data-reactid="178">havingRaw</a></li><li data-reactid="179"><b data-reactid="180"><a href="#Builder-on" data-reactid="181">On Methods:</a></b></li><li data-reactid="182"><!-- react-text: 183 --> – <!-- /react-text --><a href="#Builder-onIn" data-reactid="184">onIn</a></li><li data-reactid="185"><!-- react-text: 186 --> – <!-- /react-text --><a href="#Builder-onNotIn" data-reactid="187">onNotIn</a></li><li data-reactid="188"><!-- react-text: 189 --> – <!-- /react-text --><a href="#Builder-onNull" data-reactid="190">onNull</a></li><li data-reactid="191"><!-- react-text: 192 --> – <!-- /react-text --><a href="#Builder-onNotNull" data-reactid="193">onNotNull</a></li><li data-reactid="194"><!-- react-text: 195 --> – <!-- /react-text --><a href="#Builder-onExists" data-reactid="196">onExists</a></li><li data-reactid="197"><!-- react-text: 198 --> – <!-- /react-text --><a href="#Builder-onNotExists" data-reactid="199">onNotExists</a></li><li data-reactid="200"><!-- react-text: 201 --> – <!-- /react-text --><a href="#Builder-onBetween" data-reactid="202">onBetween</a></li><li data-reactid="203"><!-- react-text: 204 --> – <!-- /react-text --><a href="#Builder-onNotBetween" data-reactid="205">onNotBetween</a></li><li data-reactid="206"><b data-reactid="207"><a href="#Builder-clear" data-reactid="208">Clear Methods:</a></b></li><li data-reactid="209"><!-- react-text: 210 --> – <!-- /react-text --><a href="#Builder-clearSelect" data-reactid="211">clearSelect</a></li><li data-reactid="212"><!-- react-text: 213 --> – <!-- /react-text --><a href="#Builder-clearWhere" data-reactid="214">clearWhere</a></li><li data-reactid="215"><!-- react-text: 216 -->– <!-- /react-text --><a href="#Builder-distinct" data-reactid="217">distinct</a></li><li data-reactid="218"><!-- react-text: 219 -->– <!-- /react-text --><a href="#Builder-groupBy" data-reactid="220">groupBy</a></li><li data-reactid="221"><!-- react-text: 222 -->– <!-- /react-text --><a href="#Builder-groupByRaw" data-reactid="223">groupByRaw</a></li><li data-reactid="224"><!-- react-text: 225 -->– <!-- /react-text --><a href="#Builder-orderBy" data-reactid="226">orderBy</a></li><li data-reactid="227"><!-- react-text: 228 -->– <!-- /react-text --><a href="#Builder-orderByRaw" data-reactid="229">orderByRaw</a></li><li data-reactid="230"><!-- react-text: 231 -->– <!-- /react-text --><a href="#Builder-offset" data-reactid="232">offset</a></li><li data-reactid="233"><!-- react-text: 234 -->– <!-- /react-text --><a href="#Builder-limit" data-reactid="235">limit</a></li><li data-reactid="236"><!-- react-text: 237 -->– <!-- /react-text --><a href="#Builder-union" data-reactid="238">union</a></li><li data-reactid="239"><!-- react-text: 240 -->– <!-- /react-text --><a href="#Builder-unionAll" data-reactid="241">unionAll</a></li><li data-reactid="242"><!-- react-text: 243 -->– <!-- /react-text --><a href="#Builder-insert" data-reactid="244">insert</a></li><li data-reactid="245"><!-- react-text: 246 -->– <!-- /react-text --><a href="#Builder-returning" data-reactid="247">returning</a></li><li data-reactid="248"><!-- react-text: 249 -->– <!-- /react-text --><a href="#Builder-update" data-reactid="250">update</a></li><li data-reactid="251"><!-- react-text: 252 -->– <!-- /react-text --><a href="#Builder-del / delete" data-reactid="253">del / delete</a></li><li data-reactid="254"><!-- react-text: 255 -->– <!-- /react-text --><a href="#Builder-transacting" data-reactid="256">transacting</a></li><li data-reactid="257"><!-- react-text: 258 --> – <!-- /react-text --><a href="#Builder-forUpdate" data-reactid="259">forUpdate</a></li><li data-reactid="260"><!-- react-text: 261 --> – <!-- /react-text --><a href="#Builder-forShare" data-reactid="262">forShare</a></li><li data-reactid="263"><!-- react-text: 264 -->– <!-- /react-text --><a href="#Builder-count" data-reactid="265">count</a></li><li data-reactid="266"><!-- react-text: 267 -->– <!-- /react-text --><a href="#Builder-min" data-reactid="268">min</a></li><li data-reactid="269"><!-- react-text: 270 -->– <!-- /react-text --><a href="#Builder-max" data-reactid="271">max</a></li><li data-reactid="272"><!-- react-text: 273 -->– <!-- /react-text --><a href="#Builder-sum" data-reactid="274">sum</a></li><li data-reactid="275"><!-- react-text: 276 -->– <!-- /react-text --><a href="#Builder-avg" data-reactid="277">avg</a></li><li data-reactid="278"><!-- react-text: 279 -->– <!-- /react-text --><a href="#Builder-increment" data-reactid="280">increment</a></li><li data-reactid="281"><!-- react-text: 282 -->– <!-- /react-text --><a href="#Builder-decrement" data-reactid="283">decrement</a></li><li data-reactid="284"><!-- react-text: 285 -->– <!-- /react-text --><a href="#Builder-truncate" data-reactid="286">truncate</a></li><li data-reactid="287"><!-- react-text: 288 -->– <!-- /react-text --><a href="#Builder-pluck" data-reactid="289">pluck</a></li><li data-reactid="290"><!-- react-text: 291 -->– <!-- /react-text --><a href="#Builder-first" data-reactid="292">first</a></li><li data-reactid="293"><!-- react-text: 294 -->– <!-- /react-text --><a href="#Builder-clone" data-reactid="295">clone</a></li><li data-reactid="296"><!-- react-text: 297 -->– <!-- /react-text --><a href="#Builder-modify" data-reactid="298">modify</a></li><li data-reactid="299"><!-- react-text: 300 -->– <!-- /react-text --><a href="#Builder-columnInfo" data-reactid="301">columnInfo</a></li><li data-reactid="302"><!-- react-text: 303 -->– <!-- /react-text --><a href="#Builder-debug" data-reactid="304">debug</a></li><li data-reactid="305"><!-- react-text: 306 -->– <!-- /react-text --><a href="#Builder-connection" data-reactid="307">connection</a></li><li data-reactid="308"><!-- react-text: 309 -->– <!-- /react-text --><a href="#Builder-options" data-reactid="310">options</a></li></ul><a class="toc_title" href="#Transactions" data-reactid="311">Transactions</a><ul class="toc_section" data-reactid="312"><li data-reactid="313"><!-- react-text: 314 -->– <!-- /react-text --><a href="#Transactions" data-reactid="315"><b data-reactid="316">overview</b></a></li></ul><a class="toc_title" href="#Schema" data-reactid="317">Schema Builder</a><ul class="toc_section" data-reactid="318"><li data-reactid="319"><!-- react-text: 320 -->– <!-- /react-text --><a href="#Schema-with" data-reactid="321">with</a></li><li data-reactid="322"><!-- react-text: 323 -->– <!-- /react-text --><a href="#Schema-withSchema" data-reactid="324">withSchema</a></li><li data-reactid="325"><!-- react-text: 326 -->– <!-- /react-text --><a href="#Schema-createTable" data-reactid="327">createTable</a></li><li data-reactid="328"><!-- react-text: 329 -->– <!-- /react-text --><a href="#Schema-createTableIfNotExists" data-reactid="330">createTableIfNotExists</a></li><li data-reactid="331"><!-- react-text: 332 -->– <!-- /react-text --><a href="#Schema-renameTable" data-reactid="333">renameTable</a></li><li data-reactid="334"><!-- react-text: 335 -->– <!-- /react-text --><a href="#Schema-dropTable" data-reactid="336">dropTable</a></li><li data-reactid="337"><!-- react-text: 338 -->– <!-- /react-text --><a href="#Schema-hasColumn" data-reactid="339">hasColumn</a></li><li data-reactid="340"><!-- react-text: 341 -->– <!-- /react-text --><a href="#Schema-hasTable" data-reactid="342">hasTable</a></li><li data-reactid="343"><!-- react-text: 344 -->– <!-- /react-text --><a href="#Schema-dropTableIfExists" data-reactid="345">dropTableIfExists</a></li><li data-reactid="346"><!-- react-text: 347 -->– <!-- /react-text --><a href="#Schema-table" data-reactid="348">table</a></li><li data-reactid="349"><!-- react-text: 350 -->– <!-- /react-text --><a href="#Schema-raw" data-reactid="351">raw</a></li><li data-reactid="352"><b data-reactid="353"><a href="#Schema-Building" data-reactid="354">Schema Building:</a></b></li><li data-reactid="355"><!-- react-text: 356 -->– <!-- /react-text --><a href="#Schema-dropColumn" data-reactid="357">dropColumn</a></li><li data-reactid="358"><!-- react-text: 359 -->– <!-- /react-text --><a href="#Schema-dropColumns" data-reactid="360">dropColumns</a></li><li data-reactid="361"><!-- react-text: 362 -->– <!-- /react-text --><a href="#Schema-renameColumn" data-reactid="363">renameColumn</a></li><li data-reactid="364"><!-- react-text: 365 -->– <!-- /react-text --><a href="#Schema-increments" data-reactid="366">increments</a></li><li data-reactid="367"><!-- react-text: 368 -->– <!-- /react-text --><a href="#Schema-integer" data-reactid="369">integer</a></li><li data-reactid="370"><!-- react-text: 371 -->– <!-- /react-text --><a href="#Schema-bigInteger" data-reactid="372">bigInteger</a></li><li data-reactid="373"><!-- react-text: 374 -->– <!-- /react-text --><a href="#Schema-text" data-reactid="375">text</a></li><li data-reactid="376"><!-- react-text: 377 -->– <!-- /react-text --><a href="#Schema-string" data-reactid="378">string</a></li><li data-reactid="379"><!-- react-text: 380 -->– <!-- /react-text --><a href="#Schema-float" data-reactid="381">float</a></li><li data-reactid="382"><!-- react-text: 383 -->– <!-- /react-text --><a href="#Schema-decimal" data-reactid="384">decimal</a></li><li data-reactid="385"><!-- react-text: 386 -->– <!-- /react-text --><a href="#Schema-boolean" data-reactid="387">boolean</a></li><li data-reactid="388"><!-- react-text: 389 -->– <!-- /react-text --><a href="#Schema-date" data-reactid="390">date</a></li><li data-reactid="391"><!-- react-text: 392 -->– <!-- /react-text --><a href="#Schema-dateTime" data-reactid="393">dateTime</a></li><li data-reactid="394"><!-- react-text: 395 -->– <!-- /react-text --><a href="#Schema-time" data-reactid="396">time</a></li><li data-reactid="397"><!-- react-text: 398 -->– <!-- /react-text --><a href="#Schema-timestamp" data-reactid="399">timestamp</a></li><li data-reactid="400"><!-- react-text: 401 -->– <!-- /react-text --><a href="#Schema-timestamps" data-reactid="402">timestamps</a></li><li data-reactid="403"><!-- react-text: 404 -->– <!-- /react-text --><a href="#Schema-dropTimestamps" data-reactid="405">dropTimestamps</a></li><li data-reactid="406"><!-- react-text: 407 -->– <!-- /react-text --><a href="#Schema-binary" data-reactid="408">binary</a></li><li data-reactid="409"><!-- react-text: 410 -->– <!-- /react-text --><a href="#Schema-enum" data-reactid="411">enum / enu</a></li><li data-reactid="412"><!-- react-text: 413 -->– <!-- /react-text --><a href="#Schema-json" data-reactid="414">json</a></li><li data-reactid="415"><!-- react-text: 416 -->– <!-- /react-text --><a href="#Schema-jsonb" data-reactid="417">jsonb</a></li><li data-reactid="418"><!-- react-text: 419 -->– <!-- /react-text --><a href="#Schema-uuid" data-reactid="420">uuid</a></li><li data-reactid="421"><!-- react-text: 422 -->– <!-- /react-text --><a href="#Schema-comment" data-reactid="423">comment</a></li><li data-reactid="424"><!-- react-text: 425 -->– <!-- /react-text --><a href="#Schema-engine" data-reactid="426">engine</a></li><li data-reactid="427"><!-- react-text: 428 -->– <!-- /react-text --><a href="#Schema-charset" data-reactid="429">charset</a></li><li data-reactid="430"><!-- react-text: 431 -->– <!-- /react-text --><a href="#Schema-collate" data-reactid="432">collate</a></li><li data-reactid="433"><!-- react-text: 434 -->– <!-- /react-text --><a href="#Schema-inherits" data-reactid="435">inherits</a></li><li data-reactid="436"><!-- react-text: 437 -->– <!-- /react-text --><a href="#Schema-specificType" data-reactid="438">specificType</a></li><li data-reactid="439"><!-- react-text: 440 -->– <!-- /react-text --><a href="#Schema-index" data-reactid="441">index</a></li><li data-reactid="442"><!-- react-text: 443 -->– <!-- /react-text --><a href="#Schema-dropIndex" data-reactid="444">dropIndex</a></li><li data-reactid="445"><!-- react-text: 446 -->– <!-- /react-text --><a href="#Schema-unique" data-reactid="447">unique</a></li><li data-reactid="448"><!-- react-text: 449 -->– <!-- /react-text --><a href="#Schema-foreign" data-reactid="450">foreign</a></li><li data-reactid="451"><!-- react-text: 452 -->– <!-- /react-text --><a href="#Schema-dropForeign" data-reactid="453">dropForeign</a></li><li data-reactid="454"><!-- react-text: 455 -->– <!-- /react-text --><a href="#Schema-dropUnique" data-reactid="456">dropUnique</a></li><li data-reactid="457"><!-- react-text: 458 -->– <!-- /react-text --><a href="#Schema-dropPrimary" data-reactid="459">dropPrimary</a></li><li data-reactid="460"><a href="#Chainable" data-reactid="461"><b data-reactid="462">Chainable:</b></a></li><li data-reactid="463"><!-- react-text: 464 -->– <!-- /react-text --><a href="#Schema-alter" data-reactid="465">alter</a></li><li data-reactid="466"><!-- react-text: 467 -->– <!-- /react-text --><a href="#Schema-index" data-reactid="468">index</a></li><li data-reactid="469"><!-- react-text: 470 -->– <!-- /react-text --><a href="#Schema-primary" data-reactid="471">primary</a></li><li data-reactid="472"><!-- react-text: 473 -->– <!-- /react-text --><a href="#Schema-unique" data-reactid="474">unique</a></li><li data-reactid="475"><!-- react-text: 476 -->– <!-- /react-text --><a href="#Schema-references" data-reactid="477">references</a></li><li data-reactid="478"><!-- react-text: 479 -->– <!-- /react-text --><a href="#Schema-inTable" data-reactid="480">inTable</a></li><li data-reactid="481"><!-- react-text: 482 -->– <!-- /react-text --><a href="#Schema-onDelete" data-reactid="483">onDelete</a></li><li data-reactid="484"><!-- react-text: 485 -->– <!-- /react-text --><a href="#Schema-onUpdate" data-reactid="486">onUpdate</a></li><li data-reactid="487"><!-- react-text: 488 -->– <!-- /react-text --><a href="#Schema-defaultTo" data-reactid="489">defaultTo</a></li><li data-reactid="490"><!-- react-text: 491 -->– <!-- /react-text --><a href="#Schema-unsigned" data-reactid="492">unsigned</a></li><li data-reactid="493"><!-- react-text: 494 -->– <!-- /react-text --><a href="#Schema-notNullable" data-reactid="495">notNullable</a></li><li data-reactid="496"><!-- react-text: 497 -->– <!-- /react-text --><a href="#Schema-nullable" data-reactid="498">nullable</a></li><li data-reactid="499"><!-- react-text: 500 -->– <!-- /react-text --><a href="#Schema-first" data-reactid="501">first</a></li><li data-reactid="502"><!-- react-text: 503 -->– <!-- /react-text --><a href="#Schema-after" data-reactid="504">after</a></li><li data-reactid="505"><!-- react-text: 506 -->– <!-- /react-text --><a href="#Schema-comment" data-reactid="507">comment</a></li><li data-reactid="508"><!-- react-text: 509 -->– <!-- /react-text --><a href="#Schema-collate" data-reactid="510">collate</a></li></ul><a class="toc_title" href="#Raw" data-reactid="511">Raw</a><ul class="toc_section" data-reactid="512"><li data-reactid="513"><!-- react-text: 514 -->– <!-- /react-text --><a href="#Raw-Bindings" data-reactid="515">Raw Parameter Binding</a></li><li data-reactid="516"><!-- react-text: 517 -->– <!-- /react-text --><a href="#Raw-Expressions" data-reactid="518">Raw Expressions</a></li><li data-reactid="519"><!-- react-text: 520 -->– <!-- /react-text --><a href="#Raw-Queries" data-reactid="521">Raw Queries</a></li><li data-reactid="522"><!-- react-text: 523 -->- <!-- /react-text --><a href="#Raw-queries-wrapped" data-reactid="524">Wrapped Queries</a></li></ul><a class="toc_title" href="#Utility" data-reactid="525">Utility</a><ul class="toc_section" data-reactid="526"><li data-reactid="527"><!-- react-text: 528 -->- <!-- /react-text --><a href="#Utility-BatchInsert" data-reactid="529">Batch Insert</a></li></ul><a class="toc_title" href="#Interfaces" data-reactid="530">Interfaces</a><ul class="toc_section" data-reactid="531"><li data-reactid="532"><b data-reactid="533"><a href="#Interfaces-Promises" data-reactid="534">Promises</a></b></li><li data-reactid="535"><!-- react-text: 536 --> – <!-- /react-text --><a href="#Promises-then" data-reactid="537">then</a></li><li data-reactid="538"><!-- react-text: 539 --> – <!-- /react-text --><a href="#Promises-catch" data-reactid="540">catch</a></li><li data-reactid="541"><!-- react-text: 542 --> – <!-- /react-text --><a href="#Promises-tap" data-reactid="543">tap</a></li><li data-reactid="544"><!-- react-text: 545 --> – <!-- /react-text --><a href="#Promises-map" data-reactid="546">map</a></li><li data-reactid="547"><!-- react-text: 548 --> – <!-- /react-text --><a href="#Promises-reduce" data-reactid="549">reduce</a></li><li data-reactid="550"><!-- react-text: 551 --> – <!-- /react-text --><a href="#Promises-bind" data-reactid="552">bind</a></li><li data-reactid="553"><!-- react-text: 554 --> – <!-- /react-text --><a href="#Promises-return" data-reactid="555">return</a></li><li data-reactid="556"><b data-reactid="557"><a href="#Interfaces-Callbacks" data-reactid="558">Callbacks</a></b></li><li data-reactid="559"><!-- react-text: 560 --> – <!-- /react-text --><a href="#Interfaces-asCallback" data-reactid="561">asCallback</a></li><li data-reactid="562"><b data-reactid="563"><a href="#Interfaces-Streams" data-reactid="564">Streams</a></b></li><li data-reactid="565"><!-- react-text: 566 --> – <!-- /react-text --><a href="#Streams-stream" data-reactid="567">stream</a></li><li data-reactid="568"><!-- react-text: 569 --> – <!-- /react-text --><a href="#Streams-pipe" data-reactid="570">pipe</a></li><li data-reactid="571"><b data-reactid="572"><a href="#Interfaces-Events" data-reactid="573">Events</a></b></li><li data-reactid="574"><!-- react-text: 575 --> – <!-- /react-text --><a href="#Events-query" data-reactid="576">query</a></li><li data-reactid="577"><!-- react-text: 578 --> – <!-- /react-text --><a href="#Events-query-error" data-reactid="579">query-error</a></li><li data-reactid="580"><!-- react-text: 581 --> – <!-- /react-text --><a href="#Events-query-response" data-reactid="582">query-response</a></li><li data-reactid="583"><b data-reactid="584"><a href="#Interfaces-Other" data-reactid="585">Other:</a></b></li><li data-reactid="586"><!-- react-text: 587 --> – <!-- /react-text --><a href="#Other-toString" data-reactid="588">toString</a></li><li data-reactid="589"><!-- react-text: 590 --> – <!-- /react-text --><a href="#Other-toSQL" data-reactid="591">toSQL</a></li></ul><a class="toc_title" href="#Migrations" data-reactid="592">Migrations</a><ul class="toc_section" data-reactid="593"><li data-reactid="594"><a href="#Migrations-CLI" data-reactid="595"><b data-reactid="596">CLI</b></a></li><li data-reactid="597"><!-- react-text: 598 -->– <!-- /react-text --><a href="#Migrations-CLI" data-reactid="599">Migrations</a></li><li data-reactid="600"><!-- react-text: 601 -->– <!-- /react-text --><a href="#Seeds-CLI" data-reactid="602">Seed files</a></li><li data-reactid="603"><!-- react-text: 604 -->– <!-- /react-text --><a href="#knexfile" data-reactid="605">knexfile.js</a></li><li data-reactid="606"><a href="#Migrations-API" data-reactid="607"><b data-reactid="608">Migration API</b></a></li><li data-reactid="609"><!-- react-text: 610 -->– <!-- /react-text --><a href="#Migrations-make" data-reactid="611">make</a></li><li data-reactid="612"><!-- react-text: 613 -->– <!-- /react-text --><a href="#Migrations-latest" data-reactid="614">latest</a></li><li data-reactid="615"><!-- react-text: 616 -->– <!-- /react-text --><a href="#Migrations-rollback" data-reactid="617">rollback</a></li><li data-reactid="618"><!-- react-text: 619 -->– <!-- /react-text --><a href="#Migrations-currentVersion" data-reactid="620">currentVersion</a></li><li data-reactid="621"><a href="#Notes-about-locks" data-reactid="622"><b data-reactid="623">Notes about locks</b></a></li><li data-reactid="624"><a href="#Seeds-API" data-reactid="625"><b data-reactid="626">Seed API</b></a></li><li data-reactid="627"><!-- react-text: 628 -->– <!-- /react-text --><a href="#Seeds-make" data-reactid="629">make</a></li><li data-reactid="630"><!-- react-text: 631 -->– <!-- /react-text --><a href="#Seeds-run" data-reactid="632">run</a></li></ul><a class="toc_title" href="#support" data-reactid="633">Support</a><a class="toc_title" href="#faq" data-reactid="634">F.A.Q.</a><a class="toc_title" href="#changelog" data-reactid="635">Change Log</a></div><div class="language" data-reactid="636"><!-- react-text: 637 -->Show example query output as:<!-- /react-text --><br data-reactid="638"/><select data-reactid="639"><option selected="" value="mysql" data-reactid="640">MySQL / MariaDB</option><option value="pg" data-reactid="641">PostgreSQL</option><option value="sqlite3" data-reactid="642">SQLite3</option><option value="oracle" data-reactid="643">Oracle</option><option value="mssql" data-reactid="644">MSSQL</option></select></div><a href="https://github.com/tgriesser/knex" data-reactid="645"><img style="position:fixed;top:0;right:0;border:0;" src="assets/images/github.png" alt="Fork me on GitHub" data-reactid="646"/></a><div class="container" data-reactid="647"><div id="Prelude" data-reactid="648"><div data-reactid="649"><p data-reactid="650"><img height="108" id="logo" src="assets/images/knex.png" alt="Knex.js" /></p><p data-reactid="651"><strong>Knex.js</strong> is a "batteries included" SQL query builder for <strong>Postgres</strong>, <strong>MSSQL</strong>, <strong>MySQL</strong>, <strong>MariaDB</strong>, <strong>SQLite3</strong>, and <strong>Oracle</strong> designed to be flexible, portable, and fun to use. It features both traditional node style <a href="#Interfaces-Callbacks">callbacks</a> as well as a <a href="#Interfaces-Promises">promise</a> interface for cleaner async flow control, <a href="#Interfaces-Streams">a stream interface</a>, full featured <a href="#Builder">query</a> and <a href="#Schema">schema</a> builders, <a href="#Transactions"><strong>transaction support (with savepoints)</strong></a>, connection <a href="#Installation-pooling">pooling</a> and standardized responses between different query clients and dialects.</p><p data-reactid="652">The project is <a href="http://github.com/tgriesser/knex">hosted on GitHub</a>, and has a comprehensive <a href="https://travis-ci.org/tgriesser/knex">test suite</a>.</p><p data-reactid="653">Knex is available for use under the <a href="http://github.com/tgriesser/knex/blob/master/LICENSE">MIT software license</a>.</p><p data-reactid="654">You can report bugs and discuss features on the <a href="http://github.com/tgriesser/knex/issues">GitHub issues page</a>, add pages to the <a href="https://github.com/tgriesser/knex/wiki">wiki</a> or send tweets to <a href="http://twitter.com/tgriesser">@tgriesser</a>.</p><p data-reactid="655">Thanks to all of the great <a href="https://github.com/tgriesser/knex/graphs/contributors">contributions</a> to the project.</p></div><p class="info" data-reactid="656">Special thanks to <a href="https://twitter.com/taylorotwell">Taylor Otwell</a> and his work on the <a href="http://laravel.com/docs/queries">Laravel Query Builder</a>, from which much of the builder's code and syntax was originally derived.</p><h2 data-reactid="657">Latest Release: 0.12.9 - <span class="small"><a href="#changelog">Change Log</a></span></h2><p data-reactid="658">Current Develop — <a href="https://travis-ci.org/tgriesser/knex"><img src="https://travis-ci.org/tgriesser/knex.png?branch=master" alt="Travis Badge"></a></p></div><div id="Upgrading" data-reactid="659"><h2 id="Upgrading" data-reactid="660">Upgrading</h2><h3 id="Upgrading-from0.11" data-reactid="661">Upgrading 0.11 -> 0.12</h3><div data-reactid="662"><p data-reactid="663"><a href="https://github.com/coopernurse/node-pool">generic-pool</a> is swapped out for Pool2 for pooling management</p><p data-reactid="664">Documentation pull requests should be sent to <a href="https://github.com/knex/documentation">knex/documentation</a></p><p data-reactid="665">PG escaping should now be handled the same as it is by the driver</p></div></div><div id="Installation" data-reactid="666"><h2 id="Installation" data-reactid="667">Installation</h2><p data-reactid="668">Knex can be used as an SQL query builder in both Node.JS and the browser, limited to WebSQL's constraints (like the inability to drop tables or read schemas). Composing SQL queries in the browser for execution on the server is highly discouraged, as this can be the cause of serious security vulnerabilities. The browser builds outside of WebSQL are primarily for learning purposes - for example, you can pop open the console and build queries on this page using the <a href="javascript:alert(knex)">knex</a> object.</p><h3 id="Installation-node" data-reactid="669">Node.js</h3><p data-reactid="670">The primary target environment for Knex is Node.js, you will need to install the <code>knex</code> library, and then install the appropriate database library: <a href="https://github.com/brianc/node-postgres"><code>pg</code></a> for PostgreSQL, <a href="https://github.com/felixge/node-mysql"><code>mysql</code></a> for MySQL or MariaDB, <a href="https://github.com/mapbox/node-sqlite3"><code>sqlite3</code></a> for SQLite3, or <a href="https://github.com/patriksimek/node-mssql"><code>mssql</code></a> for MSSQL.</p><pre data-reactid="671"><code data-reactid="672">$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install mariasql
$ npm install strong-oracle
$ npm install oracle
$ npm install mssql</code></pre><h3 id="Installation-browser" data-reactid="673">Browser</h3><p data-reactid="674">Knex can be built using a JavaScript build tool such as <a href="http://browserify.org/">browserify</a> or <a href="https://github.com/webpack/webpack">webpack</a>. In fact, this documentation uses a webpack build which <a href="https://github.com/knex/documentation/blob/a4de1b2eb50d6699f126be8d134f3d1acc4fc69d/components/Container.jsx#L3">includes knex</a>. View source on this page to see the browser build in-action (the global <code>knex</code> variable).</p><h3 id="Installation-client" data-reactid="675">Initializing the Library</h3><p data-reactid="676">The <code>knex</code> module is itself a function which takes a configuration object for Knex, accepting a few parameters. The <code>client</code> parameter is required and determines which client adapter will be used with the library.</p><pre data-reactid="677"><code class="hljs js" data-reactid="678"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
}
});</code></pre><p data-reactid="679">The connection options are passed directly to the appropriate database client to create the connection, and may be either an object, or a connection string:</p><pre data-reactid="680"><code class="hljs js" data-reactid="681"><span class="hljs-keyword">var</span> pg = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: process.env.PG_CONNECTION_STRING,
<span class="hljs-attr">searchPath</span>: <span class="hljs-string">'knex,public'</span>
});</code></pre><p class="info" data-reactid="682">Note: When you use the SQLite3 adapter, there is a filename required, not a network connection. For example:</p><pre data-reactid="683"><code class="hljs js" data-reactid="684"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'sqlite3'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">filename</span>: <span class="hljs-string">"./mydb.sqlite"</span>
}
});</code></pre><p data-reactid="685">You can also connect via an unix domain socket, which will ignore host and port.</p><pre data-reactid="686"><code class="hljs js" data-reactid="687"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">socketPath</span> : <span class="hljs-string">'/path/to/socket.sock'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
}
});</code></pre><p class="info" data-reactid="688">Initializing the library should normally only ever happen once in your application, as it creates a connection pool for the current database, you should use the instance returned from the initialize call throughout your library.</p><p data-reactid="689">You can even use knex without a connection, just for its query building features. Just pass in an empty object when initializing the library. Specify a client if you are interested in a particular flavour of SQL.</p><pre data-reactid="690"><code class="hljs js" data-reactid="691"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({});
<span class="hljs-keyword">var</span> pg = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>});
knex(<span class="hljs-string">'table'</span>).insert({<span class="hljs-attr">a</span>: <span class="hljs-string">'b'</span>}).returning(<span class="hljs-string">'*'</span>).toString();
<span class="hljs-comment">// "insert into "table" ("a") values ('b')"</span>
pg(<span class="hljs-string">'table'</span>).insert({<span class="hljs-attr">a</span>: <span class="hljs-string">'b'</span>}).returning(<span class="hljs-string">'*'</span>).toString();
<span class="hljs-comment">// "insert into "table" ("a") values ('b') returning *"</span></code></pre><h3 id="Installation-debug" data-reactid="692">Debugging</h3><p data-reactid="693">Passing a <code>debug: true</code> flag on your initialization object will turn on <a href="#Builder-debug">debugging</a> for all queries.</p><h3 id="Installation-pooling" data-reactid="694">Pooling</h3><div data-reactid="695"><p data-reactid="696">The client created by the configuration initializes a connection pool, using the <a href="https://github.com/coopernurse/node-pool">generic-pool</a> library. This connection pool has a default setting of a <code>min: 2, max: 10</code> for the MySQL and PG libraries, and a single connection for sqlite3 (due to issues with utilizing multiple connections on a single file). To change the config settings for the pool, pass a <code>pool</code> option as one of the keys in the initialize block.</p><p data-reactid="697">Checkout the <a href="https://github.com/coopernurse/node-pool">generic-pool</a> library for more information.</p></div><pre data-reactid="698"><code class="hljs js" data-reactid="699"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
},
<span class="hljs-attr">pool</span>: { <span class="hljs-attr">min</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">max</span>: <span class="hljs-number">7</span> }
});</code></pre><p data-reactid="700">If you ever need to explicitly teardown the connection pool, you may use <code>knex.destroy([callback])</code>. You may use <code>knex.destroy</code> by passing a callback, or by chaining as a promise, just not both.</p><h3 id="Installation-pooling-afterCreate" data-reactid="701">afterCreate</h3><p data-reactid="702"><code>afterCreate</code> callback (rawDriverConnection, done) is called when the pool aquires a new connection from the database server. done(err, connection) callback must be called for <code>knex</code> to be able to decide if the connection is ok or if it should be discarded right away from the pool.</p><pre data-reactid="703"><code class="hljs js" data-reactid="704"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: {...},
<span class="hljs-attr">pool</span>: {
<span class="hljs-attr">afterCreate</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">conn, done</span>) </span>{
<span class="hljs-comment">// in this example we use pg driver's connection API</span>
conn.query(<span class="hljs-string">'SET timezone="UTC";'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-comment">// first query failed, return error and don't try to make next query</span>
done(err, conn);
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// do the second query...</span>
conn.query(<span class="hljs-string">'SELECT set_limit(0.01);'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
<span class="hljs-comment">// if err is not falsy, connection is discarded from pool</span>
<span class="hljs-comment">// if connection aquire was triggered by a query the error is passed to query promise</span>
done(err, conn);
});
}
});
}
}
});</code></pre><h3 id="Installation-acquireConnectionTimeout" data-reactid="705">acquireConnectionTimeout</h3><p data-reactid="706"><code>acquireConnectionTimeout</code> defaults to 60000ms and is used to determine how long knex should wait before throwing a timeout error when acquiring a connection is not possible. The most common cause for this is using up all the pool for transaction connections and then attempting to run queries outside of transactions while the pool is still full. The error thrown will provide information on the query the connection was for to simplify the job of locating the culprit.</p><pre data-reactid="707"><code class="hljs js" data-reactid="708"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: {...},
<span class="hljs-attr">pool</span>: {...},
<span class="hljs-attr">acquireConnectionTimeout</span>: <span class="hljs-number">10000</span>
});</code></pre><h3 id="Installation-migrations" data-reactid="709">Migrations</h3><p data-reactid="710">For convenience, the any migration configuration may be specified when initializing the library. Read the <a href="#Migrations">Migrations</a> section for more information and a full list of configuration options.</p><pre data-reactid="711"><code class="hljs js" data-reactid="712"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
},
<span class="hljs-attr">migrations</span>: {
<span class="hljs-attr">tableName</span>: <span class="hljs-string">'migrations'</span>
}
});</code></pre></div><div id="Builder" data-reactid="713"><h2 id="Builder" data-reactid="714">Knex Query Builder</h2><p data-reactid="715">The heart of the library, the knex query builder is the interface used for building and executing standard SQL queries, such as <code>select</code>, <code>insert</code>, <code>update</code>, <code>delete</code>.</p><div id="Builder-knex" data-reactid="716"><b data-reactid="717">knex</b><!-- react-text: 718 --> — <!-- /react-text --><code data-reactid="719">knex(tableName, options={only: boolean}) / knex.[methodName]</code><p data-reactid="720">The query builder starts off either by specifying a tableName you wish to query against, or by calling any method directly on the knex object. This kicks off a jQuery-like chain, with which you can call additional query builder methods as needed to construct the query, eventually calling any of the interface methods, to either convert toString, or execute the query with a promise, callback, or stream. Optional second argument for passing options:<em> <strong>only</strong>: if <code>true</code>, the ONLY keyword is used before the <code>tableName</code> to discard inheriting tables' data. <em>*NOTE:</em></em> only supported in PostgreSQL for now.</p></div><div id="Builder-timeout" data-reactid="721"><b data-reactid="722">timeout</b><!-- react-text: 723 --> — <!-- /react-text --><code data-reactid="724">.timeout(ms, options={cancel: boolean})</code><p data-reactid="725">Sets a timeout for the query and will throw a TimeoutError if the timeout is exceeded. The error contains information about the query, bindings, and the timeout that was set. Useful for complex queries that you want to make sure are not taking too long to execute. Optional second argument for passing options:<em> <strong>cancel</strong>: if <code>true</code>, cancel query if timeout is reached. <em>*NOTE:</em></em> only supported in MySQL and MariaDB for now.</p><pre class="display" data-reactid="726"><code class="js hljs" data-reactid="727">knex.select().from(<span class="hljs-string">'books'</span>).timeout(<span class="hljs-number">1000</span>)</code><br data-reactid="728"/><!-- react-text: 729 -->Outputs:<!-- /react-text --><br data-reactid="730"/><code class="sql hljs" data-reactid="731"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre><pre class="display" data-reactid="732"><code class="js hljs" data-reactid="733">knex.select().from(<span class="hljs-string">'books'</span>).timeout(<span class="hljs-number">1000</span>, {<span class="hljs-attr">cancel</span>: <span class="hljs-literal">true</span>}) <span class="hljs-comment">// MySQL and MariaDB only</span></code><br data-reactid="734"/><!-- react-text: 735 -->Outputs:<!-- /react-text --><br data-reactid="736"/><code class="sql hljs" data-reactid="737"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre></div><div id="Builder-select" data-reactid="738"><b data-reactid="739">select</b><!-- react-text: 740 --> — <!-- /react-text --><code data-reactid="741">.select([*columns])</code><p data-reactid="742">Creates a select query, taking an optional array of columns for the query, eventually defaulting to * if none are specified when the query is built. The response of a select call will resolve with an array of objects selected from the database.</p><pre class="display" data-reactid="743"><code class="js hljs" data-reactid="744">knex.select(<span class="hljs-string">'title'</span>, <span class="hljs-string">'author'</span>, <span class="hljs-string">'year'</span>).from(<span class="hljs-string">'books'</span>)</code><br data-reactid="745"/><!-- react-text: 746 -->Outputs:<!-- /react-text --><br data-reactid="747"/><code class="sql hljs" data-reactid="748"><span class="hljs-keyword">select</span> <span class="hljs-string">`title`</span>, <span class="hljs-string">`author`</span>, <span class="hljs-string">`year`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre><pre class="display" data-reactid="749"><code class="js hljs" data-reactid="750">knex.select().table(<span class="hljs-string">'books'</span>)</code><br data-reactid="751"/><!-- react-text: 752 -->Outputs:<!-- /react-text --><br data-reactid="753"/><code class="sql hljs" data-reactid="754"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre></div><div id="Builder-as" data-reactid="755"><b data-reactid="756">as</b><!-- react-text: 757 --> — <!-- /react-text --><code data-reactid="758">.as(name)</code><p data-reactid="759">Allows for aliasing a subquery, taking the string you wish to name the current query. If the query is not a sub-query, it will be ignored.</p><pre class="display" data-reactid="760"><code class="js hljs" data-reactid="761">knex.avg(<span class="hljs-string">'sum_column1'</span>).from(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.sum(<span class="hljs-string">'column1 as sum_column1'</span>).from(<span class="hljs-string">'t1'</span>).groupBy(<span class="hljs-string">'column1'</span>).as(<span class="hljs-string">'t1'</span>)
}).as(<span class="hljs-string">'ignored_alias'</span>)</code><br data-reactid="762"/><!-- react-text: 763 -->Outputs:<!-- /react-text --><br data-reactid="764"/><code class="sql hljs" data-reactid="765"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`sum_column1`</span>) <span class="hljs-keyword">from</span> (<span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-string">`column1`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`sum_column1`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`t1`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`column1`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`t1`</span></code></pre></div><div id="Builder-column" data-reactid="766"><b data-reactid="767">column</b><!-- react-text: 768 --> — <!-- /react-text --><code data-reactid="769">.column(columns)</code><p data-reactid="770">Specifically set the columns to be selected on a select query, taking an array or a list of of column names.</p><pre class="display" data-reactid="771"><code class="js hljs" data-reactid="772">knex.column(<span class="hljs-string">'title'</span>, <span class="hljs-string">'author'</span>, <span class="hljs-string">'year'</span>).select().from(<span class="hljs-string">'books'</span>)</code><br data-reactid="773"/><!-- react-text: 774 -->Outputs:<!-- /react-text --><br data-reactid="775"/><code class="sql hljs" data-reactid="776"><span class="hljs-keyword">select</span> <span class="hljs-string">`title`</span>, <span class="hljs-string">`author`</span>, <span class="hljs-string">`year`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre><pre class="display" data-reactid="777"><code class="js hljs" data-reactid="778">knex.column([<span class="hljs-string">'title'</span>, <span class="hljs-string">'author'</span>, <span class="hljs-string">'year'</span>]).select().from(<span class="hljs-string">'books'</span>)</code><br data-reactid="779"/><!-- react-text: 780 -->Outputs:<!-- /react-text --><br data-reactid="781"/><code class="sql hljs" data-reactid="782"><span class="hljs-keyword">select</span> <span class="hljs-string">`title`</span>, <span class="hljs-string">`author`</span>, <span class="hljs-string">`year`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre></div><div id="Builder-from" data-reactid="783"><b data-reactid="784">from</b><!-- react-text: 785 --> — <!-- /react-text --><code data-reactid="786">.from([tableName], options={only: boolean})</code><p data-reactid="787">Specifies the table used in the current query, replacing the current table name if one has already been specified. This is typically used in the sub-queries performed in the advanced where or union methods. Optional second argument for passing options:<em> <strong>only</strong>: if <code>true</code>, the ONLY keyword is used before the <code>tableName</code> to discard inheriting tables' data. <em>*NOTE:</em></em> only supported in PostgreSQL for now.</p><pre class="display" data-reactid="788"><code class="js hljs" data-reactid="789">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)</code><br data-reactid="790"/><!-- react-text: 791 -->Outputs:<!-- /react-text --><br data-reactid="792"/><code class="sql hljs" data-reactid="793"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-with" data-reactid="794"><b data-reactid="795">with</b><!-- react-text: 796 --> — <!-- /react-text --><code data-reactid="797">.with(alias, function|raw)</code><p data-reactid="798">Add a "with" clause to the query. "With" clauses are supported by PostgreSQL, Oracle, SQLite3 and MSSQL.</p><pre class="display" data-reactid="799"><code class="js hljs" data-reactid="800">knex.with(<span class="hljs-string">'with_alias'</span>, knex.raw(<span class="hljs-string">'select * from "books" where "author" = ?'</span>, <span class="hljs-string">'Test'</span>)).select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'with_alias'</span>)</code><br data-reactid="801"/><!-- react-text: 802 -->Outputs:<!-- /react-text --><br data-reactid="803"/><code class="sql hljs" data-reactid="804">with `with_alias` as (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">"books"</span> <span class="hljs-keyword">where</span> <span class="hljs-string">"author"</span> = <span class="hljs-string">'Test'</span>) <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`with_alias`</span></code></pre><pre class="display" data-reactid="805"><code class="js hljs" data-reactid="806">knex.with(<span class="hljs-string">'with_alias'</span>, (qb) => {
qb.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'books'</span>).where(<span class="hljs-string">'author'</span>, <span class="hljs-string">'Test'</span>)
}).select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'with_alias'</span>)</code><br data-reactid="807"/><!-- react-text: 808 -->Outputs:<!-- /react-text --><br data-reactid="809"/><code class="sql hljs" data-reactid="810">with `with_alias` as (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`author`</span> = <span class="hljs-string">'Test'</span>) <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`with_alias`</span></code></pre></div><div id="Builder-withSchema" data-reactid="811"><b data-reactid="812">withSchema</b><!-- react-text: 813 --> — <!-- /react-text --><code data-reactid="814">.withSchema([schemaName])</code><p data-reactid="815">Specifies the schema to be used as prefix of table name.</p><pre class="display" data-reactid="816"><code class="js hljs" data-reactid="817">knex.withSchema(<span class="hljs-string">'public'</span>).select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)</code><br data-reactid="818"/><!-- react-text: 819 -->Outputs:<!-- /react-text --><br data-reactid="820"/><code class="sql hljs" data-reactid="821"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`public`</span>.<span class="hljs-string">`users`</span></code></pre></div><h3 id="Builder-wheres" data-reactid="822">Where Clauses</h3><div data-reactid="823"><p data-reactid="824">Several methods exist to assist in dynamic where clauses. In many places functions may be used in place of values, constructing subqueries. In most places existing knex queries may be used to compose sub-queries, etc. Take a look at a few of the examples for each method for instruction on use:</p><p data-reactid="825"><strong>Important:</strong> Supplying knex with an <code>undefined</code> value to any of the <code>where</code> functions will cause knex to throw an error during sql compilation. This is both for yours and our sake. Knex cannot know what to do with undefined values in a where clause, and generally it would be a programmatic error to supply one to begin with. The error will throw a message containing the type of query and the compiled query-string. Example:</p></div><pre class="display" data-reactid="826"><code class="js hljs" data-reactid="827">knex(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'login'</span>, <span class="hljs-literal">undefined</span>)
.select()
.toSQL()</code><br data-reactid="828"/><!-- react-text: 829 -->Error:<!-- /react-text --><br data-reactid="830"/><code class="sql hljs" data-reactid="831">Undefined binding(s) detected when compiling SELECT query: select * from `accounts` where `login` = ?</code></pre><div id="Builder-where" data-reactid="832"><b data-reactid="833">where</b><!-- react-text: 834 --> — <!-- /react-text --><code data-reactid="835">.where(~mixed~)</code></div><p data-reactid="836">Object Syntax:</p><pre class="display" data-reactid="837"><code class="js hljs" data-reactid="838">knex(<span class="hljs-string">'users'</span>).where({
<span class="hljs-attr">first_name</span>: <span class="hljs-string">'Test'</span>,
<span class="hljs-attr">last_name</span>: <span class="hljs-string">'User'</span>
}).select(<span class="hljs-string">'id'</span>)</code><br data-reactid="839"/><!-- react-text: 840 -->Outputs:<!-- /react-text --><br data-reactid="841"/><code class="sql hljs" data-reactid="842"><span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`first_name`</span> = <span class="hljs-string">'Test'</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`last_name`</span> = <span class="hljs-string">'User'</span></code></pre><p data-reactid="843">Key, Value:</p><pre class="display" data-reactid="844"><code class="js hljs" data-reactid="845">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="846"/><!-- react-text: 847 -->Outputs:<!-- /react-text --><br data-reactid="848"/><code class="sql hljs" data-reactid="849"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre><p data-reactid="850">Grouped Chain:</p><pre class="display" data-reactid="851"><code class="js hljs" data-reactid="852">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).orWhere(<span class="hljs-string">'id'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">10</span>)
}).orWhere({<span class="hljs-attr">name</span>: <span class="hljs-string">'Tester'</span>})</code><br data-reactid="853"/><!-- react-text: 854 -->Outputs:<!-- /react-text --><br data-reactid="855"/><code class="sql hljs" data-reactid="856"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> (<span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`id`</span> > <span class="hljs-number">10</span>) <span class="hljs-keyword">or</span> (<span class="hljs-string">`name`</span> = <span class="hljs-string">'Tester'</span>)</code></pre><p data-reactid="857">Operator:</p><pre class="display" data-reactid="858"><code class="js hljs" data-reactid="859">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'columnName'</span>, <span class="hljs-string">'like'</span>, <span class="hljs-string">'%rowlikeme%'</span>)</code><br data-reactid="860"/><!-- react-text: 861 -->Outputs:<!-- /react-text --><br data-reactid="862"/><code class="sql hljs" data-reactid="863"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`columnName`</span> <span class="hljs-keyword">like</span> <span class="hljs-string">'%rowlikeme%'</span></code></pre><p data-reactid="864">The above query demonstrates the common use case of returning all users for which a specific pattern appears within a designated column.</p><pre class="display" data-reactid="865"><code class="js hljs" data-reactid="866">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">100</span>)</code><br data-reactid="867"/><!-- react-text: 868 -->Outputs:<!-- /react-text --><br data-reactid="869"/><code class="sql hljs" data-reactid="870"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> > <span class="hljs-number">100</span></code></pre><pre class="display" data-reactid="871"><code class="js hljs" data-reactid="872"><span class="hljs-keyword">var</span> subquery = knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">100</span>).andWhere(<span class="hljs-string">'status'</span>, <span class="hljs-string">'active'</span>).orWhere(<span class="hljs-string">'name'</span>, <span class="hljs-string">'John'</span>).select(<span class="hljs-string">'id'</span>);
knex(<span class="hljs-string">'accounts'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'in'</span>, subquery)</code><br data-reactid="873"/><!-- react-text: 874 -->Outputs:<!-- /react-text --><br data-reactid="875"/><code class="sql hljs" data-reactid="876"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> > <span class="hljs-number">100</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`status`</span> = <span class="hljs-string">'active'</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">'John'</span>)</code></pre><p data-reactid="877">.orWhere with an object automatically wraps the statement and creates an <code>or (and - and - and)</code> clause</p><pre class="display" data-reactid="878"><code class="js hljs" data-reactid="879">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).orWhere({<span class="hljs-attr">votes</span>: <span class="hljs-number">100</span>, <span class="hljs-attr">user</span>: <span class="hljs-string">'knex'</span>})</code><br data-reactid="880"/><!-- react-text: 881 -->Outputs:<!-- /react-text --><br data-reactid="882"/><code class="sql hljs" data-reactid="883"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> (<span class="hljs-string">`votes`</span> = <span class="hljs-number">100</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`user`</span> = <span class="hljs-string">'knex'</span>)</code></pre><div id="Builder-whereNot" data-reactid="884"><b data-reactid="885">whereNot</b><!-- react-text: 886 --> — <!-- /react-text --><code data-reactid="887">.whereNot(~mixed~)</code></div><p data-reactid="888">Object Syntax:</p><pre class="display" data-reactid="889"><code class="js hljs" data-reactid="890">knex(<span class="hljs-string">'users'</span>).whereNot({
<span class="hljs-attr">first_name</span>: <span class="hljs-string">'Test'</span>,
<span class="hljs-attr">last_name</span>: <span class="hljs-string">'User'</span>
}).select(<span class="hljs-string">'id'</span>)</code><br data-reactid="891"/><!-- react-text: 892 -->Outputs:<!-- /react-text --><br data-reactid="893"/><code class="sql hljs" data-reactid="894"><span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`first_name`</span> = <span class="hljs-string">'Test'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`last_name`</span> = <span class="hljs-string">'User'</span></code></pre><p data-reactid="895">Key, Value:</p><pre class="display" data-reactid="896"><code class="js hljs" data-reactid="897">knex(<span class="hljs-string">'users'</span>).whereNot(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="898"/><!-- react-text: 899 -->Outputs:<!-- /react-text --><br data-reactid="900"/><code class="sql hljs" data-reactid="901"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre><p data-reactid="902">Grouped Chain:</p><pre class="display" data-reactid="903"><code class="js hljs" data-reactid="904">knex(<span class="hljs-string">'users'</span>).whereNot(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).orWhereNot(<span class="hljs-string">'id'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">10</span>)
}).orWhereNot({<span class="hljs-attr">name</span>: <span class="hljs-string">'Tester'</span>})</code><br data-reactid="905"/><!-- react-text: 906 -->Outputs:<!-- /react-text --><br data-reactid="907"/><code class="sql hljs" data-reactid="908"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> (<span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`id`</span> > <span class="hljs-number">10</span>) <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">'Tester'</span></code></pre><p data-reactid="909">Operator:</p><pre class="display" data-reactid="910"><code class="js hljs" data-reactid="911">knex(<span class="hljs-string">'users'</span>).whereNot(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">100</span>)</code><br data-reactid="912"/><!-- react-text: 913 -->Outputs:<!-- /react-text --><br data-reactid="914"/><code class="sql hljs" data-reactid="915"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`votes`</span> > <span class="hljs-number">100</span></code></pre><p data-reactid="916">CAVEAT: WhereNot is not suitable for "in" and "between" type subqueries. You should use "not in" and "not between" instead.</p><pre class="display" data-reactid="917"><code class="js hljs" data-reactid="918"><span class="hljs-keyword">var</span> subquery = knex(<span class="hljs-string">'users'</span>)
.whereNot(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">100</span>)
.andWhere(<span class="hljs-string">'status'</span>, <span class="hljs-string">'active'</span>)
.orWhere(<span class="hljs-string">'name'</span>, <span class="hljs-string">'John'</span>)
.select(<span class="hljs-string">'id'</span>);
knex(<span class="hljs-string">'accounts'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'not in'</span>, subquery)</code><br data-reactid="919"/><!-- react-text: 920 -->Outputs:<!-- /react-text --><br data-reactid="921"/><code class="sql hljs" data-reactid="922"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`votes`</span> > <span class="hljs-number">100</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`status`</span> = <span class="hljs-string">'active'</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">'John'</span>)</code></pre><div id="Builder-whereIn" data-reactid="923"><b data-reactid="924">whereIn</b><!-- react-text: 925 --> — <!-- /react-text --><code data-reactid="926">.whereIn(column, array|callback|builder) / .orWhereIn</code><p data-reactid="927">Shorthand for .where('id', 'in', obj), the .whereIn and .orWhereIn methods add a "where in" clause to the query.</p><pre class="display" data-reactid="928"><code class="js hljs" data-reactid="929">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.whereIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
.orWhereIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>])</code><br data-reactid="930"/><!-- react-text: 931 -->Outputs:<!-- /react-text --><br data-reactid="932"/><code class="sql hljs" data-reactid="933"><span class="hljs-keyword">select</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>) <span class="hljs-keyword">or</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>)</code></pre><pre class="display" data-reactid="934"><code class="js hljs" data-reactid="935">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.whereIn(<span class="hljs-string">'account_id'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'accounts'</span>);
})</code><br data-reactid="936"/><!-- react-text: 937 -->Outputs:<!-- /react-text --><br data-reactid="938"/><code class="sql hljs" data-reactid="939"><span class="hljs-keyword">select</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`account_id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span>)</code></pre><pre class="display" data-reactid="940"><code class="js hljs" data-reactid="941"><span class="hljs-keyword">var</span> subquery = knex.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'accounts'</span>);
knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.whereIn(<span class="hljs-string">'account_id'</span>, subquery)</code><br data-reactid="942"/><!-- react-text: 943 -->Outputs:<!-- /react-text --><br data-reactid="944"/><code class="sql hljs" data-reactid="945"><span class="hljs-keyword">select</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`account_id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span>)</code></pre></div><div id="Builder-whereNotIn" data-reactid="946"><b data-reactid="947">whereNotIn</b><!-- react-text: 948 --> — <!-- /react-text --><code data-reactid="949">.whereNotIn(column, array|callback|builder) / .orWhereNotIn</code><pre class="display" data-reactid="950"><code class="js hljs" data-reactid="951">knex(<span class="hljs-string">'users'</span>).whereNotIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])</code><br data-reactid="952"/><!-- react-text: 953 -->Outputs:<!-- /react-text --><br data-reactid="954"/><code class="sql hljs" data-reactid="955"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)</code></pre><pre class="display" data-reactid="956"><code class="js hljs" data-reactid="957">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'name'</span>, <span class="hljs-string">'like'</span>, <span class="hljs-string">'%Test%'</span>).orWhereNotIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])</code><br data-reactid="958"/><!-- react-text: 959 -->Outputs:<!-- /react-text --><br data-reactid="960"/><code class="sql hljs" data-reactid="961"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">like</span> <span class="hljs-string">'%Test%'</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)</code></pre></div><div id="Builder-whereNull" data-reactid="962"><b data-reactid="963">whereNull</b><!-- react-text: 964 --> — <!-- /react-text --><code data-reactid="965">.whereNull(column) / .orWhereNull</code><pre class="display" data-reactid="966"><code class="js hljs" data-reactid="967">knex(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'updated_at'</span>)</code><br data-reactid="968"/><!-- react-text: 969 -->Outputs:<!-- /react-text --><br data-reactid="970"/><code class="sql hljs" data-reactid="971"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`updated_at`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-whereNotNull" data-reactid="972"><b data-reactid="973">whereNotNull</b><!-- react-text: 974 --> — <!-- /react-text --><code data-reactid="975">.whereNotNull(column) / .orWhereNotNull</code><pre class="display" data-reactid="976"><code class="js hljs" data-reactid="977">knex(<span class="hljs-string">'users'</span>).whereNotNull(<span class="hljs-string">'created_at'</span>)</code><br data-reactid="978"/><!-- react-text: 979 -->Outputs:<!-- /react-text --><br data-reactid="980"/><code class="sql hljs" data-reactid="981"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`created_at`</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-whereExists" data-reactid="982"><b data-reactid="983">whereExists</b><!-- react-text: 984 --> — <!-- /react-text --><code data-reactid="985">.whereExists(builder | callback) / .orWhereExists</code><pre class="display" data-reactid="986"><code class="js hljs" data-reactid="987">knex(<span class="hljs-string">'users'</span>).whereExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="988"/><!-- react-text: 989 -->Outputs:<!-- /react-text --><br data-reactid="990"/><code class="sql hljs" data-reactid="991"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre><pre class="display" data-reactid="992"><code class="js hljs" data-reactid="993">knex(<span class="hljs-string">'users'</span>).whereExists(knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>))</code><br data-reactid="994"/><!-- react-text: 995 -->Outputs:<!-- /react-text --><br data-reactid="996"/><code class="sql hljs" data-reactid="997"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-whereNotExists" data-reactid="998"><b data-reactid="999">whereNotExists</b><!-- react-text: 1000 --> — <!-- /react-text --><code data-reactid="1001">.whereNotExists(builder | callback) / .orWhereNotExists</code><pre class="display" data-reactid="1002"><code class="js hljs" data-reactid="1003">knex(<span class="hljs-string">'users'</span>).whereNotExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="1004"/><!-- react-text: 1005 -->Outputs:<!-- /react-text --><br data-reactid="1006"/><code class="sql hljs" data-reactid="1007"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre><pre class="display" data-reactid="1008"><code class="js hljs" data-reactid="1009">knex(<span class="hljs-string">'users'</span>).whereNotExists(knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>))</code><br data-reactid="1010"/><!-- react-text: 1011 -->Outputs:<!-- /react-text --><br data-reactid="1012"/><code class="sql hljs" data-reactid="1013"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-whereBetween" data-reactid="1014"><b data-reactid="1015">whereBetween</b><!-- react-text: 1016 --> — <!-- /react-text --><code data-reactid="1017">.whereBetween(column, range) / .orWhereBetween</code><pre class="display" data-reactid="1018"><code class="js hljs" data-reactid="1019">knex(<span class="hljs-string">'users'</span>).whereBetween(<span class="hljs-string">'votes'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">100</span>])</code><br data-reactid="1020"/><!-- react-text: 1021 -->Outputs:<!-- /react-text --><br data-reactid="1022"/><code class="sql hljs" data-reactid="1023"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> <span class="hljs-keyword">between</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-number">100</span></code></pre></div><div id="Builder-whereNotBetween" data-reactid="1024"><b data-reactid="1025">whereNotBetween</b><!-- react-text: 1026 --> — <!-- /react-text --><code data-reactid="1027">.whereNotBetween(column, range) / .orWhereNotBetween</code><pre class="display" data-reactid="1028"><code class="js hljs" data-reactid="1029">knex(<span class="hljs-string">'users'</span>).whereNotBetween(<span class="hljs-string">'votes'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">100</span>])</code><br data-reactid="1030"/><!-- react-text: 1031 -->Outputs:<!-- /react-text --><br data-reactid="1032"/><code class="sql hljs" data-reactid="1033"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">between</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-number">100</span></code></pre></div><div id="Builder-whereRaw" data-reactid="1034"><b data-reactid="1035">whereRaw</b><!-- react-text: 1036 --> — <!-- /react-text --><code data-reactid="1037">.whereRaw(query, [bindings])</code><p data-reactid="1038">Convenience helper for .where(knex.raw(query)).</p><pre class="display" data-reactid="1039"><code class="js hljs" data-reactid="1040">knex(<span class="hljs-string">'users'</span>).whereRaw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>])</code><br data-reactid="1041"/><!-- react-text: 1042 -->Outputs:<!-- /react-text --><br data-reactid="1043"/><code class="sql hljs" data-reactid="1044"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">id</span> = <span class="hljs-number">1</span></code></pre></div><h3 data-reactid="1045">Join Methods</h3><p data-reactid="1046">Several methods are provided which assist in building joins.</p><div id="Builder-join" data-reactid="1047"><b data-reactid="1048">join</b><!-- react-text: 1049 --> — <!-- /react-text --><code data-reactid="1050">.join(table, first, [operator], second)</code><p data-reactid="1051">The join builder can be used to specify joins between tables, with the first argument being the joining table, the next three arguments being the first join column, the join operator and the second join column, respectively.</p><pre class="display" data-reactid="1052"><code class="js hljs" data-reactid="1053">knex(<span class="hljs-string">'users'</span>)
.join(<span class="hljs-string">'contacts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.user_id'</span>)
.select(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'contacts.phone'</span>)</code><br data-reactid="1054"/><!-- react-text: 1055 -->Outputs:<!-- /react-text --><br data-reactid="1056"/><code class="sql hljs" data-reactid="1057"><span class="hljs-keyword">select</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span>, <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`phone`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1058"><code class="js hljs" data-reactid="1059">knex(<span class="hljs-string">'users'</span>)
.join(<span class="hljs-string">'contacts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'contacts.user_id'</span>)
.select(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'contacts.phone'</span>)</code><br data-reactid="1060"/><!-- react-text: 1061 -->Outputs:<!-- /react-text --><br data-reactid="1062"/><code class="sql hljs" data-reactid="1063"><span class="hljs-keyword">select</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span>, <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`phone`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`user_id`</span></code></pre></div><p data-reactid="1064">For grouped joins, specify a function as the second argument for the join query, and use <code>on</code> with <code>orOn</code> or <code>andOn</code> to create joins that are grouped with parentheses.</p><pre class="display" data-reactid="1065"><code class="js hljs" data-reactid="1066">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1067"/><!-- react-text: 1068 -->Outputs:<!-- /react-text --><br data-reactid="1069"/><code class="sql hljs" data-reactid="1070"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre><p data-reactid="1071">For nested join statements, specify a function as first argument of <code>on</code>, <code>orOn</code> or <code>andOn</code></p><pre class="display" data-reactid="1072"><code class="js hljs" data-reactid="1073">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>)
<span class="hljs-keyword">this</span>.orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})
})</code><br data-reactid="1074"/><!-- react-text: 1075 -->Outputs:<!-- /react-text --><br data-reactid="1076"/><code class="sql hljs" data-reactid="1077"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> (<span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span>)</code></pre><p data-reactid="1078">It is also possible to use an object to represent the join syntax.</p><pre class="display" data-reactid="1079"><code class="js hljs" data-reactid="1080">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, {<span class="hljs-string">'accounts.id'</span>: <span class="hljs-string">'users.account_id'</span>})</code><br data-reactid="1081"/><!-- react-text: 1082 -->Outputs:<!-- /react-text --><br data-reactid="1083"/><code class="sql hljs" data-reactid="1084"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span></code></pre><p data-reactid="1085">If you need to use a literal value (string, number, or boolean) in a join instead of a column, use <code>knex.raw</code>.</p><pre class="display" data-reactid="1086"><code class="js hljs" data-reactid="1087">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'accounts.type'</span>, knex.raw(<span class="hljs-string">'?'</span>, [<span class="hljs-string">'admin'</span>]))</code><br data-reactid="1088"/><!-- react-text: 1089 -->Outputs:<!-- /react-text --><br data-reactid="1090"/><code class="sql hljs" data-reactid="1091"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`type`</span> = <span class="hljs-string">'admin'</span></code></pre><div id="Builder-innerJoin" data-reactid="1092"><b data-reactid="1093">innerJoin</b><!-- react-text: 1094 --> — <!-- /react-text --><code data-reactid="1095">.innerJoin(column, ~mixed~)</code><pre class="display" data-reactid="1096"><code class="js hljs" data-reactid="1097">knex.from(<span class="hljs-string">'users'</span>).innerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1098"/><!-- react-text: 1099 -->Outputs:<!-- /react-text --><br data-reactid="1100"/><code class="sql hljs" data-reactid="1101"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1102"><code class="js hljs" data-reactid="1103">knex.table(<span class="hljs-string">'users'</span>).innerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1104"/><!-- react-text: 1105 -->Outputs:<!-- /react-text --><br data-reactid="1106"/><code class="sql hljs" data-reactid="1107"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1108"><code class="js hljs" data-reactid="1109">knex(<span class="hljs-string">'users'</span>).innerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1110"/><!-- react-text: 1111 -->Outputs:<!-- /react-text --><br data-reactid="1112"/><code class="sql hljs" data-reactid="1113"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-leftJoin" data-reactid="1114"><b data-reactid="1115">leftJoin</b><!-- react-text: 1116 --> — <!-- /react-text --><code data-reactid="1117">.leftJoin(column, ~mixed~)</code><pre class="display" data-reactid="1118"><code class="js hljs" data-reactid="1119">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1120"/><!-- react-text: 1121 -->Outputs:<!-- /react-text --><br data-reactid="1122"/><code class="sql hljs" data-reactid="1123"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1124"><code class="js hljs" data-reactid="1125">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1126"/><!-- react-text: 1127 -->Outputs:<!-- /react-text --><br data-reactid="1128"/><code class="sql hljs" data-reactid="1129"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-leftOuterJoin" data-reactid="1130"><b data-reactid="1131">leftOuterJoin</b><!-- react-text: 1132 --> — <!-- /react-text --><code data-reactid="1133">.leftOuterJoin(column, ~mixed~)</code><pre class="display" data-reactid="1134"><code class="js hljs" data-reactid="1135">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1136"/><!-- react-text: 1137 -->Outputs:<!-- /react-text --><br data-reactid="1138"/><code class="sql hljs" data-reactid="1139"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1140"><code class="js hljs" data-reactid="1141">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1142"/><!-- react-text: 1143 -->Outputs:<!-- /react-text --><br data-reactid="1144"/><code class="sql hljs" data-reactid="1145"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-rightJoin" data-reactid="1146"><b data-reactid="1147">rightJoin</b><!-- react-text: 1148 --> — <!-- /react-text --><code data-reactid="1149">.rightJoin(column, ~mixed~)</code><pre class="display" data-reactid="1150"><code class="js hljs" data-reactid="1151">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1152"/><!-- react-text: 1153 -->Outputs:<!-- /react-text --><br data-reactid="1154"/><code class="sql hljs" data-reactid="1155"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1156"><code class="js hljs" data-reactid="1157">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1158"/><!-- react-text: 1159 -->Outputs:<!-- /react-text --><br data-reactid="1160"/><code class="sql hljs" data-reactid="1161"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-rightOuterJoin" data-reactid="1162"><b data-reactid="1163">rightOuterJoin</b><!-- react-text: 1164 --> — <!-- /react-text --><code data-reactid="1165">.rightOuterJoin(column, ~mixed~)</code><pre class="display" data-reactid="1166"><code class="js hljs" data-reactid="1167">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1168"/><!-- react-text: 1169 -->Outputs:<!-- /react-text --><br data-reactid="1170"/><code class="sql hljs" data-reactid="1171"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1172"><code class="js hljs" data-reactid="1173">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1174"/><!-- react-text: 1175 -->Outputs:<!-- /react-text --><br data-reactid="1176"/><code class="sql hljs" data-reactid="1177"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-outerJoin" data-reactid="1178"><b data-reactid="1179">outerJoin</b><!-- react-text: 1180 --> — <!-- /react-text --><code data-reactid="1181">.outerJoin(column, ~mixed~)</code><pre class="display" data-reactid="1182"><code class="js hljs" data-reactid="1183">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).outerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1184"/><!-- react-text: 1185 -->Outputs:<!-- /react-text --><br data-reactid="1186"/><code class="sql hljs" data-reactid="1187"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1188"><code class="js hljs" data-reactid="1189">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).outerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1190"/><!-- react-text: 1191 -->Outputs:<!-- /react-text --><br data-reactid="1192"/><code class="sql hljs" data-reactid="1193"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-fullOuterJoin" data-reactid="1194"><b data-reactid="1195">fullOuterJoin</b><!-- react-text: 1196 --> — <!-- /react-text --><code data-reactid="1197">.fullOuterJoin(column, ~mixed~)</code><pre class="display" data-reactid="1198"><code class="js hljs" data-reactid="1199">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).fullOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1200"/><!-- react-text: 1201 -->Outputs:<!-- /react-text --><br data-reactid="1202"/><code class="sql hljs" data-reactid="1203"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1204"><code class="js hljs" data-reactid="1205">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).fullOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1206"/><!-- react-text: 1207 -->Outputs:<!-- /react-text --><br data-reactid="1208"/><code class="sql hljs" data-reactid="1209"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-crossJoin" data-reactid="1210"><b data-reactid="1211">crossJoin</b><!-- react-text: 1212 --> — <!-- /react-text --><code data-reactid="1213">.crossJoin(column, ~mixed~)</code><pre class="display" data-reactid="1214"><code class="js hljs" data-reactid="1215">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).crossJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1216"/><!-- react-text: 1217 -->Outputs:<!-- /react-text --><br data-reactid="1218"/><code class="sql hljs" data-reactid="1219"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">cross</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1220"><code class="js hljs" data-reactid="1221">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).crossJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1222"/><!-- react-text: 1223 -->Outputs:<!-- /react-text --><br data-reactid="1224"/><code class="sql hljs" data-reactid="1225"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">cross</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-joinRaw" data-reactid="1226"><b data-reactid="1227">joinRaw</b><!-- react-text: 1228 --> — <!-- /react-text --><code data-reactid="1229">.joinRaw(sql, [bindings])</code><pre class="display" data-reactid="1230"><code class="js hljs" data-reactid="1231">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).joinRaw(<span class="hljs-string">'natural full join table1'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="1232"/><!-- react-text: 1233 -->Outputs:<!-- /react-text --><br data-reactid="1234"/><code class="sql hljs" data-reactid="1235"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">natural</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">join</span> table1 <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre><pre class="display" data-reactid="1236"><code class="js hljs" data-reactid="1237">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).join(knex.raw(<span class="hljs-string">'natural full join table1'</span>)).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="1238"/><!-- react-text: 1239 -->Outputs:<!-- /react-text --><br data-reactid="1240"/><code class="sql hljs" data-reactid="1241"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-keyword">natural</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">join</span> table1 <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre></div><h3 id="Builder-on" data-reactid="1242">OnClauses</h3><div id="Builder-onIn" data-reactid="1243"><b data-reactid="1244">onIn</b><!-- react-text: 1245 --> — <!-- /react-text --><code data-reactid="1246">.onIn(column, values)</code><p data-reactid="1247">Adds a onIn clause to the query.</p><pre class="display" data-reactid="1248"><code class="js hljs" data-reactid="1249">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onIn(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>])
})</code><br data-reactid="1250"/><!-- react-text: 1251 -->Outputs:<!-- /react-text --><br data-reactid="1252"/><code class="sql hljs" data-reactid="1253"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>)</code></pre></div><div id="Builder-onNotIn" data-reactid="1254"><b data-reactid="1255">onNotIn</b><!-- react-text: 1256 --> — <!-- /react-text --><code data-reactid="1257">.onNotIn(column, values)</code><p data-reactid="1258">Adds a onNotIn clause to the query.</p><pre class="display" data-reactid="1259"><code class="js hljs" data-reactid="1260">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotIn(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>])
})</code><br data-reactid="1261"/><!-- react-text: 1262 -->Outputs:<!-- /react-text --><br data-reactid="1263"/><code class="sql hljs" data-reactid="1264"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>)</code></pre></div><div id="Builder-onNull" data-reactid="1265"><b data-reactid="1266">onNull</b><!-- react-text: 1267 --> — <!-- /react-text --><code data-reactid="1268">.onNull(column)</code><p data-reactid="1269">Adds a onNull clause to the query.</p><pre class="display" data-reactid="1270"><code class="js hljs" data-reactid="1271">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNull(<span class="hljs-string">'contacts.email'</span>)
})</code><br data-reactid="1272"/><!-- react-text: 1273 -->Outputs:<!-- /react-text --><br data-reactid="1274"/><code class="sql hljs" data-reactid="1275"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-onNotNull" data-reactid="1276"><b data-reactid="1277">onNotNull</b><!-- react-text: 1278 --> — <!-- /react-text --><code data-reactid="1279">.onNotNull(column)</code><p data-reactid="1280">Adds a onNotNull clause to the query.</p><pre class="display" data-reactid="1281"><code class="js hljs" data-reactid="1282">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotNull(<span class="hljs-string">'contacts.email'</span>)
})</code><br data-reactid="1283"/><!-- react-text: 1284 -->Outputs:<!-- /react-text --><br data-reactid="1285"/><code class="sql hljs" data-reactid="1286"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-onExists" data-reactid="1287"><b data-reactid="1288">onExists</b><!-- react-text: 1289 --> — <!-- /react-text --><code data-reactid="1290">.onExists(builder | callback)</code><p data-reactid="1291">Adds a onExists clause to the query.</p><pre class="display" data-reactid="1292"><code class="js hljs" data-reactid="1293">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})
})</code><br data-reactid="1294"/><!-- react-text: 1295 -->Outputs:<!-- /react-text --><br data-reactid="1296"/><code class="sql hljs" data-reactid="1297"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-onNotExists" data-reactid="1298"><b data-reactid="1299">onNotExists</b><!-- react-text: 1300 --> — <!-- /react-text --><code data-reactid="1301">.onNotExists(builder | callback)</code><p data-reactid="1302">Adds a onNotExists clause to the query.</p><pre class="display" data-reactid="1303"><code class="js hljs" data-reactid="1304">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})
})</code><br data-reactid="1305"/><!-- react-text: 1306 -->Outputs:<!-- /react-text --><br data-reactid="1307"/><code class="sql hljs" data-reactid="1308"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-onBetween" data-reactid="1309"><b data-reactid="1310">onBetween</b><!-- react-text: 1311 --> — <!-- /react-text --><code data-reactid="1312">.onBetween(column, range)</code><p data-reactid="1313">Adds a onBetween clause to the query.</p><pre class="display" data-reactid="1314"><code class="js hljs" data-reactid="1315">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onBetween(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">30</span>])
})</code><br data-reactid="1316"/><!-- react-text: 1317 -->Outputs:<!-- /react-text --><br data-reactid="1318"/><code class="sql hljs" data-reactid="1319"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">30</span></code></pre></div><div id="Builder-onNotBetween" data-reactid="1320"><b data-reactid="1321">onNotBetween</b><!-- react-text: 1322 --> — <!-- /react-text --><code data-reactid="1323">.onNotBetween(column, range)</code><p data-reactid="1324">Adds a onNotBetween clause to the query.</p><pre class="display" data-reactid="1325"><code class="js hljs" data-reactid="1326">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotBetween(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">30</span>])
})</code><br data-reactid="1327"/><!-- react-text: 1328 -->Outputs:<!-- /react-text --><br data-reactid="1329"/><code class="sql hljs" data-reactid="1330"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">30</span></code></pre></div><h3 id="Builder-clear" data-reactid="1331">ClearClauses</h3><div id="Builder-clearSelect" data-reactid="1332"><b data-reactid="1333">clearSelect</b><!-- react-text: 1334 --> — <!-- /react-text --><code data-reactid="1335">.clearSelect()</code><p data-reactid="1336">Clears all select clauses from the query, excluding subqueries.</p><pre class="display" data-reactid="1337"><code class="js hljs" data-reactid="1338">knex.select(<span class="hljs-string">'email'</span>, <span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).clearSelect()</code><br data-reactid="1339"/><!-- react-text: 1340 -->Outputs:<!-- /react-text --><br data-reactid="1341"/><code class="sql hljs" data-reactid="1342"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-clearWhere" data-reactid="1343"><b data-reactid="1344">clearWhere</b><!-- react-text: 1345 --> — <!-- /react-text --><code data-reactid="1346">.clearWhere()</code><p data-reactid="1347">Clears all where clauses from the query, excluding subqueries.</p><pre class="display" data-reactid="1348"><code class="js hljs" data-reactid="1349">knex.select(<span class="hljs-string">'email'</span>, <span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).clearWhere()</code><br data-reactid="1350"/><!-- react-text: 1351 -->Outputs:<!-- /react-text --><br data-reactid="1352"/><code class="sql hljs" data-reactid="1353"><span class="hljs-keyword">select</span> <span class="hljs-string">`email`</span>, <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-distinct" data-reactid="1354"><b data-reactid="1355">distinct</b><!-- react-text: 1356 --> — <!-- /react-text --><code data-reactid="1357">.distinct()</code><p data-reactid="1358">Sets a distinct clause on the query.</p><pre class="display" data-reactid="1359"><code class="js hljs" data-reactid="1360"><span class="hljs-comment">// select distinct 'first_name' from customers</span>
knex(<span class="hljs-string">'customers'</span>)
.distinct(<span class="hljs-string">'first_name'</span>, <span class="hljs-string">'last_name'</span>)
.select()</code><br data-reactid="1361"/><!-- react-text: 1362 -->Outputs:<!-- /react-text --><br data-reactid="1363"/><code class="sql hljs" data-reactid="1364"><span class="hljs-keyword">select</span> <span class="hljs-keyword">distinct</span> <span class="hljs-string">`first_name`</span>, <span class="hljs-string">`last_name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`customers`</span></code></pre></div><div id="Builder-groupBy" data-reactid="1365"><b data-reactid="1366">groupBy</b><!-- react-text: 1367 --> — <!-- /react-text --><code data-reactid="1368">.groupBy(*names)</code><p data-reactid="1369">Adds a group by clause to the query.</p><pre class="display" data-reactid="1370"><code class="js hljs" data-reactid="1371">knex(<span class="hljs-string">'users'</span>).groupBy(<span class="hljs-string">'count'</span>)</code><br data-reactid="1372"/><!-- react-text: 1373 -->Outputs:<!-- /react-text --><br data-reactid="1374"/><code class="sql hljs" data-reactid="1375"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`count`</span></code></pre></div><div id="Builder-groupByRaw" data-reactid="1376"><b data-reactid="1377">groupByRaw</b><!-- react-text: 1378 --> — <!-- /react-text --><code data-reactid="1379">.groupByRaw(sql)</code><p data-reactid="1380">Adds a raw group by clause to the query.</p><pre class="display" data-reactid="1381"><code class="js hljs" data-reactid="1382">knex.select(<span class="hljs-string">'year'</span>, knex.raw(<span class="hljs-string">'SUM(profit)'</span>)).from(<span class="hljs-string">'sales'</span>).groupByRaw(<span class="hljs-string">'year WITH ROLLUP'</span>)</code><br data-reactid="1383"/><!-- react-text: 1384 -->Outputs:<!-- /react-text --><br data-reactid="1385"/><code class="sql hljs" data-reactid="1386"><span class="hljs-keyword">select</span> <span class="hljs-string">`year`</span>, <span class="hljs-keyword">SUM</span>(profit) <span class="hljs-keyword">from</span> <span class="hljs-string">`sales`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">year</span> <span class="hljs-keyword">WITH</span> <span class="hljs-keyword">ROLLUP</span></code></pre></div><div id="Builder-orderBy" data-reactid="1387"><b data-reactid="1388">orderBy</b><!-- react-text: 1389 --> — <!-- /react-text --><code data-reactid="1390">.orderBy(column, [direction])</code><p data-reactid="1391">Adds an order by clause to the query.</p><pre class="display" data-reactid="1392"><code class="js hljs" data-reactid="1393">knex(<span class="hljs-string">'users'</span>).orderBy(<span class="hljs-string">'name'</span>, <span class="hljs-string">'desc'</span>)</code><br data-reactid="1394"/><!-- react-text: 1395 -->Outputs:<!-- /react-text --><br data-reactid="1396"/><code class="sql hljs" data-reactid="1397"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">desc</span></code></pre></div><div id="Builder-orderByRaw" data-reactid="1398"><b data-reactid="1399">orderByRaw</b><!-- react-text: 1400 --> — <!-- /react-text --><code data-reactid="1401">.orderByRaw(sql)</code><p data-reactid="1402">Adds an order by raw clause to the query.</p><pre class="display" data-reactid="1403"><code class="js hljs" data-reactid="1404">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'table'</span>).orderByRaw(<span class="hljs-string">'col DESC NULLS LAST'</span>)</code><br data-reactid="1405"/><!-- react-text: 1406 -->Outputs:<!-- /react-text --><br data-reactid="1407"/><code class="sql hljs" data-reactid="1408"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`table`</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">col</span> <span class="hljs-keyword">DESC</span> <span class="hljs-keyword">NULLS</span> <span class="hljs-keyword">LAST</span></code></pre></div><h3 id="Builder-havings" data-reactid="1409">Having Clauses</h3><div id="Builder-having" data-reactid="1410"><b data-reactid="1411">having</b><!-- react-text: 1412 --> — <!-- /react-text --><code data-reactid="1413">.having(column, operator, value)</code><p data-reactid="1414">Adds a having clause to the query.</p><pre class="display" data-reactid="1415"><code class="js hljs" data-reactid="1416">knex(<span class="hljs-string">'users'</span>)
.groupBy(<span class="hljs-string">'count'</span>)
.orderBy(<span class="hljs-string">'name'</span>, <span class="hljs-string">'desc'</span>)
.having(<span class="hljs-string">'count'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">100</span>)</code><br data-reactid="1417"/><!-- react-text: 1418 -->Outputs:<!-- /react-text --><br data-reactid="1419"/><code class="sql hljs" data-reactid="1420"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`count`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`count`</span> > <span class="hljs-number">100</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">desc</span></code></pre></div><div id="Builder-havingIn" data-reactid="1421"><b data-reactid="1422">havingIn</b><!-- react-text: 1423 --> — <!-- /react-text --><code data-reactid="1424">.havingIn(column, values)</code><p data-reactid="1425">Adds a havingIn clause to the query.</p><pre class="display" data-reactid="1426"><code class="js hljs" data-reactid="1427">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>])</code><br data-reactid="1428"/><!-- react-text: 1429 -->Outputs:<!-- /react-text --><br data-reactid="1430"/><code class="sql hljs" data-reactid="1431"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>)</code></pre></div><div id="Builder-havingNotIn" data-reactid="1432"><b data-reactid="1433">havingNotIn</b><!-- react-text: 1434 --> — <!-- /react-text --><code data-reactid="1435">.havingNotIn(column, values)</code><p data-reactid="1436">Adds a havingNotIn clause to the query.</p><pre class="display" data-reactid="1437"><code class="js hljs" data-reactid="1438">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>])</code><br data-reactid="1439"/><!-- react-text: 1440 -->Outputs:<!-- /react-text --><br data-reactid="1441"/><code class="sql hljs" data-reactid="1442"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>)</code></pre></div><div id="Builder-havingNull" data-reactid="1443"><b data-reactid="1444">havingNull</b><!-- react-text: 1445 --> — <!-- /react-text --><code data-reactid="1446">.havingNull(column)</code><p data-reactid="1447">Adds a havingNull clause to the query.</p><pre class="display" data-reactid="1448"><code class="js hljs" data-reactid="1449">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNull(<span class="hljs-string">'email'</span>)</code><br data-reactid="1450"/><!-- react-text: 1451 -->Outputs:<!-- /react-text --><br data-reactid="1452"/><code class="sql hljs" data-reactid="1453"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-havingNotNull" data-reactid="1454"><b data-reactid="1455">havingNotNull</b><!-- react-text: 1456 --> — <!-- /react-text --><code data-reactid="1457">.havingNotNull(column)</code><p data-reactid="1458">Adds a havingNotNull clause to the query.</p><pre class="display" data-reactid="1459"><code class="js hljs" data-reactid="1460">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotNull(<span class="hljs-string">'email'</span>)</code><br data-reactid="1461"/><!-- react-text: 1462 -->Outputs:<!-- /react-text --><br data-reactid="1463"/><code class="sql hljs" data-reactid="1464"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-havingExists" data-reactid="1465"><b data-reactid="1466">havingExists</b><!-- react-text: 1467 --> — <!-- /react-text --><code data-reactid="1468">.havingExists(builder | callback)</code><p data-reactid="1469">Adds a havingExists clause to the query.</p><pre class="display" data-reactid="1470"><code class="js hljs" data-reactid="1471">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="1472"/><!-- react-text: 1473 -->Outputs:<!-- /react-text --><br data-reactid="1474"/><code class="sql hljs" data-reactid="1475"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-havingNotExists" data-reactid="1476"><b data-reactid="1477">havingNotExists</b><!-- react-text: 1478 --> — <!-- /react-text --><code data-reactid="1479">.havingNotExists(builder | callback)</code><p data-reactid="1480">Adds a havingNotExists clause to the query.</p><pre class="display" data-reactid="1481"><code class="js hljs" data-reactid="1482">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="1483"/><!-- react-text: 1484 -->Outputs:<!-- /react-text --><br data-reactid="1485"/><code class="sql hljs" data-reactid="1486"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-havingBetween" data-reactid="1487"><b data-reactid="1488">havingBetween</b><!-- react-text: 1489 --> — <!-- /react-text --><code data-reactid="1490">.havingBetween(column, range)</code><p data-reactid="1491">Adds a havingBetween clause to the query.</p><pre class="display" data-reactid="1492"><code class="js hljs" data-reactid="1493">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingBetween(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">10</span>])</code><br data-reactid="1494"/><!-- react-text: 1495 -->Outputs:<!-- /react-text --><br data-reactid="1496"/><code class="sql hljs" data-reactid="1497"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">10</span></code></pre></div><div id="Builder-havingNotBetween" data-reactid="1498"><b data-reactid="1499">havingNotBetween</b><!-- react-text: 1500 --> — <!-- /react-text --><code data-reactid="1501">.havingNotBetween(column, range)</code><p data-reactid="1502">Adds a havingNotBetween clause to the query.</p><pre class="display" data-reactid="1503"><code class="js hljs" data-reactid="1504">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotBetween(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">10</span>])</code><br data-reactid="1505"/><!-- react-text: 1506 -->Outputs:<!-- /react-text --><br data-reactid="1507"/><code class="sql hljs" data-reactid="1508"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">10</span></code></pre></div><div id="Builder-havingRaw" data-reactid="1509"><b data-reactid="1510">havingRaw</b><!-- react-text: 1511 --> — <!-- /react-text --><code data-reactid="1512">.havingRaw(column, operator, value)</code><p data-reactid="1513">Adds a havingRaw clause to the query.</p><pre class="display" data-reactid="1514"><code class="js hljs" data-reactid="1515">knex(<span class="hljs-string">'users'</span>)
.groupBy(<span class="hljs-string">'count'</span>)
.orderBy(<span class="hljs-string">'name'</span>, <span class="hljs-string">'desc'</span>)
.havingRaw(<span class="hljs-string">'count > ?'</span>, [<span class="hljs-number">100</span>])</code><br data-reactid="1516"/><!-- react-text: 1517 -->Outputs:<!-- /react-text --><br data-reactid="1518"/><code class="sql hljs" data-reactid="1519"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`count`</span> <span class="hljs-keyword">having</span> <span class="hljs-keyword">count</span> > <span class="hljs-number">100</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">desc</span></code></pre></div><div id="Builder-offset" data-reactid="1520"><b data-reactid="1521">offset</b><!-- react-text: 1522 --> — <!-- /react-text --><code data-reactid="1523">.offset(value)</code><p data-reactid="1524">Adds an offset clause to the query.</p><pre class="display" data-reactid="1525"><code class="js hljs" data-reactid="1526">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).offset(<span class="hljs-number">10</span>)</code><br data-reactid="1527"/><!-- react-text: 1528 -->Outputs:<!-- /react-text --><br data-reactid="1529"/><code class="sql hljs" data-reactid="1530"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">limit</span> <span class="hljs-number">18446744073709551615</span> <span class="hljs-keyword">offset</span> <span class="hljs-number">10</span></code></pre></div><div id="Builder-limit" data-reactid="1531"><b data-reactid="1532">limit</b><!-- react-text: 1533 --> — <!-- /react-text --><code data-reactid="1534">.limit(value)</code><p data-reactid="1535">Adds a limit clause to the query.</p><pre class="display" data-reactid="1536"><code class="js hljs" data-reactid="1537">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).limit(<span class="hljs-number">10</span>).offset(<span class="hljs-number">30</span>)</code><br data-reactid="1538"/><!-- react-text: 1539 -->Outputs:<!-- /react-text --><br data-reactid="1540"/><code class="sql hljs" data-reactid="1541"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">limit</span> <span class="hljs-number">10</span> <span class="hljs-keyword">offset</span> <span class="hljs-number">30</span></code></pre></div><div id="Builder-union" data-reactid="1542"><b data-reactid="1543">union</b><!-- react-text: 1544 --> — <!-- /react-text --><code data-reactid="1545">.union([*queries], [wrap])</code><p data-reactid="1546">Creates a union query, taking an array or a list of callbacks to build the union statement, with optional boolean wrap. The queries will be individually wrapped in parentheses with a true wrap parameter.</p><pre class="display" data-reactid="1547"><code class="js hljs" data-reactid="1548">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'last_name'</span>).union(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'first_name'</span>);
})</code><br data-reactid="1549"/><!-- react-text: 1550 -->Outputs:<!-- /react-text --><br data-reactid="1551"/><code class="sql hljs" data-reactid="1552"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`last_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">union</span> <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`first_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-unionAll" data-reactid="1553"><b data-reactid="1554">unionAll</b><!-- react-text: 1555 --> — <!-- /react-text --><code data-reactid="1556">.unionAll(query)</code><p data-reactid="1557">Creates a union all query, with the same method signature as the union method.</p><pre class="display" data-reactid="1558"><code class="js hljs" data-reactid="1559">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'last_name'</span>).unionAll(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'first_name'</span>);
})</code><br data-reactid="1560"/><!-- react-text: 1561 -->Outputs:<!-- /react-text --><br data-reactid="1562"/><code class="sql hljs" data-reactid="1563"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`last_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">union</span> all <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`first_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-insert" data-reactid="1564"><b data-reactid="1565">insert</b><!-- react-text: 1566 --> — <!-- /react-text --><code data-reactid="1567">.insert(data, [returning])</code><p data-reactid="1568">Creates an insert query, taking either a hash of properties to be inserted into the row, or an array of inserts, to be executed as a single insert command. Resolves the promise / fulfills the callback with an array containing the first insert id of the inserted model, or an array containing all inserted ids for postgresql.</p><pre class="display" data-reactid="1569"><code class="js hljs" data-reactid="1570"><span class="hljs-comment">// Returns [1] in "mysql", "sqlite", "oracle"; [] in "postgresql" unless the 'returning' parameter is set.</span>
knex(<span class="hljs-string">'books'</span>).insert({<span class="hljs-attr">title</span>: <span class="hljs-string">'Slaughterhouse Five'</span>})</code><br data-reactid="1571"/><!-- react-text: 1572 -->Outputs:<!-- /react-text --><br data-reactid="1573"/><code class="sql hljs" data-reactid="1574"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Slaughterhouse Five'</span>)</code></pre><pre class="display" data-reactid="1575"><code class="js hljs" data-reactid="1576"><span class="hljs-comment">// Normalizes for empty keys on multi-row insert:</span>
knex(<span class="hljs-string">'coords'</span>).insert([{<span class="hljs-attr">x</span>: <span class="hljs-number">20</span>}, {<span class="hljs-attr">y</span>: <span class="hljs-number">30</span>}, {<span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span>}])</code><br data-reactid="1577"/><!-- react-text: 1578 -->Outputs:<!-- /react-text --><br data-reactid="1579"/><code class="sql hljs" data-reactid="1580"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`coords`</span> (<span class="hljs-string">`x`</span>, <span class="hljs-string">`y`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-number">20</span>, <span class="hljs-keyword">DEFAULT</span>), (<span class="hljs-keyword">DEFAULT</span>, <span class="hljs-number">30</span>), (<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)</code></pre><pre class="display" data-reactid="1581"><code class="js hljs" data-reactid="1582"><span class="hljs-comment">// Returns [2] in "mysql", "sqlite"; [2, 3] in "postgresql"</span>
knex.insert([{<span class="hljs-attr">title</span>: <span class="hljs-string">'Great Gatsby'</span>}, {<span class="hljs-attr">title</span>: <span class="hljs-string">'Fahrenheit 451'</span>}], <span class="hljs-string">'id'</span>).into(<span class="hljs-string">'books'</span>)</code><br data-reactid="1583"/><!-- react-text: 1584 -->Outputs:<!-- /react-text --><br data-reactid="1585"/><code class="sql hljs" data-reactid="1586"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Great Gatsby'</span>), (<span class="hljs-string">'Fahrenheit 451'</span>)</code></pre></div><p data-reactid="1587">If one prefers that undefined keys are replaced with <code>NULL</code> instead of <code>DEFAULT</code> one may give <code>useNullAsDefault</code> configuration parameter in knex config.</p><pre data-reactid="1588"><code class="hljs js" data-reactid="1589"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
},
<span class="hljs-attr">useNullAsDefault</span>: <span class="hljs-literal">true</span>
});
knex(<span class="hljs-string">'coords'</span>).insert([{<span class="hljs-attr">x</span>: <span class="hljs-number">20</span>}, {<span class="hljs-attr">y</span>: <span class="hljs-number">30</span>}, {<span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span>}])
<span class="hljs-comment">// insert into `coords` (`x`, `y`) values (20, NULL), (NULL, 30), (10, 20)"</span></code></pre><div id="Builder-returning" data-reactid="1590"><b data-reactid="1591">returning</b><!-- react-text: 1592 --> — <!-- /react-text --><code data-reactid="1593">.returning(column) / .returning([column1, column2, ...])</code><p data-reactid="1594">Utilized by PostgreSQL, MSSQL, and Oracle databases, the returning method specifies which column should be returned by the insert and update methods. Passed column parameter may be a string or an array of strings. When passed in a string, makes the SQL result be reported as an array of values from the specified column. When passed in an array of strings, makes the SQL result be reported as an array of objects, each containing a single property for each of the specified columns.</p><pre class="display" data-reactid="1595"><code class="js hljs" data-reactid="1596"><span class="hljs-comment">// Returns [1]</span>
knex(<span class="hljs-string">'books'</span>)
.returning(<span class="hljs-string">'id'</span>)
.insert({<span class="hljs-attr">title</span>: <span class="hljs-string">'Slaughterhouse Five'</span>})</code><br data-reactid="1597"/><!-- react-text: 1598 -->Outputs:<!-- /react-text --><br data-reactid="1599"/><code class="sql hljs" data-reactid="1600"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Slaughterhouse Five'</span>)</code></pre><pre class="display" data-reactid="1601"><code class="js hljs" data-reactid="1602"><span class="hljs-comment">// Returns [2] in "mysql", "sqlite"; [2, 3] in "postgresql"</span>
knex(<span class="hljs-string">'books'</span>)
.returning(<span class="hljs-string">'id'</span>)
.insert([{<span class="hljs-attr">title</span>: <span class="hljs-string">'Great Gatsby'</span>}, {<span class="hljs-attr">title</span>: <span class="hljs-string">'Fahrenheit 451'</span>}])</code><br data-reactid="1603"/><!-- react-text: 1604 -->Outputs:<!-- /react-text --><br data-reactid="1605"/><code class="sql hljs" data-reactid="1606"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Great Gatsby'</span>), (<span class="hljs-string">'Fahrenheit 451'</span>)</code></pre><pre class="display" data-reactid="1607"><code class="js hljs" data-reactid="1608"><span class="hljs-comment">// Returns [ { id: 1, title: 'Slaughterhouse Five' } ]</span>
knex(<span class="hljs-string">'books'</span>)
.returning([<span class="hljs-string">'id'</span>,<span class="hljs-string">'title'</span>])
.insert({<span class="hljs-attr">title</span>: <span class="hljs-string">'Slaughterhouse Five'</span>})</code><br data-reactid="1609"/><!-- react-text: 1610 -->Outputs:<!-- /react-text --><br data-reactid="1611"/><code class="sql hljs" data-reactid="1612"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Slaughterhouse Five'</span>)</code></pre></div><div id="Builder-update" data-reactid="1613"><b data-reactid="1614">update</b><!-- react-text: 1615 --> — <!-- /react-text --><code data-reactid="1616">.update(data, [returning]) / .update(key, value, [returning])</code><p data-reactid="1617">Creates an update query, taking a hash of properties or a key/value pair to be updated based on the other query constraints. Resolves the promise / fulfills the callback with the number of affected rows for the query. If a key to be updated has value undefined it is ignored.</p><pre class="display" data-reactid="1618"><code class="js hljs" data-reactid="1619">knex(<span class="hljs-string">'books'</span>)
.where(<span class="hljs-string">'published_date'</span>, <span class="hljs-string">'<'</span>, <span class="hljs-number">2000</span>)
.update({
<span class="hljs-attr">status</span>: <span class="hljs-string">'archived'</span>,
<span class="hljs-attr">thisKeyIsSkipped</span>: <span class="hljs-literal">undefined</span>
})</code><br data-reactid="1620"/><!-- react-text: 1621 -->Outputs:<!-- /react-text --><br data-reactid="1622"/><code class="sql hljs" data-reactid="1623"><span class="hljs-keyword">update</span> <span class="hljs-string">`books`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`status`</span> = <span class="hljs-string">'archived'</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`published_date`</span> < <span class="hljs-number">2000</span></code></pre><pre class="display" data-reactid="1624"><code class="js hljs" data-reactid="1625"><span class="hljs-comment">// Returns [1] in "mysql", "sqlite", "oracle"; [] in "postgresql" unless the 'returning' parameter is set.</span>
knex(<span class="hljs-string">'books'</span>).update(<span class="hljs-string">'title'</span>, <span class="hljs-string">'Slaughterhouse Five'</span>)</code><br data-reactid="1626"/><!-- react-text: 1627 -->Outputs:<!-- /react-text --><br data-reactid="1628"/><code class="sql hljs" data-reactid="1629"><span class="hljs-keyword">update</span> <span class="hljs-string">`books`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`title`</span> = <span class="hljs-string">'Slaughterhouse Five'</span></code></pre></div><div id="Builder-del / delete" data-reactid="1630"><b data-reactid="1631">del / delete</b><!-- react-text: 1632 --> — <!-- /react-text --><code data-reactid="1633">.del()</code><p data-reactid="1634">Aliased to del as delete is a reserved word in JavaScript, this method deletes one or more rows, based on other conditions specified in the query. Resolves the promise / fulfills the callback with the number of affected rows for the query.</p><pre class="display" data-reactid="1635"><code class="js hljs" data-reactid="1636">knex(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'activated'</span>, <span class="hljs-literal">false</span>)
.del()</code><br data-reactid="1637"/><!-- react-text: 1638 -->Outputs:<!-- /react-text --><br data-reactid="1639"/><code class="sql hljs" data-reactid="1640"><span class="hljs-keyword">delete</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`activated`</span> = <span class="hljs-literal">false</span></code></pre></div><div id="Builder-transacting" data-reactid="1641"><b data-reactid="1642">transacting</b><!-- react-text: 1643 --> — <!-- /react-text --><code data-reactid="1644">.transacting(transactionObj)</code><p data-reactid="1645">Used by knex.transaction, the transacting method may be chained to any query and passed the object you wish to join the query as part of the transaction for.</p><pre data-reactid="1646"><code class="hljs js" data-reactid="1647"><span class="hljs-keyword">var</span> <span class="hljs-built_in">Promise</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bluebird'</span>);
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">trx</span>) </span>{
knex(<span class="hljs-string">'books'</span>).transacting(trx).insert({<span class="hljs-attr">name</span>: <span class="hljs-string">'Old Books'</span>})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) </span>{
<span class="hljs-keyword">var</span> id = resp[<span class="hljs-number">0</span>];
<span class="hljs-keyword">return</span> someExternalMethod(id, trx);
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Transaction complete.'</span>);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>) </span>{
<span class="hljs-built_in">console</span>.error(err);
});</code></pre></div><div id="Builder-forUpdate" data-reactid="1648"><b data-reactid="1649">forUpdate</b><!-- react-text: 1650 --> — <!-- /react-text --><code data-reactid="1651">.transacting(t).forUpdate()</code><p data-reactid="1652">Dynamically added after a transaction is specified, the forUpdate adds a FOR UPDATE in PostgreSQL and MySQL during a select statement.</p><pre class="display" data-reactid="1653"><code class="js hljs" data-reactid="1654">knex(<span class="hljs-string">'tableName'</span>)
.transacting(trx)
.forUpdate()
.select(<span class="hljs-string">'*'</span>)</code><br data-reactid="1655"/><!-- react-text: 1656 -->Outputs:<!-- /react-text --><br data-reactid="1657"/><code class="sql hljs" data-reactid="1658"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`tableName`</span> <span class="hljs-keyword">for</span> <span class="hljs-keyword">update</span></code></pre></div><div id="Builder-forShare" data-reactid="1659"><b data-reactid="1660">forShare</b><!-- react-text: 1661 --> — <!-- /react-text --><code data-reactid="1662">.transacting(t).forShare()</code><p data-reactid="1663">Dynamically added after a transaction is specified, the forShare adds a FOR SHARE in PostgreSQL and a LOCK IN SHARE MODE for MySQL during a select statement.</p><pre class="display" data-reactid="1664"><code class="js hljs" data-reactid="1665">knex(<span class="hljs-string">'tableName'</span>)
.transacting(trx)
.forShare()
.select(<span class="hljs-string">'*'</span>)</code><br data-reactid="1666"/><!-- react-text: 1667 -->Outputs:<!-- /react-text --><br data-reactid="1668"/><code class="sql hljs" data-reactid="1669"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`tableName`</span> <span class="hljs-keyword">lock</span> <span class="hljs-keyword">in</span> <span class="hljs-keyword">share</span> <span class="hljs-keyword">mode</span></code></pre></div><div id="Builder-count" data-reactid="1670"><b data-reactid="1671">count</b><!-- react-text: 1672 --> — <!-- /react-text --><code data-reactid="1673">.count(column)</code><p data-reactid="1674">Performs a count on the specified column. Note that in Postgres, count returns a bigint type which will be a String and not a Number (more info).</p><pre class="display" data-reactid="1675"><code class="js hljs" data-reactid="1676">knex(<span class="hljs-string">'users'</span>).count(<span class="hljs-string">'active'</span>)</code><br data-reactid="1677"/><!-- react-text: 1678 -->Outputs:<!-- /react-text --><br data-reactid="1679"/><code class="sql hljs" data-reactid="1680"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(<span class="hljs-string">`active`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1681"><code class="js hljs" data-reactid="1682">knex(<span class="hljs-string">'users'</span>).count(<span class="hljs-string">'active as a'</span>)</code><br data-reactid="1683"/><!-- react-text: 1684 -->Outputs:<!-- /react-text --><br data-reactid="1685"/><code class="sql hljs" data-reactid="1686"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(<span class="hljs-string">`active`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><p data-reactid="1687">Use <strong>countDistinct</strong> to add a distinct expression inside the aggregate function.</p><pre class="display" data-reactid="1688"><code class="js hljs" data-reactid="1689">knex(<span class="hljs-string">'users'</span>).countDistinct(<span class="hljs-string">'active'</span>)</code><br data-reactid="1690"/><!-- react-text: 1691 -->Outputs:<!-- /react-text --><br data-reactid="1692"/><code class="sql hljs" data-reactid="1693"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(<span class="hljs-keyword">distinct</span> <span class="hljs-string">`active`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><div id="Builder-min" data-reactid="1694"><b data-reactid="1695">min</b><!-- react-text: 1696 --> — <!-- /react-text --><code data-reactid="1697">.min(column)</code><p data-reactid="1698">Gets the minimum value for the specified column.</p><pre class="display" data-reactid="1699"><code class="js hljs" data-reactid="1700">knex(<span class="hljs-string">'users'</span>).min(<span class="hljs-string">'age'</span>)</code><br data-reactid="1701"/><!-- react-text: 1702 -->Outputs:<!-- /react-text --><br data-reactid="1703"/><code class="sql hljs" data-reactid="1704"><span class="hljs-keyword">select</span> <span class="hljs-keyword">min</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1705"><code class="js hljs" data-reactid="1706">knex(<span class="hljs-string">'users'</span>).min(<span class="hljs-string">'age as a'</span>)</code><br data-reactid="1707"/><!-- react-text: 1708 -->Outputs:<!-- /react-text --><br data-reactid="1709"/><code class="sql hljs" data-reactid="1710"><span class="hljs-keyword">select</span> <span class="hljs-keyword">min</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-max" data-reactid="1711"><b data-reactid="1712">max</b><!-- react-text: 1713 --> — <!-- /react-text --><code data-reactid="1714">.max(column)</code><p data-reactid="1715">Gets the maximum value for the specified column.</p><pre class="display" data-reactid="1716"><code class="js hljs" data-reactid="1717">knex(<span class="hljs-string">'users'</span>).max(<span class="hljs-string">'age'</span>)</code><br data-reactid="1718"/><!-- react-text: 1719 -->Outputs:<!-- /react-text --><br data-reactid="1720"/><code class="sql hljs" data-reactid="1721"><span class="hljs-keyword">select</span> <span class="hljs-keyword">max</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1722"><code class="js hljs" data-reactid="1723">knex(<span class="hljs-string">'users'</span>).max(<span class="hljs-string">'age as a'</span>)</code><br data-reactid="1724"/><!-- react-text: 1725 -->Outputs:<!-- /react-text --><br data-reactid="1726"/><code class="sql hljs" data-reactid="1727"><span class="hljs-keyword">select</span> <span class="hljs-keyword">max</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-sum" data-reactid="1728"><b data-reactid="1729">sum</b><!-- react-text: 1730 --> — <!-- /react-text --><code data-reactid="1731">.sum(column)</code><p data-reactid="1732">Retrieve the sum of the values of a given column.</p><pre class="display" data-reactid="1733"><code class="js hljs" data-reactid="1734">knex(<span class="hljs-string">'users'</span>).sum(<span class="hljs-string">'products'</span>)</code><br data-reactid="1735"/><!-- react-text: 1736 -->Outputs:<!-- /react-text --><br data-reactid="1737"/><code class="sql hljs" data-reactid="1738"><span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-string">`products`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1739"><code class="js hljs" data-reactid="1740">knex(<span class="hljs-string">'users'</span>).sum(<span class="hljs-string">'products as p'</span>)</code><br data-reactid="1741"/><!-- react-text: 1742 -->Outputs:<!-- /react-text --><br data-reactid="1743"/><code class="sql hljs" data-reactid="1744"><span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-string">`products`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`p`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><p data-reactid="1745">Use <strong>sumDistinct</strong> to add a distinct expression inside the aggregate function.</p><pre class="display" data-reactid="1746"><code class="js hljs" data-reactid="1747">knex(<span class="hljs-string">'users'</span>).sumDistinct(<span class="hljs-string">'products'</span>)</code><br data-reactid="1748"/><!-- react-text: 1749 -->Outputs:<!-- /react-text --><br data-reactid="1750"/><code class="sql hljs" data-reactid="1751"><span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-keyword">distinct</span> <span class="hljs-string">`products`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><div id="Builder-avg" data-reactid="1752"><b data-reactid="1753">avg</b><!-- react-text: 1754 --> — <!-- /react-text --><code data-reactid="1755">.avg(column)</code><p data-reactid="1756">Retrieve the average of the values of a given column.</p><pre class="display" data-reactid="1757"><code class="js hljs" data-reactid="1758">knex(<span class="hljs-string">'users'</span>).avg(<span class="hljs-string">'age'</span>)</code><br data-reactid="1759"/><!-- react-text: 1760 -->Outputs:<!-- /react-text --><br data-reactid="1761"/><code class="sql hljs" data-reactid="1762"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1763"><code class="js hljs" data-reactid="1764">knex(<span class="hljs-string">'users'</span>).avg(<span class="hljs-string">'age as a'</span>)</code><br data-reactid="1765"/><!-- react-text: 1766 -->Outputs:<!-- /react-text --><br data-reactid="1767"/><code class="sql hljs" data-reactid="1768"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><p data-reactid="1769">Use <strong>avgDistinct</strong> to add a distinct expression inside the aggregate function.</p><pre class="display" data-reactid="1770"><code class="js hljs" data-reactid="1771">knex(<span class="hljs-string">'users'</span>).avgDistinct(<span class="hljs-string">'age'</span>)</code><br data-reactid="1772"/><!-- react-text: 1773 -->Outputs:<!-- /react-text --><br data-reactid="1774"/><code class="sql hljs" data-reactid="1775"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-keyword">distinct</span> <span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><div id="Builder-increment" data-reactid="1776"><b data-reactid="1777">increment</b><!-- react-text: 1778 --> — <!-- /react-text --><code data-reactid="1779">.increment(column, amount)</code><p data-reactid="1780">Increments a column value by the specified amount.</p><pre class="display" data-reactid="1781"><code class="js hljs" data-reactid="1782">knex(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'userid'</span>, <span class="hljs-string">'='</span>, <span class="hljs-number">1</span>)
.increment(<span class="hljs-string">'balance'</span>, <span class="hljs-number">10</span>)</code><br data-reactid="1783"/><!-- react-text: 1784 -->Outputs:<!-- /react-text --><br data-reactid="1785"/><code class="sql hljs" data-reactid="1786"><span class="hljs-keyword">update</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`balance`</span> = <span class="hljs-string">`balance`</span> + <span class="hljs-number">10</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`userid`</span> = <span class="hljs-number">1</span></code></pre></div><div id="Builder-decrement" data-reactid="1787"><b data-reactid="1788">decrement</b><!-- react-text: 1789 --> — <!-- /react-text --><code data-reactid="1790">.decrement(column, amount)</code><p data-reactid="1791">Decrements a column value by the specified amount.</p><pre class="display" data-reactid="1792"><code class="js hljs" data-reactid="1793">knex(<span class="hljs-string">'accounts'</span>).where(<span class="hljs-string">'userid'</span>, <span class="hljs-string">'='</span>, <span class="hljs-number">1</span>).decrement(<span class="hljs-string">'balance'</span>, <span class="hljs-number">5</span>)</code><br data-reactid="1794"/><!-- react-text: 1795 -->Outputs:<!-- /react-text --><br data-reactid="1796"/><code class="sql hljs" data-reactid="1797"><span class="hljs-keyword">update</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`balance`</span> = <span class="hljs-string">`balance`</span> - <span class="hljs-number">5</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`userid`</span> = <span class="hljs-number">1</span></code></pre></div><div id="Builder-truncate" data-reactid="1798"><b data-reactid="1799">truncate</b><!-- react-text: 1800 --> — <!-- /react-text --><code data-reactid="1801">.truncate()</code><p data-reactid="1802">Truncates the current table.</p><pre class="display" data-reactid="1803"><code class="js hljs" data-reactid="1804">knex(<span class="hljs-string">'accounts'</span>).truncate()</code><br data-reactid="1805"/><!-- react-text: 1806 -->Outputs:<!-- /react-text --><br data-reactid="1807"/><code class="sql hljs" data-reactid="1808"><span class="hljs-keyword">truncate</span> <span class="hljs-string">`accounts`</span></code></pre></div><div id="Builder-pluck" data-reactid="1809"><b data-reactid="1810">pluck</b><!-- react-text: 1811 --> — <!-- /react-text --><code data-reactid="1812">.pluck(id)</code><p data-reactid="1813">This will pluck the specified column from each row in your results, yielding a promise which resolves to the array of values selected.</p><pre data-reactid="1814"><code class="hljs js" data-reactid="1815">knex.table(<span class="hljs-string">'users'</span>).pluck(<span class="hljs-string">'id'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{ <span class="hljs-built_in">console</span>.log(ids); });</code></pre></div><div id="Builder-first" data-reactid="1816"><b data-reactid="1817">first</b><!-- react-text: 1818 --> — <!-- /react-text --><code data-reactid="1819">.first([columns])</code><p data-reactid="1820">Similar to select, but only retrieves & resolves with the first record from the query.</p><pre data-reactid="1821"><code class="hljs js" data-reactid="1822">knex.table(<span class="hljs-string">'users'</span>).first(<span class="hljs-string">'id'</span>, <span class="hljs-string">'name'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">row</span>) </span>{ <span class="hljs-built_in">console</span>.log(row); });</code></pre></div><div id="Builder-clone" data-reactid="1823"><b data-reactid="1824">clone</b><!-- react-text: 1825 --> — <!-- /react-text --><code data-reactid="1826">.clone()</code><p data-reactid="1827">Clones the current query chain, useful for re-using partial query snippets in other queries without mutating the original.</p></div><div id="Builder-modify" data-reactid="1828"><b data-reactid="1829">modify</b><!-- react-text: 1830 --> — <!-- /react-text --><code data-reactid="1831">.modify(fn, *arguments)</code><p data-reactid="1832">Allows encapsulating and re-using query snippets and common behaviors as functions. The callback function should receive the query builder as its first argument, followed by the rest of the (optional) parameters passed to modify.</p><pre data-reactid="1833"><code class="hljs js" data-reactid="1834"><span class="hljs-keyword">var</span> withUserName = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">queryBuilder, foreignKey</span>) </span>{
queryBuilder.leftJoin(<span class="hljs-string">'users'</span>, foreignKey, <span class="hljs-string">'users.id'</span>).select(<span class="hljs-string">'users.user_name'</span>);
};
knex.table(<span class="hljs-string">'articles'</span>).select(<span class="hljs-string">'title'</span>, <span class="hljs-string">'body'</span>).modify(withUserName, <span class="hljs-string">'articles_user.id'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">article</span>) </span>{
<span class="hljs-built_in">console</span>.log(article.user_name);
});</code></pre></div><div id="Builder-columnInfo" data-reactid="1835"><b data-reactid="1836">columnInfo</b><!-- react-text: 1837 --> — <!-- /react-text --><code data-reactid="1838">.columnInfo([columnName])</code><p data-reactid="1839">Returns an object with the column info about the current table, or an individual column if one is passed, returning an object with the following keys:<em> <strong>defaultValue</strong>: the default value for the column</em> <strong>type</strong>: the column type<em> <strong>maxLength</strong>: the max length set for the column</em> <strong>nullable</strong>: whether the column may be null</p><pre data-reactid="1840"><code class="hljs js" data-reactid="1841">knex(<span class="hljs-string">'users'</span>).columnInfo().then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">info</span>) </span>{ <span class="hljs-comment">// ... });</span></code></pre></div><div id="Builder-debug" data-reactid="1842"><b data-reactid="1843">debug</b><!-- react-text: 1844 --> — <!-- /react-text --><code data-reactid="1845">.debug([enabled])</code><p data-reactid="1846">Overrides the global debug setting for the current query chain. If enabled is omitted, query debugging will be turned on.</p></div><div id="Builder-connection" data-reactid="1847"><b data-reactid="1848">connection</b><!-- react-text: 1849 --> — <!-- /react-text --><code data-reactid="1850">.connection(dbConnection)</code><p data-reactid="1851">Explicitly specify the connection for the query, allowing you to use the knex chain outside of the built-in pooling capabilities.</p></div><div id="Builder-options" data-reactid="1852"><b data-reactid="1853">options</b><!-- react-text: 1854 --> — <!-- /react-text --><code data-reactid="1855">.options()</code><p data-reactid="1856">Allows for mixing in additional options as defined by database client specific libraries:</p><pre data-reactid="1857"><code class="hljs js" data-reactid="1858">knex(<span class="hljs-string">'accounts as a1'</span>)
.leftJoin(<span class="hljs-string">'accounts as a2'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'a1.email'</span>, <span class="hljs-string">'<>'</span>, <span class="hljs-string">'a2.email'</span>);
})
.select([<span class="hljs-string">'a1.email'</span>, <span class="hljs-string">'a2.email'</span>])
.where(knex.raw(<span class="hljs-string">'a1.id = 1'</span>))
.option({ <span class="hljs-attr">nestTables</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">rowMode</span>: <span class="hljs-string">'array'</span> })
.limit(<span class="hljs-number">2</span>)
.then(...</code></pre></div></div><div id="Transactions" data-reactid="1859"><h2 id="Transactions" data-reactid="1860">Transactions</h2><div data-reactid="1861"><p data-reactid="1862">Transactions are an important feature of relational databases, as they allow correct recovery from failures and keep a database consistent even in cases of system failure. All queries within a transaction are executed on the same database connection, and run the entire set of queries as a single unit of work. Any failure will mean the database will rollback any queries executed on that connection to the pre-transaction state.</p><p data-reactid="1863">Transactions are handled by passing a handler function into <code>knex.transaction</code>. The handler function accepts a single argument, an object which may be used in two ways:<ol>
<li>As the "promise aware" knex connection</li>
<li>As an object passed into a query with <a href="#Builder-transacting"></a>and eventually call commit or rollback.</li>
</ol>
Consider these two examples:</p></div><pre data-reactid="1864"><code class="hljs js" data-reactid="1865"><span class="hljs-keyword">var</span> <span class="hljs-built_in">Promise</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bluebird'</span>);
<span class="hljs-comment">// Using trx as a query builder:</span>
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">trx</span>) </span>{
<span class="hljs-keyword">var</span> books = [
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Canterbury Tales'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Moby Dick'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Hamlet'</span>}
];
<span class="hljs-keyword">return</span> trx
.insert({<span class="hljs-attr">name</span>: <span class="hljs-string">'Old Books'</span>}, <span class="hljs-string">'id'</span>)
.into(<span class="hljs-string">'catalogues'</span>)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.map(books, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">book</span>) </span>{
book.catalogue_id = ids[<span class="hljs-number">0</span>];
<span class="hljs-comment">// Some validation could take place here.</span>
<span class="hljs-keyword">return</span> trx.insert(info).into(<span class="hljs-string">'books'</span>);
});
});
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">inserts</span>) </span>{
<span class="hljs-built_in">console</span>.log(inserts.length + <span class="hljs-string">' new books saved.'</span>);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-comment">// If we get here, that means that neither the 'Old Books' catalogues insert,</span>
<span class="hljs-comment">// nor any of the books inserts will have taken place.</span>
<span class="hljs-built_in">console</span>.error(error);
});</code></pre><p data-reactid="1866">And then this example:</p><pre data-reactid="1867"><code class="hljs js" data-reactid="1868"><span class="hljs-keyword">var</span> <span class="hljs-built_in">Promise</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bluebird'</span>);
<span class="hljs-comment">// Using trx as a transaction object:</span>
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">trx</span>) </span>{
<span class="hljs-keyword">var</span> books = [
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Canterbury Tales'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Moby Dick'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Hamlet'</span>}
];
knex.insert({<span class="hljs-attr">name</span>: <span class="hljs-string">'Old Books'</span>}, <span class="hljs-string">'id'</span>)
.into(<span class="hljs-string">'catalogues'</span>)
.transacting(trx)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.map(books, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">book</span>) </span>{
book.catalogue_id = ids[<span class="hljs-number">0</span>];
<span class="hljs-comment">// Some validation could take place here.</span>
<span class="hljs-keyword">return</span> knex.insert(info).into(<span class="hljs-string">'books'</span>).transacting(trx);
});
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">inserts</span>) </span>{
<span class="hljs-built_in">console</span>.log(inserts.length + <span class="hljs-string">' new books saved.'</span>);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-comment">// If we get here, that means that neither the 'Old Books' catalogues insert,</span>
<span class="hljs-comment">// nor any of the books inserts will have taken place.</span>
<span class="hljs-built_in">console</span>.error(error);
});</code></pre><div data-reactid="1869"><p data-reactid="1870">Throwing an error directly from the transaction handler function automatically rolls back the transaction, same as returning a rejected promise.</p><p data-reactid="1871">Notice that if a promise is not returned within the handler, it is up to you to ensure <code>trx.commit</code>, or <code>trx.rollback</code> are called, otherwise the transaction connection will hang.</p></div></div><div id="Schema" data-reactid="1872"><h2 id="Schema" data-reactid="1873">Schema Builder</h2><p data-reactid="1874">The <code>knex.schema</code> is a <strong>getter function</strong>, which returns a stateful object containing the query. Therefore be sure to obtain a new instance of the <code>knex.schema</code> for every query. These methods return <a href="http://knexjs.org/#Interfaces-Promises">promises</a>.</p><div id="Schema-withSchema" data-reactid="1875"><b data-reactid="1876">withSchema</b><!-- react-text: 1877 --> — <!-- /react-text --><code data-reactid="1878">knex.schema.withSchema([schemaName])</code><p data-reactid="1879">Specifies the schema to be used when using the schema-building commands.</p><pre class="display" data-reactid="1880"><code class="js hljs" data-reactid="1881">knex.schema.withSchema(<span class="hljs-string">'public'</span>).createTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.increments();
})</code><br data-reactid="1882"/><!-- react-text: 1883 -->Outputs:<!-- /react-text --><br data-reactid="1884"/><code class="sql hljs" data-reactid="1885"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`public`</span>.<span class="hljs-string">`users`</span> (<span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> auto_increment primary <span class="hljs-keyword">key</span>)</code></pre></div><div id="Schema-createTable" data-reactid="1886"><b data-reactid="1887">createTable</b><!-- react-text: 1888 --> — <!-- /react-text --><code data-reactid="1889">knex.schema.createTable(tableName, callback)</code><p data-reactid="1890">Creates a new table on the database, with a callback function to modify the table's structure, using the schema-building commands.</p><pre class="display" data-reactid="1891"><code class="js hljs" data-reactid="1892">knex.schema.createTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.increments();
table.string(<span class="hljs-string">'name'</span>);
table.timestamps();
})</code><br data-reactid="1893"/><!-- react-text: 1894 -->Outputs:<!-- /react-text --><br data-reactid="1895"/><code class="sql hljs" data-reactid="1896"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> (<span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> auto_increment primary <span class="hljs-keyword">key</span>, <span class="hljs-string">`name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-string">`created_at`</span> datetime, <span class="hljs-string">`updated_at`</span> datetime)</code></pre></div><div id="Schema-createTableIfNotExists" data-reactid="1897"><b data-reactid="1898">createTableIfNotExists</b><!-- react-text: 1899 --> — <!-- /react-text --><code data-reactid="1900">knex.schema.createTableIfNotExists(tableName, callback)</code><p data-reactid="1901">Creates a new table on the database if it doesn't exists on database, with a callback function to modify the table's structure, using the schema-building commands.</p><pre class="display" data-reactid="1902"><code class="js hljs" data-reactid="1903">knex.schema.createTableIfNotExists(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.increments();
table.string(<span class="hljs-string">'name'</span>);
table.timestamps();
})</code><br data-reactid="1904"/><!-- react-text: 1905 -->Outputs:<!-- /react-text --><br data-reactid="1906"/><code class="sql hljs" data-reactid="1907"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> <span class="hljs-string">`users`</span> (<span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> auto_increment primary <span class="hljs-keyword">key</span>, <span class="hljs-string">`name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-string">`created_at`</span> datetime, <span class="hljs-string">`updated_at`</span> datetime)</code></pre></div><div id="Schema-renameTable" data-reactid="1908"><b data-reactid="1909">renameTable</b><!-- react-text: 1910 --> — <!-- /react-text --><code data-reactid="1911">knex.schema.renameTable(from, to)</code><p data-reactid="1912">Renames a table from a current tableName to another.</p><pre class="display" data-reactid="1913"><code class="js hljs" data-reactid="1914">knex.schema.renameTable(<span class="hljs-string">'users'</span>, <span class="hljs-string">'old_users'</span>)</code><br data-reactid="1915"/><!-- react-text: 1916 -->Outputs:<!-- /react-text --><br data-reactid="1917"/><code class="sql hljs" data-reactid="1918"><span class="hljs-keyword">rename</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">to</span> <span class="hljs-string">`old_users`</span></code></pre></div><div id="Schema-dropTable" data-reactid="1919"><b data-reactid="1920">dropTable</b><!-- react-text: 1921 --> — <!-- /react-text --><code data-reactid="1922">knex.schema.dropTable(tableName)</code><p data-reactid="1923">Drops a table, specified by tableName.</p><pre class="display" data-reactid="1924"><code class="js hljs" data-reactid="1925">knex.schema.dropTable(<span class="hljs-string">'users'</span>)</code><br data-reactid="1926"/><!-- react-text: 1927 -->Outputs:<!-- /react-text --><br data-reactid="1928"/><code class="sql hljs" data-reactid="1929"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Schema-hasTable" data-reactid="1930"><b data-reactid="1931">hasTable</b><!-- react-text: 1932 --> — <!-- /react-text --><code data-reactid="1933">knex.schema.hasTable(tableName)</code><p data-reactid="1934">Checks for a table's existence by tableName, resolving with a boolean to signal if the table exists.</p><pre data-reactid="1935"><code class="hljs js" data-reactid="1936">knex.schema.hasTable(<span class="hljs-string">'users'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">exists</span>) </span>{
<span class="hljs-keyword">if</span> (!exists) {
<span class="hljs-keyword">return</span> knex.schema.createTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">t</span>) </span>{
t.increments(<span class="hljs-string">'id'</span>).primary();
t.string(<span class="hljs-string">'first_name'</span>, <span class="hljs-number">100</span>);
t.string(<span class="hljs-string">'last_name'</span>, <span class="hljs-number">100</span>);
t.text(<span class="hljs-string">'bio'</span>);
});
}
});</code></pre></div><div id="Schema-hasColumn" data-reactid="1937"><b data-reactid="1938">hasColumn</b><!-- react-text: 1939 --> — <!-- /react-text --><code data-reactid="1940">knex.schema.hasColumn(tableName, columnName)</code><p data-reactid="1941">Checks if a column exists in the current table, resolves the promise with a boolean, true if the column exists, false otherwise.</p></div><div id="Schema-dropTableIfExists" data-reactid="1942"><b data-reactid="1943">dropTableIfExists</b><!-- react-text: 1944 --> — <!-- /react-text --><code data-reactid="1945">knex.schema.dropTableIfExists(tableName)</code><p data-reactid="1946">Drops a table conditionally if the table exists, specified by tableName.</p><pre class="display" data-reactid="1947"><code class="js hljs" data-reactid="1948">knex.schema.dropTableIfExists(<span class="hljs-string">'users'</span>)</code><br data-reactid="1949"/><!-- react-text: 1950 -->Outputs:<!-- /react-text --><br data-reactid="1951"/><code class="sql hljs" data-reactid="1952"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">exists</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Schema-table" data-reactid="1953"><b data-reactid="1954">table</b><!-- react-text: 1955 --> — <!-- /react-text --><code data-reactid="1956">knex.schema.table(tableName, callback)</code><p data-reactid="1957">Chooses a database table, and then modifies the table, using the Schema Building functions inside of the callback.</p><pre class="display" data-reactid="1958"><code class="js hljs" data-reactid="1959">knex.schema.table(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.dropColumn(<span class="hljs-string">'name'</span>);
table.string(<span class="hljs-string">'first_name'</span>);
table.string(<span class="hljs-string">'last_name'</span>);
})</code><br data-reactid="1960"/><!-- react-text: 1961 -->Outputs:<!-- /react-text --><br data-reactid="1962"/><code class="sql hljs" data-reactid="1963"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">add</span> <span class="hljs-string">`first_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-keyword">add</span> <span class="hljs-string">`last_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>);
<span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">drop</span> <span class="hljs-string">`name`</span></code></pre></div><div id="Schema-raw" data-reactid="1964"><b data-reactid="1965">raw</b><!-- react-text: 1966 --> — <!-- /react-text --><code data-reactid="1967">knex.schema.raw(statement)</code><p data-reactid="1968">Run an arbitrary sql query in the schema builder chain.</p><pre class="display" data-reactid="1969"><code class="js hljs" data-reactid="1970">knex.schema.raw(<span class="hljs-string">"SET sql_mode='TRADITIONAL'"</span>)
.table(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.dropColumn(<span class="hljs-string">'name'</span>);
table.string(<span class="hljs-string">'first_name'</span>);
table.string(<span class="hljs-string">'last_name'</span>);
})</code><br data-reactid="1971"/><!-- react-text: 1972 -->Outputs:<!-- /react-text --><br data-reactid="1973"/><code class="sql hljs" data-reactid="1974"><span class="hljs-keyword">SET</span> sql_mode=<span class="hljs-string">'TRADITIONAL'</span>;
<span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">add</span> <span class="hljs-string">`first_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-keyword">add</span> <span class="hljs-string">`last_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>);
<span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">drop</span> <span class="hljs-string">`name`</span></code></pre></div><h3 id="Schema-Building" data-reactid="1975">Schema Building:</h3><div id="Schema-dropColumn" data-reactid="1976"><b data-reactid="1977">dropColumn</b><!-- react-text: 1978 --> — <!-- /react-text --><code data-reactid="1979">table.dropColumn(name)</code><p data-reactid="1980">Drops a column, specified by the column's name</p></div><div id="Schema-dropColumns" data-reactid="1981"><b data-reactid="1982">dropColumns</b><!-- react-text: 1983 --> — <!-- /react-text --><code data-reactid="1984">table.dropColumns(*columns)</code><p data-reactid="1985">Drops multiple columns, taking a variable number of column names.</p></div><div id="Schema-renameColumn" data-reactid="1986"><b data-reactid="1987">renameColumn</b><!-- react-text: 1988 --> — <!-- /react-text --><code data-reactid="1989">table.renameColumn(from, to)</code><p data-reactid="1990">Renames a column from one name to another.</p></div><div id="Schema-increments" data-reactid="1991"><b data-reactid="1992">increments</b><!-- react-text: 1993 --> — <!-- /react-text --><code data-reactid="1994">table.increments(name)</code><p data-reactid="1995">Adds an auto incrementing column, in PostgreSQL this is a serial. This will be used as the primary key for the table. Also available is a bigIncrements if you wish to add a bigint incrementing number (in PostgreSQL bigserial).</p></div><div id="Schema-integer" data-reactid="1996"><b data-reactid="1997">integer</b><!-- react-text: 1998 --> — <!-- /react-text --><code data-reactid="1999">table.integer(name)</code><p data-reactid="2000">Adds an integer column.</p></div><div id="Schema-bigInteger" data-reactid="2001"><b data-reactid="2002">bigInteger</b><!-- react-text: 2003 --> — <!-- /react-text --><code data-reactid="2004">table.bigInteger(name)</code><p data-reactid="2005">In MySQL or PostgreSQL, adds a bigint column, otherwise adds a normal integer. Note that bigint data is returned as a string in queries because JavaScript may be unable to parse them without loss of precision.</p></div><div id="Schema-text" data-reactid="2006"><b data-reactid="2007">text</b><!-- react-text: 2008 --> — <!-- /react-text --><code data-reactid="2009">table.text(name, [textType])</code><p data-reactid="2010">Adds a text column, with optional textType for MySql text datatype preference. textType may be mediumtext or longtext, otherwise defaults to text.</p></div><div id="Schema-string" data-reactid="2011"><b data-reactid="2012">string</b><!-- react-text: 2013 --> — <!-- /react-text --><code data-reactid="2014">table.string(name, [length])</code><p data-reactid="2015">Adds a string column, with optional length defaulting to 255.</p></div><div id="Schema-float" data-reactid="2016"><b data-reactid="2017">float</b><!-- react-text: 2018 --> — <!-- /react-text --><code data-reactid="2019">table.float(column, [precision], [scale])</code><p data-reactid="2020">Adds a float column, with optional precision (defaults to 8) and scale (defaults to 2).</p></div><div id="Schema-decimal" data-reactid="2021"><b data-reactid="2022">decimal</b><!-- react-text: 2023 --> — <!-- /react-text --><code data-reactid="2024">table.decimal(column, [precision], [scale])</code><p data-reactid="2025">Adds a decimal column, with optional precision (defaults to 8) and scale (defaults to 2).</p></div><div id="Schema-boolean" data-reactid="2026"><b data-reactid="2027">boolean</b><!-- react-text: 2028 --> — <!-- /react-text --><code data-reactid="2029">table.boolean(name)</code><p data-reactid="2030">Adds a boolean column.</p></div><div id="Schema-date" data-reactid="2031"><b data-reactid="2032">date</b><!-- react-text: 2033 --> — <!-- /react-text --><code data-reactid="2034">table.date(name)</code><p data-reactid="2035">Adds a date column.</p></div><div id="Schema-dateTime" data-reactid="2036"><b data-reactid="2037">dateTime</b><!-- react-text: 2038 --> — <!-- /react-text --><code data-reactid="2039">table.dateTime(name)</code><p data-reactid="2040">Adds a dateTime column.</p></div><div id="Schema-time" data-reactid="2041"><b data-reactid="2042">time</b><!-- react-text: 2043 --> — <!-- /react-text --><code data-reactid="2044">table.time(name)</code><p data-reactid="2045">Adds a time column.</p></div><div id="Schema-timestamp" data-reactid="2046"><b data-reactid="2047">timestamp</b><!-- react-text: 2048 --> — <!-- /react-text --><code data-reactid="2049">table.timestamp(name, [standard])</code><p data-reactid="2050">Adds a timestamp column, defaults to timestamptz in PostgreSQL, unless true is passed as the second argument. For Example:</p><pre data-reactid="2051"><code class="hljs js" data-reactid="2052">table.timestamp(<span class="hljs-string">'created_at'</span>).defaultTo(knex.fn.now());</code></pre></div><div id="Schema-timestamps" data-reactid="2053"><b data-reactid="2054">timestamps</b><!-- react-text: 2055 --> — <!-- /react-text --><code data-reactid="2056">table.timestamps([useTimestamps], [defaultToNow])</code><p data-reactid="2057">Adds a created_at and updated_at column on the database, setting these each to dateTime types. When true is passed as the first argument a timestamp type is used. Both colums default to being not null and the current timestamp when true is passed as the second argument.</p></div><div id="Schema-dropTimestamps" data-reactid="2058"><b data-reactid="2059">dropTimestamps</b><!-- react-text: 2060 --> — <!-- /react-text --><code data-reactid="2061">table.dropTimestamps()</code><p data-reactid="2062">Drops the columns created_at and updated_at from the table, which can be created via timestamps.</p></div><div id="Schema-binary" data-reactid="2063"><b data-reactid="2064">binary</b><!-- react-text: 2065 --> — <!-- /react-text --><code data-reactid="2066">table.binary(name, [length])</code><p data-reactid="2067">Adds a binary column, with optional length argument for MySQL.</p></div><div id="Schema-enum / enu" data-reactid="2068"><b data-reactid="2069">enum / enu</b><!-- react-text: 2070 --> — <!-- /react-text --><code data-reactid="2071">table.enu(col, values)</code><p data-reactid="2072">Adds a enum column, (aliased to enu, as enum is a reserved word in JavaScript). Note that the second argument is an array of values. Example:</p><pre data-reactid="2073"><code class="hljs js" data-reactid="2074">table.enu(<span class="hljs-string">'column'</span>, [<span class="hljs-string">'value1'</span>, <span class="hljs-string">'value2'</span>])</code></pre></div><div id="Schema-json" data-reactid="2075"><b data-reactid="2076">json</b><!-- react-text: 2077 --> — <!-- /react-text --><code data-reactid="2078">table.json(name)</code><p data-reactid="2079">Adds a json column, using the built-in json type in postgresql, defaulting to a text column in older versions of postgresql or in unsupported databases. Note that when setting an array (or a value that could be an array) as the value of a json or jsonb column, you should use JSON.stringify() to convert your value to a string prior to passing it to the query builder, e.g.</p><pre data-reactid="2080"><code class="hljs js" data-reactid="2081">knex.table(<span class="hljs-string">'users'</span>)
.where({<span class="hljs-attr">id</span>: <span class="hljs-number">1</span>})
.update({<span class="hljs-attr">json_data</span>: <span class="hljs-built_in">JSON</span>.stringify(mightBeAnArray)});</code></pre><p data-reactid="2082">This is because postgresql has a native array type which uses a syntax incompatible with json; knex has no way of knowing which syntax to use, and calling JSON.stringify() forces json-style syntax.</p></div><div id="Schema-jsonb" data-reactid="2083"><b data-reactid="2084">jsonb</b><!-- react-text: 2085 --> — <!-- /react-text --><code data-reactid="2086">table.jsonb(name)</code><p data-reactid="2087">Adds a jsonb column. Works similar to table.json(), but uses native jsonb type if possible.</p></div><div id="Schema-uuid" data-reactid="2088"><b data-reactid="2089">uuid</b><!-- react-text: 2090 --> — <!-- /react-text --><code data-reactid="2091">table.uuid(name)</code><p data-reactid="2092">Adds a uuid column - this uses the built-in uuid type in postgresql, and falling back to a char(36) in other databases.</p></div><div id="Schema-comment" data-reactid="2093"><b data-reactid="2094">comment</b><!-- react-text: 2095 --> — <!-- /react-text --><code data-reactid="2096">table.comment(value)</code><p data-reactid="2097">Sets the comment for a table.</p></div><div id="Schema-engine" data-reactid="2098"><b data-reactid="2099">engine</b><!-- react-text: 2100 --> — <!-- /react-text --><code data-reactid="2101">table.engine(val)</code><p data-reactid="2102">Sets the engine for the database table, only available within a createTable call, and only applicable to MySQL.</p></div><div id="Schema-charset" data-reactid="2103"><b data-reactid="2104">charset</b><!-- react-text: 2105 --> — <!-- /react-text --><code data-reactid="2106">table.charset(val)</code><p data-reactid="2107">Sets the charset for the database table, only available within a createTable call, and only applicable to MySQL.</p></div><div id="Schema-collate" data-reactid="2108"><b data-reactid="2109">collate</b><!-- react-text: 2110 --> — <!-- /react-text --><code data-reactid="2111">table.collate(val)</code><p data-reactid="2112">Sets the collation for the database table, only available within a createTable call, and only applicable to MySQL.</p></div><div id="Schema-inherits" data-reactid="2113"><b data-reactid="2114">inherits</b><!-- react-text: 2115 --> — <!-- /react-text --><code data-reactid="2116">table.inherits(val)</code><p data-reactid="2117">Sets the tables that this table inherits, only available within a createTable call, and only applicable to PostgreSQL.</p></div><div id="Schema-specificType" data-reactid="2118"><b data-reactid="2119">specificType</b><!-- react-text: 2120 --> — <!-- /react-text --><code data-reactid="2121">table.specificType(column, value)</code><p data-reactid="2122">Sets a specific type for the column creation, if you'd like to add a column type that isn't supported here.</p></div><div id="Schema-index" data-reactid="2123"><b data-reactid="2124">index</b><!-- react-text: 2125 --> — <!-- /react-text --><code data-reactid="2126">table.index(columns, [indexName], [indexType])</code><p data-reactid="2127">Adds an index to a table over the given columns. A default index name using the columns is used unless indexName is specified. The indexType can be optionally specified for PostgreSQL.</p></div><div id="Schema-dropIndex" data-reactid="2128"><b data-reactid="2129">dropIndex</b><!-- react-text: 2130 --> — <!-- /react-text --><code data-reactid="2131">table.dropIndex(columns, [indexName])</code><p data-reactid="2132">Drops an index from a table. A default index name using the columns is used unless indexName is specified (in which case columns is ignored).</p></div><div id="Schema-unique" data-reactid="2133"><b data-reactid="2134">unique</b><!-- react-text: 2135 --> — <!-- /react-text --><code data-reactid="2136">table.unique(columns)</code><p data-reactid="2137">Adds an unique index to a table over the given <code>columns</code></p><pre data-reactid="2138"><code class="hljs js" data-reactid="2139">knex.schema.alterTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">t</span>) </span>{
t.unique(<span class="hljs-string">'email'</span>)
})
knex.schema.alterTable(<span class="hljs-string">'job'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">t</span>) </span>{
t.unique([<span class="hljs-string">'account_id'</span>, <span class="hljs-string">'program_id'</span>])
})</code></pre></div><div id="Schema-foreign" data-reactid="2140"><b data-reactid="2141">foreign</b><!-- react-text: 2142 --> — <!-- /react-text --><code data-reactid="2143">table.foreign(columns)</code><p data-reactid="2144">Adds a foreign key constraint to a table for an existing column using <code>table.foreign(column).references(column)</code> or multiple columns using <code>table.foreign(columns).references(columns)</code>. You can also chain onDelete and/or onUpdate to set the reference option (RESTRICT, CASCADE, SET NULL, NO ACTION) for the operation. Note, this is the same as column.references(column) but works for existing columns.</p><pre data-reactid="2145"><code class="hljs js" data-reactid="2146">knex.schema.table(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.integer(<span class="hljs-string">'user_id'</span>).unsigned()
table.foreign(<span class="hljs-string">'user_id'</span>).references(<span class="hljs-string">'Items.user_id_in_items'</span>)
})</code></pre></div><div id="Schema-dropForeign" data-reactid="2147"><b data-reactid="2148">dropForeign</b><!-- react-text: 2149 --> — <!-- /react-text --><code data-reactid="2150">table.dropForeign(columns, [foreignKeyName])</code><p data-reactid="2151">Drops a foreign key constraint from a table. A default foreign key name using the columns is used unless foreignKeyName is specified (in which case columns is ignored).</p></div><div id="Schema-dropUnique" data-reactid="2152"><b data-reactid="2153">dropUnique</b><!-- react-text: 2154 --> — <!-- /react-text --><code data-reactid="2155">table.dropUnique(columns, [indexName])</code><p data-reactid="2156">Drops a unique key constraint from a table. A default unique key name using the columns is used unless indexName is specified (in which case columns is ignored).</p></div><div id="Schema-dropPrimary" data-reactid="2157"><b data-reactid="2158">dropPrimary</b><!-- react-text: 2159 --> — <!-- /react-text --><code data-reactid="2160">table.dropPrimary([constraintName])</code><p data-reactid="2161">Drops the primary key constraint on a table. Defaults to tablename_pkey unless constraintName is specified.</p></div><h3 id="Chainable" data-reactid="2162">Chainable Methods:</h3><p data-reactid="2163">The following three methods may be chained on the schema building methods, as modifiers to the column.</p><div id="Schema-alter" data-reactid="2164"><b data-reactid="2165">alter</b><!-- react-text: 2166 --> — <!-- /react-text --><code data-reactid="2167">column.alter()</code><p data-reactid="2168">Marks the column as an alter / modify, instead of the default add. Note: This only works in .alterTable() and is not supported by SQlite. Alter is <em>not</em> done incrementally over older column type so if you like to add <code>notNull</code> and keep the old default value, the alter statement must contain both <code>.notNull().defaultTo(1).alter()</code>. If one just tries to add <code>.notNull().alter()</code> the old default value will be dropped.</p></div><pre data-reactid="2169"><code data-reactid="2170">knex.schema.alterTable('user', function(t) {
t.increments().primary(); // add
// drops previous default value from column, change type to string and add not nullable constraint
t.string('username', 35).notNullable().alter();
// drops both not null contraint and the default value
t.integer('age').alter();
});</code></pre><div id="Schema-index" data-reactid="2171"><b data-reactid="2172">index</b><!-- react-text: 2173 --> — <!-- /react-text --><code data-reactid="2174">column.index([indexName], [indexType])</code><p data-reactid="2175">Specifies a field as an index. If an indexName is specified, it is used in place of the standard index naming convention of tableName_columnName. The indexType can be optionally specified for PostgreSQL. No-op if this is chained off of a field that cannot be indexed.</p></div><div id="Schema-primary" data-reactid="2176"><b data-reactid="2177">primary</b><!-- react-text: 2178 --> — <!-- /react-text --><code data-reactid="2179">column.primary([constraintName])</code><p data-reactid="2180"><pre><code> When called on a single column it will set that column as the primary key for a table.
To create a compound primary key, pass an array of column names:
`table.primary(['column1', 'column2'])`.
Constraint name defaults to `tablename_pkey` unless `constraintName` is specified.
</code></pre></p></div><div id="Schema-unique" data-reactid="2181"><b data-reactid="2182">unique</b><!-- react-text: 2183 --> — <!-- /react-text --><code data-reactid="2184">column.unique()</code><p data-reactid="2185">Sets the column as unique.</p></div><div id="Schema-references" data-reactid="2186"><b data-reactid="2187">references</b><!-- react-text: 2188 --> — <!-- /react-text --><code data-reactid="2189">column.references(column)</code><p data-reactid="2190">Sets the "column" that the current column references as a foreign key. "column" can either be ".<column>" syntax, or just the column name followed up with a call to inTable to specify the table.</column><table></table></p></div><div id="Schema-inTable" data-reactid="2191"><b data-reactid="2192">inTable</b><!-- react-text: 2193 --> — <!-- /react-text --><code data-reactid="2194">column.inTable(table)</code><p data-reactid="2195">Sets the "table" where the foreign key column is located after calling column.references.</p></div><div id="Schema-onDelete" data-reactid="2196"><b data-reactid="2197">onDelete</b><!-- react-text: 2198 --> — <!-- /react-text --><code data-reactid="2199">column.onDelete(command)</code><p data-reactid="2200">Sets the SQL command to be run "onDelete".</p></div><div id="Schema-onUpdate" data-reactid="2201"><b data-reactid="2202">onUpdate</b><!-- react-text: 2203 --> — <!-- /react-text --><code data-reactid="2204">column.onUpdate(command)</code><p data-reactid="2205">Sets the SQL command to be run "onUpdate".</p></div><div id="Schema-defaultTo" data-reactid="2206"><b data-reactid="2207">defaultTo</b><!-- react-text: 2208 --> — <!-- /react-text --><code data-reactid="2209">column.defaultTo(value)</code><p data-reactid="2210">Sets the default value for the column on an insert.</p></div><div id="Schema-unsigned" data-reactid="2211"><b data-reactid="2212">unsigned</b><!-- react-text: 2213 --> — <!-- /react-text --><code data-reactid="2214">column.unsigned()</code><p data-reactid="2215">Specifies an integer as unsigned. No-op if this is chained off of a non-integer field.</p></div><div id="Schema-notNullable" data-reactid="2216"><b data-reactid="2217">notNullable</b><!-- react-text: 2218 --> — <!-- /react-text --><code data-reactid="2219">column.notNullable()</code><p data-reactid="2220">Adds a not null on the current column being created.</p></div><div id="Schema-nullable" data-reactid="2221"><b data-reactid="2222">nullable</b><!-- react-text: 2223 --> — <!-- /react-text --><code data-reactid="2224">column.nullable()</code><p data-reactid="2225">Default on column creation, this explicitly sets a field to be nullable.</p></div><div id="Schema-first" data-reactid="2226"><b data-reactid="2227">first</b><!-- react-text: 2228 --> — <!-- /react-text --><code data-reactid="2229">column.first()</code><p data-reactid="2230">Sets the column to be inserted on the first position, only used in MySQL alter tables.</p></div><div id="Schema-after" data-reactid="2231"><b data-reactid="2232">after</b><!-- react-text: 2233 --> — <!-- /react-text --><code data-reactid="2234">column.after(field)</code><p data-reactid="2235">Sets the column to be inserted after another, only used in MySQL alter tables.</p></div><div id="Schema-comment" data-reactid="2236"><b data-reactid="2237">comment</b><!-- react-text: 2238 --> — <!-- /react-text --><code data-reactid="2239">column.comment(value)</code><p data-reactid="2240">Sets the comment for a column.</p></div><pre data-reactid="2241"><code data-reactid="2242">knex.schema.createTable('accounts', function(t) {
t.increments().primary();
t.string('email').unique().comment('This is the email field');
});</code></pre><div id="Schema-collate" data-reactid="2243"><b data-reactid="2244">collate</b><!-- react-text: 2245 --> — <!-- /react-text --><code data-reactid="2246">column.collate(collation)</code><p data-reactid="2247">Sets the collation for a column (only works in MySQL). Here is a list of all available collations: <a href="https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html">https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html</a></p></div><pre data-reactid="2248"><code data-reactid="2249">knex.schema.createTable('users', function(t) {
t.increments();
t.string('email').unique().collate('utf8_unicode_ci');
});</code></pre></div><div id="Raw" data-reactid="2250"><h2 id="Raw" data-reactid="2251">Raw</h2><p data-reactid="2252">Sometimes you may need to use a raw expression in a query. Raw query object may be injected pretty much anywhere you want, and using proper bindings can ensure your values are escaped properly, preventing SQL-injection attacks.</p><h3 id="Raw-Bindings" data-reactid="2253">Raw Parameter Binding:</h3><p data-reactid="2254">One can paramterize sql given to <code>knex.raw(sql, bindings)</code>. Parameters can be positional named. One can also choose if parameter should be treated as value or as sql identifier e.g. in case of <code>'TableName.ColumnName'</code> reference.</p><pre class="display" data-reactid="2255"><code class="js hljs" data-reactid="2256">knex(<span class="hljs-string">'users'</span>)
.select(knex.raw(<span class="hljs-string">'count(*) as user_count, status'</span>))
.where(knex.raw(<span class="hljs-number">1</span>))
.orWhere(knex.raw(<span class="hljs-string">'status <> ?'</span>, [<span class="hljs-number">1</span>]))
.groupBy(<span class="hljs-string">'status'</span>)</code><br data-reactid="2257"/><!-- react-text: 2258 -->Outputs:<!-- /react-text --><br data-reactid="2259"/><code class="sql hljs" data-reactid="2260"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(*) <span class="hljs-keyword">as</span> user_count, <span class="hljs-keyword">status</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">status</span> <> <span class="hljs-number">1</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`status`</span></code></pre><p data-reactid="2261">Positional bindings <code>?</code> are interpreted as values and <code>??</code> are interpreted as identifiers.</p><pre class="display" data-reactid="2262"><code class="js hljs" data-reactid="2263">knex(<span class="hljs-string">'users'</span>).where(knex.raw(<span class="hljs-string">'?? = ?'</span>, [<span class="hljs-string">'user.name'</span>, <span class="hljs-number">1</span>]))</code><br data-reactid="2264"/><!-- react-text: 2265 -->Outputs:<!-- /react-text --><br data-reactid="2266"/><code class="sql hljs" data-reactid="2267"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`user`</span>.<span class="hljs-string">`name`</span> = <span class="hljs-number">1</span></code></pre><p data-reactid="2268">Named bindings such as <code>:name</code> are interpreted as values and <code>:name:</code> interpreted as identifiers. Named bindings are processed so long as the value is anything other than <code>undefined</code>.</p><pre class="display" data-reactid="2269"><code class="js hljs" data-reactid="2270">knex(<span class="hljs-string">'users'</span>)
.where(knex.raw(<span class="hljs-string">':name: = :thisGuy or :name: = :otherGuy or :name: = :undefinedBinding'</span>, {
<span class="hljs-attr">name</span>: <span class="hljs-string">'users.name'</span>,
<span class="hljs-attr">thisGuy</span>: <span class="hljs-string">'Bob'</span>,
<span class="hljs-attr">otherGuy</span>: <span class="hljs-string">'Jay'</span>,
<span class="hljs-attr">undefinedBinding</span>: <span class="hljs-literal">undefined</span>
}))</code><br data-reactid="2271"/><!-- react-text: 2272 -->Error:<!-- /react-text --><br data-reactid="2273"/><code class="sql hljs" data-reactid="2274">Undefined binding(s) detected when compiling RAW query: `users`.`name` = ? or `users`.`name` = ? or `users`.`name` = :undefinedBinding</code></pre><p data-reactid="2275">For simpler queries where one only has a single binding, <code>.raw</code> can accept said binding as its second parameter.</p><pre class="display" data-reactid="2276"><code class="js hljs" data-reactid="2277">knex(<span class="hljs-string">'users'</span>)
.where(
knex.raw(<span class="hljs-string">'LOWER("login") = ?'</span>, <span class="hljs-string">'knex'</span>)
)
.orWhere(
knex.raw(<span class="hljs-string">'accesslevel = ?'</span>, <span class="hljs-number">1</span>)
)
.orWhere(
knex.raw(<span class="hljs-string">'updtime = ?'</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>.UTC(<span class="hljs-string">'01-01-2016'</span>))
)</code><br data-reactid="2278"/><!-- react-text: 2279 -->Error:<!-- /react-text --><br data-reactid="2280"/><code class="sql hljs" data-reactid="2281">Date.UTC is not a constructor</code></pre><p data-reactid="2282">Note that due to ambiguity, arrays must be passed as arguments within a containing array.</p><pre class="display" data-reactid="2283"><code class="js hljs" data-reactid="2284">knex.raw(<span class="hljs-string">'select * from users where id in (?)'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
<span class="hljs-comment">// Error: Expected 3 bindings, saw 1</span>
knex.raw(<span class="hljs-string">'select * from users where id in (?)'</span>, [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]])</code><br data-reactid="2285"/><!-- react-text: 2286 -->Outputs:<!-- /react-text --><br data-reactid="2287"/><code class="sql hljs" data-reactid="2288"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-keyword">users</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">id</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)</code></pre><p data-reactid="2289">To prevent replacement of <code>?</code> one can use the escape sequence <code>\\?</code>.</p><pre class="display" data-reactid="2290"><code class="js hljs" data-reactid="2291">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-number">1</span>).whereRaw(<span class="hljs-string">'?? \\? ?'</span>, [<span class="hljs-string">'jsonColumn'</span>, <span class="hljs-string">'jsonKey'</span>])</code><br data-reactid="2292"/><!-- react-text: 2293 -->Outputs:<!-- /react-text --><br data-reactid="2294"/><code class="sql hljs" data-reactid="2295"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`jsonColumn`</span> ? <span class="hljs-string">'jsonKey'</span></code></pre><p data-reactid="2296">To prevent replacement of named bindings one can use the escape sequence <code>\\:</code>.</p><pre class="display" data-reactid="2297"><code class="js hljs" data-reactid="2298">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereRaw(<span class="hljs-string">":property: = '\\:value' OR \\:property: = :value"</span>, {
<span class="hljs-attr">property</span>: <span class="hljs-string">'name'</span>,
<span class="hljs-attr">value</span>: <span class="hljs-string">'Bob'</span>
})</code><br data-reactid="2299"/><!-- react-text: 2300 -->Outputs:<!-- /react-text --><br data-reactid="2301"/><code class="sql hljs" data-reactid="2302"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">':value'</span> <span class="hljs-keyword">OR</span> :property: = <span class="hljs-string">'Bob'</span></code></pre><h3 id="Raw-Expressions" data-reactid="2303">Raw Expressions:</h3><p data-reactid="2304">Raw expressions are created by using <code>knex.raw(sql, [bindings])</code> and passing this as a value for any value in the query chain.</p><pre class="display" data-reactid="2305"><code class="js hljs" data-reactid="2306">knex(<span class="hljs-string">'users'</span>)
.select(knex.raw(<span class="hljs-string">'count(*) as user_count, status'</span>))
.where(knex.raw(<span class="hljs-number">1</span>))
.orWhere(knex.raw(<span class="hljs-string">'status <> ?'</span>, [<span class="hljs-number">1</span>]))
.groupBy(<span class="hljs-string">'status'</span>)</code><br data-reactid="2307"/><!-- react-text: 2308 -->Outputs:<!-- /react-text --><br data-reactid="2309"/><code class="sql hljs" data-reactid="2310"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(*) <span class="hljs-keyword">as</span> user_count, <span class="hljs-keyword">status</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">status</span> <> <span class="hljs-number">1</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`status`</span></code></pre><h3 id="Raw-Queries" data-reactid="2311">Raw Queries:</h3><p data-reactid="2312">The <code>knex.raw</code> may also be used to build a full query and execute it, as a standard query builder query would be executed. The benefit of this is that it uses the connection pool and provides a standard interface for the different client libraries.</p><pre data-reactid="2313"><code class="hljs js" data-reactid="2314">knex.raw(<span class="hljs-string">'select * from users where id = ?'</span>, [<span class="hljs-number">1</span>]).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) </span>{ ... });</code></pre><p class="info" data-reactid="2315">Note that the response will be whatever the underlying sql library would typically return on a normal query, so you may need to look at the documentation for the base library the queries are executing against to determine how to handle the response.</p><h3 id="Raw-queries-wrapped" data-reactid="2316">Wrapped Queries:</h3><p data-reactid="2317">The raw query builder also comes with a <code>wrap</code> method, which allows wrapping the query in a value:</p><pre class="display" data-reactid="2318"><code class="js hljs" data-reactid="2319"><span class="hljs-keyword">var</span> subcolumn = knex.raw(<span class="hljs-string">'select avg(salary) from employee where dept_no = e.dept_no'</span>)
.wrap(<span class="hljs-string">'('</span>, <span class="hljs-string">') avg_sal_dept'</span>);
knex.select(<span class="hljs-string">'e.lastname'</span>, <span class="hljs-string">'e.salary'</span>, subcolumn)
.from(<span class="hljs-string">'employee as e'</span>)
.whereRaw(<span class="hljs-string">'dept_no = e.dept_no'</span>)</code><br data-reactid="2320"/><!-- react-text: 2321 -->Outputs:<!-- /react-text --><br data-reactid="2322"/><code class="sql hljs" data-reactid="2323"><span class="hljs-keyword">select</span> <span class="hljs-string">`e`</span>.<span class="hljs-string">`lastname`</span>, <span class="hljs-string">`e`</span>.<span class="hljs-string">`salary`</span>, (<span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(salary) <span class="hljs-keyword">from</span> employee <span class="hljs-keyword">where</span> dept_no = e.dept_no) avg_sal_dept <span class="hljs-keyword">from</span> <span class="hljs-string">`employee`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`e`</span> <span class="hljs-keyword">where</span> dept_no = e.dept_no</code></pre><p data-reactid="2324">Note that the example above be achieved more easily using the <a href="#Builder-as">as</a> method.</p><pre class="display" data-reactid="2325"><code class="js hljs" data-reactid="2326"><span class="hljs-keyword">var</span> subcolumn = knex.avg(<span class="hljs-string">'salary'</span>)
.from(<span class="hljs-string">'employee'</span>)
.whereRaw(<span class="hljs-string">'dept_no = e.dept_no'</span>)
.as(<span class="hljs-string">'avg_sal_dept'</span>);
knex.select(<span class="hljs-string">'e.lastname'</span>, <span class="hljs-string">'e.salary'</span>, subcolumn)
.from(<span class="hljs-string">'employee as e'</span>)
.whereRaw(<span class="hljs-string">'dept_no = e.dept_no'</span>)</code><br data-reactid="2327"/><!-- react-text: 2328 -->Outputs:<!-- /react-text --><br data-reactid="2329"/><code class="sql hljs" data-reactid="2330"><span class="hljs-keyword">select</span> <span class="hljs-string">`e`</span>.<span class="hljs-string">`lastname`</span>, <span class="hljs-string">`e`</span>.<span class="hljs-string">`salary`</span>, (<span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`salary`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`employee`</span> <span class="hljs-keyword">where</span> dept_no = e.dept_no) <span class="hljs-keyword">as</span> <span class="hljs-string">`avg_sal_dept`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`employee`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`e`</span> <span class="hljs-keyword">where</span> dept_no = e.dept_no</code></pre></div><div id="Utility" data-reactid="2331"><h2 id="Utility" data-reactid="2332">Utility</h2><p data-reactid="2333">A collection of utilities that the knex library provides for convenience.</p><h3 id="Utility-BatchInsert" data-reactid="2334">Batch Insert</h3><div data-reactid="2335"><p data-reactid="2336">The <code>batchInsert</code> utility will insert a batch of rows wrapped inside a transaction <em>(which is automatically created unless explicitly given a transaction using <a href="#Builder-transacting">transacting</a>)</em>, at a given <code>chunkSize</code>.</p><p data-reactid="2337">It's primarily designed to be used when you have thousands of rows to insert into a table.</p><p data-reactid="2338">By default, the <code>chunkSize</code> is set to 1000.</p><p data-reactid="2339">BatchInsert also allows for <a href="#Builder-returning">returning values</a> and supplying transactions using <a href="#Builder-transacting">transacting</a>.</p></div><pre data-reactid="2340"><code class="hljs js" data-reactid="2341"><span class="hljs-keyword">var</span> rows = [{...}, {...}];
<span class="hljs-keyword">var</span> chunkSize = <span class="hljs-number">30</span>;
knex.batchInsert(<span class="hljs-string">'TableName'</span>, rows, chunkSize)
.returning(<span class="hljs-string">'id'</span>)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{ ... })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ ... });
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">tr</span>) </span>{
<span class="hljs-keyword">return</span> knex.batchInsert(<span class="hljs-string">'TableName'</span>, rows, chunkSize)
.transacting(tr)
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ ... })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ ... });</code></pre></div><div id="Interfaces" data-reactid="2342"><h2 id="Interfaces" data-reactid="2343">Interfaces</h2><p data-reactid="2344">Knex.js provides several options to deal with query output. The following methods are present on the query builder, schema builder, and the raw builder:</p><h3 id="Interfaces-Promises" data-reactid="2345">Promises</h3><p data-reactid="2346"><a href="https://github.com/petkaantonov/bluebird#what-are-promises-and-why-should-i-use-them">Promises</a> are the preferred way of dealing with queries in knex, as they allow you to return values from a fulfillment handler, which in turn become the value of the promise. The main benefit of promises are the ability to catch thrown errors without crashing the node app, making your code behave like a <strong>.try / .catch / .finally</strong> in synchronous code.</p><pre data-reactid="2347"><code class="hljs js" data-reactid="2348">knex.select(<span class="hljs-string">'name'</span>)
.from(<span class="hljs-string">'users'</span>)
.where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">20</span>)
.andWhere(<span class="hljs-string">'id'</span>, <span class="hljs-string">'<'</span>, <span class="hljs-number">200</span>)
.limit(<span class="hljs-number">10</span>)
.offset(x)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-keyword">return</span> _.pluck(rows, <span class="hljs-string">'name'</span>);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">names</span>) </span>{
<span class="hljs-keyword">return</span> knex.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'nicknames'</span>).whereIn(<span class="hljs-string">'nickname'</span>, names);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-built_in">console</span>.log(rows);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-built_in">console</span>.error(error)
});</code></pre><div id="Interfaces-then" data-reactid="2349"><b data-reactid="2350">then</b><!-- react-text: 2351 --> — <!-- /react-text --><code data-reactid="2352">.then(onFulfilled, [onRejected])</code><p data-reactid="2353">Coerces the current query builder chain into a promise state, accepting the resolve and reject handlers as specified by the Promises/A+ spec. As stated in the spec, more than one call to the then method for the current query chain will resolve with the same value, in the order they were called; the query will not be executed multiple times.</p><pre data-reactid="2354"><code class="hljs js" data-reactid="2355">knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'users'</span>)
.where({<span class="hljs-attr">name</span>: <span class="hljs-string">'Tim'</span>})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-keyword">return</span> knex.insert({<span class="hljs-attr">user_id</span>: rows[<span class="hljs-number">0</span>].id, <span class="hljs-attr">name</span>: <span class="hljs-string">'Test'</span>}, <span class="hljs-string">'id'</span>).into(<span class="hljs-string">'accounts'</span>);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">id</span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Inserted Account '</span> + id);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ <span class="hljs-built_in">console</span>.error(error); });</code></pre></div><div id="Interfaces-catch" data-reactid="2356"><b data-reactid="2357">catch</b><!-- react-text: 2358 --> — <!-- /react-text --><code data-reactid="2359">.catch(onRejected)</code><p data-reactid="2360">Coerces the current query builder into a promise state, catching any error thrown by the query, the same as calling .then(null, onRejected).</p><pre data-reactid="2361"><code class="hljs js" data-reactid="2362"><span class="hljs-keyword">return</span> knex.insert({<span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">'Test'</span>}, <span class="hljs-string">'id'</span>)
.into(<span class="hljs-string">'accounts'</span>)
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-built_in">console</span>.error(error);
}).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>);
}).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-built_in">console</span>.log(rows[<span class="hljs-number">0</span>]);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-built_in">console</span>.error(error);
});</code></pre></div><div id="Interfaces-tap" data-reactid="2363"><b data-reactid="2364">tap</b><!-- react-text: 2365 --> — <!-- /react-text --><code data-reactid="2366">.tap(sideEffectHandler)</code><p data-reactid="2367">Executes side effects on the resolved response, ultimately returning a promise that fulfills with the original value. A thrown error or rejected promise will cause the promise to transition into a rejected state.</p><pre data-reactid="2368"><code class="hljs js" data-reactid="2369"><span class="hljs-comment">// Using only .then()</span>
query.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">x</span>) </span>{
doSideEffectsHere(x);
<span class="hljs-keyword">return</span> x;
});
<span class="hljs-comment">// Using .tap()</span>
promise.tap(doSideEffectsHere);</code></pre></div><div id="Interfaces-map" data-reactid="2370"><b data-reactid="2371">map</b><!-- react-text: 2372 --> — <!-- /react-text --><code data-reactid="2373">.map(mapper)</code><p data-reactid="2374">A passthrough to Bluebird's map implementation with the result set.</p><pre data-reactid="2375"><code class="hljs js" data-reactid="2376">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).limit(<span class="hljs-number">10</span>).map(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">row</span>) </span>{
<span class="hljs-keyword">return</span> row.name;
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">names</span>) </span>{ <span class="hljs-built_in">console</span>.log(names); })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{ <span class="hljs-built_in">console</span>.error(e); });</code></pre></div><div id="Interfaces-reduce" data-reactid="2377"><b data-reactid="2378">reduce</b><!-- react-text: 2379 --> — <!-- /react-text --><code data-reactid="2380">.reduce(reducer, [initialValue])</code><p data-reactid="2381">A passthrough to Bluebird's reduce implementation with the result set.</p><pre data-reactid="2382"><code class="hljs js" data-reactid="2383">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).limit(<span class="hljs-number">10</span>).reduce(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">memo, row</span>) </span>{
memo.names.push(row.name);
memo.count++;
<span class="hljs-keyword">return</span> memo;
}, {<span class="hljs-attr">count</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">names</span>: []})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>) </span>{ <span class="hljs-built_in">console</span>.log(obj); })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{ <span class="hljs-built_in">console</span>.error(e); });</code></pre></div><div id="Interfaces-bind" data-reactid="2384"><b data-reactid="2385">bind</b><!-- react-text: 2386 --> — <!-- /react-text --><code data-reactid="2387">.bind(context)</code><p data-reactid="2388">A passthrough to Bluebird's bind method which sets the context value (this) for the returned promise.</p><pre data-reactid="2389"><code class="hljs js" data-reactid="2390">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.limit(<span class="hljs-number">10</span>)
.bind(<span class="hljs-built_in">console</span>)
.then(<span class="hljs-built_in">console</span>.log)
.catch(<span class="hljs-built_in">console</span>.error)</code></pre></div><div id="Interfaces-return" data-reactid="2391"><b data-reactid="2392">return</b><!-- react-text: 2393 --> — <!-- /react-text --><code data-reactid="2394">.return(value)</code><p data-reactid="2395">Shorthand for calling .then(function() { return value }).</p><pre data-reactid="2396"><code class="hljs js" data-reactid="2397"><span class="hljs-comment">// Without return:</span>
knex.insert(values).into(<span class="hljs-string">'users'</span>)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> {<span class="hljs-attr">inserted</span>: <span class="hljs-literal">true</span>};
});
knex.insert(values).into(<span class="hljs-string">'users'</span>).return({<span class="hljs-attr">inserted</span>: <span class="hljs-literal">true</span>});</code></pre></div><h3 id="Interfaces-Callbacks" data-reactid="2398">Callbacks</h3><div id="Interfaces-asCallback" data-reactid="2399"><b data-reactid="2400">asCallback</b><!-- react-text: 2401 --> — <!-- /react-text --><code data-reactid="2402">.asCallback(callback)</code><p data-reactid="2403">If you'd prefer a callback interface over promises, the asCallback function accepts a standard node style callback for executing the query chain. Note that as with the then method, subsequent calls to the same query chain will return the same result.</p><pre data-reactid="2404"><code class="hljs js" data-reactid="2405">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'>'</span>, <span class="hljs-number">20</span>)
.andWhere(<span class="hljs-string">'id'</span>, <span class="hljs-string">'<'</span>, <span class="hljs-number">200</span>)
.limit(<span class="hljs-number">10</span>)
.offset(x)
.asCallback(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err, rows</span>) </span>{
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">return</span> <span class="hljs-built_in">console</span>.error(err);
knex.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'nicknames'</span>)
.whereIn(<span class="hljs-string">'nickname'</span>, _.pluck(rows, <span class="hljs-string">'name'</span>))
.asCallback(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err, rows</span>) </span>{
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">return</span> <span class="hljs-built_in">console</span>.error(err);
<span class="hljs-built_in">console</span>.log(rows);
});
});</code></pre></div><h3 id="Interfaces-Streams" data-reactid="2406">Streams</h3><p data-reactid="2407">Streams are a powerful way of piping data through as it comes in, rather than all at once. You can read more about streams <a href="https://github.com/substack/stream-handbook">here at substack's stream handbook</a>. See the following for example uses of stream & pipe. If you wish to use streams with PostgreSQL, you must also install the <a href="https://github.com/brianc/node-pg-query-stream">pg-query-stream</a> module. On an HTTP server, make sure to <a href="https://github.com/tgriesser/knex/wiki/Manually-Closing-Streams">manually close your streams</a> if a request is aborted.</p><div id="Interfaces-stream" data-reactid="2408"><b data-reactid="2409">stream</b><!-- react-text: 2410 --> — <!-- /react-text --><code data-reactid="2411">.stream([options], [callback])</code><p data-reactid="2412">If called with a callback, the callback is passed the stream and a promise is returned. Otherwise, the readable stream is returned.</p><pre data-reactid="2413"><code class="hljs js" data-reactid="2414"><span class="hljs-comment">// Retrieve the stream:</span>
<span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).stream();
stream.pipe(writableStream);
<span class="hljs-comment">// With options:</span>
<span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).stream({<span class="hljs-attr">highWaterMark</span>: <span class="hljs-number">5</span>});
stream.pipe(writableStream);
<span class="hljs-comment">// Use as a promise:</span>
<span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)
.where(knex.raw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>]))
.stream(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">stream</span>) </span>{
stream.pipe(writableStream);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// ... })</span>
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{ <span class="hljs-built_in">console</span>.error(e); });</code></pre></div><div id="Interfaces-pipe" data-reactid="2415"><b data-reactid="2416">pipe</b><!-- react-text: 2417 --> — <!-- /react-text --><code data-reactid="2418">.pipe(writableStream)</code><p data-reactid="2419">Pipe a stream for the current query to a writableStream.</p><pre data-reactid="2420"><code class="hljs js" data-reactid="2421"><span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).pipe(writableStream);</code></pre></div><h3 id="Interfaces-Events" data-reactid="2422">Events</h3><div id="Interfaces-query" data-reactid="2423"><b data-reactid="2424">query</b><p data-reactid="2425">A query event is fired just before a query takes place, providing data about the query, including the connection's __cid property and any other information about the query as described in toSQL. Useful for logging all queries throughout your application.</p><pre data-reactid="2426"><code class="hljs js" data-reactid="2427">knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'users'</span>)
.on(<span class="hljs-string">'query'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) </span>{
app.log(data);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-comment">// ...</span>
});</code></pre></div><div id="Interfaces-query-error" data-reactid="2428"><b data-reactid="2429">query-error</b><p data-reactid="2430">A query-error event is fired when an error occurs when running a query, providing the error object and data about the query, including the connection's __cid property and any other information about the query as described in toSQL. Useful for logging all query errors throughout your application.</p><pre data-reactid="2431"><code class="hljs js" data-reactid="2432">knex.select([<span class="hljs-string">'NonExistentColumn'</span>])
.from(<span class="hljs-string">'users'</span>)
.on(<span class="hljs-string">'query-error'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error, obj</span>) </span>{
app.log(error);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// ... })</span>
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-comment">// Same error object as the query-error event provides.</span>
});</code></pre></div><div id="Interfaces-query-response" data-reactid="2433"><b data-reactid="2434">query-response</b><p data-reactid="2435">A query-response event is fired when a successful query has been run, providing the response of the query and data about the query, including the connection's __cid property and any other information about the query as described in toSQL, and finally the query builder used for the query.</p><pre data-reactid="2436"><code class="hljs js" data-reactid="2437">knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'users'</span>)
.on(<span class="hljs-string">'query-response'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response, obj, builder</span>) </span>{
<span class="hljs-comment">// ...</span>
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
<span class="hljs-comment">// Same response as the emitted event</span>
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ });</code></pre></div><h3 id="Interfaces-Other" data-reactid="2438">Other</h3><div id="Interfaces-toString" data-reactid="2439"><b data-reactid="2440">toString</b><!-- react-text: 2441 --> — <!-- /react-text --><code data-reactid="2442">.toString()</code><p data-reactid="2443">Returns an array of query strings filled out with the correct values based on bindings, etc. Useful for debugging.</p><pre class="display" data-reactid="2444"><code class="js hljs" data-reactid="2445">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).where(knex.raw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>])).toString()</code><br data-reactid="2446"/><!-- react-text: 2447 -->Outputs:<!-- /react-text --><br data-reactid="2448"/><code class="sql hljs" data-reactid="2449"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">id</span> = <span class="hljs-number">1</span></code></pre></div><div id="Interfaces-toSQL" data-reactid="2450"><b data-reactid="2451">toSQL</b><!-- react-text: 2452 --> — <!-- /react-text --><code data-reactid="2453">.toSQL()</code><p data-reactid="2454">Returns an array of query strings filled out with the correct values based on bindings, etc. Useful for debugging.</p><pre data-reactid="2455"><code class="hljs js" data-reactid="2456">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).where(knex.raw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>])).toSQL()
<span class="hljs-comment">// Ouputs: { bindings: [1], method: 'select', sql: 'select * from "users" where id = ?', options: undefined, }</span></code></pre></div></div><div id="Migrations" data-reactid="2457"><h2 id="Migrations" data-reactid="2458">Migrations</h2><p data-reactid="2459">Migrations allow for you to define sets of schema changes so upgrading a database is a breeze.</p><h3 id="Migrations-CLI" data-reactid="2460">Migration CLI</h3><p data-reactid="2461">The migration CLI is bundled with the knex install, and is driven by the <a href="https://github.com/tkellen/node-liftoff">node-liftoff</a> module. To install globally, run:</p><pre data-reactid="2462"><code data-reactid="2463">$ npm install knex -g</code></pre><p data-reactid="2464">Migrations use a <strong>knexfile</strong>, which specify various configuration settings for the module. To create a new knexfile, run the following:</p><pre data-reactid="2465"><code data-reactid="2466">$ knex init
# or for .coffee
$ knex init -x coffee</code></pre><p data-reactid="2467">will create a sample knexfile.js - the file which contains our various database configurations. Once you have a knexfile.js, you can use the migration tool to create migration files to the specified directory (default migrations). Creating new migration files can be achieved by running:</p><pre data-reactid="2468"><code data-reactid="2469">$ knex migrate:make migration_name</code></pre><p data-reactid="2470">Once you have finished writing the migrations, you can update the database matching your <code>NODE_ENV</code> by running:</p><pre data-reactid="2471"><code data-reactid="2472">$ knex migrate:latest</code></pre><p data-reactid="2473">You can also pass the <code>--env</code> flag or set <code>NODE_ENV</code> to select an alternative environment:</p><pre data-reactid="2474"><code data-reactid="2475">$ knex migrate:latest --env production
# or
$ NODE_ENV=production knex migrate:latest</code></pre><p data-reactid="2476">To rollback the last batch of migrations:</p><pre data-reactid="2477"><code data-reactid="2478">$ knex migrate:rollback</code></pre><h2 id="Seeds" data-reactid="2479">Seed files</h2><p data-reactid="2480">Seed files allow you to populate your database with test or seed data independent of your migration files.</p><h3 id="Seeds-CLI" data-reactid="2481">Seed CLI</h3><p data-reactid="2482">To create a seed file, run:</p><pre data-reactid="2483"><code data-reactid="2484">$ knex seed:make seed_name</code></pre><p data-reactid="2485">Seed files are created in the directory specified in your knexfile.js for the current environment. A sample seed configuration looks like:</p><pre data-reactid="2486"><code data-reactid="2487">development: {
client: ...,
connection: { ... },
seeds: {
directory: './seeds/dev'
}
}</code></pre><div data-reactid="2488"><p data-reactid="2489">If no <code>seeds.directory</code> is defined, files are created in <code>./seeds</code>. Note that the seed directory needs to be a relative path. Absolute paths are not supported (nor is it good practice).</p><p data-reactid="2490">To run seed files, execute:</p></div><pre data-reactid="2491"><code data-reactid="2492">$ knex seed:run</code></pre><p data-reactid="2493">Seed files are executed in alphabetical order. Unlike migrations, <em>every</em> seed file will be executed when you run the command. You should design your seed files to reset tables as needed before inserting data.</p><h3 id="knexfile" data-reactid="2494">knexfile.js</h3><p data-reactid="2495">A knexfile.js or knexfile.coffee generally contains all of the configuration for your database. It can optionally provide different configuration for different environments. You may pass a <code>--knexfile</code> option to any of the command line statements to specify an alternate path to your knexfile.</p><h4 data-reactid="2496">Basic configuration:</h4><pre data-reactid="2497"><code class="hljs js" data-reactid="2498"><span class="hljs-built_in">module</span>.exports = {
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: process.env.DATABASE_URL || { <span class="hljs-attr">user</span>: <span class="hljs-string">'me'</span>, <span class="hljs-attr">database</span>: <span class="hljs-string">'my_app'</span> }
};</code></pre><h4 data-reactid="2499">Environment configuration:</h4><pre data-reactid="2500"><code class="hljs js" data-reactid="2501"><span class="hljs-built_in">module</span>.exports = {
<span class="hljs-attr">development</span>: {
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: { <span class="hljs-attr">user</span>: <span class="hljs-string">'me'</span>, <span class="hljs-attr">database</span>: <span class="hljs-string">'my_app'</span> }
},
<span class="hljs-attr">production</span>: { <span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>, <span class="hljs-attr">connection</span>: process.env.DATABASE_URL }
};</code></pre><h4 data-reactid="2502">Custom migration:</h4><p data-reactid="2503">You may provide a custom migration stub to be used in place of the default option.</p><pre data-reactid="2504"><code class="hljs js" data-reactid="2505"><span class="hljs-built_in">module</span>.exports = {
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">migration</span>: {
<span class="hljs-attr">stub</span>: <span class="hljs-string">'migration.stub'</span>
}
};</code></pre><h3 id="Migrations-API" data-reactid="2506">Migration API</h3><div data-reactid="2507"><p data-reactid="2508"><code>knex.migrate</code> is the class utilized by the knex migrations cli.</p><p data-reactid="2509">Each method takes an optional <code>config</code> object, which may specify the following properties:</p></div><ul data-reactid="2510"><li data-reactid="2511"><code>directory</code>: a relative path to the directory containing the migration files (default <code>./migrations</code>)</li><li data-reactid="2512"><code>extension</code>: the file extension used for the generated migration files (default <code>js</code>)</li><li data-reactid="2513"><code>tableName</code>: the table name used for storing the migration state (default <code>knex_migrations</code>)</li><li data-reactid="2514"><code>disableTransactions</code>: don't run migrations inside transactions (default <code>false</code>)</li></ul><h4 id="Migrations-API-transactions" data-reactid="2515">Transactions in migrations</h4><p data-reactid="2516">By default, each migration is run inside a transaction. Whenever needed, one can disable transactions for all migrations via the common migration config option <code>config.disableTransactions</code> or per-migration, via exposing a boolean property <code>config.transaction</code> from a migration file:</p><pre data-reactid="2517"><code class="hljs js" data-reactid="2518">exports.up = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">knex, Promise</span>) </span>{ <span class="hljs-comment">/* ... */</span> };
exports.down = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">knex, Promise</span>) </span>{ <span class="hljs-comment">/* ... */</span> };
exports.config = { <span class="hljs-attr">transaction</span>: <span class="hljs-literal">false</span> };</code></pre><p data-reactid="2519">The same config property can be used for enabling transaction per-migration in case the common configuration has <code>disableTransactions: true</code>.</p><div id="Migrations-make" data-reactid="2520"><b data-reactid="2521">make</b><!-- react-text: 2522 --> — <!-- /react-text --><code data-reactid="2523">knex.migrate.make(name, [config])</code><p data-reactid="2524">Creates a new migration, with the name of the migration being added.</p></div><div id="Migrations-latest" data-reactid="2525"><b data-reactid="2526">latest</b><!-- react-text: 2527 --> — <!-- /react-text --><code data-reactid="2528">knex.migrate.latest([config])</code><p data-reactid="2529">Runs all migrations that have not yet been run.</p></div><p data-reactid="2530">If you need to run something only after all migrations have finished their execution, you can do something like this:</p><pre data-reactid="2531"><code class="hljs js" data-reactid="2532">knex.migrate.latest()
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> knex.seed.run();
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-comment">// migrations are finished</span>
});</code></pre><div id="Migrations-rollback" data-reactid="2533"><b data-reactid="2534">rollback</b><!-- react-text: 2535 --> — <!-- /react-text --><code data-reactid="2536">knex.migrate.rollback([config])</code><p data-reactid="2537">Rolls back the latest migration group.</p></div><div id="Migrations-currentVersion" data-reactid="2538"><b data-reactid="2539">currentVersion</b><!-- react-text: 2540 --> — <!-- /react-text --><code data-reactid="2541">knex.migrate.currentVersion([config])</code><p data-reactid="2542">Retrieves and returns the current migration version, as a promise. If there aren't any migrations run yet, returns "none" as the value for the currentVersion.</p></div><h3 id="Notes-about-locks" data-reactid="2543">Notes about locks</h3><p data-reactid="2544">A lock system is there to prevent multiple processes from running the same migration batch in the same time. When a batch of migrations is about to be run, the migration system first tries to get a lock using a <code>SELECT ... FOR UPDATE</code> statement (preventing race conditions from happening). If it can get a lock, the migration batch will run. If it can't, it will wait until the lock is released.</p><p data-reactid="2545">Please note that if your process unfortunately crashes, the lock will have to be <em>manually</em> removed in order to let migrations run again. The locks are saved in a table called "<code>tableName</code>_lock"; it has single one column called <code>is_locked</code> that you need to set to <code>0</code> in order to release the lock.</p></div><div id="Seeds" data-reactid="2546"><h3 id="Seeds-API" data-reactid="2547">Seed API</h3><p data-reactid="2548"><code>knex.seed</code> is the class utilized by the knex seed CLI. Each method takes an optional <code>config</code> object, which may specify the relative <code>directory</code> for the migrations.</p><div id="Seeds-make" data-reactid="2549"><b data-reactid="2550">make</b><!-- react-text: 2551 --> — <!-- /react-text --><code data-reactid="2552">knex.seed.make(name, [config])</code><p data-reactid="2553">Creates a new seed file, with the name of the seed file being added.</p></div><div id="Seeds-run" data-reactid="2554"><b data-reactid="2555">run</b><!-- react-text: 2556 --> — <!-- /react-text --><code data-reactid="2557">knex.seed.run([config])</code><p data-reactid="2558">Runs all seed files for the current environment.</p></div></div><div id="Support" data-reactid="2559"><h2 id="support" data-reactid="2560">Support</h2><p data-reactid="2561">Have questions about the library? Come join us in the <a href="http://webchat.freenode.net/?channels=bookshelf">#bookshelf freenode IRC</a> channel for support on knex.js and <a href="http://bookshelfjs.org">bookshelf.js</a>, or post an issue on <a href="http://stackoverflow.com/questions/tagged/knex.js">Stack Overflow</a> or in the GitHub <a href="https://github.com/tgriesser/knex/issues">issue tracker</a>.</p></div><div id="Faq" data-reactid="2562"><h2 id="faq" data-reactid="2563">F.A.Q.</h2><div data-reactid="2564"><p data-reactid="2565"><strong>How do I help contribute?</strong><br>Glad you ask! Pull requests, or feature requests, though not always implemented, are a great way to help make Knex even better than it is now. If you're looking for something specific to help out with, there's a number of unit tests that aren't implemented yet, the library could never have too many of those. If you want to submit a fix or feature, take a look at the <a href="https://github.com/tgriesser/knex/blob/master/CONTRIBUTING.md">Contributing</a> readme in the Github and go ahead and open a ticket.</p><p data-reactid="2566"><strong>How do I debug?</strong><br>Knex is beginning to make use of the <a href="https://github.com/visionmedia/debug">debug</a> module internally, so you can set the <code>DEBUG</code> environment variable to <code>knex:*</code> to see all debugging, or select individual namespaces <code>DEBUG=knex:query,knex:tx</code> to constrain a bit.</p><p data-reactid="2567">If you pass <code>{debug: true}</code> as one of the options in your initialize settings, you can see all of the query calls being made. Sometimes you need to dive a bit further into the various calls and see what all is going on behind the scenes. I'd recommend <a href="https://github.com/dannycoates/node-inspector">node-inspector</a>, which allows you to debug code with <code>debugger</code> statements like you would in the browser.</p><p data-reactid="2568">At the start of your application code will catch any errors not otherwise caught in the normal promise chain handlers, which is very helpful in debugging.</p><p data-reactid="2569"><strong>How do I run the test suite?</strong><br>The test suite looks for an environment variable called <code>KNEX_TEST</code> for the path to the database configuration. If you run the following command:</p></div><pre data-reactid="2570"><code data-reactid="2571">$ export KNEX_TEST='/path/to/your/knex_config.js'
$ npm test</code></pre><div data-reactid="2572"><p data-reactid="2573">replacing with the path to your config file, and the config file is valid, the test suite should run properly.</p><p data-reactid="2574"><strong>My tests are failing because slow DB connection and short test timeouts! How to extend test timeouts?</strong><br>Sometimes, e.g. when running CI on travis, test suite's default timeout of 5 seconds might be too short. In such cases an alternative test timeout value in milliseconds can be specified using the <code>KNEX_TEST_TIMEOUT</code> environment variable.</p></div><pre data-reactid="2575"><code data-reactid="2576">$ export KNEX_TEST_TIMEOUT=30000
$ npm test</code></pre><p data-reactid="2577"><strong>Can I use Knex outside of Node.js</strong><br>Yes. While the WebSQL spec is deprecated, there is still an adapter that provides support. You will need to use a build tool like browserify or webpack for a browser build.</p></div><div id="changelog" data-reactid="2578"><h2 data-reactid="2579">Change Log</h2><div data-reactid="2580"><b class="header" data-reactid="2581">0.12.9</b><!-- react-text: 2582 --> — <!-- /react-text --><small data-reactid="2583"><i data-reactid="2584">23 Mar, 2017</i></small><br data-reactid="2585"/><p data-reactid="2586">Fixed unhandled exception in batchInsert when the rows to be inserted resulted in duplicate key violation <a href="https://github.com/tgriesser/knex/issues/1880">#1880</a></p></div><div data-reactid="2587"><b class="header" data-reactid="2588">0.12.8</b><!-- react-text: 2589 --> — <!-- /react-text --><small data-reactid="2590"><i data-reactid="2591">15 Mar, 2017</i></small><br data-reactid="2592"/><ul data-reactid="2593"><li data-reactid="2594">Added clearSelect and clearWhere to query builder <a href="https://github.com/tgriesser/knex/issues/1912">#1912</a></li><li data-reactid="2595">Properly close Postgres query streams on error <a href="https://github.com/tgriesser/knex/issues/1935">#1935</a></li><li data-reactid="2596">Transactions should never reject with undefined <a href="https://github.com/tgriesser/knex/issues/1970">#1970</a></li><li data-reactid="2597">Clear acquireConnectionTimeout if an error occurs when acquiring a connection <a href="https://github.com/tgriesser/knex/issues/1973">#1973</a></li></ul></div><div data-reactid="2598"><b class="header" data-reactid="2599">0.12.7</b><!-- react-text: 2600 --> — <!-- /react-text --><small data-reactid="2601"><i data-reactid="2602">17 Feb, 2017</i></small><br data-reactid="2603"/><b data-reactid="2604">Accidental Breaking Change:</b><ul data-reactid="2605"><li data-reactid="2606">Ensure that 'client' is provided in knex config object <a href="https://github.com/tgriesser/knex/issues/1822">#1822</a></li></ul><b data-reactid="2607">Other Changes:</b><ul data-reactid="2608"><li data-reactid="2609">Support custom foreign key names <a href="https://github.com/tgriesser/knex/issues/1311">#1311</a>, <a href="https://github.com/tgriesser/knex/issues/1726">#1726</a></li><li data-reactid="2610">Fixed named bindings to work with queries containing <code>:</code>-chars <a href="https://github.com/tgriesser/knex/issues/1890">#1890</a></li><li data-reactid="2611">Exposed more promise functions <a href="https://github.com/tgriesser/knex/issues/1896">#1896</a></li><li data-reactid="2612">Pass rollback errors to transaction promise in mssql <a href="https://github.com/tgriesser/knex/issues/1885">#1885</a></li><li data-reactid="2613">ONLY keyword support for PostgreSQL (for table inheritance) <a href="https://github.com/tgriesser/knex/issues/1874">#1874</a></li><li data-reactid="2614">Fixed Mssql update with join syntax <a href="https://github.com/tgriesser/knex/issues/1777">#1777</a></li><li data-reactid="2615">Replace migrations and seed for react-native packager <a href="https://github.com/tgriesser/knex/issues/1813">#1813</a></li><li data-reactid="2616">Support knexfile, migration and seeds in TypeScript <a href="https://github.com/tgriesser/knex/issues/1769">#1769</a></li><li data-reactid="2617">Fix float to integer conversion of decimal fields in MSSQL <a href="https://github.com/tgriesser/knex/issues/1781">#1781</a></li><li data-reactid="2618">External authentication capability when using oracledb driver <a href="https://github.com/tgriesser/knex/issues/1716">#1716</a></li><li data-reactid="2619">Fixed MSSQL incorect query build when locks are used <a href="https://github.com/tgriesser/knex/issues/1707">#1707</a></li><li data-reactid="2620">Allow to use <code>first</code> method as aliased select <a href="https://github.com/tgriesser/knex/issues/1784">#1784</a></li><li data-reactid="2621">Alter column for nullability, type and default value <a href="https://github.com/tgriesser/knex/issues/46">#46</a>, <a href="https://github.com/tgriesser/knex/issues/1759">#1759</a></li><li data-reactid="2622">Add more having<em> methods / join clause on</em> methods <a href="https://github.com/tgriesser/knex/issues/1674">#1674</a></li><li data-reactid="2623">Compatibility fixes and cleanups <a href="https://github.com/tgriesser/knex/issues/1788">#1788</a>, <a href="https://github.com/tgriesser/knex/issues/1792">#1792</a>, <a href="https://github.com/tgriesser/knex/issues/1794">#1794</a>, <a href="https://github.com/tgriesser/knex/issues/1814">#1814</a>, <a href="https://github.com/tgriesser/knex/issues/1857">#1857</a>, <a href="https://github.com/tgriesser/knex/issues/1649">#1649</a></li></ul></div><div data-reactid="2624"><b class="header" data-reactid="2625">0.12.6</b><!-- react-text: 2626 --> — <!-- /react-text --><small data-reactid="2627"><i data-reactid="2628">19 Oct, 2016</i></small><br data-reactid="2629"/><ul data-reactid="2630"><li data-reactid="2631">Address warnings mentioned in <a href="https://github.com/tgriesser/knex/issues/1388">#1388</a> (<a href="https://github.com/tgriesser/knex/issues/1740">#1740</a>)</li><li data-reactid="2632">Remove postinstall script (<a href="https://github.com/tgriesser/knex/issues/1746">#1746</a>)</li></ul></div><div data-reactid="2633"><b class="header" data-reactid="2634">0.12.5</b><!-- react-text: 2635 --> — <!-- /react-text --><small data-reactid="2636"><i data-reactid="2637">12 Oct, 2016</i></small><br data-reactid="2638"/><ul data-reactid="2639"><li data-reactid="2640">Fix broken 0.12.4 build (removed from npm)</li><li data-reactid="2641">Fix <a href="https://github.com/tgriesser/knex/issues/1733">#1733</a>, <a href="https://github.com/tgriesser/knex/issues/920">#920</a>, incorrect postgres array bindings</li></ul></div><div data-reactid="2642"><b class="header" data-reactid="2643">0.12.3</b><!-- react-text: 2644 --> — <!-- /react-text --><small data-reactid="2645"><i data-reactid="2646">9 Oct, 2016</i></small><br data-reactid="2647"/><ul data-reactid="2648"><li data-reactid="2649">Fix <a href="https://github.com/tgriesser/knex/issues/1703">#1703</a>, <a href="https://github.com/tgriesser/knex/issues/1694">#1694</a> - connections should be returned to pool if acquireConnectionTimeout is triggered</li><li data-reactid="2650">Fix <a href="https://github.com/tgriesser/knex/issues/1710">#1710</a> regression in postgres array escaping</li></ul></div><div data-reactid="2651"><b class="header" data-reactid="2652">0.12.2</b><!-- react-text: 2653 --> — <!-- /react-text --><small data-reactid="2654"><i data-reactid="2655">27 Sep, 2016</i></small><br data-reactid="2656"/><ul data-reactid="2657"><li data-reactid="2658">Restore pool min: 1 for sqlite3, <a href="https://github.com/tgriesser/knex/issues/1701">#1701</a></li><li data-reactid="2659">Fix for connection error after it's closed / released, <a href="https://github.com/tgriesser/knex/issues/1691">#1691</a></li><li data-reactid="2660">Fix oracle prefetchRowCount setting, <a href="https://github.com/tgriesser/knex/issues/1675">#1675</a></li></ul></div><div data-reactid="2661"><b class="header" data-reactid="2662">0.12.1</b><!-- react-text: 2663 --> — <!-- /react-text --><small data-reactid="2664"><i data-reactid="2665">16 Sep, 2016</i></small><br data-reactid="2666"/><ul data-reactid="2667"><li data-reactid="2668">Fix MSSQL sql execution error, <a href="https://github.com/tgriesser/knex/issues/1669">#1669</a></li><li data-reactid="2669">Added DEBUG=knex:bindings for debugging query bindings, <a href="https://github.com/tgriesser/knex/issues/1557">#1557</a></li></ul></div><div data-reactid="2670"><b class="header" data-reactid="2671">0.12.0</b><!-- react-text: 2672 --> — <!-- /react-text --><small data-reactid="2673"><i data-reactid="2674">13 Sep, 2016</i></small><br data-reactid="2675"/><ul data-reactid="2676"><li data-reactid="2677">Remove build / built files, <a href="https://github.com/tgriesser/knex/issues/1616">#1616</a></li><li data-reactid="2678">Upgrade to Babel 6, <a href="https://github.com/tgriesser/knex/issues/1617">#1617</a></li><li data-reactid="2679">Reference Bluebird module directly, remove deprecated .exec method, <a href="https://github.com/tgriesser/knex/issues/1618">#1618</a></li><li data-reactid="2680">Remove documentation files from main repo</li><li data-reactid="2681">Fix broken behavior on WebSQL build, <a href="https://github.com/tgriesser/knex/issues/1638">#1638</a></li><li data-reactid="2682">Oracle id sequence now handles manual inserts, <a href="https://github.com/tgriesser/knex/issues/906">#906</a></li><li data-reactid="2683">Cleanup PG escaping, fix <a href="https://github.com/tgriesser/knex/issues/1602">#1602</a>, <a href="https://github.com/tgriesser/knex/issues/1548">#1548</a></li><li data-reactid="2684">Added <a href="#Builder-with"><code>with</code></a> to builder for <a href="https://www.postgresql.org/docs/9.4/static/queries-with.html">common table expressions</a>, <a href="https://github.com/tgriesser/knex/issues/1599">#1599</a></li><li data-reactid="2685">Fix <a href="https://github.com/tgriesser/knex/issues/1619">#1619</a>, pluck with explicit column names</li><li data-reactid="2686">Switching back to <a href="https://github.com/coopernurse/node-pool">generic-pool</a> for pooling resource management</li><li data-reactid="2687">Removed index.html, please direct all PR's for docs against the files in <a href="https://github.com/knex/documentation">knex/documentation</a></li></ul></div><div data-reactid="2688"><a href="#" data-reactid="2689">Show Full Changelog</a><div class="more-changelog" data-reactid="2690"><div data-reactid="2691"><b class="header" data-reactid="2692">0.11.10</b><!-- react-text: 2693 --> — <!-- /react-text --><small data-reactid="2694"><i data-reactid="2695">9 Aug, 2016</i></small><br data-reactid="2696"/><ul data-reactid="2697"><li data-reactid="2698">Added CHANGELOG.md for a <a href="https://github.com/knex/documentation">new documentation</a> builder coming soon, <a href="https://github.com/tgriesser/knex/issues/1615">#1615</a></li><li data-reactid="2699">Minor documentation tweaks</li><li data-reactid="2700">PG: Fix Uint8Array being considered undefined, <a href="https://github.com/tgriesser/knex/issues/1601">#1601</a></li><li data-reactid="2701">MSSQL: Make columnInfo schema dynamic, <a href="https://github.com/tgriesser/knex/issues/1585">#1585</a></li></ul></div><div data-reactid="2702"><b class="header" data-reactid="2703">0.11.9</b><!-- react-text: 2704 --> — <!-- /react-text --><small data-reactid="2705"><i data-reactid="2706">21 Jul, 2016</i></small><br data-reactid="2707"/><p data-reactid="2708">Reverted knex client breaking change (commit b74cd69e906), fixes <a href="https://github.com/tgriesser/knex/issues/1587">#1587</a></p></div><div data-reactid="2709"><b class="header" data-reactid="2710">0.11.8</b><!-- react-text: 2711 --> — <!-- /react-text --><small data-reactid="2712"><i data-reactid="2713">21 Jul, 2016</i></small><br data-reactid="2714"/><ul data-reactid="2715"><li data-reactid="2716">Oracledb dialect <a href="https://github.com/tgriesser/knex/issues/990">#990</a></li><li data-reactid="2717">Documentation fix <a href="https://github.com/tgriesser/knex/issues/1532">#1532</a></li><li data-reactid="2718">Allow named bindings to be escaped. <a href="https://github.com/tgriesser/knex/issues/1576">#1576</a></li><li data-reactid="2719">Several bugs with MS SQL schema creation and installing from gihub fix <a href="https://github.com/tgriesser/knex/issues/1577">#1577</a></li><li data-reactid="2720">Fix incorrect escaping of backslashes in SqlString.escape <a href="https://github.com/tgriesser/knex/issues/1545">#1545</a></li></ul></div><div data-reactid="2721"><b class="header" data-reactid="2722">0.11.7</b><!-- react-text: 2723 --> — <!-- /react-text --><small data-reactid="2724"><i data-reactid="2725">19 Jun, 2016</i></small><br data-reactid="2726"/><p data-reactid="2727">Add missing dependency. <a href="https://github.com/tgriesser/knex/issues/1516">#1516</a></p></div><div data-reactid="2728"><b class="header" data-reactid="2729">0.11.6</b><!-- react-text: 2730 --> — <!-- /react-text --><small data-reactid="2731"><i data-reactid="2732">18 Jun, 2016</i></small><br data-reactid="2733"/><ul data-reactid="2734"><li data-reactid="2735">Allow cancellation on timeout (MySQL) <a href="https://github.com/tgriesser/knex/issues/1454">#1454</a></li><li data-reactid="2736">Better bigint support. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1445">#1445</a></li><li data-reactid="2737">More consistent handling of <code>undefined</code> values in <code>QueryBuilder#where</code> and <code>Raw</code>. <a href="https://github.com/tgriesser/knex/issues/1459">#1459</a></li><li data-reactid="2738">Fix Webpack build. <a href="https://github.com/tgriesser/knex/issues/1447">#1447</a></li><li data-reactid="2739">Fix code that triggered Bluebird warnings. <a href="https://github.com/tgriesser/knex/issues/1460">#1460</a>, <a href="https://github.com/tgriesser/knex/issues/1489">#1489</a></li><li data-reactid="2740">Fix <code>ping</code> function. (Oracle) <a href="https://github.com/tgriesser/knex/issues/1486">#1486</a></li><li data-reactid="2741">Fix <code>columnInfo</code>. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1464">#1464</a></li><li data-reactid="2742">Fix <code>ColumnCompiler#binary</code>. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1464">#1464</a></li><li data-reactid="2743">Allow connection strings that do not contain a password. <a href="https://github.com/tgriesser/knex/issues/1473">#1473</a></li><li data-reactid="2744">Fix race condition in seed stubs. <a href="https://github.com/tgriesser/knex/issues/1493">#1493</a></li><li data-reactid="2745">Give each query a UUID. <a href="https://github.com/tgriesser/knex/issues/1510">#1510</a></li></ul></div><div data-reactid="2746"><b class="header" data-reactid="2747">0.11.5</b><!-- react-text: 2748 --> — <!-- /react-text --><small data-reactid="2749"><i data-reactid="2750">26 May, 2016</i></small><br data-reactid="2751"/><p data-reactid="2752">Bugfix: Using <code>Raw</code> or <code>QueryBuilder</code> as a binding to <code>Raw</code> now works as intended</p></div><div data-reactid="2753"><b class="header" data-reactid="2754">0.11.4</b><!-- react-text: 2755 --> — <!-- /react-text --><small data-reactid="2756"><i data-reactid="2757">22 May, 2016</i></small><br data-reactid="2758"/><ul data-reactid="2759"><li data-reactid="2760">Bugfix: Inconsistency of <code>.primary()</code> and <code>.dropPrimary()</code> between dialects <a href="https://github.com/tgriesser/knex/issues/1430">#1430</a></li><li data-reactid="2761">Feature: Allow using custom Client/Dialect (you can pass your own client in knex config) <a href="https://github.com/tgriesser/knex/issues/1428">#1428</a></li><li data-reactid="2762">Docs: Add documentation for .dropTimestamps <a href="https://github.com/tgriesser/knex/issues/1432">#1432</a></li><li data-reactid="2763">Bugfix: Fixed passing undefined fields for insert/update inside transaction <a href="https://github.com/tgriesser/knex/issues/1423">#1423</a></li><li data-reactid="2764">Feature: <code>batchInsert</code> with existing transaction <a href="https://github.com/tgriesser/knex/issues/1354">#1354</a></li><li data-reactid="2765">Build: eslint instead of jshint <a href="https://github.com/tgriesser/knex/issues/1416">#1416</a></li><li data-reactid="2766">Bugfix: Pooled connections not releasing <a href="https://github.com/tgriesser/knex/issues/1382">#1382</a></li><li data-reactid="2767">Bugfix: Support passing <code>knex.raw</code> to <code>.whereNot</code> <a href="https://github.com/tgriesser/knex/issues/1402">#1402</a></li><li data-reactid="2768">Docs: Fixed list of dialects which supports <code>.returning</code> <a href="https://github.com/tgriesser/knex/issues/1398">#1398</a></li><li data-reactid="2769">Bugfix: rename table does not fail anymore even with schema defined <a href="https://github.com/tgriesser/knex/issues/1403">#1403</a></li></ul></div><div data-reactid="2770"><b class="header" data-reactid="2771">0.11.3</b><!-- react-text: 2772 --> — <!-- /react-text --><small data-reactid="2773"><i data-reactid="2774">14 May, 2016</i></small><br data-reactid="2775"/><p data-reactid="2776">Support nested joins. <a href="https://github.com/tgriesser/knex/issues/1397">#1397</a></p></div><div data-reactid="2777"><b class="header" data-reactid="2778">0.11.2</b><!-- react-text: 2779 --> — <!-- /react-text --><small data-reactid="2780"><i data-reactid="2781">14 May, 2016</i></small><br data-reactid="2782"/><ul data-reactid="2783"><li data-reactid="2784">Prevent crash on <code>knex seed:make</code>. <a href="https://github.com/tgriesser/knex/issues/1389">#1389</a></li><li data-reactid="2785">Improvements to <code>batchInsert</code>. <a href="https://github.com/tgriesser/knex/issues/1391">#1391</a></li><li data-reactid="2786">Improvements to inserting <code>DEFAULT</code> with <code>undefined</code> binding. <a href="https://github.com/tgriesser/knex/issues/1396">#1396</a></li><li data-reactid="2787">Correct generated code for adding/dropping multiple columns. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1401">#1401</a></li></ul></div><div data-reactid="2788"><b class="header" data-reactid="2789">0.11.1</b><!-- react-text: 2790 --> — <!-- /react-text --><small data-reactid="2791"><i data-reactid="2792">6 May, 2016</i></small><br data-reactid="2793"/><p data-reactid="2794">Fix error in CLI command <code>migrate:make</code>. <a href="https://github.com/tgriesser/knex/issues/1386">#1386</a></p></div><div data-reactid="2795"><b class="header" data-reactid="2796">0.11.0</b><!-- react-text: 2797 --> — <!-- /react-text --><small data-reactid="2798"><i data-reactid="2799">5 May, 2016</i></small><br data-reactid="2800"/><b data-reactid="2801">Breaking Changes:</b><ul data-reactid="2802"><li data-reactid="2803"><code>QueryBuilder#orWhere</code> joins multiple arguments with <code>AND</code>. <a href="https://github.com/tgriesser/knex/issues/1164">#1164</a></li></ul><b data-reactid="2804">Other Changes:</b><ul data-reactid="2805"><li data-reactid="2806">Collate for columns. (MySQL) <a href="https://github.com/tgriesser/knex/issues/1147">#1147</a></li><li data-reactid="2807">Add <code>QueryBuilder#timeout</code>, <code>Raw#timeout</code>. <a href="https://github.com/tgriesser/knex/issues/1201">#1201</a> <a href="https://github.com/tgriesser/knex/issues/1260">#1260</a></li><li data-reactid="2808">Exit with error code when appropriate. <a href="https://github.com/tgriesser/knex/issues/1238">#1238</a></li><li data-reactid="2809">MSSQL connection accepts <code>host</code> as an alias for <code>server</code> in accordance with other dialects. <a href="https://github.com/tgriesser/knex/issues/1239">#1239</a></li><li data-reactid="2810">Add <code>query-response</code> event. <a href="https://github.com/tgriesser/knex/issues/1231">#1231</a></li><li data-reactid="2811">Correct behaviour of sibling nested transactions. <a href="https://github.com/tgriesser/knex/issues/1226">#1226</a></li><li data-reactid="2812">Support <code>RETURNING</code> with <code>UPDATE</code>. (Oracle) <a href="https://github.com/tgriesser/knex/issues/1253">#1253</a></li><li data-reactid="2813">Throwing callbacks from transactions automatically rolls them back. <a href="https://github.com/tgriesser/knex/issues/1257">#1257</a></li><li data-reactid="2814">Fixes to named <code>Raw</code> bindings. <a href="https://github.com/tgriesser/knex/issues/1251">#1251</a></li><li data-reactid="2815"><code>timestamps</code> accepts an argument to set <code>NOT NULL</code> and default to current timestamp.</li><li data-reactid="2816">Add <code>TableBuilder#inherits</code> for PostgreSQL. <a href="https://github.com/tgriesser/knex/issues/601">#601</a></li><li data-reactid="2817">Wrap index names. <a href="https://github.com/tgriesser/knex/issues/1289">#1289</a></li><li data-reactid="2818">Restore coffeescript knexfiles and configurations. <a href="https://github.com/tgriesser/knex/issues/1292">#1292</a></li><li data-reactid="2819">Add <code>andWhereBetween</code> and <code>andWhereNotBetween</code> <a href="https://github.com/tgriesser/knex/issues/1132">#1132</a></li><li data-reactid="2820">Fix <code>valueForUndefined</code> failure. <a href="https://github.com/tgriesser/knex/issues/1269">#1269</a></li><li data-reactid="2821"><code>renameColumn</code> no longer drops default value or nullability. <a href="https://github.com/tgriesser/knex/issues/1326">#1326</a></li><li data-reactid="2822">Correct MySQL2 error handling. <a href="https://github.com/tgriesser/knex/issues/1315">#1315</a></li><li data-reactid="2823">Fix MSSQL <code>createTableIfNotExists</code>. <a href="https://github.com/tgriesser/knex/issues/1362">#1362</a></li><li data-reactid="2824">Fix MSSQL URL parsing. <a href="https://github.com/tgriesser/knex/issues/1342">#1342</a></li><li data-reactid="2825">Update Lodash to 4.6.0 <a href="https://github.com/tgriesser/knex/issues/1242">#1242</a></li><li data-reactid="2826">Update Bluebird to 3.3.4 <a href="https://github.com/tgriesser/knex/issues/1279">#1279</a></li></ul></div><div data-reactid="2827"><b class="header" data-reactid="2828">0.10.0</b><!-- react-text: 2829 --> — <!-- /react-text --><small data-reactid="2830"><i data-reactid="2831">15 Feb, 2016</i></small><br data-reactid="2832"/><b data-reactid="2833">Breaking Changes:</b><ul data-reactid="2834"><li data-reactid="2835"><code>insert</code> and <code>update</code> now ignore <code>undefined</code> values. Back compatibility is provided through the option <code>useNullAsDefault</code>. <a href="https://github.com/tgriesser/knex/issues/1174">#1174</a>, <a href="https://github.com/tgriesser/knex/issues/1043">#1043</a></li></ul><b data-reactid="2836">Other Changes:</b><ul data-reactid="2837"><li data-reactid="2838">Add <a href="#Builder-countDistinct"><code>countDistinct</code></a>, <a href="#Builder-avgDistinct"><code>avgDistinct</code></a> and <a href="#Builder-sumDistinct"><code>sumDistinct</code></a>. <a href="https://github.com/tgriesser/knex/issues/1046">#1046</a></li><li data-reactid="2839">Add <a href="#Schema-jsonb"><code>schema.jsonb</code></a>. Deprecated <code>schema.json(column, true)</code>. <a href="https://github.com/tgriesser/knex/issues/991">#991</a></li><li data-reactid="2840">Support binding identifiers with <code>??</code>. <a href="https://github.com/tgriesser/knex/issues/1103">#1103</a></li><li data-reactid="2841">Restore <code>query</code> event when triggered by transactions. <a href="https://github.com/tgriesser/knex/issues/855">#855</a></li><li data-reactid="2842">Correct question mark escaping in rendered queries. <a href="https://github.com/tgriesser/knex/issues/519">#519</a>, <a href="https://github.com/tgriesser/knex/issues/1058">#1058</a></li><li data-reactid="2843">Add per-dialect escaping, allowing quotes to be escaped correctly. <a href="https://github.com/tgriesser/knex/issues/886">#886</a>, <a href="https://github.com/tgriesser/knex/issues/1095">#1095</a></li><li data-reactid="2844">Add MSSQL support. <a href="https://github.com/tgriesser/knex/issues/1090">#1090</a></li><li data-reactid="2845">Add migration locking. <a href="https://github.com/tgriesser/knex/issues/1094">#1094</a></li><li data-reactid="2846">Allow column aliases to contain <code>.</code>. <a href="https://github.com/tgriesser/knex/issues/1181">#1181</a></li><li data-reactid="2847">Add <code>batchInsert</code>. <a href="https://github.com/tgriesser/knex/issues/1182">#1182</a></li><li data-reactid="2848">Support non-array arguments to <a href="#Raw-Bindings"><code>knex.raw</code></a>.</li><li data-reactid="2849">Global <code>query-error</code> event. <a href="https://github.com/tgriesser/knex/issues/1163">#1163</a></li><li data-reactid="2850">Add <code>batchInsert</code>. <a href="https://github.com/tgriesser/knex/issues/1182">#1182</a></li><li data-reactid="2851">Better support for Mysql2 dialect options. <a href="https://github.com/tgriesser/knex/issues/980">#980</a></li><li data-reactid="2852">Support for <code>acquireConnectionTimeout</code> default 60 seconds preventing <a href="https://github.com/tgriesser/knex/issues/1040">#1040</a> from happening. <a href="https://github.com/tgriesser/knex/issues/1177">#1177</a></li><li data-reactid="2853">Fixed constraint name escaping when dropping a constraint. <a href="https://github.com/tgriesser/knex/issues/1177">#1177</a></li><li data-reactid="2854">Show also <code>.raw</code> queries in debug output. <a href="https://github.com/tgriesser/knex/issues/1169">#1169</a></li><li data-reactid="2855">Support for <code>cli</code> to use basic configuration without specific environment set. <a href="https://github.com/tgriesser/knex/issues/1101">#1101</a></li></ul></div><div data-reactid="2856"><b class="header" data-reactid="2857">0.9.0</b><!-- react-text: 2858 --> — <!-- /react-text --><small data-reactid="2859"><i data-reactid="2860">Nov 2, 2015</i></small><br data-reactid="2861"/><ul data-reactid="2862"><li data-reactid="2863">Fix error when merging <code>knex.raw</code> instances without arguments. <a href="https://github.com/tgriesser/knex/issues/853">#853</a></li><li data-reactid="2864">Fix error that caused the connection to time out while streaming. <a href="https://github.com/tgriesser/knex/issues/849">#849</a></li><li data-reactid="2865">Correctly parse SSL query parameter for PostgreSQL. <a href="https://github.com/tgriesser/knex/issues/852">#852</a></li><li data-reactid="2866">Pass <code>compress</code> option to MySQL2. <a href="https://github.com/tgriesser/knex/issues/843">#843</a></li><li data-reactid="2867">Schema: Use <code>timestamp with timezone</code> by default for <code>time</code>, <code>datetime</code> and <code>timestamp</code> for Oracle. <a href="https://github.com/tgriesser/knex/issues/876">#876</a></li><li data-reactid="2868">Add <a href="#Builder-modify"><code>QueryBuilder#modify</code></a> <a href="https://github.com/tgriesser/knex/issues/881">#881</a></li><li data-reactid="2869">Add LiveScript and Early Gray support for seeds and migrations.</li><li data-reactid="2870">Add <a href="#Builder-withSchema"><code>QueryBuilder#withSchema</code></a> <a href="https://github.com/tgriesser/knex/issues/518">#518</a></li><li data-reactid="2871">Allow escaping of <code>?</code> in <code>knex.raw</code> queries. <a href="https://github.com/tgriesser/knex/issues/946">#946</a></li><li data-reactid="2872">Allow <code>0</code> in join clause. <a href="https://github.com/tgriesser/knex/issues/953">#953</a></li><li data-reactid="2873">Add migration config to allow disabling/enabling transactions per migration. <a href="https://github.com/tgriesser/knex/issues/834">#834</a></li></ul></div><div data-reactid="2874"><b class="header" data-reactid="2875">0.8.6</b><!-- react-text: 2876 --> — <!-- /react-text --><small data-reactid="2877"><i data-reactid="2878">May 20, 2015</i></small><br data-reactid="2879"/><p data-reactid="2880">Fix for several transaction / migration issues, <a href="https://github.com/tgriesser/knex/issues/832">#832</a>, <a href="https://github.com/tgriesser/knex/issues/833">#833</a>, <a href="https://github.com/tgriesser/knex/issues/834">#834</a>, <a href="https://github.com/tgriesser/knex/issues/835">#835</a></p></div><div data-reactid="2881"><b class="header" data-reactid="2882">0.8.5</b><!-- react-text: 2883 --> — <!-- /react-text --><small data-reactid="2884"><i data-reactid="2885">May 14, 2015</i></small><br data-reactid="2886"/><p data-reactid="2887">Pool should be initialized if no pool options are specified</p></div><div data-reactid="2888"><b class="header" data-reactid="2889">0.8.4</b><!-- react-text: 2890 --> — <!-- /react-text --><small data-reactid="2891"><i data-reactid="2892">May 13, 2015</i></small><br data-reactid="2893"/><p data-reactid="2894">Pool should not be initialized if {max: 0} is sent in config options</p></div><div data-reactid="2895"><b class="header" data-reactid="2896">0.8.3</b><!-- react-text: 2897 --> — <!-- /react-text --><small data-reactid="2898"><i data-reactid="2899">May 2, 2015</i></small><br data-reactid="2900"/><p data-reactid="2901">Alias postgresql -> postgres in connection config options</p></div><div data-reactid="2902"><b class="header" data-reactid="2903">0.8.2</b><!-- react-text: 2904 --> — <!-- /react-text --><small data-reactid="2905"><i data-reactid="2906">May 1, 2015</i></small><br data-reactid="2907"/><p data-reactid="2908">Fix regression in using query string in connection config</p></div><div data-reactid="2909"><b class="header" data-reactid="2910">0.8.1</b><!-- react-text: 2911 --> — <!-- /react-text --><small data-reactid="2912"><i data-reactid="2913">May 1, 2015</i></small><br data-reactid="2914"/><ul data-reactid="2915"><li data-reactid="2916">Warn rather than error when implicit commits wipe out savepoints in mysql / mariadb, <a href="https://github.com/tgriesser/knex/issues/805">#805</a>.</li><li data-reactid="2917">Fix for incorrect seed config reference, <a href="https://github.com/tgriesser/knex/issues/804">#804</a></li></ul></div><div data-reactid="2918"><b class="header" data-reactid="2919">0.8.0</b><!-- react-text: 2920 --> — <!-- /react-text --><small data-reactid="2921"><i data-reactid="2922">Apr 30, 2015</i></small><br data-reactid="2923"/><b data-reactid="2924">New Features:</b><ul data-reactid="2925"><li data-reactid="2926">Fixes several major outstanding bugs with the connection pool, switching to <a href="https://github.com/myndzi/pool2">Pool2</a> in place of generic-pool-redux</li><li data-reactid="2927">strong-oracle module support</li><li data-reactid="2928">Nested transactions automatically become savepoints, with <code>commit</code> & <code>rollback</code> releasing or rolling back the current savepoint.</li><li data-reactid="2929">Database seed file support, <a href="https://github.com/tgriesser/knex/issues/391">#391</a></li><li data-reactid="2930">Improved support for sub-raw queries within raw statements</li><li data-reactid="2931">Migrations are now wrapped in transactions where possible</li><li data-reactid="2932">Subqueries supported in insert statements, <a href="https://github.com/tgriesser/knex/issues/627">#627</a></li><li data-reactid="2933">Support for nested having, <a href="https://github.com/tgriesser/knex/issues/572">#572</a></li><li data-reactid="2934">Support object syntax for joins, similar to "where" <a href="https://github.com/tgriesser/knex/issues/743">#743</a></li></ul><b data-reactid="2935">Major Changes:</b><ul data-reactid="2936"><li data-reactid="2937">Transactions are immediately invoked as A+ promises, <a href="https://github.com/tgriesser/knex/issues/470">#470</a> (this is a feature and should not actually break anything in practice)</li><li data-reactid="2938">Heavy refactoring internal APIs (public APIs should not be affected)</li></ul><b data-reactid="2939">"Other Changes:</b><ul data-reactid="2940"><li data-reactid="2941">Allow mysql2 to use non-default port, <a href="https://github.com/tgriesser/knex/issues/588">#588</a></li><li data-reactid="2942">Support creating & dropping extensions in PostgreSQL, <a href="https://github.com/tgriesser/knex/issues/540">#540</a></li><li data-reactid="2943">CLI support for knexfiles that do not provide environment keys, <a href="https://github.com/tgriesser/knex/issues/527">#527</a></li><li data-reactid="2944">Added sqlite3 dialect version of whereRaw/andWhereRaw (<a href="https://github.com/tgriesser/knex/issues/477">#477</a>)</li></ul></div><div data-reactid="2945"><b class="header" data-reactid="2946">0.7.5</b><!-- react-text: 2947 --> — <!-- /react-text --><small data-reactid="2948"><i data-reactid="2949">Mar 9, 2015</i></small><br data-reactid="2950"/><p data-reactid="2951">Fix bug in validateMigrationList, (<a href="https://github.com/tgriesser/knex/issues/697">#697</a>)</p></div><div data-reactid="2952"><b class="header" data-reactid="2953">0.7.4</b><!-- react-text: 2954 --> — <!-- /react-text --><small data-reactid="2955"><i data-reactid="2956">Feb 25, 2015</i></small><br data-reactid="2957"/><ul data-reactid="2958"><li data-reactid="2959">Fix incorrect order of query parameters when using subqueries, <a href="https://github.com/tgriesser/knex/issues/704">#704</a></li><li data-reactid="2960">Properly handle limit 0, (<a href="https://github.com/tgriesser/knex/issues/655">#655</a>)</li><li data-reactid="2961">Apply promise args from then instead of <a href="https://github.com/petkaantonov/bluebird/issues/482">explicitly passing</a>.</li><li data-reactid="2962">Respect union parameter as last argument (<a href="https://github.com/tgriesser/knex/issues/660">#660</a>).</li><li data-reactid="2963">Added sqlite3 dialect version of whereRaw/andWhereRaw (<a href="https://github.com/tgriesser/knex/issues/477">#477</a>).</li><li data-reactid="2964">Fix SQLite dropColumn doesn't work for last column (<a href="https://github.com/tgriesser/knex/issues/544">#544</a>).</li><li data-reactid="2965">Add POSIX operator support for Postgres (<a href="https://github.com/tgriesser/knex/issues/562">#562</a>)</li><li data-reactid="2966">Sample seed files now correctly (<a href="https://github.com/tgriesser/knex/issues/391">#391</a>)</li></ul></div><div data-reactid="2967"><b class="header" data-reactid="2968">0.7.3</b><!-- react-text: 2969 --> — <!-- /react-text --><small data-reactid="2970"><i data-reactid="2971">Oct 3, 2014</i></small><br data-reactid="2972"/><ul data-reactid="2973"><li data-reactid="2974">Support for <code>join(table, rawOrBuilder)</code> syntax.</li><li data-reactid="2975">Fix for regression in PostgreSQL connection (<a href="https://github.com/tgriesser/knex/issues/516">#516</a>)</li></ul></div><div data-reactid="2976"><b class="header" data-reactid="2977">0.7.2</b><!-- react-text: 2978 --> — <!-- /react-text --><small data-reactid="2979"><i data-reactid="2980">Oct 1, 2014</i></small><br data-reactid="2981"/><p data-reactid="2982">Fix for regression in migrations</p></div><div data-reactid="2983"><b class="header" data-reactid="2984">0.7.1</b><!-- react-text: 2985 --> — <!-- /react-text --><small data-reactid="2986"><i data-reactid="2987">Oct 1, 2014</i></small><br data-reactid="2988"/><p data-reactid="2989">Better disconnect handling & pool removal for MySQL clients, <a href="https://github.com/tgriesser/knex/issues/452">#452</a></p></div><div data-reactid="2990"><b class="header" data-reactid="2991">0.7.0</b><!-- react-text: 2992 --> — <!-- /react-text --><small data-reactid="2993"><i data-reactid="2994">Oct 1, 2014</i></small><br data-reactid="2995"/><b data-reactid="2996">New Features</b><ul data-reactid="2997"><li data-reactid="2998">Oracle support, <a href="https://github.com/tgriesser/knex/issues/419">#419</a></li><li data-reactid="2999">Database seed file support, <a href="https://github.com/tgriesser/knex/issues/391">#391</a></li><li data-reactid="3000">Improved support for sub-raw queries within raw statements</li></ul><b data-reactid="3001">Breaking Changes</b><ul data-reactid="3002"><li data-reactid="3003">"collate nocase" no longer used by default in sqlite3 <a href="https://github.com/tgriesser/knex/issues/396">#396</a></li></ul><b data-reactid="3004">Other Changes</b><ul data-reactid="3005"><li data-reactid="3006">Bumping Bluebird to ^2.x</li><li data-reactid="3007">Transactions in websql are now a no-op (unsupported) <a href="https://github.com/tgriesser/knex/issues/375">#375</a></li><li data-reactid="3008">Improved test suite</li><li data-reactid="3009">knex.fn namespace as function helper (knex.fn.now), <a href="https://github.com/tgriesser/knex/issues/372">#372</a></li><li data-reactid="3010">Better handling of disconnect errors</li><li data-reactid="3011">Support for offset without limit, <a href="https://github.com/tgriesser/knex/issues/446">#446</a></li><li data-reactid="3012">Chainable first method for mysql schema, <a href="https://github.com/tgriesser/knex/issues/406">#406</a></li><li data-reactid="3013">Support for empty array in <code>whereIn</code></li><li data-reactid="3014">Create/drop schema for postgres, <a href="https://github.com/tgriesser/knex/issues/511">#511</a></li><li data-reactid="3015">Inserting multiple rows with default values, <a href="https://github.com/tgriesser/knex/issues/468">#468</a></li><li data-reactid="3016">Join columns are optional for cross-join, <a href="https://github.com/tgriesser/knex/issues/508">#508</a></li><li data-reactid="3017">Flag for creating jsonb columns in Postgresql, <a href="https://github.com/tgriesser/knex/issues/500">#500</a></li></ul></div><div data-reactid="3018"><b class="header" data-reactid="3019">0.6.22</b><!-- react-text: 3020 --> — <!-- /react-text --><small data-reactid="3021"><i data-reactid="3022">July 10, 2014</i></small><br data-reactid="3023"/><p data-reactid="3024">Bug fix for properly binding postgresql streaming queries, (<a href="https://github.com/tgriesser/knex/issues/363">#363</a>)</p></div><div data-reactid="3025"><b class="header" data-reactid="3026">0.6.21</b><!-- react-text: 3027 --> — <!-- /react-text --><small data-reactid="3028"><i data-reactid="3029">July 9, 2014</i></small><br data-reactid="3030"/><ul data-reactid="3031"><li data-reactid="3032">Bug fix for raw queries not being transaction context aware, (<a href="https://github.com/tgriesser/knex/issues/351">#351</a>).</li><li data-reactid="3033">Properly forward stream errors in sqlite3 runner, (<a href="https://github.com/tgriesser/knex/issues/359">#359</a>)</li></ul></div><div data-reactid="3034"><b class="header" data-reactid="3035">0.6.20</b><!-- react-text: 3036 --> — <!-- /react-text --><small data-reactid="3037"><i data-reactid="3038">June 30, 2014</i></small><br data-reactid="3039"/><p data-reactid="3040">Allow case insensitive operators in sql clauses, (<a href="https://github.com/tgriesser/knex/issues/344">#344</a>)</p></div><div data-reactid="3041"><b class="header" data-reactid="3042">0.6.19</b><!-- react-text: 3043 --> — <!-- /react-text --><small data-reactid="3044"><i data-reactid="3045">June 27, 2014</i></small><br data-reactid="3046"/><ul data-reactid="3047"><li data-reactid="3048">Add <code>groupByRaw</code> / <code>orderByRaw</code> methods, better support for raw statements in group / order (<a href="https://github.com/tgriesser/knex/issues/282">#282</a>).</li><li data-reactid="3049">Support more config options for node-mysql2 dialect (<a href="https://github.com/tgriesser/knex/issues/341">#341</a>).</li><li data-reactid="3050">CLI help text fix, (<a href="https://github.com/tgriesser/knex/issues/342">#342</a>)</li></ul></div><div data-reactid="3051"><b class="header" data-reactid="3052">0.6.18</b><!-- react-text: 3053 --> — <!-- /react-text --><small data-reactid="3054"><i data-reactid="3055">June 25, 2014</i></small><br data-reactid="3056"/><p data-reactid="3057">Patch for the method, calling without a handler should return the stream, not a promise (<a href="https://github.com/tgriesser/knex/issues/337">#337</a>)</p></div><div data-reactid="3058"><b class="header" data-reactid="3059">0.6.17</b><!-- react-text: 3060 --> — <!-- /react-text --><small data-reactid="3061"><i data-reactid="3062">June 23, 2014</i></small><br data-reactid="3063"/><p data-reactid="3064">Adding missing map / reduce proxies to bluebird's implementation</p></div><div data-reactid="3065"><b class="header" data-reactid="3066">0.6.16</b><!-- react-text: 3067 --> — <!-- /react-text --><small data-reactid="3068"><i data-reactid="3069">June 18, 2014</i></small><br data-reactid="3070"/><ul data-reactid="3071"><li data-reactid="3072">Increment / decrement returns the number of affectedRows (<a href="https://github.com/tgriesser/knex/issues/330">#330</a>).</li><li data-reactid="3073">Allow --cwd option flag to be passed to CLI tool (<a href="https://github.com/tgriesser/knex/issues/326">#326</a>)</li></ul></div><div data-reactid="3074"><b class="header" data-reactid="3075">0.6.15</b><!-- react-text: 3076 --> — <!-- /react-text --><small data-reactid="3077"><i data-reactid="3078">June 14, 2014</i></small><br data-reactid="3079"/><p data-reactid="3080">Added the as method for aliasing subqueries</p></div><div data-reactid="3081"><b class="header" data-reactid="3082">0.6.14</b><!-- react-text: 3083 --> — <!-- /react-text --><small data-reactid="3084"><i data-reactid="3085">June 14, 2014</i></small><br data-reactid="3086"/><p data-reactid="3087">whereExists / whereNotExists may now take a query builder instance as well as a callback</p></div><div data-reactid="3088"><b class="header" data-reactid="3089">0.6.13</b><!-- react-text: 3090 --> — <!-- /react-text --><small data-reactid="3091"><i data-reactid="3092">June 12, 2014</i></small><br data-reactid="3093"/><ul data-reactid="3094"><li data-reactid="3095">Fix regression with onUpdate / onDelete in PostgreSQL, (<a href="https://github.com/tgriesser/knex/issues/308">#308</a>).</li><li data-reactid="3096">Add missing <code>Promise</code> require to knex.js, unit test for knex.destroy (<a href="https://github.com/tgriesser/knex/issues/314">#314</a>)</li></ul></div><div data-reactid="3097"><b class="header" data-reactid="3098">0.6.12</b><!-- react-text: 3099 --> — <!-- /react-text --><small data-reactid="3100"><i data-reactid="3101">June 10, 2014</i></small><br data-reactid="3102"/><p data-reactid="3103">Fix for regression with boolean default types in PostgreSQL</p></div><div data-reactid="3104"><b class="header" data-reactid="3105">0.6.11</b><!-- react-text: 3106 --> — <!-- /react-text --><small data-reactid="3107"><i data-reactid="3108">June 10, 2014</i></small><br data-reactid="3109"/><p data-reactid="3110">Fix for regression with queries containing multiple order by statements in sqlite3</p></div><div data-reactid="3111"><b class="header" data-reactid="3112">0.6.10</b><!-- react-text: 3113 --> — <!-- /react-text --><small data-reactid="3114"><i data-reactid="3115">June 10, 2014</i></small><br data-reactid="3116"/><p data-reactid="3117">Fix for big regression in memoization of column names from 0.5 -> 0.6</p></div><div data-reactid="3118"><b class="header" data-reactid="3119">0.6.9</b><!-- react-text: 3120 --> — <!-- /react-text --><small data-reactid="3121"><i data-reactid="3122">June 9, 2014</i></small><br data-reactid="3123"/><p data-reactid="3124">Fix for regression in specificType method</p></div><div data-reactid="3125"><b class="header" data-reactid="3126">0.6.8</b><!-- react-text: 3127 --> — <!-- /react-text --><small data-reactid="3128"><i data-reactid="3129">June 9, 2014</i></small><br data-reactid="3130"/><p data-reactid="3131">Package.json fix for CLI</p></div><div data-reactid="3132"><b class="header" data-reactid="3133">0.6.7</b><!-- react-text: 3134 --> — <!-- /react-text --><small data-reactid="3135"><i data-reactid="3136">June 9, 2014</i></small><br data-reactid="3137"/><ul data-reactid="3138"><li data-reactid="3139">Adds support for <a href="https://github.com/sidorares/node-mysql2">node-mysql2</a> library.</li><li data-reactid="3140">Bundles CLI with the knex install, various related migrate CLI fixes</li></ul></div><div data-reactid="3141"><b class="header" data-reactid="3142">0.6.6</b><!-- react-text: 3143 --> — <!-- /react-text --><small data-reactid="3144"><i data-reactid="3145">June 9, 2014</i></small><br data-reactid="3146"/><ul data-reactid="3147"><li data-reactid="3148">console.warn rather than throw when adding foreignKeys in SQLite3.</li><li data-reactid="3149">Add support for dropColumn in SQLite3.</li><li data-reactid="3150">Document <code>raw.wrap</code></li></ul></div><div data-reactid="3151"><b class="header" data-reactid="3152">0.6.5</b><!-- react-text: 3153 --> — <!-- /react-text --><small data-reactid="3154"><i data-reactid="3155">June 9, 2014</i></small><br data-reactid="3156"/><p data-reactid="3157">Add missing _ require to WebSQL builds</p></div><div data-reactid="3158"><b class="header" data-reactid="3159">0.6.4</b><!-- react-text: 3160 --> — <!-- /react-text --><small data-reactid="3161"><i data-reactid="3162">June 9, 2014</i></small><br data-reactid="3163"/><p data-reactid="3164">Fix & document schema.raw method</p></div><div data-reactid="3165"><b class="header" data-reactid="3166">0.6.3</b><!-- react-text: 3167 --> — <!-- /react-text --><small data-reactid="3168"><i data-reactid="3169">June 6, 2014</i></small><br data-reactid="3170"/><ul data-reactid="3171"><li data-reactid="3172">Schema methods on transaction object are now transaction aware (<a href="https://github.com/tgriesser/knex/issues/301">#301</a>).</li><li data-reactid="3173">Fix for resolved value from transactions, (<a href="https://github.com/tgriesser/knex/issues/298">#298</a>).</li><li data-reactid="3174">Undefined columns are not added to builder</li></ul></div><div data-reactid="3175"><b class="header" data-reactid="3176">0.6.2</b><!-- react-text: 3177 --> — <!-- /react-text --><small data-reactid="3178"><i data-reactid="3179">June 4, 2014</i></small><br data-reactid="3180"/><ul data-reactid="3181"><li data-reactid="3182">Fix regression in raw query output, (<a href="https://github.com/tgriesser/knex/issues/297">#297</a>).</li><li data-reactid="3183">Fix regression in "pluck" method (<a href="https://github.com/tgriesser/knex/issues/296">#296</a>).</li><li data-reactid="3184">Document <a href="#Builder-first">first</a> method</li></ul></div><div data-reactid="3185"><b class="header" data-reactid="3186">0.6.1</b><!-- react-text: 3187 --> — <!-- /react-text --><small data-reactid="3188"><i data-reactid="3189">June 4, 2014</i></small><br data-reactid="3190"/><p data-reactid="3191">Reverting to using .npmignore, the "files" syntax forgot the knex.js file</p></div><div data-reactid="3192"><b class="header" data-reactid="3193">0.6.0</b><!-- react-text: 3194 --> — <!-- /react-text --><small data-reactid="3195"><i data-reactid="3196">June 4, 2014</i></small><br data-reactid="3197"/><b data-reactid="3198">Major Library refactor:</b><ul data-reactid="3199"><li data-reactid="3200">Major internal overhaul to clean up the various dialect code.</li><li data-reactid="3201">Improved unit test suite.</li><li data-reactid="3202">Support for the <a href="https://github.com/mscdex/node-mariasql">mariasql</a> driver.</li><li data-reactid="3203">More consistent use of raw query bindings throughout the library.</li><li data-reactid="3204">Queries are more composable, may be injected in various points throughout the builder.</li><li data-reactid="3205">Added <a href="#Interfaces-Streams">streaming</a> interface</li><li data-reactid="3206">Deprecated 5 argument <a href="#Builder-join">join</a> in favor of additional join methods.</li><li data-reactid="3207">The wrapValue function to allow for array column operations in PostgreSQL (<a href="https://github.com/tgriesser/knex/issues/287">#287</a>).</li><li data-reactid="3208">An explicit connection can be passed for any query (<a href="https://github.com/tgriesser/knex/issues/56">#56</a>).</li><li data-reactid="3209">Drop column support for sqlite3</li><li data-reactid="3210">All schema actions are run sequentially on the same connection if chained.</li><li data-reactid="3211">Schema actions can now be wrapped in a transaction</li><li data-reactid="3212"><code>.references(tableName.columnName)</code> as shorthand for <code>.references(columnName).inTable(tableName)</code></li><li data-reactid="3213"><code>.join('table.column', 'otherTable.column')</code> as shorthand for .join('table.column', '=', 'otherTable.column')</li><li data-reactid="3214">Streams are supported for selects, passing through to the streaming capabilities of node-mysql and node-postgres</li><li data-reactid="3215">For More information, see this <a href="https://github.com/tgriesser/knex/pull/252">pull-request</a></li></ul></div><div data-reactid="3216"><b class="header" data-reactid="3217">0.5.15</b><!-- react-text: 3218 --> — <!-- /react-text --><small data-reactid="3219"><i data-reactid="3220">June 4, 2014</i></small><br data-reactid="3221"/><p data-reactid="3222">Dropped indexes feature now functions correctly, (<a href="https://github.com/tgriesser/knex/issues/278">#278</a>)</p></div><div data-reactid="3223"><b class="header" data-reactid="3224">0.5.14</b><!-- react-text: 3225 --> — <!-- /react-text --><small data-reactid="3226"><i data-reactid="3227">May 6, 2014</i></small><br data-reactid="3228"/><p data-reactid="3229">Remove the charset encoding if it's utf8 for mysql, as it's the default but also currently causes some issues in recent versions of node-mysql</p></div><div data-reactid="3230"><b class="header" data-reactid="3231">0.5.13</b><!-- react-text: 3232 --> — <!-- /react-text --><small data-reactid="3233"><i data-reactid="3234">April 2, 2014</i></small><br data-reactid="3235"/><p data-reactid="3236">Fix regression in array bindings for postgresql (<a href="https://github.com/tgriesser/knex/issues/228">#228</a>)</p></div><div data-reactid="3237"><b class="header" data-reactid="3238">0.5.12</b><!-- react-text: 3239 --> — <!-- /react-text --><small data-reactid="3240"><i data-reactid="3241">Mar 31, 2014</i></small><br data-reactid="3242"/><p data-reactid="3243">Add more operators for where clauses, including && (<a href="https://github.com/tgriesser/knex/issues/226">#226</a>)</p></div><div data-reactid="3244"><b class="header" data-reactid="3245">0.5.11</b><!-- react-text: 3246 --> — <!-- /react-text --><small data-reactid="3247"><i data-reactid="3248">Mar 25, 2014</i></small><br data-reactid="3249"/><ul data-reactid="3250"><li data-reactid="3251"><code>.where(col, 'is', null)</code> or <code>.where(col, 'is not', null)</code> are not supported (<a href="https://github.com/tgriesser/knex/issues/221">#221</a>).</li><li data-reactid="3252">Case insensitive <code>where</code> operators now allowed (<a href="https://github.com/tgriesser/knex/issues/212">#212</a>).</li><li data-reactid="3253">Fix bug in increment/decrement truncating to an integer (<a href="https://github.com/tgriesser/knex/issues/210">#210</a>).</li><li data-reactid="3254">Disconnected connections are now properly handled & removed from the pool (<a href="https://github.com/tgriesser/knex/issues/206">#206</a>).</li><li data-reactid="3255">Internal tweaks to binding concatenations for performance (<a href="https://github.com/tgriesser/knex/issues/207">#207</a>)</li></ul></div><div data-reactid="3256"><b class="header" data-reactid="3257">0.5.10</b><!-- react-text: 3258 --> — <!-- /react-text --><small data-reactid="3259"><i data-reactid="3260">Mar 19, 2014</i></small><br data-reactid="3261"/><p data-reactid="3262">Add the .exec method to the internal promise shim</p></div><div data-reactid="3263"><b class="header" data-reactid="3264">0.5.9</b><!-- react-text: 3265 --> — <!-- /react-text --><small data-reactid="3266"><i data-reactid="3267">Mar 18, 2014</i></small><br data-reactid="3268"/><p data-reactid="3269">Remove error'ed connections from the connection pool (<a href="https://github.com/tgriesser/knex/issues/206">#206</a>), added support for node-postgres-pure (pg.js) (<a href="https://github.com/tgriesser/knex/issues/200">#200</a>)</p></div><div data-reactid="3270"><b class="header" data-reactid="3271">0.5.8</b><!-- react-text: 3272 --> — <!-- /react-text --><small data-reactid="3273"><i data-reactid="3274">Feb 27, 2014</i></small><br data-reactid="3275"/><p data-reactid="3276">Fix for chaining on forUpdate / forShare, adding map & reduce from bluebird</p></div><div data-reactid="3277"><b class="header" data-reactid="3278">0.5.7</b><!-- react-text: 3279 --> — <!-- /react-text --><small data-reactid="3280"><i data-reactid="3281">Feb 18, 2014</i></small><br data-reactid="3282"/><p data-reactid="3283">Fix for a null limit / offset breaking query chain (<a href="https://github.com/tgriesser/knex/issues/182">#182</a>)</p></div><div data-reactid="3284"><b class="header" data-reactid="3285">0.5.6</b><!-- react-text: 3286 --> — <!-- /react-text --><small data-reactid="3287"><i data-reactid="3288">Feb 5, 2014</i></small><br data-reactid="3289"/><p data-reactid="3290">Bump bluebird dependency to ~1.0.0, fixing regression in Bluebird 1.0.2 (<a href="https://github.com/tgriesser/knex/issues/176">#176</a>)</p></div><div data-reactid="3291"><b class="header" data-reactid="3292">0.5.5</b><!-- react-text: 3293 --> — <!-- /react-text --><small data-reactid="3294"><i data-reactid="3295">Jan 28, 2014</i></small><br data-reactid="3296"/><ul data-reactid="3297"><li data-reactid="3298">Fix for the exit code on the migrations cli (<a href="https://github.com/tgriesser/knex/issues/151">#151</a>).</li><li data-reactid="3299">The <code>init</code> method in <code>knex.migrate</code> now uses <code>this.config</code> if one isn't passed in (<a href="https://github.com/tgriesser/knex/issues/156">#156</a>)</li></ul></div><div data-reactid="3300"><b class="header" data-reactid="3301">0.5.4</b><!-- react-text: 3302 --> — <!-- /react-text --><small data-reactid="3303"><i data-reactid="3304">Jan 7, 2014</i></small><br data-reactid="3305"/><p data-reactid="3306">Fix for using raw statements in defaultTo schema builder methods (<a href="https://github.com/tgriesser/knex/issues/146">#146</a>)</p></div><div data-reactid="3307"><b class="header" data-reactid="3308">0.5.3</b><!-- react-text: 3309 --> — <!-- /react-text --><small data-reactid="3310"><i data-reactid="3311">Jan 2, 2014</i></small><br data-reactid="3312"/><p data-reactid="3313">Fix for incorrectly formed sql when aggregates are used with columns (<a href="https://github.com/tgriesser/knex/issues/144">#144</a>)</p></div><div data-reactid="3314"><b class="header" data-reactid="3315">0.5.2</b><!-- react-text: 3316 --> — <!-- /react-text --><small data-reactid="3317"><i data-reactid="3318">Dec 18, 2013</i></small><br data-reactid="3319"/><p data-reactid="3320">Adding passthrough "catch", "finally" to bluebird implementations, use bluebird's "nodeify" internally for exec</p></div><div data-reactid="3321"><b class="header" data-reactid="3322">0.5.1</b><!-- react-text: 3323 --> — <!-- /react-text --><small data-reactid="3324"><i data-reactid="3325">Dec 12, 2013</i></small><br data-reactid="3326"/><ul data-reactid="3327"><li data-reactid="3328">The <a href="#Builder-returning">returning</a> in PostgreSQL may now accept * or an array of columns to return. If either of these are passed, the response will be an array of objects rather than an array of values. Updates may also now use a <code>returning</code> value. (<a href="https://github.com/tgriesser/knex/issues/132">#132</a>)</li><li data-reactid="3329">Added <code>bigint</code> and <code>bigserial</code> type to PostgreSQL. (<a href="https://github.com/tgriesser/knex/issues/111">#111</a>)</li><li data-reactid="3330">Fix for the <a href="#Schema-specificType">specificType</a> schema call (<a href="https://github.com/tgriesser/knex/issues/118">#118</a>)</li><li data-reactid="3331">Several fixes for migrations, including migration file path fixes, passing a Promise constructor to the migration <code>up</code> and <code>down</code> methods, allowing the "knex" module to be used globally, file ordering on migrations, and other small improvements. (<a href="https://github.com/tgriesser/knex/issues/112">#112</a>-115, <a href="https://github.com/tgriesser/knex/issues/125">#125</a>, <a href="https://github.com/tgriesser/knex/issues/135">#135</a>)</li></ul></div><div data-reactid="3332"><b class="header" data-reactid="3333">0.5.0</b><!-- react-text: 3334 --> — <!-- /react-text --><small data-reactid="3335"><i data-reactid="3336">Nov 25, 2013</i></small><br data-reactid="3337"/><ul data-reactid="3338"><li data-reactid="3339">Initial pass at a <a href="#Migrations">migration</a> api.</li><li data-reactid="3340">Aggregate methods are no longer aliased as "aggregate", but may now be aliased and have more than one aggregate in a query (<a href="https://github.com/tgriesser/knex/issues/108">#108</a>, <a href="https://github.com/tgriesser/knex/issues/110">#110</a>).</li><li data-reactid="3341">Adding bigint and bigserial to PostgreSQL (<a href="https://github.com/tgriesser/knex/issues/111">#111</a>).</li><li data-reactid="3342">Bugfix on increment/decrement values (<a href="https://github.com/tgriesser/knex/issues/100">#100</a>).</li><li data-reactid="3343">Bugfix with having method (<a href="https://github.com/tgriesser/knex/issues/107">#107</a>).</li><li data-reactid="3344">Switched from when.js to <a href="https://github.com/petkaantonov/bluebird">bluebird</a> for promise implementation, with shim for backward compatibility.</li><li data-reactid="3345">Switched from underscore to lodash, for semver reliability</li></ul></div><div data-reactid="3346"><b class="header" data-reactid="3347">0.4.13</b><!-- react-text: 3348 --> — <!-- /react-text --><small data-reactid="3349"><i data-reactid="3350">Oct 31, 2013</i></small><br data-reactid="3351"/><p data-reactid="3352">Fix for aggregate methods on toString and clone, (<a href="https://github.com/tgriesser/knex/issues/98">#98</a>)</p></div><div data-reactid="3353"><b class="header" data-reactid="3354">0.4.12</b><!-- react-text: 3355 --> — <!-- /react-text --><small data-reactid="3356"><i data-reactid="3357">Oct 29, 2013</i></small><br data-reactid="3358"/><p data-reactid="3359">Fix incorrect values passed to float in MySQL and decimal in PostgreSQL</p></div><div data-reactid="3360"><b class="header" data-reactid="3361">0.4.11</b><!-- react-text: 3362 --> — <!-- /react-text --><small data-reactid="3363"><i data-reactid="3364">Oct 15, 2013</i></small><br data-reactid="3365"/><p data-reactid="3366">Fix potential sql injection vulnerability in orderBy, thanks to @sebgie</p></div><div data-reactid="3367"><b class="header" data-reactid="3368">0.4.10</b><!-- react-text: 3369 --> — <!-- /react-text --><small data-reactid="3370"><i data-reactid="3371">Oct 14, 2013</i></small><br data-reactid="3372"/><ul data-reactid="3373"><li data-reactid="3374">Added <a href="#Builder-forUpdate">forUpdate</a> and <a href="#Builder-forShare">forShare</a> for select modes in transactions. (<a href="https://github.com/tgriesser/knex/issues/84">#84</a>)</li><li data-reactid="3375">Fix bug where current query chain type is not copied on <a href="#Builder-clone">clone</a>. (<a href="https://github.com/tgriesser/knex/issues/90">#90</a>)</li><li data-reactid="3376">Charset and collate are now added as methods on the schema builder. (<a href="https://github.com/tgriesser/knex/issues/89">#89</a>)</li><li data-reactid="3377">Added <code>into</code> as an alias of <a href="#Builder-from">from</a>, for builder syntax of: <code>insert(value).into(tableName)</code></li><li data-reactid="3378">Internal pool fixes. (<a href="https://github.com/tgriesser/knex/issues/90">#90</a>)</li></ul></div><div data-reactid="3379"><b class="header" data-reactid="3380">0.4.9</b><!-- react-text: 3381 --> — <!-- /react-text --><small data-reactid="3382"><i data-reactid="3383">Oct 7, 2013</i></small><br data-reactid="3384"/><ul data-reactid="3385"><li data-reactid="3386">Fix for documentation of <a href="#Schema-hasColumn">hasColumn</a>, ensure that <code>hasColumn</code> works with MySQL (<a href="https://github.com/tgriesser/knex/issues/87">#87</a>).</li><li data-reactid="3387">More cleanup of error messages, showing the original error message concatenated with the sql and bindings</li></ul></div><div data-reactid="3388"><b class="header" data-reactid="3389">0.4.8</b><!-- react-text: 3390 --> — <!-- /react-text --><small data-reactid="3391"><i data-reactid="3392">Oct 2, 2013</i></small><br data-reactid="3393"/><p data-reactid="3394">Connections are no longer pushed back into the pool if they never existed to begin with (<a href="https://github.com/tgriesser/knex/issues/85">#85</a>)</p></div><div data-reactid="3395"><b class="header" data-reactid="3396">0.4.7</b><!-- react-text: 3397 --> — <!-- /react-text --><small data-reactid="3398"><i data-reactid="3399">Sep 27, 2013</i></small><br data-reactid="3400"/><p data-reactid="3401">The column is now a documented method on the builder api, and takes either an individual column or an array of columns to select</p></div><div data-reactid="3402"><b class="header" data-reactid="3403">0.4.6</b><!-- react-text: 3404 --> — <!-- /react-text --><small data-reactid="3405"><i data-reactid="3406">Sep 25, 2013</i></small><br data-reactid="3407"/><p data-reactid="3408">Standardizing handling of errors for easier debugging, as noted in (<a href="https://github.com/tgriesser/knex/issues/39">#39</a>)</p></div><div data-reactid="3409"><b class="header" data-reactid="3410">0.4.5</b><!-- react-text: 3411 --> — <!-- /react-text --><small data-reactid="3412"><i data-reactid="3413">Sep 24, 2013</i></small><br data-reactid="3414"/><p data-reactid="3415">Fix for hasTable always returning true in MySQL (<a href="https://github.com/tgriesser/knex/issues/82">#82</a>), fix where sql queries were duplicated with multiple calls on toSql with the schema builder</p></div><div data-reactid="3416"><b class="header" data-reactid="3417">0.4.4</b><!-- react-text: 3418 --> — <!-- /react-text --><small data-reactid="3419"><i data-reactid="3420">Sep 22, 2013</i></small><br data-reactid="3421"/><p data-reactid="3422">Fix for debug method not properly debugging individual queries</p></div><div data-reactid="3423"><b class="header" data-reactid="3424">0.4.3</b><!-- react-text: 3425 --> — <!-- /react-text --><small data-reactid="3426"><i data-reactid="3427">Sep 18, 2013</i></small><br data-reactid="3428"/><p data-reactid="3429">Fix for underscore not being defined in various grammar files</p></div><div data-reactid="3430"><b class="header" data-reactid="3431">0.4.2</b><!-- react-text: 3432 --> — <!-- /react-text --><small data-reactid="3433"><i data-reactid="3434">Sep 17, 2013</i></small><br data-reactid="3435"/><p data-reactid="3436">Fix for an error being thrown when an initialized ClientBase instance was passed into Knex.initialize. pool.destroy now optionally accepts a callback to notify when it has completed draining and destroying all connections</p></div><div data-reactid="3437"><b class="header" data-reactid="3438">0.4.1</b><!-- react-text: 3439 --> — <!-- /react-text --><small data-reactid="3440"><i data-reactid="3441">Sep 16, 2013</i></small><br data-reactid="3442"/><p data-reactid="3443">Cleanup from the 0.4.0 release, fix a potential exploit in "where" clauses pointed out by Andri Möll, fix for clients not being properly released from the pool <a href="https://github.com/tgriesser/knex/issues/70">#70</a>, fix for where("foo", "<>", null) doing an "IS NULL" statement</p></div><div data-reactid="3444"><b class="header" data-reactid="3445">0.4.0</b><!-- react-text: 3446 --> — <!-- /react-text --><small data-reactid="3447"><i data-reactid="3448">Sep 13, 2013</i></small><br data-reactid="3449"/><b data-reactid="3450">Breaking Changes:</b><ul data-reactid="3451"><li data-reactid="3452">Global state is no longer stored in the library, an instance is returned from <code>Knex.initialize</code>, so you will need to call this once and then reference this <code>knex</code> client elsewhere in your application.</li><li data-reactid="3453">Lowercasing of <code>knex.raw</code>, <code>knex.transaction</code>, and <code>knex.schema</code>.</li><li data-reactid="3454">Created columns are now nullable by default, unless <code>notNullable</code> is chained as an option.</li><li data-reactid="3455">Keys created with <code>increments</code> are now assumed to be unsigned (MySQL) by default.</li><li data-reactid="3456">The <code>destroyAllNow</code> is no longer called by the library on <code>process.exit</code> event. If you need to call it explicitly yourself, you may use <code>knex.client.destroyPool</code></li></ul></div><div data-reactid="3457"><b class="header" data-reactid="3458">0.2.6</b><!-- react-text: 3459 --> — <!-- /react-text --><small data-reactid="3460"><i data-reactid="3461">Aug 29, 2013</i></small><br data-reactid="3462"/><p data-reactid="3463">Reject the transaction promise if the transaction "commit" fails, (<a href="https://github.com/tgriesser/knex/issues/50">#50</a>)</p></div><div data-reactid="3464"><b class="header" data-reactid="3465">0.2.5</b><!-- react-text: 3466 --> — <!-- /react-text --><small data-reactid="3467"><i data-reactid="3468">Aug 25, 2013</i></small><br data-reactid="3469"/><p data-reactid="3470">Fix error if a callback isn't specified for exec, (<a href="https://github.com/tgriesser/knex/issues/49">#49</a>)</p></div><div data-reactid="3471"><b class="header" data-reactid="3472">0.2.4</b><!-- react-text: 3473 --> — <!-- /react-text --><small data-reactid="3474"><i data-reactid="3475">Aug 22, 2013</i></small><br data-reactid="3476"/><p data-reactid="3477">Fix SQLite3 delete not returning affected row count, (<a href="https://github.com/tgriesser/knex/issues/45">#45</a>)</p></div><div data-reactid="3478"><b class="header" data-reactid="3479">0.2.3</b><!-- react-text: 3480 --> — <!-- /react-text --><small data-reactid="3481"><i data-reactid="3482">Aug 22, 2013</i></small><br data-reactid="3483"/><p data-reactid="3484">Fix insert with default values in PostgreSQL and SQLite3, (<a href="https://github.com/tgriesser/knex/issues/44">#44</a>)</p></div><div data-reactid="3485"><b class="header" data-reactid="3486">0.2.2</b><!-- react-text: 3487 --> — <!-- /react-text --><small data-reactid="3488"><i data-reactid="3489">Aug 20, 2013</i></small><br data-reactid="3490"/><p data-reactid="3491">Allowing Raw queries to be passed as the primary table names</p></div><div data-reactid="3492"><b class="header" data-reactid="3493">0.2.1</b><!-- react-text: 3494 --> — <!-- /react-text --><small data-reactid="3495"><i data-reactid="3496">Aug 13, 2013</i></small><br data-reactid="3497"/><p data-reactid="3498">Fix for an array passed to insert being mutated</p></div><div data-reactid="3499"><b class="header" data-reactid="3500">0.2.0</b><!-- react-text: 3501 --> — <!-- /react-text --><small data-reactid="3502"><i data-reactid="3503">Aug 7, 2013</i></small><br data-reactid="3504"/><b data-reactid="3505">Breaking changes:</b><ul data-reactid="3506"><li data-reactid="3507"><a href="#Schema-hasTable">hasTable</a> now returns a boolean rather than a failed promise.</li><li data-reactid="3508">Changed syntax for insert in postgresql, where the <code>id</code> is not assumed on inserts (<a href="https://github.com/tgriesser/knex/issues/18">#18</a>). The second parameter of <a href="#Builder-insert">insert</a> is now required to return an array of insert id's for the last insert.</li><li data-reactid="3509">The <a href="#Schema-timestamp">timestamp</a> method on the schema builder now uses a <code>dateTime</code> rather than a <code>timestamp</code></li></ul></div><div data-reactid="3510"><b class="header" data-reactid="3511">0.1.8</b><!-- react-text: 3512 --> — <!-- /react-text --><small data-reactid="3513"><i data-reactid="3514">July 7, 2013</i></small><br data-reactid="3515"/><p data-reactid="3516">Somehow missing the != operator. Using <em>.find rather than </em>.where in getCommandsByName(<a href="https://github.com/tgriesser/knex/issues/22">#22</a>)</p></div><div data-reactid="3517"><b class="header" data-reactid="3518">0.1.7</b><!-- react-text: 3519 --> — <!-- /react-text --><small data-reactid="3520"><i data-reactid="3521">June 12, 2013</i></small><br data-reactid="3522"/><p data-reactid="3523">Ensures unhandled errors in the exec callback interface are re-thrown</p></div><div data-reactid="3524"><b class="header" data-reactid="3525">0.1.6</b><!-- react-text: 3526 --> — <!-- /react-text --><small data-reactid="3527"><i data-reactid="3528">June 9, 2013</i></small><br data-reactid="3529"/><p data-reactid="3530">Renaming beforeCreate to afterCreate. Better handling of errors in the connection pooling</p></div><div data-reactid="3531"><b class="header" data-reactid="3532">0.1.5</b><!-- react-text: 3533 --> — <!-- /react-text --><small data-reactid="3534"><i data-reactid="3535">June 9, 2013</i></small><br data-reactid="3536"/><p data-reactid="3537">Added the ability to specify beforeCreate and beforeDestroy hooks on the initialize's options.pool to perform any necessary database setup/teardown on connections before use (<a href="https://github.com/tgriesser/knex/issues/14">#14</a>). where and having may now accept Knex.Raw instances, for consistency (<a href="https://github.com/tgriesser/knex/issues/15">#15</a>). Added an orHaving method to the builder. The ability to specify bindings on Raw queries has been removed</p></div><div data-reactid="3538"><b class="header" data-reactid="3539">0.1.4</b><!-- react-text: 3540 --> — <!-- /react-text --><small data-reactid="3541"><i data-reactid="3542">May 22, 2013</i></small><br data-reactid="3543"/><p data-reactid="3544">defaultTo now accepts "false" for boolean columns, allows for empty strings as default values</p></div><div data-reactid="3545"><b class="header" data-reactid="3546">0.1.3</b><!-- react-text: 3547 --> — <!-- /react-text --><small data-reactid="3548"><i data-reactid="3549">May 18, 2013</i></small><br data-reactid="3550"/><p data-reactid="3551">Enabling table aliases (<a href="https://github.com/tgriesser/knex/issues/11">#11</a>). Fix for issues with transactions not functioning (<a href="https://github.com/tgriesser/knex/issues/12">#12</a>)</p></div><div data-reactid="3552"><b class="header" data-reactid="3553">0.1.2</b><!-- react-text: 3554 --> — <!-- /react-text --><small data-reactid="3555"><i data-reactid="3556">May 15, 2013</i></small><br data-reactid="3557"/><p data-reactid="3558">Bug fixes for groupBy (<a href="https://github.com/tgriesser/knex/issues/7">#7</a>). Mysql using collation, charset config settings in createTable. Added engine on schemaBuilder specifier (<a href="https://github.com/tgriesser/knex/issues/6">#6</a>). Other doc fixes, tests</p></div><div data-reactid="3559"><b class="header" data-reactid="3560">0.1.1</b><!-- react-text: 3561 --> — <!-- /react-text --><small data-reactid="3562"><i data-reactid="3563">May 14, 2013</i></small><br data-reactid="3564"/><p data-reactid="3565">Bug fixes for sub-queries, minor changes to initializing "main" instance, adding "pg" as a valid parameter for the client name in the connection settings</p></div><div data-reactid="3566"><b class="header" data-reactid="3567">0.1.0</b><!-- react-text: 3568 --> — <!-- /react-text --><small data-reactid="3569"><i data-reactid="3570">May 13, 2013</i></small><br data-reactid="3571"/><p data-reactid="3572">Initial Knex release</p></div></div></div></div></div></div></div>
</body>
<script type="text/javascript" src="build/bundle.js"></script>
<script type="text/javascript" src="assets/ga.js"></script>
</html>