-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1687 lines (1447 loc) · 59.4 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" />
<title>Knex.js - A Query Builder for Javascript</title>
<style>
body {
font-size: 14px;
line-height: 22px;
background: #FEFFFC;
color: #000;
font-family: Helvetica Neue, Helvetica, Arial;
}
.interface {
font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, sans-serif !important;
}
div#sidebar {
background: #fff;
position: fixed;
top: 0; left: 0; bottom: 0;
width: 200px;
overflow-y: auto;
overflow-x: hidden;
-webkit-overflow-scrolling: touch;
padding: 15px 0 30px 30px;
border-right: 1px solid #bbb;
box-shadow: 0 0 20px #ccc; -webkit-box-shadow: 0 0 20px #ccc; -moz-box-shadow: 0 0 20px #ccc;
}
a.toc_title, a.toc_title:visited {
display: block;
color: black;
font-weight: bold;
margin-top: 15px;
}
a.toc_title:hover {
text-decoration: underline;
}
#sidebar .version {
font-size: 10px;
font-weight: normal;
}
ul.toc_section {
font-size: 11px;
line-height: 14px;
margin: 5px 0 0 0;
padding-left: 0px;
list-style-type: none;
font-family: Lucida Grande;
}
.toc_section li {
cursor: pointer;
margin: 0 0 3px 0;
}
.toc_section li a {
text-decoration: none;
color: black;
}
.toc_section li a:hover {
text-decoration: underline;
}
div.container {
width: 550px;
margin: 40px 0 50px 260px;
}
div.container ul.small {
font-size: 12px;
}
img#logo {
width: 450px;
}
.warning {
font-size: 12px;
line-height: 18px;
font-style: italic;
}
p {
margin: 20px 0;
width: 550px;
}
a, a:visited {
color: #444;
}
a:active, a:hover {
color: #000;
}
h1, h2, h3, h4, h5, h6 {
padding-top: 20px;
}
h2 {
font-size: 20px;
}
b.header {
font-size: 16px;
line-height: 30px;
}
span.small {
font-size: 12px;
font-style: italic;
}
span.alias {
font-size: 14px;
font-style: italic;
margin-left: 20px;
}
table, tr, td {
margin: 0; padding: 0;
}
td {
padding: 2px 12px 2px 0;
}
table .rule {
height: 1px;
background: #ccc;
margin: 5px 0;
}
ul {
list-style-type: circle;
padding: 0 0 0 20px;
}
li {
width: 500px;
margin-bottom: 10px;
}
code, pre, tt {
font-family: Monaco, Consolas, "Lucida Console", monospace;
font-size: 12px;
line-height: 18px;
font-style: normal;
}
tt {
padding: 0px 3px;
background: #fff;
border: 1px solid #ddd;
zoom: 1;
}
code {
margin-left: 20px;
}
pre {
font-size: 12px;
padding: 2px 0 2px 15px;
border-left: 5px solid #bbb;
margin: 0px 0 30px;
}
@media only screen and (-webkit-min-device-pixel-ratio: 1.5) and (max-width: 640px),
only screen and (-o-min-device-pixel-ratio: 3/2) and (max-width: 640px),
only screen and (min-device-pixel-ratio: 1.5) and (max-width: 640px) {
img {
max-width: 100%;
}
div#sidebar {
-webkit-overflow-scrolling: initial;
position: relative;
width: 90%;
height: 120px;
left: 0;
top: -7px;
padding: 10px 0 10px 30px;
border: 0;
}
img#logo {
width: auto;
height: auto;
}
div.container {
margin: 0;
width: 100%;
}
p, div.container ul {
max-width: 98%;
overflow-x: scroll;
}
pre {
overflow: scroll;
}
}
</style>
</head>
<body>
<div id="sidebar" class="interface">
<a class="toc_title" href="#">
Knex.js <span class="version">(0.4.13)</span>
</a>
<ul class="toc_section">
<li>» <a href="https://github.com/tgriesser/knex">GitHub Repository</a></li>
<li>» <a href="docs/knex.html">Annotated Source</a></li>
<li>» <a href="#changelog">Change Log</a></li>
</ul>
<a class="toc_title" href="#Installation">
Installation
</a>
<a class="toc_title" href="#Upgrading">
Upgrading
</a>
<a class="toc_title" href="#Initialize">
Initialize
</a>
<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-column">column</a></li>
<li>– <a href="#Builder-from">from</a></li>
<li>– <a href="#Builder-where">where <b>+18 methods</b></a></li>
<li>– <a href="#Builder-distinct">distinct</a></li>
<li>– <a href="#Builder-join">join</a></li>
<li>– <a href="#Builder-groupBy">groupBy</a></li>
<li>– <a href="#Builder-orderBy">orderBy</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-debug">debug</a></li>
<li>– <a href="#Builder-options">options</a></li>
<li><b><a href="#Builder-Interface">Interface:</a></b></li>
<li>– <a href="#Builder-tap">tap</a></li>
<li>– <a href="#Builder-then">then</a></li>
<li>– <a href="#Builder-exec">exec</a></li>
<li>– <a href="#Builder-toString">toString</a></li>
</ul>
<a class="toc_title" href="#Transaction">
Transaction
</a>
<a class="toc_title" href="#Schema">
Schema
</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-charset">charset</a></li>
<li>– <a href="#Schema-collate">collate</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-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>
</ul>
<a class="toc_title" href="#Migrate">
Migrate
</a>
<ul class="toc_section">
<li>– <a href="#Migrate-up">up</a></li>
<li>– <a href="#Migrate-down">down</a></li>
<li>– <a href="#Migrate-generate">generate</a></li>
<li>– <a href="#Migrate-currentVersion">currentVersion</a></li>
</ul>
<a class="toc_title" href="#faq">
F.A.Q.
</a>
<a class="toc_title" href="#changelog">
Change Log
</a>
</div>
<div class="container">
<p>
<img id="logo" src="docs/images/knex.png" alt="Knex.js">
</p>
<p>
Knex.js is a query builder for <b>Postgres</b>, <b>MySQL</b> and <b>SQLite3</b>, designed to be flexible,
portable, and fun to use. It features both traditional node style <a href="#Builder-exec">callbacks</a>
as well as a <a href="#Builder-then">promise</a> interface for cleaner async flow control, full featured
query and schema builders, <b>transaction support</b>, connection pooling 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 the <a href="docs/knex.html">annotated source code</a> is available,
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 code and syntax is derived.
</p>
<h2>Latest Release: 0.4.13 - <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="Installation">
Installation
</h2>
<p>
All dependencies are specified in <tt>package.json</tt> file but include <tt>underscore.js</tt>,
<tt>when.js</tt>, and the <tt>generic-pool-redux</tt> library. You then need to install
either <tt>mysql</tt>, <tt>pg</tt>, or <tt>sqlite3</tt> from <tt>npm</tt> if you wish
to use one of these databases with node.js, or create your own client
adapter and specify it in <a href="#Initialize">knex.initialize</a>.
</p>
<pre>
$ npm install knex
# Then add one of the following:
$ npm install mysql
$ npm install pg
$ npm install sqlite3
</pre>
<h2 id="Upgrading">Upgrading from 0.2.x</h2>
<p>
If you are upgrading Knex from 0.2.x to 0.4.x, you'll notice that some things have broken.
Luckily, with a few quick fixes to your code, you should be up and runnning in no-time.
<ul>
<li>
Global state is no longer stored in the library, an instance is returned from <tt>Knex.initialize</tt>,
so you will need to call this once and then reference this <tt>knex</tt> client elsewhere in your application.
</li>
<li>
Lowercasing of <tt>knex.raw</tt>, <tt>knex.transaction</tt>, and <tt>knex.schema</tt>.
</li>
<li>
Created columns are now nullable by default, unless <tt>notNullable</tt> is chained as an option.
</li>
<li>
Keys created with <tt>increments</tt> are now assumed to be unsigned (MySQL) by default.
</li>
<li>
The <tt>destroyAllNow</tt> is no longer called by the library on <tt>process.exit</tt> event. If you need to call
it explicitly yourself, you may use <tt>knex.client.destroyPool</tt>
</li>
</ul>
</p>
<h2 id="Initialize">Initialize</h2>
<p>
<tt>Knex.initialize</tt> is the initializing function that must be called prior to using
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>
var knex = Knex.initialize({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test',
charset : 'utf8'
}
});
</pre>
<p class="warning">
An initialize should 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'll need to store this instance created by the <tt>initialize</tt> somewhere in the application
you can reference it. A common pattern to follow is to set the client as a property on the <tt>Knex</tt>
library when your application starts, so you can easily reference it later:
</p>
<pre>
// When the app starts
var Knex = require('knex');
Knex.knex = Knex.initialize({
client: 'pg',
connection: {
// your connection config
}
});
// elsewhere, to use the client:
var knex = require('knex').knex;
knex('table').where('id', 1).select().then(...
</pre>
<h2 id="Builder">knex.builder</h2>
<p>
The <tt>knex.builder</tt> 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>. The query is run
by specifying a <a href="#Builder-table">tableName</a>, adding additional query parameters, and
executing the query with one of the public interface methods.
</p>
<p id="Builder-main">
<b class="header">knex</b><code>knex(tableName)...</code>
<br />
The <b>Knex</b> query builder starts off by specifying a <b>tableName</b> you wish to query against,
which returns a chainable interface, similar to a jQuery chain. You can then call any additional
query builder methods needed to construct the query, eventually calling either
<a href="#Builder-then">then</a> or <a href="#Builder-exec">exec</a>, to execute the query with a
promise or a callback, respectively.
</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>
// select "title", "author", "year" from "books"
Knex('books').select('title', 'author', 'year');
// select * from "books"
Knex('books').select();
</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>
// select "title", "author", "year" from "books"
Knex('books').column('title', 'author', 'year').select();
// or:
Knex('books').column(['title', 'author', 'year']).select();
</pre>
<p id="Builder-from">
<b class="header">from</b><code>.from([tableName])</code> <span class="alias">Alias: <b>into</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>
<p id="Builder-where">
<b class="header">where</b><code>.where(~dynamic~)</code>
<br />
There are several helpers for creating dynamic <tt>where</tt> clauses on queries. Take a look at a few
examples to see how these may be mixed and matched to create fluent constraints on the query.
</p>
<pre>
// Basic Uses:
// objects
Knex('users').where({
first_name: 'Test',
last_name: 'User'
}).select('id').then(...
// key, value
Knex('users').where('id', 1).select('first_name', 'last_name').then(...
// operators: '=', '<', '>', '<=', '>=', 'like', 'not like', 'between', 'ilike'
Knex('users').where('votes', '>', 100).exec(function(err, resp) { ... });
// chained with "andWhere" / "orWhere"
Knex('users').where('votes', '>', 100)
.andWhere('status', 'active')
.orWhere('name', 'John')
.then(function(resp) { ... })
// Even more where types (see list below):
Knex('users').whereBetween('votes', [1, 100]).exec(...
Knex('users').whereIn('id', [1, 2, 3]).then(...
Knex('users').whereNotIn('id', [1, 2, 3]).then(...
Knex('users').whereNull('updated_at').exec(...
</pre>
<p>
<b>Grouped Where Clauses:</b>
</p>
<pre>
// select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
Knex('users')
.where('name', '=', 'John')
.orWhere(function() {
this.where('votes', '>', 100).andWhere('title', '<>', 'Admin');
})
.then(function() {...
</pre>
<p>
<b>Exists Statements:</b>
</p>
<pre>
Knex('users')
.whereExists(function() {
this.select(Knex.raw(1))
.from('orders')
.whereRaw('orders.user_id = users.id');
})
.then(...
</pre>
<p>
Most of the where clauses may also accept a function as the second argument, to generate a sub-select
on the query:
</p>
<pre>
// select author_id, content from comments where author_id in (select id from accounts where type = 'admin')
Knex('comments').whereIn('author_id', function() {
this.select('id').from('accounts').where('type', 'admin');
}).select('author_id', 'content').then(...
</pre>
<p>
These different where clauses may be joined together in any number of ways to make valid SQL statements.
</p>
<ul class="small">
<li><a href="docs/knex.html#section-56">where</a></li>
<li><a href="docs/knex.html#section-57">andWhere</a></li>
<li><a href="docs/knex.html#section-58">orWhere</a></li>
<li><a href="docs/knex.html#section-59">whereRaw</a></li>
<li><a href="docs/knex.html#section-60">orWhereRaw</a></li>
<li><a href="docs/knex.html#section-61">whereExists</a></li>
<li><a href="docs/knex.html#section-62">orWhereExists</a></li>
<li><a href="docs/knex.html#section-63">whereNotExists</a></li>
<li><a href="docs/knex.html#section-64">orWhereNotExists</a></li>
<li><a href="docs/knex.html#section-65">whereIn</a></li>
<li><a href="docs/knex.html#section-66">orWhereIn</a></li>
<li><a href="docs/knex.html#section-67">whereNotIn</a></li>
<li><a href="docs/knex.html#section-68">orWhereNotIn</a></li>
<li><a href="docs/knex.html#section-69">whereNull</a></li>
<li><a href="docs/knex.html#section-70">orWhereNull</a></li>
<li><a href="docs/knex.html#section-71">whereNotNull</a></li>
<li><a href="docs/knex.html#section-72">orWhereNotNull</a></li>
<li><a href="docs/knex.html#section-73">whereBetween</a></li>
<li><a href="docs/knex.html#section-74">orWhereBetween</a></li>
</ul>
</p>
<p id="Builder-distinct">
<b class="header">distinct</b><code>.distinct()</code>
<br />
Sets a <tt>distinct</tt> clause on the query.
</p>
<pre>
// select distinct 'first_name' from customers
knex('customers')
.distinct('first_name', 'last_name')
.select()
</pre>
<p id="Builder-join">
<b class="header">join</b><code>.join(table, first, operator, second, [type])</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. The last argument is optional and spefies the type of join.
</p>
<pre>
knex('users')
.join('contacts', 'users.id', '=', 'contacts.user_id')
.join('orders', 'users.id', '=', 'orders.user_id', 'outer')
.select('users.id', 'contacts.phone', 'orders.price')
.then(function() { ... });
</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>
knex('users')
.join('contacts', function() {
this.on('users.id', '=', 'contacts.user_id').orOn(...);
})
.exec(function(err, resp) { ... });
</pre>
</p>
<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>
<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>
<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.
<pre>
knex('users')
.groupBy('count')
.orderBy('name', 'desc')
.having('count', '>', 100)
.select()
</pre>
</p>
<p id="Builder-offset">
<b class="header">offset</b><code>.offset(value)</code>
<br />
Adds an <tt>offset</tt> clause to the query.
</p>
<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>
knex('users')
.limit(10)
.offset(30)
.select()
</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>
<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>
knex('users').whereNull('last_name').union(function() {
this.select('*').from('users').whereNull('first_name');
}).select().then(function() { ... });
</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>
// Returns [1] in "mysql", "sqlite"; [] in "postgresql" unless the 'returning' parameter is set.
knex('books').insert({title: 'Slaughterhouse Five'})
// Returns [2] in "mysql", "sqlite"; [2, 3] in "postgresql"
knex('books')
.insert([{title: 'Great Gatsby'}, {title: 'Fahrenheit 451'}], 'id')
</pre>
<p id="Builder-returning">
<b class="header">returning</b><code>.returning(column)</code>
<br />
Only utilitzed by PostgreSQL databases, the returning method specifies which column should be returned
by the <a href="#Builder-insert">insert</a> method.
</p>
<pre>
// Returns [1]
knex('books')
.returning('id')
.insert({title: 'Slaughterhouse Five'})
// Returns [2] in "mysql", "sqlite"; [2, 3] in "postgresql"
knex('books')
.returning('id')
.insert([{title: 'Great Gatsby'}, {title: 'Fahrenheit 451'}])
</pre>
<p id="Builder-update">
<b class="header">update</b><code>.update(data) / .update(key, value)</code>
<br />
Creates an <tt>update</tt> query, taking either a single property / value, or a hash of properties
to be updated based on the other query constraints. Resolves the promise / fulfills the
callback with the number of affected rows for the query.
</p>
<pre>
knex('books')
.where('published_date', '<', 2000)
.update({
status: 'archived'
})
.then(...
</pre>
<p id="Builder-del">
<b class="header">del / delete</b><code>.del()</code>
<br />
Aliased to <tt>del</tt> as <tt>delete</tt> 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>
knex('accounts')
.where('activated', false)
.del()
</pre>
<p id="Builder-transacting">
<b class="header">transacting</b><code>.transacting(transactionObj)</code>
<br />
Used by <a href="#Transaction">knex.transaction</a>, 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>
knex.transaction(function(t) {
knex('books')
.transacting(t)
.insert({name: 'Old Books'})
.then(function(row) {
return when.all(_.map([
{title: 'Canterbury Tales'},
{title: 'Moby Dick'},
{title: 'Hamlet'}
], function(info) {
info.row_id = row.id;
// Some validation could take place here.
return knex('book').transacting(t).insert(info);
}));
})
.then(function() {
t.commit('You saved 3 books');
}, t.rollback);
}).then(function(resp) {
console.log(resp);
}, function(err) {
console.log(err.message);
});
</pre>
<p id="Builder-forUpdate">
<b class="header">forUpdate</b><code>.transacting(t).forUpdate()</code>
<br />
Dynamically added after a <a href="#Builder-transacting">transaction</a> is specified,
the <tt>forUpdate</tt> adds a <tt>FOR UPDATE</tt> in PostgreSQL and MySQL during a select statement.
</p>
<p id="Builder-forShare">
<b class="header">forShare</b><code>.transacting(t).forShare()</code>
<br />
Dynamically added after a <a href="#Builder-transacting">transaction</a> is specified,
the <tt>forShare</tt> adds a <tt>FOR SHARE</tt> in PostgreSQL and a <tt>LOCK IN SHARE MODE</tt>
for MySQL during a select statement.
</p>
<p id="Builder-count">
<b class="header">count</b><code>.count(column)</code>
<br />
Performs a count on the specified <b>column</b>.
</p>
<p id="Builder-min">
<b class="header">min</b><code>.min(column)</code>
<br />
Gets the minimum value for the specified <b>column</b>.
</p>
<p id="Builder-max">
<b class="header">max</b><code>.max(column)</code>
<br />
Gets the maximum value for the specified <b>column</b>.
</p>
<p id="Builder-sum">
<b class="header">sum</b><code>.sum(column)</code>
<br />
Retrieve the sum of the values of a given <b>column</b>.
</p>
<p id="Builder-increment">
<b class="header">increment</b><code>.increment(column, amount)</code>
<br />
Increments a <b>column</b> value by the specified <b>amount</b>.
</p>
<p id="Builder-decrement">
<b class="header">decrement</b><code>.decrement(column, amount)</code>
<br />
Decrements a <b>column</b> value by the specified <b>amount</b>.
</p>
<p id="Builder-truncate">
<b class="header">truncate</b><code>.truncate()</code>
<br />
Truncates the current table.
</p>
<p id="Builder-debug">
<b class="header">debug</b><code>.debug()</code>
<br />
Turns on debugging for the current query chain.
</p>
<p id="Builder-options">
<b class="header">options</b><code>.options()</code>
<br />
Allows for mixing in additional options as defined by database client specific libraries:
</p>
<pre>
knex('accounts as a1')
.join('accounts as a2', function() {
this.on('a1.email', '<>', 'a2.email');
}, 'left')
.select(['a1.email', 'a2.email'])
.where(Knex.Raw('a1.id = 1'))
.option({ nestTables: true, rowMode: 'array' })
.limit(2);
</pre>
<h3 id="Builder-Interface">Builder Interface Methods:</h3>
<p id="Builder-then">
<b class="header">then</b><code>.then(onFulfilled, onRejected)</code>
<br />
Coerces the current query builder chain into a promise state, accepting the resolve
and reject handlers as specified by the <a href="http://promises-aplus.github.com/promises-spec">Promises/A+ spec</a>.
As stated in the spec, more than one call to the <tt>then</tt> 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>
<p id="Builder-exec">
<b class="header">exec</b><code>.exec(callback)</code>
<br />
If you'd prefer a callback interface over promises, the <b>exec</b> function
accepts a standard node style callback for executing the query chain. Note that as
with the <a href="#Builder-then">then</a> method, subsequent calls to the same
query chain will return the same result.
</p>
<p id="Builder-tap">
<b class="header">tap</b><code>.tap(onFulfilled, onRejected)</code>
<br />
Similar to the <a href="https://github.com/cujojs/when/blob/master/docs/api.md#tap">tap</a> functionality in when.js,
this will execute 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>
// Using only .then()
query.then(function(x) {
doSideEffectsHere(x);
return x;
});
// Using .tap()
promise.tap(doSideEffectsHere);
</pre>
<p id="Builder-toString">
<b class="header">toString</b><code>.toString()</code>
<br />
Returns an array of query strings filled out with the
correct values based on bindings, etc. Useful for debugging.
</p>
<h2 id="Transaction">Knex.Transaction</h2>
<p>
Transactions are handled by passing a handler function into <tt>knex.transaction</tt>.
The handler function accepts a single argument, the promise for committing or rolling back
the transaction. This argument is then passed into any queries which are involved in the current
transcaction, working by explicitly passing the .
</p>
<pre>