forked from knex/knex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
3791 lines (3141 loc) · 135 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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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="icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="docs/assets/style.css" />
<link rel="stylesheet" href="docs/assets/highlight.min.css" />
<title>Knex.js - A SQL Query Builder for Javascript</title>
<!--
._____. ______
| | / /
| | / /
| | _____ ._____ ._____. ,_____________. ______ / /
| | / / | \ | | / \ \ \ / /
| | / / | \ | | / \ \ \ / /
| | / / | \ | | / ,______. \ \ \ / /
| | / / | \ | | / / \ \ \ \/ /
| |/ \ | \ | | | | ____\ | \ /
| |\ \ | \| | | | / | \ \
| | \ \ | |\ | | | /__________| / \
| | \ \ | | \ | \ \ / /\ \
| | \ \ | | \ | \ \__________/\ / / \ \
| | \ \ | | \ | \ \ / / \ \
|_____| \_____\ |_____| \______| \_______________/ /_____/ \_____\
-->
</head>
<body>
<div id="sidebar" class="interface">
<a class="toc_title" href="#">
Knex.js <span class="version">(0.10.0)</span>
</a>
<ul class="toc_section">
<li>» <a href="https://github.com/tgriesser/knex">GitHub Repository</a></li>
<li>» <a href="#support">Support</a></li>
<li>» <a href="#faq">FAQ</a></li>
<li>» <a href="#changelog">Change Log</a></li>
</ul>
<a class="toc_title" href="#Upgrading">
Upgrading
</a>
<ul class="toc_section">
<li>– <a href="#Upgrading-from0.9">0.9.x -> 0.10</a></li>
</ul>
<a class="toc_title" href="#Installation">
Installation
</a>
<ul class="toc_section">
<li>– <a href="#Installation-node">Node.js</a></li>
<li>– <a href="#Installation-browser">Browser</a></li>
<li><b><a href="#Installation-client">Config Options:</a></b></li>
<li> – <a href="#Installation-client">client</a></li>
<li> – <a href="#Installation-debug">debug</a></li>
<li> – <a href="#Installation-pooling">pooling</a></li>
<li> – <a href="#Installation-acquireConnectionTimeout">acquireConnectionTimeout</a></li>
<li> – <a href="#Installation-migrations">migrations</a></li>
</ul>
<a class="toc_title" href="#Builder">
Query Builder
</a>
<ul class="toc_section">
<li>– <a href="#Builder-main"><b>constructor</b></a></li>
<li>– <a href="#Builder-timeout">timeout</a></li>
<li>– <a href="#Builder-select">select</a></li>
<li>– <a href="#Builder-as">as</a></li>
<li>– <a href="#Builder-column">column</a></li>
<li>– <a href="#Builder-from">from</a></li>
<li>– <a href="#Builder-withSchema">withSchema</a></li>
<li><b><a href="#Builder-wheres">Where Methods:</a></b></li>
<li> - <a href="#Builder-where">where</a></li>
<li> - <a href="#Builder-whereNot">whereNot</a></li>
<li> - <a href="#Builder-whereIn">whereIn</a></li>
<li> - <a href="#Builder-whereNotIn">whereNotIn</a></li>
<li> - <a href="#Builder-whereNull">whereNull</a></li>
<li> - <a href="#Builder-whereNotNull">whereNotNull</a></li>
<li> - <a href="#Builder-whereExists">whereExists</a></li>
<li> - <a href="#Builder-whereNotExists">whereNotExists</a></li>
<li> - <a href="#Builder-whereBetween">whereBetween</a></li>
<li> - <a href="#Builder-whereNotBetween">whereNotBetween</a></li>
<li> - <a href="#Builder-whereRaw">whereRaw</a></li>
<li><b><a href="#Builder-join">Join Methods:</a></b></li>
<li> – <a href="#Builder-innerJoin">innerJoin</a></li>
<li> – <a href="#Builder-leftJoin">leftJoin</a></li>
<li> – <a href="#Builder-leftOuterJoin">leftOuterJoin</a></li>
<li> – <a href="#Builder-rightJoin">rightJoin</a></li>
<li> – <a href="#Builder-rightOuterJoin">rightOuterJoin</a></li>
<li> – <a href="#Builder-outerJoin">outerJoin</a></li>
<li> – <a href="#Builder-fullOuterJoin">fullOuterJoin</a></li>
<li> – <a href="#Builder-crossJoin">crossJoin</a></li>
<li> – <a href="#Builder-joinRaw">joinRaw</a></li>
<li>– <a href="#Builder-distinct">distinct</a></li>
<li>– <a href="#Builder-groupBy">groupBy</a></li>
<li>– <a href="#Builder-groupByRaw">groupByRaw</a></li>
<li>– <a href="#Builder-orderBy">orderBy</a></li>
<li>– <a href="#Builder-orderByRaw">orderByRaw</a></li>
<li>– <a href="#Builder-having">having</a></li>
<li>– <a href="#Builder-offset">offset</a></li>
<li>– <a href="#Builder-limit">limit</a></li>
<li>– <a href="#Builder-union">union</a></li>
<li>– <a href="#Builder-unionAll">unionAll</a></li>
<li>– <a href="#Builder-insert">insert</a></li>
<li>– <a href="#Builder-returning">returning</a></li>
<li>– <a href="#Builder-update">update</a></li>
<li>– <a href="#Builder-del">del / delete</a></li>
<li>– <a href="#Builder-transacting">transacting</a></li>
<li> – <a href="#Builder-forUpdate">forUpdate</a></li>
<li> – <a href="#Builder-forShare">forShare</a></li>
<li>– <a href="#Builder-count">count</a></li>
<li>– <a href="#Builder-min">min</a></li>
<li>– <a href="#Builder-max">max</a></li>
<li>– <a href="#Builder-sum">sum</a></li>
<li>– <a href="#Builder-avg">avg</a></li>
<li>– <a href="#Builder-increment">increment</a></li>
<li>– <a href="#Builder-decrement">decrement</a></li>
<li>– <a href="#Builder-truncate">truncate</a></li>
<li>– <a href="#Builder-pluck">pluck</a></li>
<li>– <a href="#Builder-first">first</a></li>
<li>– <a href="#Builder-clone">clone</a></li>
<li>– <a href="#Builder-modify">modify</a></li>
<li>– <a href="#Builder-columnInfo">columnInfo</a></li>
<li>– <a href="#Builder-debug">debug</a></li>
<li>– <a href="#Builder-connection">connection</a></li>
<li>– <a href="#Builder-options">options</a></li>
</ul>
<a class="toc_title" href="#Transactions">
Transactions
</a>
<ul class="toc_section">
<li>– <a href="#Transactions"><b>overview</b></a></li>
</ul>
<a class="toc_title" href="#Schema">
Schema Builder
</a>
<ul class="toc_section">
<li>– <a href="#Schema-withSchema">withSchema</a></li>
<li>– <a href="#Schema-createTable">createTable</a></li>
<li>– <a href="#Schema-createTableIfNotExists">createTableIfNotExists</a></li>
<li>– <a href="#Schema-renameTable">renameTable</a></li>
<li>– <a href="#Schema-dropTable">dropTable</a></li>
<li>– <a href="#Schema-hasColumn">hasColumn</a></li>
<li>– <a href="#Schema-hasTable">hasTable</a></li>
<li>– <a href="#Schema-dropTableIfExists">dropTableIfExists</a></li>
<li>– <a href="#Schema-table">table</a></li>
<li>– <a href="#Schema-raw">raw</a></li>
<li><b><a href="#Schema-Building">Schema Building:</a></b></li>
<li>– <a href="#Schema-dropColumn">dropColumn</a></li>
<li>– <a href="#Schema-dropColumns">dropColumns</a></li>
<li>– <a href="#Schema-renameColumn">renameColumn</a></li>
<li>– <a href="#Schema-increments">increments</a></li>
<li>– <a href="#Schema-integer">integer</a></li>
<li>– <a href="#Schema-bigInteger">bigInteger</a></li>
<li>– <a href="#Schema-text">text</a></li>
<li>– <a href="#Schema-string">string</a></li>
<li>– <a href="#Schema-float">float</a></li>
<li>– <a href="#Schema-decimal">decimal</a></li>
<li>– <a href="#Schema-boolean">boolean</a></li>
<li>– <a href="#Schema-date">date</a></li>
<li>– <a href="#Schema-dateTime">dateTime</a></li>
<li>– <a href="#Schema-time">time</a></li>
<li>– <a href="#Schema-timestamp">timestamp</a></li>
<li>– <a href="#Schema-timestamps">timestamps</a></li>
<li>– <a href="#Schema-binary">binary</a></li>
<li>– <a href="#Schema-enum">enum / enu</a></li>
<li>– <a href="#Schema-json">json</a></li>
<li>– <a href="#Schema-jsonb">jsonb</a></li>
<li>– <a href="#Schema-uuid">uuid</a></li>
<li>– <a href="#Schema-comment">comment</a></li>
<li>– <a href="#Schema-engine">engine</a></li>
<li>– <a href="#Schema-charset">charset</a></li>
<li>– <a href="#Schema-collate">collate</a></li>
<li>– <a href="#Schema-inherits">inherits</a></li>
<li>– <a href="#Schema-specificType">specificType</a></li>
<li>– <a href="#Schema-index">index</a></li>
<li>– <a href="#Schema-dropIndex">dropIndex</a></li>
<li>– <a href="#Schema-foreign">Foreign</a></li>
<li>– <a href="#Schema-dropForeign">dropForeign</a></li>
<li>– <a href="#Schema-dropUnique">dropUnique</a></li>
<li>– <a href="#Schema-dropPrimary">dropPrimary</a></li>
<li><a href="#Chainable"><b>Chainable:</b></li>
<li>– <a href="#Chainable-index">index</a></li>
<li>– <a href="#Chainable-primary">primary</a></li>
<li>– <a href="#Chainable-unique">unique</a></li>
<li>– <a href="#Chainable-references">references</a></li>
<li>– <a href="#Chainable-inTable">inTable</a></li>
<li>– <a href="#Chainable-onDelete">onDelete</a></li>
<li>– <a href="#Chainable-onUpdate">onUpdate</a></li>
<li>– <a href="#Chainable-defaultTo">defaultTo</a></li>
<li>– <a href="#Chainable-unsigned">unsigned</a></li>
<li>– <a href="#Chainable-notNullable">notNullable</a></li>
<li>– <a href="#Chainable-nullable">nullable</a></li>
<li>– <a href="#Chainable-first">first</a></li>
<li>– <a href="#Chainable-after">after</a></li>
<li>– <a href="#Chainable-comment">comment</a></li>
<li>– <a href="#Chainable-collate">collate</a></li>
</ul>
<a class="toc_title" href="#Raw">
Raw
</a>
<ul class="toc_section">
<li>– <a href="#Raw-Bindings">Raw Parameter Binding</a></li>
<li>– <a href="#Raw-Expressions">Raw Expressions</a></li>
<li>– <a href="#Raw-Queries">Raw Queries</a></li>
<li>- <a href="#Raw-queries-wrapped">Wrapped Queries</a></li>
</ul>
<a class="toc_title" href="#Utility">
Utility
</a>
<ul class="toc_section">
<li>- <a href="#Utility-BatchInsert">Batch Insert</a></li>
</ul>
<a class="toc_title" href="#Interfaces">
Interfaces
</a>
<ul class="toc_section">
<li><b><a href="#Interfaces-Promises">Promises</a></b></li>
<li> – <a href="#Promises-then">then</a></li>
<li> – <a href="#Promises-catch">catch</a></li>
<li> – <a href="#Promises-tap">tap</a></li>
<li> – <a href="#Promises-map">map</a></li>
<li> – <a href="#Promises-reduce">reduce</a></li>
<li> – <a href="#Promises-bind">bind</a></li>
<li> – <a href="#Promises-return">return</a></li>
<li><b><a href="#Interfaces-Callbacks">Callbacks</a></b></li>
<li> – <a href="#Interfaces-asCallback">asCallback</a></li>
<li><b><a href="#Interfaces-Streams">Streams</a></b></li>
<li> – <a href="#Streams-stream">stream</a></li>
<li> – <a href="#Streams-pipe">pipe</a></li>
<li><b><a href="#Interfaces-Events">Events</a></b></li>
<li> – <a href="#Events-query">query</a></li>
<li> – <a href="#Events-query-error">query-error</a></li>
<li> – <a href="#Events-query-response">query-response</a></li>
<li><b><a href="#Interfaces-Other">Other:</a></b></li>
<li> – <a href="#Other-toString">toString</a></li>
<li> – <a href="#Other-toSQL">toSQL</a></li>
</ul>
<a class="toc_title" href="#Migrations">
Migrations
</a>
<ul class="toc_section">
<li><a href="#Migrations-CLI"><b>CLI</b></li>
<li>– <a href="#Migrations-CLI">Migrations</a></li>
<li>– <a href="#Seeds-CLI">Seed files</a></li>
<li>– <a href="#knexfile">knexfile.js</a></li>
<li><a href="#Migrations-API"><b>Migration API</b></li>
<li>– <a href="#Migrations-make">make</a></li>
<li>– <a href="#Migrations-latest">latest</a></li>
<li>– <a href="#Migrations-rollback">rollback</a></li>
<li>– <a href="#Migrations-currentVersion">currentVersion</a></li>
<li><a href="#Seeds-API"><b>Seed API</b></li>
<li>– <a href="#Seeds-make">make</a></li>
<li>– <a href="#Seeds-run">run</a></li>
</ul>
<a class="toc_title" href="#support">
Support
</a>
<a class="toc_title" href="#faq">
F.A.Q.
</a>
<a class="toc_title" href="#changelog">
Change Log
</a>
</div>
<div class="language">
Show example query output as:<br />
<select class="js-query-output">
<option value="mysql">MySQL / MariaDB</option>
<option value="pg">PostgreSQL</option>
<option value="sqlite3">SQLite3</option>
<option value="oracle">Oracle</option>
<option value="mssql">MSSQL</option>
</select>
</div>
<a href="https://github.com/tgriesser/knex"><img style="position: fixed; top: 0; right: 0; border: 0;" src="docs/images/github.png" alt="Fork me on GitHub"></a>
<div class="container">
<p>
<img height="108" id="logo" src="docs/images/knex.png" alt="Knex.js">
</p>
<p>
<b>Knex.js</b> is a "batteries included" SQL query builder for <b>Postgres</b>, <b>MySQL</b>, <b>MariaDB</b>, <b>SQLite3</b>, and <b>Oracle</b> 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"><b>transaction support (with savepoints)</b></a>, connection <a href="#Installation-pooling">pooling</a> and standardized responses between
different query clients and dialects.
</p>
<p>
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>
Knex is available for use under the <a href="http://github.com/tgriesser/knex/blob/master/LICENSE">MIT software license</a>.
</p>
<p>
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>
Thanks to all of the great <a href="https://github.com/tgriesser/knex/graphs/contributors">contributions</a> to the project.
</a>
<p class="warning">
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>Latest Release: 0.10.0 - <span class="small"><a href="#changelog">Change Log</a></span></h2>
<p>
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>
<h2 id="Upgrading">Upgrading</h2>
<h3 id="Upgrading-from0.9">Upgrading 0.9 -> 0.10</h3>
<p>
Knex will no longer interpret `undefined` values as `null` for `insert` or
`update`. See changelog for links to relevant issues.
</p>
<h3 id="Upgrading-from0.8">Upgrading 0.8 -> 0.9</h3>
<p>
There should be no major changes breaking the external API. If you encounter issues,
please <a href="https://github.com/tgriesser/knex/issues">open a ticket</a>.
</p>
<h3 id="Upgrading-from0.7">Upgrading 0.7 -> 0.8</h3>
<p>
There should be no major changes breaking the external API. If you encounter issues,
please <a href="https://github.com/tgriesser/knex/issues">open a ticket</a>.
</p>
<h3 id="Upgrading-from0.6">Upgrading 0.6 -> 0.7</h3>
<p>
Should be a painless upgrade, the only breaking change should be "collate nocase"
no longer used as the default <tt>order by</tt> in sqlite3
for <a href="https://github.com/tgriesser/knex/pull/396">performance reasons</a>.
If there are other issues, please <a href="https://github.com/tgriesser/knex/issues">open a ticket</a>.
</p>
<h2 id="Installation">
Installation
</h2>
<p>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(pg)">pg</a>, <a href="javascript:alert(mysql)">mysql</a>, and <a href="javascript:alert(sqlite3)">sqlite3</a> objects.</p>
<h3 id="Installation-node">Node.js</h3>
<p>
The primary target environment for Knex is Node.js, you will need to install the <tt>knex</tt> library,
and then install the appropriate database library: <tt><a href="https://github.com/brianc/node-postgres">pg</a></tt> for PostgreSQL, <tt><a href="https://github.com/felixge/node-mysql">mysql</a></tt> for MySQL or MariaDB, or <a href="https://github.com/mapbox/node-sqlite3"><tt>sqlite3</tt></a> for SQLite3.
</p>
<pre>
$ 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
</pre>
<h3 id="Installation-browser">Browser</h3>
<p>
Knex can be built using browserify or webpack, and a pre-built version can be found in the <a href="https://github.com/tgriesser/knex/tree/master/build">build directory</a>. View source on this page to see the browser build in-action.
</p>
<h3 id="Installation-client">Initializing the Library</h3>
<p>
The <tt>knex</tt> module is itself a function which takes a configuration object for
Knex, accepting a few parameters. The <tt>client</tt> parameter is required
and determines which client adapter will be used with the library.
</p>
<pre>
<code class="js">var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
</code>
</pre>
<p>
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>
<code class="js">var pg = require('knex')({
client: 'pg',
connection: process.env.PG_CONNECTION_STRING,
searchPath: 'knex,public'
});
</code>
</pre>
<p class="warning">
Note: When you use the SQLite3 adapter, there is a filename required, not a network connection. For example:
</p>
<pre>
<code class="js">var knex = require('knex')({
client: 'sqlite3',
connection: {
filename: "./mydb.sqlite"
}
});
</code>
</pre>
<p>
You can also connect via an unix domain socket, which will ignore host and port.
</p>
<pre>
<code class="js">var knex = require('knex')({
client: 'mysql',
connection: {
socketPath : '/path/to/socket.sock',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
</code>
</pre>
<p class="warning">
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>
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>
<code class="js">var knex = require('knex')({});
var pg = require('knex')({client: 'pg'});
knex('table').insert({a: 'b'}).returning('*').toString();
// "insert into "table" ("a") values ('b')"
pg('table').insert({a: 'b'}).returning('*').toString();
// "insert into "table" ("a") values ('b') returning *"
</code>
</pre>
<h3 id="Installation-debug">Debugging</h3>
<p>
Passing a <tt>debug: true</tt> flag on your initialization object will turn on <a href="#Builder-debug">debugging</a> for all queries.
</p>
<h3 id="Installation-pooling">Pooling</h3>
<p>
The client created by the configuration initializes a connection pool, using the <a href="https://github.com/myndzi/pool2">Pool2</a> library. This connection pool has a default setting of a <tt>min: 2, max: 10</tt> 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 <tt>pool</tt> option as one of the keys in the initialize block.
</p>
<p>
The default connection pool also applies a <tt>ping</tt> function to verify that a connection is still alive by running a <tt>SELECT 1</tt> query.
If you don't want this check you may supply your own ping function to overwrite knex's default behavior.
</p>
<p>
Checkout the <a href="https://github.com/myndzi/pool2">Pool2</a> library for more information.
</p>
<pre>
<code class="js">var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
pool: {
min: 0,
max: 7
}
});
</code>
</pre>
<p>
If you ever need to explicitly teardown the connection pool, you may use <tt>knex.destroy([callback])</tt>.
You may use <tt>knex.destroy</tt> by passing a callback, or by chaining as a promise, just not both.
</p>
<h3 id="Installation-acquireConnectionTimeout">acquireConnectionTimeout</h3>
<p>
<tt>acquireConnectionTimeout</tt> 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>
<code class="js">var knex = require('knex')({
client: 'pg',
connection: {...},
pool: {...},
acquireConnectionTimeout: 10000
});
</code>
</pre>
<h3 id="Installation-migrations">Migrations</h3>
<p>
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>
<code class="js">var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
migrations: {
tableName: 'migrations'
}
});
</code>
</pre>
<h2 id="Builder">Knex Query Builder</h2>
<p>
The heart of the library, the knex query builder is the interface used for building
and executing standard SQL queries, such as <tt>select</tt>, <tt>insert</tt>, <tt>update</tt>, <tt>delete</tt>.
</p>
<p id="Builder-main">
<b class="header">knex</b><code>knex(tableName) / knex.[methodName]</code>
<br />
The query builder starts off either by specifying a <b>tableName</b> 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
<a href="#Interfaces">interface methods</a>, to either convert toString, or execute the
query with a <a href="#Interfaces-Promises">promise</a>, <a href="#Interfaces-Callbacks">callback</a>, or <a href="#Interfaces-Streams">stream</a>.
</p>
<p id="Builder-timeout">
<b class="header">timeout</b><code>.timeout(ms)</code>
<br />
Sets a timeout for the query and will throw a <tt>TimeoutError</tt> 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.
</p>
<p id="Builder-select">
<b class="header">select</b><code>.select([*columns])</code>
<br />
Creates a <tt>select</tt> query, taking an optional array of <b>columns</b> for the query, eventually
defaulting to <tt>*</tt> 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">
knex.select('title', 'author', 'year').from('books')
</pre>
<pre class="display">
knex.select().table('books')
</pre>
<p id="Builder-as">
<b class="header">as</b><code>.as(name)</code>
<br />
Allows for aliasing a subquery, taking the string you wish to <b>name</b> the current query.
If the query is not a sub-query, it will be ignored.
</p>
<pre class="display">
knex.avg('sum_column1').from(function() {
this.sum('column1 as sum_column1').from('t1').groupBy('column1').as('t1')
}).as('ignored_alias')
</pre>
<p id="Builder-column">
<b class="header">column</b><code>.column(columns)</code>
<br />
Specifically set the <b>columns</b> to be selected on a select query, taking an array or a list of of column names.
</p>
<pre class="display">
knex.column('title', 'author', 'year').select().from('books')
</pre>
<pre class="display">
knex.column(['title', 'author', 'year']).select().from('books')
</pre>
<p id="Builder-from">
<b class="header">from</b><code>.from([tableName])</code> <span class="alias">Alias: <b>into</b>, <b>table</b></span>
<br />
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 <a href="#Builder-where">where</a> or <a href="#Builder-union">union</a> methods.
</p>
<pre class="display">
knex.select('*').from('users')
</pre>
<p id="Builder-withSchema">
<b class="header">withSchema</b><code>.withSchema([schemaName])</code>
<br />
Specifies the schema to be used as prefix of table name.
</p>
<pre class="display">
knex.withSchema('public').select('*').from('users')
</pre>
<h3 id="Builder-wheres">Where Clauses</h3>
<p>
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 id="Builder-where">
<b class="header">where</b><code>.where(~mixed~)</code>
</p>
<p class="title">Object Syntax:</p>
<pre class="display">
knex('users').where({
first_name: 'Test',
last_name: 'User'
}).select('id')
</pre>
<p class="title">Key, Value:</p>
<pre class="display">
knex('users').where('id', 1)
</pre>
<p class="title">Grouped Chain:</p>
<pre class="display">
knex('users').where(function() {
this.where('id', 1).orWhere('id', '>', 10)
}).orWhere({name: 'Tester'})
</pre>
<p class="title">Operator:</p>
<pre class="display">
knex('users').where('votes', '>', 100)
</pre>
<pre class="display">
var subquery = knex('users').where('votes', '>', 100).andWhere('status', 'active').orWhere('name', 'John').select('id');
knex('accounts').where('id', 'in', subquery)
</pre>
<p class="title">.orWhere with an object automatically wraps the statement and creates an <tt>or (and - and - and)</tt> clause</p>
<pre class="display">
knex('users').where('id', 1).orWhere({votes: 100, user: 'knex'})
</pre>
<p id="Builder-whereNot">
<b class="header">whereNot</b><code>.whereNot(~mixed~)</code>
</p>
<p class="title">Object Syntax:</p>
<pre class="display">
knex('users').whereNot({
first_name: 'Test',
last_name: 'User'
}).select('id')
</pre>
<p class="title">Key, Value:</p>
<pre class="display">
knex('users').whereNot('id', 1)
</pre>
<p class="title">Grouped Chain:</p>
<pre class="display">
knex('users').whereNot(function() {
this.where('id', 1).orWhereNot('id', '>', 10)
}).orWhereNot({name: 'Tester'})
</pre>
<p class="title">Operator:</p>
<pre class="display">
knex('users').whereNot('votes', '>', 100)
</pre>
<p>
CAVEAT:
WhereNot is not suitable for "in" and "between" type subqueries. You should use "not in" and "not between" instead.
</p>
<pre class="js">
var subquery = knex('users').whereNot('votes', '>', 100).andWhere('status', 'active').orWhere('name', 'John').select('id');
<span style='text-decoration:line-through'>knex('accounts').whereNot('id', 'in', subquery)</span>
knex('accounts').where('id', 'not in', subquery)
</pre>
<p id="Builder-whereIn">
<b class="header">whereIn</b><code>.whereIn(column, array|callback|builder) / .orWhereIn</code><br />
Shorthand for <tt>.where('id', 'in', obj)</tt>, the .whereIn and .orWhereIn methods add a "where in" clause
to the query. Click the "play" button below to see the queries.
</p>
<pre class="display">
knex.select('name').from('users')
.whereIn('id', [1, 2, 3])
.orWhereIn('id', [4, 5, 6])
</pre>
<pre class="display">
knex.select('name').from('users')
.whereIn('account_id', function() {
this.select('id').from('accounts');
})
</pre>
<pre class="display">
var subquery = knex.select('id').from('accounts');
knex.select('name').from('users')
.whereIn('account_id', subquery)
</pre>
<pre class="display">
knex('users')
.where('name', '=', 'John')
.orWhere(function() {
this.where('votes', '>', 100).andWhere('title', '<>', 'Admin');
})
</pre>
<p id="Builder-whereNotIn">
<b class="header">whereNotIn</b><code>.whereNotIn(column, array|callback|builder) / .orWhereNotIn</code>
</p>
<pre class="display">
knex('users').whereNotIn('id', [1, 2, 3])
</pre>
<pre class="display">
knex('users').where('name', 'like', '%Test%').orWhereNotIn('id', [1, 2, 3])
</pre>
<p id="Builder-whereNull">
<b class="header">whereNull</b><code>.whereNull(column) / .orWhereNull</code>
</p>
<pre class="display">
knex('users').whereNull('updated_at')
</pre>
<p id="Builder-whereNotNull">
<b class="header">whereNotNull</b><code>.whereNotNull(column) / .orWhereNotNull</code>
</p>
<pre class="display">
knex('users').whereNotNull('created_at')
</pre>
<p id="Builder-whereExists">
<b class="header">whereExists</b><code>.whereExists(builder | callback) / .orWhereExists</code>
</p>
<pre class="display">
knex('users').whereExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
</pre>
<pre class="display">
knex('users').whereExists(knex.select('*').from('accounts').whereRaw('users.account_id = accounts.id'))
</pre>
<p id="Builder-whereNotExists">
<b class="header">whereNotExists</b><code>.whereNotExists(builder | callback) / .orWhereNotExists</code>
</p>
<pre class="display">
knex('users').whereNotExists(function() {
this.select('*').from('accounts').whereRaw('users.account_id = accounts.id');
})
</pre>
<pre class="display">
knex('users').whereNotExists(knex.select('*').from('accounts').whereRaw('users.account_id = accounts.id'))
</pre>
<p id="Builder-whereBetween">
<b class="header">whereBetween</b><code>.whereBetween(column, range) / .orWhereBetween</code>
</p>
<pre class="display">
knex('users').whereBetween('votes', [1, 100])
</pre>
<p id="Builder-whereNotBetween">
<b class="header">whereNotBetween</b><code>.whereNotBetween(column, range) / .orWhereNotBetween</code>
</p>
<pre class="display">
knex('users').whereNotBetween('votes', [1, 100])
</pre>
<p id="Builder-whereRaw">
<b class="header">whereRaw</b><code>.whereRaw(query, [bindings])</code>
<br />
Convenience helper for <tt>.where(knex.raw(query))</tt>.
</p>
<pre class="display">
knex('users').whereRaw('id = ?', [1])
</pre>
<h3>Join Methods</h3>
<p>
Several methods are provided which assist in building joins.
</p>
<p id="Builder-join">
<b class="header">join</b><code>.join(table, first, [operator], second)</code>
<br />
The <tt>join</tt> builder can be used to specify joins between tables,
with the first argument being the joining <b>table</b>, the next three arguments
being the <b>first</b> join column, the join <b>operator</b> and the <b>second</b>
join column, respectively.
</p>
<pre class="display">
knex('users')
.join('contacts', 'users.id', '=', 'contacts.user_id')
.select('users.id', 'contacts.phone')
</pre>
<pre class="display">
knex('users')
.join('contacts', 'users.id', 'contacts.user_id')
.select('users.id', 'contacts.phone')
</pre>
<p>
For grouped joins, specify a function as the second argument for the
join query, and use <tt>on</tt> with <tt>orOn</tt> or <tt>andOn</tt> to create joins that are
grouped with parentheses.
</p>
<pre class="display">
knex.select('*').from('users').join('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p>
It is also possible to use an object to represent the join syntax.
</p>
<pre class="display">
knex.select('*').from('users').join('accounts', {'accounts.id': 'users.account_id'})
</pre>
<p>
If you need to use a literal value (string, number, or boolean) in a join instead of a column, use <tt>knex.raw</tt>.</p>
<pre class="display">
knex.select('*').from('users').join('accounts', 'accounts.type', knex.raw('?', ['admin']))
</pre>
<p id="Builder-innerJoin">
<b class="header">innerJoin</b><code>.innerJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">
knex.from('users').innerJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.table('users').innerJoin('accounts', 'users.id', '=', 'accounts.user_id')
</pre>
<pre class="display">
knex('users').innerJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-leftJoin">
<b class="header">leftJoin</b><code>.leftJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">
knex.select('*').from('users').leftJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.select('*').from('users').leftJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-leftOuterJoin">
<b class="header">leftOuterJoin</b><code>.leftOuterJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">
knex.select('*').from('users').leftOuterJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.select('*').from('users').leftOuterJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-rightJoin">
<b class="header">rightJoin</b><code>.rightJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">