-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2815 lines (2338 loc) · 101 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.6.22)</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.5">0.5.x -> 0.6</a></li>
<li>– <a href="#Upgrading-from0.4">0.4.x -> 0.5</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-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-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><b><a href="#Builder-wheres">Where Methods:</a></b></li>
<li> - <a href="#Builder-where">where</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-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-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-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-createTable">createTable</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-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-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-specificType">specificType</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-after">after</a></li>
<li>– <a href="#Chainable-comment">comment</a></li>
</ul>
<a class="toc_title" href="#Raw">
Raw
</a>
<ul class="toc_section">
<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="#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-exec">exec</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><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="#knexfile">knexfile.js</a></li>
<li><a href="#Migrations-API"><b>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>
</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>
</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 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> and <b>SQLite3</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="#Transaction-overview"><b>transaction support</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.6.22 - <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.5">Upgrading 0.5 -> 0.6</h3>
<p>
Big and exciting improvements from the 0.5 release, the 0.6 release brings refactored internals, streams, the ability
to properly inject & bind knex.raw statements throughout any queries, the ability to re-use existing snippets.
If you were only using the publicly documented API, no major changes should be necessary. If you notice any issues,
please <a href="https://github.com/tgriesser/knex/issues">open a ticket</a>. For a full list of changes, see the entry in the <a href="#release-0.6.0">change log.</a>
</p>
<h3 id="Upgrading-from0.4">Upgrading 0.4 -> 0.5</h3>
<p>
Minor changes from the 0.4.x series, include the fact that aggregate functions are
no longer aliased as "aggregate", but rather left as-is, unless explicitly aliased.
See <a href="https://github.com/tgriesser/knex/pull/110">this ticket</a> for more info.
</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@0.6 --save
# Then add one of the following (adding a --save) flag:
$ npm install mysql
$ npm install mariasql
$ npm install pg
$ npm install sqlite3
</pre>
<h3 id="Installation-browser">Browser</h3>
<p>
The browser builds and pre-built dependencies can be found in the <a href="https://github.com/tgriesser/knex/tree/master/browser">browser directory</a>. View source on this page to see the browser builds in-action.
</p>
<h3 id="Installation-client">Initializing the Library</h3>
<p>
The <tt>knex</tt> module is its self 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
});
</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 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>
<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/bookshelf/generic-pool-redux">generic-pool-redux</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>
<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-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.
</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: migrationConfig
});
</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-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>
<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 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) / .orWhereNotNull</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
</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> and <tt>orOn</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 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">
knex.select('*').from('users').rightJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.select('*').from('users').rightJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-rightOuterJoin">
<b class="header">rightOuterJoin</b><code>.rightOuterJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">
knex.select('*').from('users').rightOuterJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.select('*').from('users').rightOuterJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-outerJoin">
<b class="header">outerJoin</b><code>.outerJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">
knex.select('*').from('users').outerJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.select('*').from('users').outerJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-fullOuterJoin">
<b class="header">fullOuterJoin</b><code>.fullOuterJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">
knex.select('*').from('users').fullOuterJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.select('*').from('users').fullOuterJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-crossJoin">
<b class="header">crossJoin</b><code>.crossJoin(column, ~mixed~)</code>
<br>
</p>
<pre class="display">
knex.select('*').from('users').crossJoin('accounts', 'users.id', 'accounts.user_id')
</pre>
<pre class="display">
knex.select('*').from('users').crossJoin('accounts', function() {
this.on('accounts.id', '=', 'users.account_id').orOn('accounts.owner_id', '=', 'users.id')
})
</pre>
<p id="Builder-distinct">
<b class="header">distinct</b><code>.distinct()</code>
<br />
Sets a <tt>distinct</tt> clause on the query.
</p>
<pre class="display">
// select distinct 'first_name' from customers
knex('customers')
.distinct('first_name', 'last_name')
.select()
</pre>
<p id="Builder-groupBy">
<b class="header">groupBy</b><code>.groupBy(*names)</code>
<br />
Adds a <tt>group by</tt> clause to the query.
</p>
<pre class="display">
knex('users').groupBy('count')
</pre>
<p id="Builder-groupByRaw">
<b class="header">groupByRaw</b><code>.groupBy(sql)</code>
<br />
Adds a raw <tt>group by</tt> clause to the query.
</p>
<pre class="display">
knex.select('year', knex.raw('SUM(profit)')).from('sales').groupByRaw('year WITH ROLLUP')
</pre>
<p id="Builder-orderBy">
<b class="header">orderBy</b><code>.orderBy(column, [direction])</code>
<br />
Adds an <tt>order by</tt> clause to the query.
</p>
<pre class="display">
knex('users').orderBy('name', 'desc')
</pre>
<p id="Builder-orderByRaw">
<b class="header">orderByRaw</b><code>.orderByRaw(sql)</code>
<br />
Adds an <tt>order by raw</tt> clause to the query.
</p>
<pre class="display">
knex.select('*').from('table').orderByRaw('col NULLS LAST DESC')
</pre>
<p id="Builder-having">
<b class="header">having</b><code>.having(column, operator, value)</code>
<br />
Adds a <tt>having</tt> clause to the query.
</p>
<pre class="display">
knex('users')
.groupBy('count')
.orderBy('name', 'desc')
.having('count', '>', 100)
</pre>
<p id="Builder-havingRaw">
<b class="header">havingRaw</b><code>.havingRaw(column, operator, value)</code>
<br />
Adds a <tt>havingRaw</tt> clause to the query.
</p>
<pre class="display">
knex('users')
.groupBy('count')
.orderBy('name', 'desc')
.havingRaw('count > ?', [100])
</pre>
<p id="Builder-offset">
<b class="header">offset</b><code>.offset(value)</code>
<br />
Adds an <tt>offset</tt> clause to the query.
</p>
<pre class="display">
knex.select('*').from('users').offset(10)
</pre>
<p id="Builder-limit">
<b class="header">limit</b><code>.limit(value)</code>
<br />
Adds a <tt>limit</tt> clause to the query.
</p>
<pre class="display">
knex.select('*').from('users').limit(10).offset(30)
</pre>
<p id="Builder-union">
<b class="header">union</b><code>.union(query)</code>
<br />
Creates a <tt>union</tt> query, taking a callback to build the union statement.
</p>
<pre class="display">
knex.select('*').from('users').whereNull('last_name').union(function() {
this.select('*').from('users').whereNull('first_name');
})
</pre>
<p id="Builder-unionAll">
<b class="header">unionAll</b><code>.unionAll(query)</code>
<br />
Creates a <tt>union all</tt> query, with the same method signature as
the <a href="#Builder-union">union</a> method.
</p>
<pre class="display">
knex.select('*').from('users').whereNull('last_name').unionAll(function() {
this.select('*').from('users').whereNull('first_name');
})
</pre>
<p id="Builder-insert">
<b class="header">insert</b><code>.insert(data, [returning])</code>
<br />
Creates an <tt>insert</tt> 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 <tt>ids</tt>
for postgresql.
</p>
<pre class="display">
// Returns [1] in "mysql", "sqlite"; [] in "postgresql" unless the 'returning' parameter is set.
knex('books').insert({title: 'Slaughterhouse Five'})
</pre>