You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

670 lines
371 KiB
HTML

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="http://knexjs.org" />
<link rel="apple-touch-icon" sizes="180x180" href="assets/favicons/apple-touch-icon.png" />
<link rel="icon" type="image/png" href="assets/favicons/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="assets/favicons/favicon-16x16.png" sizes="16x16" />
<link rel="manifest" href="assets/favicons/manifest.json" />
<link rel="mask-icon" href="assets/favicons/safari-pinned-tab.svg" color="#e16426" />
<meta name="theme-color" content="#ffffff" />
<link rel="stylesheet" href="build/bundle.css" />
<title>Knex.js - A SQL Query Builder for Javascript</title>
</head>
<body>
<div id="documentation"><div data-reactroot="" data-reactid="1" data-react-checksum="-611604291"><div id="sidebar" class="interface" data-reactid="2"><a class="toc_title" href="#changelog" data-reactid="3"><!-- react-text: 4 -->Knex.js <!-- /react-text --><span class="version" data-reactid="5"><!-- react-text: 6 -->(<!-- /react-text --><!-- react-text: 7 -->0.14.2<!-- /react-text --><!-- react-text: 8 -->)<!-- /react-text --></span></a><ul class="toc_section" data-reactid="9"><li data-reactid="10"><!-- react-text: 11 -->» <!-- /react-text --><a href="https://github.com/tgriesser/knex" data-reactid="12">GitHub Repository</a></li><li data-reactid="13"><!-- react-text: 14 -->» <!-- /react-text --><a href="#support" data-reactid="15">Support</a></li><li data-reactid="16"><!-- react-text: 17 -->» <!-- /react-text --><a href="#faq" data-reactid="18">FAQ</a></li><li data-reactid="19"><!-- react-text: 20 -->» <!-- /react-text --><a href="#changelog" data-reactid="21">Change Log</a></li></ul><a class="toc_title" href="#Installation" data-reactid="22">Installation</a><ul class="toc_section" data-reactid="23"><li data-reactid="24"><!-- react-text: 25 --> <!-- /react-text --><a href="#Installation-node" data-reactid="26">Node.js</a></li><li data-reactid="27"><!-- react-text: 28 --> <!-- /react-text --><a href="#Installation-browser" data-reactid="29">Browser</a></li><li data-reactid="30"><b data-reactid="31"><a href="#Installation-client" data-reactid="32">Config Options:</a></b></li><li data-reactid="33"><!-- react-text: 34 -->   <!-- /react-text --><a href="#Installation-client" data-reactid="35">client</a></li><li data-reactid="36"><!-- react-text: 37 -->   <!-- /react-text --><a href="#Installation-debug" data-reactid="38">debug</a></li><li data-reactid="39"><!-- react-text: 40 -->   <!-- /react-text --><a href="#Installation-pooling" data-reactid="41">pooling</a></li><li data-reactid="42"><!-- react-text: 43 -->   <!-- /react-text --><a href="#Installation-pooling-afterCreate" data-reactid="44">afterCreate</a></li><li data-reactid="45"><!-- react-text: 46 -->   <!-- /react-text --><a href="#Installation-acquireConnectionTimeout" data-reactid="47">acquireConnectionTimeout</a></li><li data-reactid="48"><!-- react-text: 49 -->   <!-- /react-text --><a href="#Installation-fetchAsString" data-reactid="50">fetchAsString</a></li><li data-reactid="51"><!-- react-text: 52 -->   <!-- /react-text --><a href="#Installation-migrations" data-reactid="53">migrations</a></li><li data-reactid="54"><!-- react-text: 55 -->   <!-- /react-text --><a href="#Installation-post-process-response" data-reactid="56">postProcessResponse</a></li><li data-reactid="57"><!-- react-text: 58 -->   <!-- /react-text --><a href="#Installation-wrap-identifier" data-reactid="59">wrapIdentifier</a></li></ul><a class="toc_title" href="#Builder" data-reactid="60">Query Builder</a><ul class="toc_section" data-reactid="61"><li data-reactid="62"><!-- react-text: 63 --> <!-- /react-text --><a href="#Builder-identifier-syntax" data-reactid="64"><b data-reactid="65">Identifier Syntax</b></a></li><li data-reactid="66"><!-- react-text: 67 --> <!-- /react-text --><a href="#Builder-knex" data-reactid="68"><b data-reactid="69">constructor</b></a></li><li data-reactid="70"><!-- react-text: 71 --> <!-- /react-text --><a href="#Builder-timeout" data-reactid="72">timeout</a></li><li data-reactid="73"><!-- react-text: 74 --> <!-- /react-text --><a href="#Builder-select" data-reactid="75">select</a></li><li data-reactid="76"><!-- react-text: 77 --> <!-- /react-text --><a href="#Builder-as" data-reactid="78">as</a></li><li data-reactid="79"><!-- react-text: 80 --> <!-- /react-text --><a href="#Builder-column" data-reactid="81">column</a></li><li data-reactid="82"><!-- react-text: 83 --> <!-- /react-text --><a href="#Builder-from" data-reactid="84">from</a></li><li data-reactid="85"><!-- react-text: 86 --> <!-- /react-text --><a href="#Builder-with" data-reactid="87">with</a></li><li data-reactid="88"><!-- react-text: 89 --> <!-- /react-text --><a href="#Builder-withSchema" data-reactid="90">withSchema</a></li><li data-reactid="91"><b data-reactid="92"><a href="#Builder-wheres" data-reactid="93">Where Methods:</a></b></li><li data-reactid="94"><!-- react-text: 95 -->   <!-- /react-text --><a href="#Builder-where" data-reactid="96">where</a></li><li data-reactid="97"><!-- react-text: 98 -->   <!-- /react-text --><a href="#Builder-whereNot" data-reactid="99">whereNot</a></li><li data-reactid="100"><!-- react-text: 101 -->   <!-- /react-text --><a href="#Builder-whereIn" data-reactid="102">whereIn</a></li><li data-reactid="103"><!-- react-text: 104 -->   <!-- /react-text --><a href="#Builder-whereNotIn" data-reactid="105">whereNotIn</a></li><li data-reactid="106"><!-- react-text: 107 -->   <!-- /react-text --><a href="#Builder-whereNull" data-reactid="108">whereNull</a></li><li data-reactid="109"><!-- react-text: 110 -->   <!-- /react-text --><a href="#Builder-whereNotNull" data-reactid="111">whereNotNull</a></li><li data-reactid="112"><!-- react-text: 113 -->   <!-- /react-text --><a href="#Builder-whereExists" data-reactid="114">whereExists</a></li><li data-reactid="115"><!-- react-text: 116 -->   <!-- /react-text --><a href="#Builder-whereNotExists" data-reactid="117">whereNotExists</a></li><li data-reactid="118"><!-- react-text: 119 -->   <!-- /react-text --><a href="#Builder-whereBetween" data-reactid="120">whereBetween</a></li><li data-reactid="121"><!-- react-text: 122 -->   <!-- /react-text --><a href="#Builder-whereNotBetween" data-reactid="123">whereNotBetween</a></li><li data-reactid="124"><!-- react-text: 125 -->   <!-- /react-text --><a href="#Builder-whereRaw" data-reactid="126">whereRaw</a></li><li data-reactid="127"><b data-reactid="128"><a href="#Builder-join" data-reactid="129">Join Methods:</a></b></li><li data-reactid="130"><!-- react-text: 131 -->   <!-- /react-text --><a href="#Builder-innerJoin" data-reactid="132">innerJoin</a></li><li data-reactid="133"><!-- react-text: 134 -->   <!-- /react-text --><a href="#Builder-leftJoin" data-reactid="135">leftJoin</a></li><li data-reactid="136"><!-- react-text: 137 -->   <!-- /react-text --><a href="#Builder-leftOuterJoin" data-reactid="138">leftOuterJoin</a></li><li data-reactid="139"><!-- react-text: 140 -->   <!-- /react-text --><a href="#Builder-rightJoin" data-reactid="141">rightJoin</a></li><li data-reactid="142"><!-- react-text: 143 -->   <!-- /react-text --><a href="#Builder-rightOuterJoin" data-reactid="144">rightOuterJoin</a></li><li data-reactid="145"><!-- react-text: 146 -->   <!-- /react-text --><a href="#Builder-outerJoin" data-reactid="147">outerJoin</a></li><li data-reactid="148"><!-- react-text: 149 -->   <!-- /react-text --><a href="#Builder-fullOuterJoin" data-reactid="150">fullOuterJoin</a></li><li data-reactid="151"><!-- react-text: 152 -->   <!-- /react-text --><a href="#Builder-crossJoin" data-reactid="153">crossJoin</a></li><li data-reactid="154"><!-- react-text: 155 -->   <!-- /react-text --><a href="#Builder-joinRaw" data-reactid="156">joinRaw</a></li><li data-reactid="157"><b data-reactid="158"><a href="#Builder-havings" data-reactid="159">Having Methods:</a></b></li><li data-reactid="160"><!-- react-text: 161 -->   <!-- /react-text --><a href="#Builder-having" data-reactid="162">having</a></li><li data-reactid="163"><!-- react-text: 164 -->   <!-- /react-text --><a href="#Builder-havingIn" data-reactid="165">havingIn</a></li><li data-reactid="166"><!-- react-text: 167 -->   <!-- /react-text --><a href="#Builder-havingNotIn" data-reactid="168">havingNotIn</a></li><li data-reactid="169"><!-- react-text: 170 -->   <!-- /react-text --><a href="#Builder-havingNull" data-reactid="171">havingNull</a></li><li data-reactid="172"><!-- react-text: 173 -->   <!-- /react-text --><a href="#Builder-havingNotNull" data-reactid="174">havingNotNull</a></li><li data-reactid="175"><!-- react-text: 176 -->   <!-- /react-text --><a href="#Builder-havingExists" data-reactid="177">havingExists</a></li><li data-reactid="178"><!-- react-text: 179 -->   <!-- /react-text --><a href="#Builder-havingNotExists" data-reactid="180">havingNotExists</a></li><li data-reactid="181"><!-- react-text: 182 -->   <!-- /react-text --><a href="#Builder-havingBetween" data-reactid="183">havingBetween</a></li><li data-reactid="184"><!-- react-text: 185 -->   <!-- /react-text --><a href="#Builder-havingNotBetween" data-reactid="186">havingNotBetween</a></li><li data-reactid="187"><!-- react-text: 188 -->   <!-- /react-text --><a href="#Builder-havingRaw" data-reactid="189">havingRaw</a></li><li data-reactid="190"><b data-reactid="191"><a href="#Builder-on" data-reactid="192">On Methods:</a></b></li><li data-reactid="193"><!-- react-text: 194 -->   <!-- /react-text --><a href="#Builder-onIn" data-reactid="195">onIn</a></li><li data-reactid="196"><!-- react-text: 197 -->   <!-- /react-text --><a href="#Builder-onNotIn" data-reactid="198">onNotIn</a></li><li data-reactid="199"><!-- react-text: 200 -->   <!-- /react-text --><a href="#Builder-onNull" data-reactid="201">onNull</a></li><li data-reactid="202"><!-- react-text: 203 -->   <!-- /react-text --><a href="#Builder-onNotNull" data-reactid="204">onNotNull</a></li><li data-reactid="205"><!-- react-text: 206 -->   <!-- /react-text --><a href="#Builder-onExists" data-reactid="207">onExists</a></li><li data-reactid="208"><!-- react-text: 209 -->   <!-- /react-text --><a href="#Builder-onNotExists" data-reactid="210">onNotExists</a></li><li data-reactid="211"><!-- react-text: 212 -->   <!-- /react-text --><a href="#Builder-onBetween" data-reactid="213">onBetween</a></li><li data-reactid="214"><!-- react-text: 215 -->   <!-- /react-text --><a href="#Builder-onNotBetween" data-reactid="216">onNotBetween</a></li><li data-reactid="217"><b data-reactid="218"><a href="#Builder-clear" data-reactid="219">Clear Methods:</a></b></li><li data-reactid="220"><!-- react-text: 221 -->   <!-- /react-text --><a href="#Builder-clearSelect" data-reactid="222">clearSelect</a></li><li data-reactid="223"><!-- react-text: 224 -->   <!-- /react-text --><a href="#Builder-clearWhere" data-reactid="225">clearWhere</a></li><li data-reactid="226"><!-- react-text: 227 --> <!-- /react-text --><a href="#Builder-distinct" data-reactid="228">distinct</a></li><li data-reactid="229"><!-- react-text: 230 --> <!-- /react-text --><a href="#Builder-groupBy" data-reactid="231">groupBy</a></li><li data-reactid="232"><!-- react-text: 233 --> <!-- /react-text --><a href="#Builder-groupByRaw" data-reactid="234">groupByRaw</a></li><li data-reactid="235"><!-- react-text: 236 --> <!-- /react-text --><a href="#Builder-orderBy" data-reactid="237">orderBy</a></li><li data-reactid="238"><!-- react-text: 239 --> <!-- /react-text --><a href="#Builder-orderByRaw" data-reactid="240">orderByRaw</a></li><li data-reactid="241"><!-- react-text: 242 --> <!-- /react-text --><a href="#Builder-offset" data-reactid="243">offset</a></li><li data-reactid="244"><!-- react-text: 245 --> <!-- /react-text --><a href="#Builder-limit" data-reactid="246">limit</a></li><li data-reactid="247"><!-- react-text: 248 --> <!-- /react-text --><a href="#Builder-union" data-reactid="249">union</a></li><li data-reactid="250"><!-- react-text: 251 --> <!-- /react-text --><a href="#Builder-unionAll" data-reactid="252">unionAll</a></li><li data-reactid="253"><!-- react-text: 254 --> <!-- /react-text --><a href="#Builder-insert" data-reactid="255">insert</a></li><li data-reactid="256"><!-- react-text: 257 --> <!-- /react-text --><a href="#Builder-returning" data-reactid="258">returning</a></li><li data-reactid="259"><!-- react-text: 260 --> <!-- /react-text --><a href="#Builder-update" data-reactid="261">update</a></li><li data-reactid="262"><!-- react-text: 263 --> <!-- /react-text --><a href="#Builder-del / delete" data-reactid="264">del / delete</a></li><li data-reactid="265"><!-- react-text: 266 --> <!-- /react-text --><a href="#Builder-transacting" data-reactid="267">transacting</a></li><li data-reactid="268"><!-- react-text: 269 -->   <!-- /react-text --><a href="#Builder-forUpdate" data-reactid="270">forUpdate</a></li><li data-reactid="271"><!-- react-text: 272 -->   <!-- /react-text --><a href="#Builder-forShare" data-reactid="273">forShare</a></li><li data-reactid="274"><!-- react-text: 275 --> <!-- /react-text --><a href="#Builder-count" data-reactid="276">count</a></li><li data-reactid="277"><!-- react-text: 278 --> <!-- /react-text --><a href="#Builder-min" data-reactid="279">min</a></li><li data-reactid="280"><!-- react-text: 281 --> <!-- /react-text --><a href="#Builder-max" data-reactid="282">max</a></li><li data-reactid="283"><!-- react-text: 284 --> <!-- /react-text --><a href="#Builder-sum" data-reactid="285">sum</a></li><li data-reactid="286"><!-- react-text: 287 --> <!-- /react-text --><a href="#Builder-avg" data-reactid="288">avg</a></li><li data-reactid="289"><!-- react-text: 290 --> <!-- /react-text --><a href="#Builder-increment" data-reactid="291">increment</a></li><li data-reactid="292"><!-- react-text: 293 --> <!-- /react-text --><a href="#Builder-decrement" data-reactid="294">decrement</a></li><li data-reactid="295"><!-- react-text: 296 --> <!-- /react-text --><a href="#Builder-truncate" data-reactid="297">truncate</a></li><li data-reactid="298"><!-- react-text: 299 --> <!-- /react-text --><a href="#Builder-pluck" data-reactid="300">pluck</a></li><li data-reactid="301"><!-- react-text: 302 --> <!-- /react-text --><a href="#Builder-first" data-reactid="303">first</a></li><li data-reactid="304"><!-- react-text: 305 --> <!-- /react-text --><a href="#Builder-clone" data-reactid="306">clone</a></li><li data-reactid="307"><!-- react-text: 308 --> <!-- /react-text --><a href="#Builder-modify" data-reactid="309">modify</a></li><li data-reactid="310"><!-- react-text: 311 --> <!-- /react-text --><a href="#Builder-columnInfo" data-reactid="312">columnInfo</a></li><li data-reactid="313"><!-- react-text: 314 --> <!-- /react-text --><a href="#Builder-debug" data-reactid="315">debug</a></li><li data-reactid="316"><!-- react-text: 317 --> <!-- /react-text --><a href="#Builder-connection" data-reactid="318">connection</a></li><li data-reactid="319"><!-- react-text: 320 --> <!-- /react-text --><a href="#Builder-options" data-reactid="321">options</a></li></ul><a class="toc_title" href="#Transactions" data-reactid="322">Transactions</a><ul class="toc_section" data-reactid="323"><li data-reactid="324"><!-- react-text: 325 --> <!-- /react-text --><a href="#Transactions" data-reactid="326"><b data-reactid="327">overview</b></a></li></ul><a class="toc_title" href="#Schema" data-reactid="328">Schema Builder</a><ul class="toc_section" data-reactid="329"><li data-reactid="330"><!-- react-text: 331 --> <!-- /react-text --><a href="#Schema-with" data-reactid="332">with</a></li><li data-reactid="333"><!-- react-text: 334 --> <!-- /react-text --><a href="#Schema-withSchema" data-reactid="335">withSchema</a></li><li data-reactid="336"><!-- react-text: 337 --> <!-- /react-text --><a href="#Schema-createTable" data-reactid="338">createTable</a></li><li data-reactid="339"><!-- react-text: 340 --> <!-- /react-text --><a href="#Schema-createTableIfNotExists" data-reactid="341">createTableIfNotExists</a></li><li data-reactid="342"><!-- react-text: 343 --> <!-- /react-text --><a href="#Schema-renameTable" data-reactid="344">renameTable</a></li><li data-reactid="345"><!-- react-text: 346 --> <!-- /react-text --><a href="#Schema-dropTable" data-reactid="347">dropTable</a></li><li data-reactid="348"><!-- react-text: 349 --> <!-- /react-text --><a href="#Schema-hasColumn" data-reactid="350">hasColumn</a></li><li data-reactid="351"><!-- react-text: 352 --> <!-- /react-text --><a href="#Schema-hasTable" data-reactid="353">hasTable</a></li><li data-reactid="354"><!-- react-text: 355 --> <!-- /react-text --><a href="#Schema-dropTableIfExists" data-reactid="356">dropTableIfExists</a></li><li data-reactid="357"><!-- react-text: 358 --> <!-- /react-text --><a href="#Schema-table" data-reactid="359">table</a></li><li data-reactid="360"><!-- react-text: 361 --> <!-- /react-text --><a href="#Schema-raw" data-reactid="362">raw</a></li><li data-reactid="363"><b data-reactid="364"><a href="#Schema-Building" data-reactid="365">Schema Building:</a></b></li><li data-reactid="366"><!-- react-text: 367 --> <!-- /react-text --><a href="#Schema-dropColumn" data-reactid="368">dropColumn</a></li><li data-reactid="369"><!-- react-text: 370 --> <!-- /react-text --><a href="#Schema-dropColumns" data-reactid="371">dropColumns</a></li><li data-reactid="372"><!-- react-text: 373 --> <!-- /react-text --><a href="#Schema-renameColumn" data-reactid="374">renameColumn</a></li><li data-reactid="375"><!-- react-text: 376 --> <!-- /react-text --><a href="#Schema-increments" data-reactid="377">increments</a></li><li data-reactid="378"><!-- react-text: 379 --> <!-- /react-text --><a href="#Schema-integer" data-reactid="380">integer</a></li><li data-reactid="381"><!-- react-text: 382 --> <!-- /react-text --><a href="#Schema-bigInteger" data-reactid="383">bigInteger</a></li><li data-reactid="384"><!-- react-text: 385 --> <!-- /react-text --><a href="#Schema-text" data-reactid="386">text</a></li><li data-reactid="387"><!-- react-text: 388 --> <!-- /react-text --><a href="#Schema-string" data-reactid="389">string</a></li><li data-reactid="390"><!-- react-text: 391 --> <!-- /react-text --><a href="#Schema-float" data-reactid="392">float</a></li><li data-reactid="393"><!-- react-text: 394 --> <!-- /react-text --><a href="#Schema-decimal" data-reactid="395">decimal</a></li><li data-reactid="396"><!-- react-text: 397 --> <!-- /react-text --><a href="#Schema-boolean" data-reactid="398">boolean</a></li><li data-reactid="399"><!-- react-text: 400 --> <!-- /react-text --><a href="#Schema-date" data-reactid="401">date</a></li><li data-reactid="402"><!-- react-text: 403 --> <!-- /react-text --><a href="#Schema-dateTime" data-reactid="404">dateTime</a></li><li data-reactid="405"><!-- react-text: 406 --> <!-- /react-text --><a href="#Schema-time" data-reactid="407">time</a></li><li data-reactid="408"><!-- react-text: 409 --> <!-- /react-text --><a href="#Schema-timestamp" data-reactid="410">timestamp</a></li><li data-reactid="411"><!-- react-text: 412 --> <!-- /react-text --><a href="#Schema-timestamps" data-reactid="413">timestamps</a></li><li data-reactid="414"><!-- react-text: 415 --> <!-- /react-text --><a href="#Schema-dropTimestamps" data-reactid="416">dropTimestamps</a></li><li data-reactid="417"><!-- react-text: 418 --> <!-- /react-text --><a href="#Schema-binary" data-reactid="419">binary</a></li><li data-reactid="420"><!-- react-text: 421 --> <!-- /react-text --><a href="#Schema-enum" data-reactid="422">enum / enu</a></li><li data-reactid="423"><!-- react-text: 424 --> <!-- /react-text --><a href="#Schema-json" data-reactid="425">json</a></li><li data-reactid="426"><!-- react-text: 427 --> <!-- /react-text --><a href="#Schema-jsonb" data-reactid="428">jsonb</a></li><li data-reactid="429"><!-- react-text: 430 --> <!-- /react-text --><a href="#Schema-uuid" data-reactid="431">uuid</a></li><li data-reactid="432"><!-- react-text: 433 --> <!-- /react-text --><a href="#Schema-comment" data-reactid="434">comment</a></li><li data-reactid="435"><!-- react-text: 436 --> <!-- /react-text --><a href="#Schema-engine" data-reactid="437">engine</a></li><li data-reactid="438"><!-- react-text: 439 --> <!-- /react-text --><a href="#Schema-charset" data-reactid="440">charset</a></li><li data-reactid="441"><!-- react-text: 442 --> <!-- /react-text --><a href="#Schema-collate" data-reactid="443">collate</a></li><li data-reactid="444"><!-- react-text: 445 --> <!-- /react-text --><a href="#Schema-inherits" data-reactid="446">inherits</a></li><li data-reactid="447"><!-- react-text: 448 --> <!-- /react-text --><a href="#Schema-specificType" data-reactid="449">specificType</a></li><li data-reactid="450"><!-- react-text: 451 --> <!-- /react-text --><a href="#Schema-index" data-reactid="452">index</a></li><li data-reactid="453"><!-- react-text: 454 --> <!-- /react-text --><a href="#Schema-dropIndex" data-reactid="455">dropIndex</a></li><li data-reactid="456"><!-- react-text: 457 --> <!-- /react-text --><a href="#Schema-unique" data-reactid="458">unique</a></li><li data-reactid="459"><!-- react-text: 460 --> <!-- /react-text --><a href="#Schema-foreign" data-reactid="461">foreign</a></li><li data-reactid="462"><!-- react-text: 463 --> <!-- /react-text --><a href="#Schema-dropForeign" data-reactid="464">dropForeign</a></li><li data-reactid="465"><!-- react-text: 466 --> <!-- /react-text --><a href="#Schema-dropUnique" data-reactid="467">dropUnique</a></li><li data-reactid="468"><!-- react-text: 469 --> <!-- /react-text --><a href="#Schema-dropPrimary" data-reactid="470">dropPrimary</a></li><li data-reactid="471"><a href="#Chainable" data-reactid="472"><b data-reactid="473">Chainable:</b></a></li><li data-reactid="474"><!-- react-text: 475 --> <!-- /react-text --><a href="#Schema-alter" data-reactid="476">alter</a></li><li data-reactid="477"><!-- react-text: 478 --> <!-- /react-text --><a href="#Schema-index" data-reactid="479">index</a></li><li data-reactid="480"><!-- react-text: 481 --> <!-- /react-text --><a href="#Schema-primary" data-reactid="482">primary</a></li><li data-reactid="483"><!-- react-text: 484 --> <!-- /react-text --><a href="#Schema-unique" data-reactid="485">unique</a></li><li data-reactid="486"><!-- react-text: 487 --> <!-- /react-text --><a href="#Schema-references" data-reactid="488">references</a></li><li data-reactid="489"><!-- react-text: 490 --> <!-- /react-text --><a href="#Schema-inTable" data-reactid="491">inTable</a></li><li data-reactid="492"><!-- react-text: 493 --> <!-- /react-text --><a href="#Schema-onDelete" data-reactid="494">onDelete</a></li><li data-reactid="495"><!-- react-text: 496 --> <!-- /react-text --><a href="#Schema-onUpdate" data-reactid="497">onUpdate</a></li><li data-reactid="498"><!-- react-text: 499 --> <!-- /react-text --><a href="#Schema-defaultTo" data-reactid="500">defaultTo</a></li><li data-reactid="501"><!-- react-text: 502 --> <!-- /react-text --><a href="#Schema-unsigned" data-reactid="503">unsigned</a></li><li data-reactid="504"><!-- react-text: 505 --> <!-- /react-text --><a href="#Schema-notNullable" data-reactid="506">notNullable</a></li><li data-reactid="507"><!-- react-text: 508 --> <!-- /react-text --><a href="#Schema-nullable" data-reactid="509">nullable</a></li><li data-reactid="510"><!-- react-text: 511 --> <!-- /react-text --><a href="#Schema-first" data-reactid="512">first</a></li><li data-reactid="513"><!-- react-text: 514 --> <!-- /react-text --><a href="#Schema-after" data-reactid="515">after</a></li><li data-reactid="516"><!-- react-text: 517 --> <!-- /react-text --><a href="#Schema-comment" data-reactid="518">comment</a></li><li data-reactid="519"><!-- react-text: 520 --> <!-- /react-text --><a href="#Schema-collate" data-reactid="521">collate</a></li></ul><a class="toc_title" href="#Raw" data-reactid="522">Raw</a><ul class="toc_section" data-reactid="523"><li data-reactid="524"><!-- react-text: 525 --> <!-- /react-text --><a href="#Raw-Bindings" data-reactid="526">Raw Parameter Binding</a></li><li data-reactid="527"><!-- react-text: 528 --> <!-- /react-text --><a href="#Raw-Expressions" data-reactid="529">Raw Expressions</a></li><li data-reactid="530"><!-- react-text: 531 --> <!-- /react-text --><a href="#Raw-Queries" data-reactid="532">Raw Queries</a></li><li data-reactid="533"><!-- react-text: 534 -->- <!-- /react-text --><a href="#Raw-queries-wrapped" data-reactid="535">Wrapped Queries</a></li></ul><a class="toc_title" href="#Utility" data-reactid="536">Utility</a><ul class="toc_section" data-reactid="537"><li data-reactid="538"><!-- react-text: 539 -->- <!-- /react-text --><a href="#Utility-BatchInsert" data-reactid="540">Batch Insert</a></li></ul><a class="toc_title" href="#Interfaces" data-reactid="541">Interfaces</a><ul class="toc_section" data-reactid="542"><li data-reactid="543"><b data-reactid="544"><a href="#Interfaces-Promises" data-reactid="545">Promises</a></b></li><li data-reactid="546"><!-- react-text: 547 -->   <!-- /react-text --><a href="#Interfaces-then" data-reactid="548">then</a></li><li data-reactid="549"><!-- react-text: 550 -->   <!-- /react-text --><a href="#Interfaces-catch" data-reactid="551">catch</a></li><li data-reactid="552"><!-- react-text: 553 -->   <!-- /react-text --><a href="#Interfaces-tap" data-reactid="554">tap</a></li><li data-reactid="555"><!-- react-text: 556 -->   <!-- /react-text --><a href="#Interfaces-map" data-reactid="557">map</a></li><li data-reactid="558"><!-- react-text: 559 -->   <!-- /react-text --><a href="#Interfaces-reduce" data-reactid="560">reduce</a></li><li data-reactid="561"><!-- react-text: 562 -->   <!-- /react-text --><a href="#Interfaces-bind" data-reactid="563">bind</a></li><li data-reactid="564"><!-- react-text: 565 -->   <!-- /react-text --><a href="#Interfaces-return" data-reactid="566">return</a></li><li data-reactid="567"><b data-reactid="568"><a href="#Interfaces-Callbacks" data-reactid="569">Callbacks</a></b></li><li data-reactid="570"><!-- react-text: 571 -->   <!-- /react-text --><a href="#Interfaces-asCallback" data-reactid="572">asCallback</a></li><li data-reactid="573"><b data-reactid="574"><a href="#Interfaces-Streams" data-reactid="575">Streams</a></b></li><li data-reactid="576"><!-- react-text: 577 -->   <!-- /react-text --><a href="#Interfaces-stream" data-reactid="578">stream</a></li><li data-reactid="579"><!-- react-text: 580 -->   <!-- /react-text --><a href="#Interfaces-pipe" data-reactid="581">pipe</a></li><li data-reactid="582"><b data-reactid="583"><a href="#Interfaces-Events" data-reactid="584">Events</a></b></li><li data-reactid="585"><!-- react-text: 586 -->   <!-- /react-text --><a href="#Interfaces-query" data-reactid="587">query</a></li><li data-reactid="588"><!-- react-text: 589 -->   <!-- /react-text --><a href="#Interfaces-query-error" data-reactid="590">query-error</a></li><li data-reactid="591"><!-- react-text: 592 -->   <!-- /react-text --><a href="#Interfaces-query-response" data-reactid="593">query-response</a></li><li data-reactid="594"><b data-reactid="595"><a href="#Interfaces-Other" data-reactid="596">Other:</a></b></li><li data-reactid="597"><!-- react-text: 598 -->   <!-- /react-text --><a href="#Interfaces-toString" data-reactid="599">toString</a></li><li data-reactid="600"><!-- react-text: 601 -->   <!-- /react-text --><a href="#Interfaces-toSQL" data-reactid="602">toSQL</a></li><li data-reactid="603"><!-- react-text: 604 -->   <!-- /react-text --><a href="#Interfaces-toSQL" data-reactid="605">toSQL().toNative()</a></li></ul><a class="toc_title" href="#Migrations" data-reactid="606">Migrations</a><ul class="toc_section" data-reactid="607"><li data-reactid="608"><a href="#Migrations-CLI" data-reactid="609"><b data-reactid="610">CLI</b></a></li><li data-reactid="611"><!-- react-text: 612 --> <!-- /react-text --><a href="#Migrations-CLI" data-reactid="613">Migrations</a></li><li data-reactid="614"><!-- react-text: 615 --> <!-- /react-text --><a href="#Seeds-CLI" data-reactid="616">Seed files</a></li><li data-reactid="617"><!-- react-text: 618 --> <!-- /react-text --><a href="#knexfile" data-reactid="619">knexfile.js</a></li><li data-reactid="620"><a href="#Migrations-API" data-reactid="621"><b data-reactid="622">Migration API</b></a></li><li data-reactid="623"><!-- react-text: 624 --> <!-- /react-text --><a href="#Migrations-make" data-reactid="625">make</a></li><li data-reactid="626"><!-- react-text: 627 --> <!-- /react-text --><a href="#Migrations-latest" data-reactid="628">latest</a></li><li data-reactid="629"><!-- react-text: 630 --> <!-- /react-text --><a href="#Migrations-rollback" data-reactid="631">rollback</a></li><li data-reactid="632"><!-- react-text: 633 --> <!-- /react-text --><a href="#Migrations-currentVersion" data-reactid="634">currentVersion</a></li><li data-reactid="635"><a href="#Notes-about-locks" data-reactid="636"><b data-reactid="637">Notes about locks</b></a></li><li data-reactid="638"><a href="#Seeds-API" data-reactid="639"><b data-reactid="640">Seed API</b></a></li><li data-reactid="641"><!-- react-text: 642 --> <!-- /react-text --><a href="#Seeds-make" data-reactid="643">make</a></li><li data-reactid="644"><!-- react-text: 645 --> <!-- /react-text --><a href="#Seeds-run" data-reactid="646">run</a></li></ul><a class="toc_title" href="#support" data-reactid="647">Support</a><a class="toc_title" href="#faq" data-reactid="648">F.A.Q.</a><a class="toc_title" href="#changelog" data-reactid="649">Change Log</a></div><div class="language" data-reactid="650"><!-- react-text: 651 -->Show example query output as:<!-- /react-text --><br data-reactid="652"/><select data-reactid="653"><option selected="" value="mysql" data-reactid="654">MySQL / MariaDB</option><option value="pg" data-reactid="655">PostgreSQL</option><option value="sqlite3" data-reactid="656">SQLite3</option><option value="oracle" data-reactid="657">Oracle</option><option value="mssql" data-reactid="658">MSSQL</option></select></div><a href="https://github.com/tgriesser/knex" data-reactid="659"><img style="position:fixed;top:0;right:0;border:0;" src="assets/images/github.png" alt="Fork me on GitHub" data-reactid="660"/></a><div class="container" data-reactid="661"><div id="Prelude" data-reactid="662"><div data-reactid="663"><p data-reactid="664"><img height="108" id="logo" src="assets/images/knex.png" alt="Knex.js" /></p><p data-reactid="665"><strong>Knex.js</strong> is a &quot;batteries included&quot; SQL query builder for <strong>Postgres</strong>, <strong>MSSQL</strong>, <strong>MySQL</strong>, <strong>MariaDB</strong>, <strong>SQLite3</strong>, and <strong>Oracle</strong> designed to be flexible, portable, and fun to use. It features both traditional node style <a href="#Interfaces-Callbacks">callbacks</a> as well as a <a href="#Interfaces-Promises">promise</a> interface for cleaner async flow control, <a href="#Interfaces-Streams">a stream interface</a>, full featured <a href="#Builder">query</a> and <a href="#Schema">schema</a> builders, <a href="#Transactions"><strong>transaction support (with savepoints)</strong></a>, connection <a href="#Installation-pooling">pooling</a> and standardized responses between different query clients and dialects.</p><p data-reactid="666">The project is <a href="http://github.com/tgriesser/knex">hosted on GitHub</a>, and has a comprehensive <a href="https://travis-ci.org/tgriesser/knex">test suite</a>.</p><p data-reactid="667">Knex is available for use under the <a href="http://github.com/tgriesser/knex/blob/master/LICENSE">MIT software license</a>.</p><p data-reactid="668">You can report bugs and discuss features on the <a href="http://github.com/tgriesser/knex/issues">GitHub issues page</a>, add pages to the <a href="https://github.com/tgriesser/knex/wiki">wiki</a> or send tweets to <a href="http://twitter.com/tgriesser">@tgriesser</a>.</p><p data-reactid="669">Thanks to all of the great <a href="https://github.com/tgriesser/knex/graphs/contributors">contributions</a> to the project.</p></div><p class="info" data-reactid="670">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&#39;s code and syntax was originally derived.</p><h2 data-reactid="671">Latest Release: 0.14.2 - <span class="small"><a href="#changelog">Change Log</a></span></h2><p data-reactid="672">Current Develop — <a href="https://travis-ci.org/tgriesser/knex"><img src="https://travis-ci.org/tgriesser/knex.png?branch=master" alt="Travis Badge"></a></p></div><div id="Upgrading" data-reactid="673"><h2 data-reactid="674">Upgrading 0.13 -&gt; 0.14</h2><div data-reactid="675"><p data-reactid="676"><a href="https://github.com/coopernurse/node-pool">generic-pool</a> was upgraded to v3. If you have specified <code>idleTimeoutMillis</code> or <code>softIdleTimeoutMillis</code> in the pool config then you will need to add <code>evictionRunIntervalMillis: 1000</code> when upgrading to 0.14.</p><p data-reactid="677">See original <a href="https://github.com/tgriesser/knex/issues/2322">issue #2322</a> for details.</p></div></div><div id="Installation" data-reactid="678"><h2 id="Installation" data-reactid="679">Installation</h2><p data-reactid="680">Knex can be used as an SQL query builder in both Node.JS and the browser, limited to WebSQL&#39;s constraints (like the inability to drop tables or read schemas). Composing SQL queries in the browser for execution on the server is highly discouraged, as this can be the cause of serious security vulnerabilities. The browser builds outside of WebSQL are primarily for learning purposes - for example, you can pop open the console and build queries on this page using the <a href="javascript:alert(knex)">knex</a> object.</p><h3 id="Installation-node" data-reactid="681">Node.js</h3><p data-reactid="682">The primary target environment for Knex is Node.js, you will need to install the <code>knex</code> library, and then install the appropriate database library: <a href="https://github.com/brianc/node-postgres"><code>pg</code></a> for PostgreSQL, <a href="https://github.com/felixge/node-mysql"><code>mysql</code></a> for MySQL or MariaDB, <a href="https://github.com/mapbox/node-sqlite3"><code>sqlite3</code></a> for SQLite3, or <a href="https://github.com/patriksimek/node-mssql"><code>mssql</code></a> for MSSQL.</p><pre data-reactid="683"><code data-reactid="684">$ npm install knex --save
# Then add one of the following (adding a --save) flag:
$ npm install pg
$ npm install sqlite3
$ npm install mysql
$ npm install mysql2
$ npm install mariasql
$ npm install strong-oracle
$ npm install oracle
$ npm install mssql</code></pre><h3 id="Installation-browser" data-reactid="685">Browser</h3><p data-reactid="686">Knex can be built using a JavaScript build tool such as <a href="http://browserify.org/">browserify</a> or <a href="https://github.com/webpack/webpack">webpack</a>. In fact, this documentation uses a webpack build which <a href="https://github.com/knex/documentation/blob/a4de1b2eb50d6699f126be8d134f3d1acc4fc69d/components/Container.jsx#L3">includes knex</a>. View source on this page to see the browser build in-action (the global <code>knex</code> variable).</p><h3 id="Installation-client" data-reactid="687">Initializing the Library</h3><p data-reactid="688">The <code>knex</code> module is itself a function which takes a configuration object for Knex, accepting a few parameters. The <code>client</code> parameter is required and determines which client adapter will be used with the library.</p><pre data-reactid="689"><code class="hljs js" data-reactid="690"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
}
});</code></pre><p data-reactid="691">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><p class="info" data-reactid="692">Note: Knex&#39;s PostgreSQL client allows you to set the initial search path for each connection automatically using an additional option &quot;searchPath&quot; as shown below.</p><pre data-reactid="693"><code class="hljs js" data-reactid="694"><span class="hljs-keyword">var</span> pg = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: process.env.PG_CONNECTION_STRING,
<span class="hljs-attr">searchPath</span>: [<span class="hljs-string">'knex'</span>, <span class="hljs-string">'public'</span>],
});</code></pre><p class="info" data-reactid="695">Note: When you use the SQLite3 adapter, there is a filename required, not a network connection. For example:</p><pre data-reactid="696"><code class="hljs js" data-reactid="697"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'sqlite3'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">filename</span>: <span class="hljs-string">"./mydb.sqlite"</span>
}
});</code></pre><p class="info" data-reactid="698">Note: The database version can be added in knex configuration, when you use the PostgreSQL adapter to connect a non-standard database.</p><pre data-reactid="699"><code class="hljs js" data-reactid="700"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">version</span>: <span class="hljs-string">'7.2'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
}
});</code></pre><p data-reactid="701">You can also connect via an unix domain socket, which will ignore host and port.</p><pre data-reactid="702"><code class="hljs js" data-reactid="703"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">socketPath</span> : <span class="hljs-string">'/path/to/socket.sock'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
}
});</code></pre><p class="info" data-reactid="704">Initializing the library should normally only ever happen once in your application, as it creates a connection pool for the current database, you should use the instance returned from the initialize call throughout your library.</p><p data-reactid="705">Specify the client for the particular flavour of SQL you are interested in.</p><pre data-reactid="706"><code class="hljs js" data-reactid="707"><span class="hljs-keyword">var</span> pg = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>});
knex(<span class="hljs-string">'table'</span>).insert({<span class="hljs-attr">a</span>: <span class="hljs-string">'b'</span>}).returning(<span class="hljs-string">'*'</span>).toString();
<span class="hljs-comment">// "insert into "table" ("a") values ('b')"</span>
pg(<span class="hljs-string">'table'</span>).insert({<span class="hljs-attr">a</span>: <span class="hljs-string">'b'</span>}).returning(<span class="hljs-string">'*'</span>).toString();
<span class="hljs-comment">// "insert into "table" ("a") values ('b') returning *"</span></code></pre><h3 id="Installation-debug" data-reactid="708">Debugging</h3><p data-reactid="709">Passing a <code>debug: true</code> flag on your initialization object will turn on <a href="#Builder-debug">debugging</a> for all queries.</p><h3 id="Installation-pooling" data-reactid="710">Pooling</h3><div data-reactid="711"><p data-reactid="712">The client created by the configuration initializes a connection pool, using the <a href="https://github.com/coopernurse/node-pool">generic-pool</a> library. This connection pool has a default setting of a <code>min: 2, max: 10</code> for the MySQL and PG libraries, and a single connection for sqlite3 (due to issues with utilizing multiple connections on a single file). To change the config settings for the pool, pass a <code>pool</code> option as one of the keys in the initialize block.</p><p data-reactid="713">Checkout the <a href="https://github.com/coopernurse/node-pool">generic-pool</a> library for more information.</p></div><pre data-reactid="714"><code class="hljs js" data-reactid="715"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
},
<span class="hljs-attr">pool</span>: { <span class="hljs-attr">min</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">max</span>: <span class="hljs-number">7</span> }
});</code></pre><p data-reactid="716">If you ever need to explicitly teardown the connection pool, you may use <code>knex.destroy([callback])</code>. You may use <code>knex.destroy</code> by passing a callback, or by chaining as a promise, just not both.</p><h3 id="Installation-pooling-afterCreate" data-reactid="717">afterCreate</h3><p data-reactid="718"><code>afterCreate</code> callback (rawDriverConnection, done) is called when the pool aquires a new connection from the database server. done(err, connection) callback must be called for <code>knex</code> to be able to decide if the connection is ok or if it should be discarded right away from the pool.</p><pre data-reactid="719"><code class="hljs js" data-reactid="720"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: {...},
<span class="hljs-attr">pool</span>: {
<span class="hljs-attr">afterCreate</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">conn, done</span>) </span>{
<span class="hljs-comment">// in this example we use pg driver's connection API</span>
conn.query(<span class="hljs-string">'SET timezone="UTC";'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-comment">// first query failed, return error and don't try to make next query</span>
done(err, conn);
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// do the second query...</span>
conn.query(<span class="hljs-string">'SELECT set_limit(0.01);'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
<span class="hljs-comment">// if err is not falsy, connection is discarded from pool</span>
<span class="hljs-comment">// if connection aquire was triggered by a query the error is passed to query promise</span>
done(err, conn);
});
}
});
}
}
});</code></pre><h3 id="Installation-acquireConnectionTimeout" data-reactid="721">acquireConnectionTimeout</h3><p data-reactid="722"><code>acquireConnectionTimeout</code> defaults to 60000ms and is used to determine how long knex should wait before throwing a timeout error when acquiring a connection is not possible. The most common cause for this is using up all the pool for transaction connections and then attempting to run queries outside of transactions while the pool is still full. The error thrown will provide information on the query the connection was for to simplify the job of locating the culprit.</p><pre data-reactid="723"><code class="hljs js" data-reactid="724"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: {...},
<span class="hljs-attr">pool</span>: {...},
<span class="hljs-attr">acquireConnectionTimeout</span>: <span class="hljs-number">10000</span>
});</code></pre><h3 id="Installation-fetchAsString" data-reactid="725">fetchAsString</h3><p data-reactid="726">Utilized by Oracledb. An array of types. The valid types are &#39;DATE&#39;, &#39;NUMBER&#39; and &#39;CLOB&#39;. When any column having one of the specified types is queried, the column data is returned as a string instead of the default representation.</p><pre data-reactid="727"><code class="hljs js" data-reactid="728"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'oracledb'</span>,
<span class="hljs-attr">connection</span>: {...},
<span class="hljs-attr">fetchAsString</span>: [ <span class="hljs-string">'number'</span>, <span class="hljs-string">'clob'</span> ]
});</code></pre><h3 id="Installation-migrations" data-reactid="729">Migrations</h3><p data-reactid="730">For convenience, the any migration configuration may be specified when initializing the library. Read the <a href="#Migrations">Migrations</a> section for more information and a full list of configuration options.</p><pre data-reactid="731"><code class="hljs js" data-reactid="732"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
},
<span class="hljs-attr">migrations</span>: {
<span class="hljs-attr">tableName</span>: <span class="hljs-string">'migrations'</span>
}
});</code></pre><h3 id="Installation-post-process-response" data-reactid="733">postProcessResponse</h3><p data-reactid="734">Hook for modifying returned rows, before passing them forward to user. One can do for example snake_case -&gt; camelCase conversion for returned columns with this hook.</p><pre data-reactid="735"><code class="hljs js" data-reactid="736"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-comment">// overly simplified snake_case -&gt; camelCase converter</span>
postProcessResponse: <span class="hljs-function">(<span class="hljs-params">result</span>) =&gt;</span> {
<span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> add special case for raw results (depends on dialect)</span>
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">Array</span>.isArray(result)) {
<span class="hljs-keyword">return</span> result.map(<span class="hljs-function"><span class="hljs-params">row</span> =&gt;</span> convertToCamel(row));
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> convertToCamel(result);
}
}
});</code></pre><h3 id="Installation-wrap-identifier" data-reactid="737">wrapIdentifier</h3><p data-reactid="738">Knex supports transforming identifier names automatically to quoted versions for each dialect. For example <code>&#39;Table.columnName as foo&#39;</code> for PostgreSQL is converted to &quot;Table&quot;.&quot;columnName&quot; as &quot;foo&quot;.</p><p data-reactid="739">With <code>wrapIdentifier</code> one may override the way how identifiers are transformed. It can be used to override default functionality and for example to help doing <code>camelCase</code> -&gt; <code>snake_case</code> conversion.</p><p data-reactid="740">Conversion function <code>wrapIdentifier(value, dialectImpl): string</code> gets each part of the identifier as a single <code>value</code> and the second parameter is the original conversion function from the dialect implementation. For example <code>knex(&#39;table&#39;).withSchema(&#39;foo&#39;).select(&#39;table.field as otherName&#39;).where(&#39;id&#39;, 1)</code> will call <code>wrapIdentifier</code> converter for following values <code>&#39;table&#39;</code>, <code>&#39;foo&#39;</code>, <code>&#39;table&#39;</code>, <code>&#39;field&#39;</code>, <code>&#39;otherName&#39;</code> and <code>&#39;id&#39;</code>.</p><pre data-reactid="741"><code class="hljs js" data-reactid="742"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-comment">// overly simplified camelCase -&gt; snake_case converter</span>
wrapIdentifier: <span class="hljs-function">(<span class="hljs-params">value, origImpl</span>) =&gt;</span> origImpl(convertToSnakeCase(value))
});</code></pre></div><div id="Builder" data-reactid="743"><h2 id="Builder" data-reactid="744">Knex Query Builder</h2><p data-reactid="745">The heart of the library, the knex query builder is the interface used for building and executing standard SQL queries, such as <code>select</code>, <code>insert</code>, <code>update</code>, <code>delete</code>.</p><h3 id="Builder-identifier-syntax" data-reactid="746">Identifier Syntax</h3><div data-reactid="747"><p data-reactid="748">In many places in APIs identifiers like table name or column name can be passed to methods.</p><p data-reactid="749">Most commonly one needs just plain <code>tableName.columnName</code>, <code>tableName</code> or <code>columnName</code>, but in many cases one also needs to pass an alias how that identifier is referred later on in the query.</p><p data-reactid="750">There are two ways to declare an alias for identifier. One can directly give <code>as aliasName</code> prefix for the identifier or oen can pass an object <code>{ aliasName: &#39;identifierName&#39; }</code>.</p><p data-reactid="751">If in the object has multiple aliases <code>{ alias1: &#39;identifier1&#39;, alias2: &#39;identifier2&#39; }</code>, then all the aliased identifiers are expanded to comma separated list.</p><p data-reactid="752">NOTE: identifier syntax has no place for selecting schema, so if you are doing <code>schemaName.tableName</code>, query might be rendered wrong. Use <code>.withSchema(&#39;schemaName&#39;)</code> instead.</p></div><pre class="display" data-reactid="753"><code class="js hljs" data-reactid="754">knex({ <span class="hljs-attr">a</span>: <span class="hljs-string">'table'</span>, <span class="hljs-attr">b</span>: <span class="hljs-string">'table'</span> })
.select({
<span class="hljs-attr">aTitle</span>: <span class="hljs-string">'a.title'</span>,
<span class="hljs-attr">bTitle</span>: <span class="hljs-string">'b.title'</span>
})
.whereRaw(<span class="hljs-string">'?? = ??'</span>, [<span class="hljs-string">'a.column_1'</span>, <span class="hljs-string">'b.column_2'</span>])</code><br data-reactid="755"/><!-- react-text: 756 -->Outputs:<!-- /react-text --><br data-reactid="757"/><code class="sql hljs" data-reactid="758"><span class="hljs-keyword">select</span> <span class="hljs-string">`a`</span>.<span class="hljs-string">`title`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`aTitle`</span>, <span class="hljs-string">`b`</span>.<span class="hljs-string">`title`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`bTitle`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`table`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span>, <span class="hljs-string">`table`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`b`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`a`</span>.<span class="hljs-string">`column_1`</span> = <span class="hljs-string">`b`</span>.<span class="hljs-string">`column_2`</span></code></pre><div id="Builder-knex" data-reactid="759"><b data-reactid="760">knex</b><!-- react-text: 761 --><!-- /react-text --><code data-reactid="762">knex(tableName, options={only: boolean}) / knex.[methodName]</code><p data-reactid="763">The query builder starts off either by specifying a tableName you wish to query against, or by calling any method directly on the knex object. This kicks off a jQuery-like chain, with which you can call additional query builder methods as needed to construct the query, eventually calling any of the interface methods, to either convert toString, or execute the query with a promise, callback, or stream. Optional second argument for passing options:<em> <strong>only</strong>: if <code>true</code>, the ONLY keyword is used before the <code>tableName</code> to discard inheriting tables&#39; data. <em>*NOTE:</em></em> only supported in PostgreSQL for now.</p></div><div id="Builder-timeout" data-reactid="764"><b data-reactid="765">timeout</b><!-- react-text: 766 --><!-- /react-text --><code data-reactid="767">.timeout(ms, options={cancel: boolean})</code><p data-reactid="768">Sets a timeout for the query and will throw a TimeoutError if the timeout is exceeded. The error contains information about the query, bindings, and the timeout that was set. Useful for complex queries that you want to make sure are not taking too long to execute. Optional second argument for passing options:<em> <strong>cancel</strong>: if <code>true</code>, cancel query if timeout is reached. <em>*NOTE:</em></em> only supported in MySQL and MariaDB for now.</p><pre class="display" data-reactid="769"><code class="js hljs" data-reactid="770">knex.select().from(<span class="hljs-string">'books'</span>).timeout(<span class="hljs-number">1000</span>)</code><br data-reactid="771"/><!-- react-text: 772 -->Outputs:<!-- /react-text --><br data-reactid="773"/><code class="sql hljs" data-reactid="774"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre><pre class="display" data-reactid="775"><code class="js hljs" data-reactid="776">knex.select().from(<span class="hljs-string">'books'</span>).timeout(<span class="hljs-number">1000</span>, {<span class="hljs-attr">cancel</span>: <span class="hljs-literal">true</span>}) <span class="hljs-comment">// MySQL and MariaDB only</span></code><br data-reactid="777"/><!-- react-text: 778 -->Outputs:<!-- /react-text --><br data-reactid="779"/><code class="sql hljs" data-reactid="780"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre></div><div id="Builder-select" data-reactid="781"><b data-reactid="782">select</b><!-- react-text: 783 --><!-- /react-text --><code data-reactid="784">.select([*columns])</code><p data-reactid="785">Creates a select query, taking an optional array of columns for the query, eventually defaulting to * if none are specified when the query is built. The response of a select call will resolve with an array of objects selected from the database.</p><pre class="display" data-reactid="786"><code class="js hljs" data-reactid="787">knex.select(<span class="hljs-string">'title'</span>, <span class="hljs-string">'author'</span>, <span class="hljs-string">'year'</span>).from(<span class="hljs-string">'books'</span>)</code><br data-reactid="788"/><!-- react-text: 789 -->Outputs:<!-- /react-text --><br data-reactid="790"/><code class="sql hljs" data-reactid="791"><span class="hljs-keyword">select</span> <span class="hljs-string">`title`</span>, <span class="hljs-string">`author`</span>, <span class="hljs-string">`year`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre><pre class="display" data-reactid="792"><code class="js hljs" data-reactid="793">knex.select().table(<span class="hljs-string">'books'</span>)</code><br data-reactid="794"/><!-- react-text: 795 -->Outputs:<!-- /react-text --><br data-reactid="796"/><code class="sql hljs" data-reactid="797"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre></div><div id="Builder-as" data-reactid="798"><b data-reactid="799">as</b><!-- react-text: 800 --><!-- /react-text --><code data-reactid="801">.as(name)</code><p data-reactid="802">Allows for aliasing a subquery, taking the string you wish to name the current query. If the query is not a sub-query, it will be ignored.</p><pre class="display" data-reactid="803"><code class="js hljs" data-reactid="804">knex.avg(<span class="hljs-string">'sum_column1'</span>).from(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.sum(<span class="hljs-string">'column1 as sum_column1'</span>).from(<span class="hljs-string">'t1'</span>).groupBy(<span class="hljs-string">'column1'</span>).as(<span class="hljs-string">'t1'</span>)
}).as(<span class="hljs-string">'ignored_alias'</span>)</code><br data-reactid="805"/><!-- react-text: 806 -->Outputs:<!-- /react-text --><br data-reactid="807"/><code class="sql hljs" data-reactid="808"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`sum_column1`</span>) <span class="hljs-keyword">from</span> (<span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-string">`column1`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`sum_column1`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`t1`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`column1`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`t1`</span></code></pre></div><div id="Builder-column" data-reactid="809"><b data-reactid="810">column</b><!-- react-text: 811 --><!-- /react-text --><code data-reactid="812">.column(columns)</code><p data-reactid="813">Specifically set the columns to be selected on a select query, taking an array, an object or a list of column names. Passing an object will automatically alias the columns with the given keys.</p><pre class="display" data-reactid="814"><code class="js hljs" data-reactid="815">knex.column(<span class="hljs-string">'title'</span>, <span class="hljs-string">'author'</span>, <span class="hljs-string">'year'</span>).select().from(<span class="hljs-string">'books'</span>)</code><br data-reactid="816"/><!-- react-text: 817 -->Outputs:<!-- /react-text --><br data-reactid="818"/><code class="sql hljs" data-reactid="819"><span class="hljs-keyword">select</span> <span class="hljs-string">`title`</span>, <span class="hljs-string">`author`</span>, <span class="hljs-string">`year`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre><pre class="display" data-reactid="820"><code class="js hljs" data-reactid="821">knex.column([<span class="hljs-string">'title'</span>, <span class="hljs-string">'author'</span>, <span class="hljs-string">'year'</span>]).select().from(<span class="hljs-string">'books'</span>)</code><br data-reactid="822"/><!-- react-text: 823 -->Outputs:<!-- /react-text --><br data-reactid="824"/><code class="sql hljs" data-reactid="825"><span class="hljs-keyword">select</span> <span class="hljs-string">`title`</span>, <span class="hljs-string">`author`</span>, <span class="hljs-string">`year`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre><pre class="display" data-reactid="826"><code class="js hljs" data-reactid="827">knex.column(<span class="hljs-string">'title'</span>, {<span class="hljs-attr">by</span>: <span class="hljs-string">'author'</span>}, <span class="hljs-string">'year'</span>).select().from(<span class="hljs-string">'books'</span>)</code><br data-reactid="828"/><!-- react-text: 829 -->Outputs:<!-- /react-text --><br data-reactid="830"/><code class="sql hljs" data-reactid="831"><span class="hljs-keyword">select</span> <span class="hljs-string">`title`</span>, <span class="hljs-string">`author`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`by`</span>, <span class="hljs-string">`year`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span></code></pre></div><div id="Builder-from" data-reactid="832"><b data-reactid="833">from</b><!-- react-text: 834 --><!-- /react-text --><code data-reactid="835">.from([tableName], options={only: boolean})</code><p data-reactid="836">Specifies the table used in the current query, replacing the current table name if one has already been specified. This is typically used in the sub-queries performed in the advanced where or union methods. Optional second argument for passing options:<em> <strong>only</strong>: if <code>true</code>, the ONLY keyword is used before the <code>tableName</code> to discard inheriting tables&#39; data. <em>*NOTE:</em></em> only supported in PostgreSQL for now.</p><pre class="display" data-reactid="837"><code class="js hljs" data-reactid="838">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)</code><br data-reactid="839"/><!-- react-text: 840 -->Outputs:<!-- /react-text --><br data-reactid="841"/><code class="sql hljs" data-reactid="842"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-with" data-reactid="843"><b data-reactid="844">with</b><!-- react-text: 845 --><!-- /react-text --><code data-reactid="846">.with(alias, function|raw)</code><p data-reactid="847">Add a &quot;with&quot; clause to the query. &quot;With&quot; clauses are supported by PostgreSQL, Oracle, SQLite3 and MSSQL.</p><pre class="display" data-reactid="848"><code class="js hljs" data-reactid="849">knex.with(<span class="hljs-string">'with_alias'</span>, knex.raw(<span class="hljs-string">'select * from "books" where "author" = ?'</span>, <span class="hljs-string">'Test'</span>)).select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'with_alias'</span>)</code><br data-reactid="850"/><!-- react-text: 851 -->Outputs:<!-- /react-text --><br data-reactid="852"/><code class="sql hljs" data-reactid="853">with `with_alias` as (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">"books"</span> <span class="hljs-keyword">where</span> <span class="hljs-string">"author"</span> = <span class="hljs-string">'Test'</span>) <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`with_alias`</span></code></pre><pre class="display" data-reactid="854"><code class="js hljs" data-reactid="855">knex.with(<span class="hljs-string">'with_alias'</span>, (qb) =&gt; {
qb.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'books'</span>).where(<span class="hljs-string">'author'</span>, <span class="hljs-string">'Test'</span>)
}).select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'with_alias'</span>)</code><br data-reactid="856"/><!-- react-text: 857 -->Outputs:<!-- /react-text --><br data-reactid="858"/><code class="sql hljs" data-reactid="859">with `with_alias` as (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`books`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`author`</span> = <span class="hljs-string">'Test'</span>) <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`with_alias`</span></code></pre></div><div id="Builder-withSchema" data-reactid="860"><b data-reactid="861">withSchema</b><!-- react-text: 862 --><!-- /react-text --><code data-reactid="863">.withSchema([schemaName])</code><p data-reactid="864">Specifies the schema to be used as prefix of table name.</p><pre class="display" data-reactid="865"><code class="js hljs" data-reactid="866">knex.withSchema(<span class="hljs-string">'public'</span>).select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)</code><br data-reactid="867"/><!-- react-text: 868 -->Outputs:<!-- /react-text --><br data-reactid="869"/><code class="sql hljs" data-reactid="870"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`public`</span>.<span class="hljs-string">`users`</span></code></pre></div><h3 id="Builder-wheres" data-reactid="871">Where Clauses</h3><div data-reactid="872"><p data-reactid="873">Several methods exist to assist in dynamic where clauses. In many places functions may be used in place of values, constructing subqueries. In most places existing knex queries may be used to compose sub-queries, etc. Take a look at a few of the examples for each method for instruction on use:</p><p data-reactid="874"><strong>Important:</strong> Supplying knex with an <code>undefined</code> value to any of the <code>where</code> functions will cause knex to throw an error during sql compilation. This is both for yours and our sake. Knex cannot know what to do with undefined values in a where clause, and generally it would be a programmatic error to supply one to begin with. The error will throw a message containing the type of query and the compiled query-string. Example:</p></div><pre class="display" data-reactid="875"><code class="js hljs" data-reactid="876">knex(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'login'</span>, <span class="hljs-literal">undefined</span>)
.select()
.toSQL()</code><br data-reactid="877"/><!-- react-text: 878 -->Error:<!-- /react-text --><br data-reactid="879"/><code class="sql hljs" data-reactid="880">Undefined binding(s) detected when compiling SELECT query: select * from `accounts` where `login` = ?</code></pre><div id="Builder-where" data-reactid="881"><b data-reactid="882">where</b><!-- react-text: 883 --><!-- /react-text --><code data-reactid="884">.where(~mixed~)</code></div><p data-reactid="885">Object Syntax:</p><pre class="display" data-reactid="886"><code class="js hljs" data-reactid="887">knex(<span class="hljs-string">'users'</span>).where({
<span class="hljs-attr">first_name</span>: <span class="hljs-string">'Test'</span>,
<span class="hljs-attr">last_name</span>: <span class="hljs-string">'User'</span>
}).select(<span class="hljs-string">'id'</span>)</code><br data-reactid="888"/><!-- react-text: 889 -->Outputs:<!-- /react-text --><br data-reactid="890"/><code class="sql hljs" data-reactid="891"><span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`first_name`</span> = <span class="hljs-string">'Test'</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`last_name`</span> = <span class="hljs-string">'User'</span></code></pre><p data-reactid="892">Key, Value:</p><pre class="display" data-reactid="893"><code class="js hljs" data-reactid="894">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="895"/><!-- react-text: 896 -->Outputs:<!-- /react-text --><br data-reactid="897"/><code class="sql hljs" data-reactid="898"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre><p data-reactid="899">Grouped Chain:</p><pre class="display" data-reactid="900"><code class="js hljs" data-reactid="901">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).orWhere(<span class="hljs-string">'id'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">10</span>)
}).orWhere({<span class="hljs-attr">name</span>: <span class="hljs-string">'Tester'</span>})</code><br data-reactid="902"/><!-- react-text: 903 -->Outputs:<!-- /react-text --><br data-reactid="904"/><code class="sql hljs" data-reactid="905"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> (<span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`id`</span> &gt; <span class="hljs-number">10</span>) <span class="hljs-keyword">or</span> (<span class="hljs-string">`name`</span> = <span class="hljs-string">'Tester'</span>)</code></pre><p data-reactid="906">Operator:</p><pre class="display" data-reactid="907"><code class="js hljs" data-reactid="908">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'columnName'</span>, <span class="hljs-string">'like'</span>, <span class="hljs-string">'%rowlikeme%'</span>)</code><br data-reactid="909"/><!-- react-text: 910 -->Outputs:<!-- /react-text --><br data-reactid="911"/><code class="sql hljs" data-reactid="912"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`columnName`</span> <span class="hljs-keyword">like</span> <span class="hljs-string">'%rowlikeme%'</span></code></pre><p data-reactid="913">The above query demonstrates the common use case of returning all users for which a specific pattern appears within a designated column.</p><pre class="display" data-reactid="914"><code class="js hljs" data-reactid="915">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">100</span>)</code><br data-reactid="916"/><!-- react-text: 917 -->Outputs:<!-- /react-text --><br data-reactid="918"/><code class="sql hljs" data-reactid="919"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> &gt; <span class="hljs-number">100</span></code></pre><pre class="display" data-reactid="920"><code class="js hljs" data-reactid="921"><span class="hljs-keyword">var</span> subquery = knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">100</span>).andWhere(<span class="hljs-string">'status'</span>, <span class="hljs-string">'active'</span>).orWhere(<span class="hljs-string">'name'</span>, <span class="hljs-string">'John'</span>).select(<span class="hljs-string">'id'</span>);
knex(<span class="hljs-string">'accounts'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'in'</span>, subquery)</code><br data-reactid="922"/><!-- react-text: 923 -->Outputs:<!-- /react-text --><br data-reactid="924"/><code class="sql hljs" data-reactid="925"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> &gt; <span class="hljs-number">100</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`status`</span> = <span class="hljs-string">'active'</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">'John'</span>)</code></pre><p data-reactid="926">.orWhere with an object automatically wraps the statement and creates an <code>or (and - and - and)</code> clause</p><pre class="display" data-reactid="927"><code class="js hljs" data-reactid="928">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).orWhere({<span class="hljs-attr">votes</span>: <span class="hljs-number">100</span>, <span class="hljs-attr">user</span>: <span class="hljs-string">'knex'</span>})</code><br data-reactid="929"/><!-- react-text: 930 -->Outputs:<!-- /react-text --><br data-reactid="931"/><code class="sql hljs" data-reactid="932"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> (<span class="hljs-string">`votes`</span> = <span class="hljs-number">100</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`user`</span> = <span class="hljs-string">'knex'</span>)</code></pre><div id="Builder-whereNot" data-reactid="933"><b data-reactid="934">whereNot</b><!-- react-text: 935 --><!-- /react-text --><code data-reactid="936">.whereNot(~mixed~)</code></div><p data-reactid="937">Object Syntax:</p><pre class="display" data-reactid="938"><code class="js hljs" data-reactid="939">knex(<span class="hljs-string">'users'</span>).whereNot({
<span class="hljs-attr">first_name</span>: <span class="hljs-string">'Test'</span>,
<span class="hljs-attr">last_name</span>: <span class="hljs-string">'User'</span>
}).select(<span class="hljs-string">'id'</span>)</code><br data-reactid="940"/><!-- react-text: 941 -->Outputs:<!-- /react-text --><br data-reactid="942"/><code class="sql hljs" data-reactid="943"><span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`first_name`</span> = <span class="hljs-string">'Test'</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`last_name`</span> = <span class="hljs-string">'User'</span></code></pre><p data-reactid="944">Key, Value:</p><pre class="display" data-reactid="945"><code class="js hljs" data-reactid="946">knex(<span class="hljs-string">'users'</span>).whereNot(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="947"/><!-- react-text: 948 -->Outputs:<!-- /react-text --><br data-reactid="949"/><code class="sql hljs" data-reactid="950"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre><p data-reactid="951">Grouped Chain:</p><pre class="display" data-reactid="952"><code class="js hljs" data-reactid="953">knex(<span class="hljs-string">'users'</span>).whereNot(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).orWhereNot(<span class="hljs-string">'id'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">10</span>)
}).orWhereNot({<span class="hljs-attr">name</span>: <span class="hljs-string">'Tester'</span>})</code><br data-reactid="954"/><!-- react-text: 955 -->Outputs:<!-- /react-text --><br data-reactid="956"/><code class="sql hljs" data-reactid="957"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> (<span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`id`</span> &gt; <span class="hljs-number">10</span>) <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">'Tester'</span></code></pre><p data-reactid="958">Operator:</p><pre class="display" data-reactid="959"><code class="js hljs" data-reactid="960">knex(<span class="hljs-string">'users'</span>).whereNot(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">100</span>)</code><br data-reactid="961"/><!-- react-text: 962 -->Outputs:<!-- /react-text --><br data-reactid="963"/><code class="sql hljs" data-reactid="964"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`votes`</span> &gt; <span class="hljs-number">100</span></code></pre><p data-reactid="965">CAVEAT: WhereNot is not suitable for &quot;in&quot; and &quot;between&quot; type subqueries. You should use &quot;not in&quot; and &quot;not between&quot; instead.</p><pre class="display" data-reactid="966"><code class="js hljs" data-reactid="967"><span class="hljs-keyword">var</span> subquery = knex(<span class="hljs-string">'users'</span>)
.whereNot(<span class="hljs-string">'votes'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">100</span>)
.andWhere(<span class="hljs-string">'status'</span>, <span class="hljs-string">'active'</span>)
.orWhere(<span class="hljs-string">'name'</span>, <span class="hljs-string">'John'</span>)
.select(<span class="hljs-string">'id'</span>);
knex(<span class="hljs-string">'accounts'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'not in'</span>, subquery)</code><br data-reactid="968"/><!-- react-text: 969 -->Outputs:<!-- /react-text --><br data-reactid="970"/><code class="sql hljs" data-reactid="971"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-string">`votes`</span> &gt; <span class="hljs-number">100</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`status`</span> = <span class="hljs-string">'active'</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">'John'</span>)</code></pre><div id="Builder-whereIn" data-reactid="972"><b data-reactid="973">whereIn</b><!-- react-text: 974 --><!-- /react-text --><code data-reactid="975">.whereIn(column, array|callback|builder) / .orWhereIn</code><p data-reactid="976">Shorthand for .where(&#39;id&#39;, &#39;in&#39;, obj), the .whereIn and .orWhereIn methods add a &quot;where in&quot; clause to the query.</p><pre class="display" data-reactid="977"><code class="js hljs" data-reactid="978">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.whereIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
.orWhereIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>])</code><br data-reactid="979"/><!-- react-text: 980 -->Outputs:<!-- /react-text --><br data-reactid="981"/><code class="sql hljs" data-reactid="982"><span class="hljs-keyword">select</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>) <span class="hljs-keyword">or</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>)</code></pre><pre class="display" data-reactid="983"><code class="js hljs" data-reactid="984">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.whereIn(<span class="hljs-string">'account_id'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'accounts'</span>);
})</code><br data-reactid="985"/><!-- react-text: 986 -->Outputs:<!-- /react-text --><br data-reactid="987"/><code class="sql hljs" data-reactid="988"><span class="hljs-keyword">select</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`account_id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span>)</code></pre><pre class="display" data-reactid="989"><code class="js hljs" data-reactid="990"><span class="hljs-keyword">var</span> subquery = knex.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'accounts'</span>);
knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.whereIn(<span class="hljs-string">'account_id'</span>, subquery)</code><br data-reactid="991"/><!-- react-text: 992 -->Outputs:<!-- /react-text --><br data-reactid="993"/><code class="sql hljs" data-reactid="994"><span class="hljs-keyword">select</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`account_id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-keyword">select</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span>)</code></pre></div><div id="Builder-whereNotIn" data-reactid="995"><b data-reactid="996">whereNotIn</b><!-- react-text: 997 --><!-- /react-text --><code data-reactid="998">.whereNotIn(column, array|callback|builder) / .orWhereNotIn</code><pre class="display" data-reactid="999"><code class="js hljs" data-reactid="1000">knex(<span class="hljs-string">'users'</span>).whereNotIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])</code><br data-reactid="1001"/><!-- react-text: 1002 -->Outputs:<!-- /react-text --><br data-reactid="1003"/><code class="sql hljs" data-reactid="1004"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)</code></pre><pre class="display" data-reactid="1005"><code class="js hljs" data-reactid="1006">knex(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'name'</span>, <span class="hljs-string">'like'</span>, <span class="hljs-string">'%Test%'</span>).orWhereNotIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])</code><br data-reactid="1007"/><!-- react-text: 1008 -->Outputs:<!-- /react-text --><br data-reactid="1009"/><code class="sql hljs" data-reactid="1010"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">like</span> <span class="hljs-string">'%Test%'</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)</code></pre></div><div id="Builder-whereNull" data-reactid="1011"><b data-reactid="1012">whereNull</b><!-- react-text: 1013 --><!-- /react-text --><code data-reactid="1014">.whereNull(column) / .orWhereNull</code><pre class="display" data-reactid="1015"><code class="js hljs" data-reactid="1016">knex(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'updated_at'</span>)</code><br data-reactid="1017"/><!-- react-text: 1018 -->Outputs:<!-- /react-text --><br data-reactid="1019"/><code class="sql hljs" data-reactid="1020"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`updated_at`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-whereNotNull" data-reactid="1021"><b data-reactid="1022">whereNotNull</b><!-- react-text: 1023 --><!-- /react-text --><code data-reactid="1024">.whereNotNull(column) / .orWhereNotNull</code><pre class="display" data-reactid="1025"><code class="js hljs" data-reactid="1026">knex(<span class="hljs-string">'users'</span>).whereNotNull(<span class="hljs-string">'created_at'</span>)</code><br data-reactid="1027"/><!-- react-text: 1028 -->Outputs:<!-- /react-text --><br data-reactid="1029"/><code class="sql hljs" data-reactid="1030"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`created_at`</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-whereExists" data-reactid="1031"><b data-reactid="1032">whereExists</b><!-- react-text: 1033 --><!-- /react-text --><code data-reactid="1034">.whereExists(builder | callback) / .orWhereExists</code><pre class="display" data-reactid="1035"><code class="js hljs" data-reactid="1036">knex(<span class="hljs-string">'users'</span>).whereExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="1037"/><!-- react-text: 1038 -->Outputs:<!-- /react-text --><br data-reactid="1039"/><code class="sql hljs" data-reactid="1040"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre><pre class="display" data-reactid="1041"><code class="js hljs" data-reactid="1042">knex(<span class="hljs-string">'users'</span>).whereExists(knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>))</code><br data-reactid="1043"/><!-- react-text: 1044 -->Outputs:<!-- /react-text --><br data-reactid="1045"/><code class="sql hljs" data-reactid="1046"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-whereNotExists" data-reactid="1047"><b data-reactid="1048">whereNotExists</b><!-- react-text: 1049 --><!-- /react-text --><code data-reactid="1050">.whereNotExists(builder | callback) / .orWhereNotExists</code><pre class="display" data-reactid="1051"><code class="js hljs" data-reactid="1052">knex(<span class="hljs-string">'users'</span>).whereNotExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="1053"/><!-- react-text: 1054 -->Outputs:<!-- /react-text --><br data-reactid="1055"/><code class="sql hljs" data-reactid="1056"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre><pre class="display" data-reactid="1057"><code class="js hljs" data-reactid="1058">knex(<span class="hljs-string">'users'</span>).whereNotExists(knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>))</code><br data-reactid="1059"/><!-- react-text: 1060 -->Outputs:<!-- /react-text --><br data-reactid="1061"/><code class="sql hljs" data-reactid="1062"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-whereBetween" data-reactid="1063"><b data-reactid="1064">whereBetween</b><!-- react-text: 1065 --><!-- /react-text --><code data-reactid="1066">.whereBetween(column, range) / .orWhereBetween</code><pre class="display" data-reactid="1067"><code class="js hljs" data-reactid="1068">knex(<span class="hljs-string">'users'</span>).whereBetween(<span class="hljs-string">'votes'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">100</span>])</code><br data-reactid="1069"/><!-- react-text: 1070 -->Outputs:<!-- /react-text --><br data-reactid="1071"/><code class="sql hljs" data-reactid="1072"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> <span class="hljs-keyword">between</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-number">100</span></code></pre></div><div id="Builder-whereNotBetween" data-reactid="1073"><b data-reactid="1074">whereNotBetween</b><!-- react-text: 1075 --><!-- /react-text --><code data-reactid="1076">.whereNotBetween(column, range) / .orWhereNotBetween</code><pre class="display" data-reactid="1077"><code class="js hljs" data-reactid="1078">knex(<span class="hljs-string">'users'</span>).whereNotBetween(<span class="hljs-string">'votes'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">100</span>])</code><br data-reactid="1079"/><!-- react-text: 1080 -->Outputs:<!-- /react-text --><br data-reactid="1081"/><code class="sql hljs" data-reactid="1082"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`votes`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">between</span> <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-number">100</span></code></pre></div><div id="Builder-whereRaw" data-reactid="1083"><b data-reactid="1084">whereRaw</b><!-- react-text: 1085 --><!-- /react-text --><code data-reactid="1086">.whereRaw(query, [bindings])</code><p data-reactid="1087">Convenience helper for .where(knex.raw(query)).</p><pre class="display" data-reactid="1088"><code class="js hljs" data-reactid="1089">knex(<span class="hljs-string">'users'</span>).whereRaw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>])</code><br data-reactid="1090"/><!-- react-text: 1091 -->Outputs:<!-- /react-text --><br data-reactid="1092"/><code class="sql hljs" data-reactid="1093"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">id</span> = <span class="hljs-number">1</span></code></pre></div><h3 data-reactid="1094">Join Methods</h3><p data-reactid="1095">Several methods are provided which assist in building joins.</p><div id="Builder-join" data-reactid="1096"><b data-reactid="1097">join</b><!-- react-text: 1098 --><!-- /react-text --><code data-reactid="1099">.join(table, first, [operator], second)</code><p data-reactid="1100">The join builder can be used to specify joins between tables, with the first argument being the joining table, the next three arguments being the first join column, the join operator and the second join column, respectively.</p><pre class="display" data-reactid="1101"><code class="js hljs" data-reactid="1102">knex(<span class="hljs-string">'users'</span>)
.join(<span class="hljs-string">'contacts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.user_id'</span>)
.select(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'contacts.phone'</span>)</code><br data-reactid="1103"/><!-- react-text: 1104 -->Outputs:<!-- /react-text --><br data-reactid="1105"/><code class="sql hljs" data-reactid="1106"><span class="hljs-keyword">select</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span>, <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`phone`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1107"><code class="js hljs" data-reactid="1108">knex(<span class="hljs-string">'users'</span>)
.join(<span class="hljs-string">'contacts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'contacts.user_id'</span>)
.select(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'contacts.phone'</span>)</code><br data-reactid="1109"/><!-- react-text: 1110 -->Outputs:<!-- /react-text --><br data-reactid="1111"/><code class="sql hljs" data-reactid="1112"><span class="hljs-keyword">select</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span>, <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`phone`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`user_id`</span></code></pre></div><p data-reactid="1113">For grouped joins, specify a function as the second argument for the join query, and use <code>on</code> with <code>orOn</code> or <code>andOn</code> to create joins that are grouped with parentheses.</p><pre class="display" data-reactid="1114"><code class="js hljs" data-reactid="1115">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1116"/><!-- react-text: 1117 -->Outputs:<!-- /react-text --><br data-reactid="1118"/><code class="sql hljs" data-reactid="1119"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre><p data-reactid="1120">For nested join statements, specify a function as first argument of <code>on</code>, <code>orOn</code> or <code>andOn</code></p><pre class="display" data-reactid="1121"><code class="js hljs" data-reactid="1122">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>)
<span class="hljs-keyword">this</span>.orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})
})</code><br data-reactid="1123"/><!-- react-text: 1124 -->Outputs:<!-- /react-text --><br data-reactid="1125"/><code class="sql hljs" data-reactid="1126"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> (<span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span>)</code></pre><p data-reactid="1127">It is also possible to use an object to represent the join syntax.</p><pre class="display" data-reactid="1128"><code class="js hljs" data-reactid="1129">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, {<span class="hljs-string">'accounts.id'</span>: <span class="hljs-string">'users.account_id'</span>})</code><br data-reactid="1130"/><!-- react-text: 1131 -->Outputs:<!-- /react-text --><br data-reactid="1132"/><code class="sql hljs" data-reactid="1133"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span></code></pre><p data-reactid="1134">If you need to use a literal value (string, number, or boolean) in a join instead of a column, use <code>knex.raw</code>.</p><pre class="display" data-reactid="1135"><code class="js hljs" data-reactid="1136">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'accounts.type'</span>, knex.raw(<span class="hljs-string">'?'</span>, [<span class="hljs-string">'admin'</span>]))</code><br data-reactid="1137"/><!-- react-text: 1138 -->Outputs:<!-- /react-text --><br data-reactid="1139"/><code class="sql hljs" data-reactid="1140"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`type`</span> = <span class="hljs-string">'admin'</span></code></pre><div id="Builder-innerJoin" data-reactid="1141"><b data-reactid="1142">innerJoin</b><!-- react-text: 1143 --><!-- /react-text --><code data-reactid="1144">.innerJoin(table, ~mixed~)</code><pre class="display" data-reactid="1145"><code class="js hljs" data-reactid="1146">knex.from(<span class="hljs-string">'users'</span>).innerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1147"/><!-- react-text: 1148 -->Outputs:<!-- /react-text --><br data-reactid="1149"/><code class="sql hljs" data-reactid="1150"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1151"><code class="js hljs" data-reactid="1152">knex.table(<span class="hljs-string">'users'</span>).innerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1153"/><!-- react-text: 1154 -->Outputs:<!-- /react-text --><br data-reactid="1155"/><code class="sql hljs" data-reactid="1156"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1157"><code class="js hljs" data-reactid="1158">knex(<span class="hljs-string">'users'</span>).innerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1159"/><!-- react-text: 1160 -->Outputs:<!-- /react-text --><br data-reactid="1161"/><code class="sql hljs" data-reactid="1162"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-leftJoin" data-reactid="1163"><b data-reactid="1164">leftJoin</b><!-- react-text: 1165 --><!-- /react-text --><code data-reactid="1166">.leftJoin(table, ~mixed~)</code><pre class="display" data-reactid="1167"><code class="js hljs" data-reactid="1168">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1169"/><!-- react-text: 1170 -->Outputs:<!-- /react-text --><br data-reactid="1171"/><code class="sql hljs" data-reactid="1172"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1173"><code class="js hljs" data-reactid="1174">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1175"/><!-- react-text: 1176 -->Outputs:<!-- /react-text --><br data-reactid="1177"/><code class="sql hljs" data-reactid="1178"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-leftOuterJoin" data-reactid="1179"><b data-reactid="1180">leftOuterJoin</b><!-- react-text: 1181 --><!-- /react-text --><code data-reactid="1182">.leftOuterJoin(table, ~mixed~)</code><pre class="display" data-reactid="1183"><code class="js hljs" data-reactid="1184">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1185"/><!-- react-text: 1186 -->Outputs:<!-- /react-text --><br data-reactid="1187"/><code class="sql hljs" data-reactid="1188"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1189"><code class="js hljs" data-reactid="1190">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).leftOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1191"/><!-- react-text: 1192 -->Outputs:<!-- /react-text --><br data-reactid="1193"/><code class="sql hljs" data-reactid="1194"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">left</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-rightJoin" data-reactid="1195"><b data-reactid="1196">rightJoin</b><!-- react-text: 1197 --><!-- /react-text --><code data-reactid="1198">.rightJoin(table, ~mixed~)</code><pre class="display" data-reactid="1199"><code class="js hljs" data-reactid="1200">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1201"/><!-- react-text: 1202 -->Outputs:<!-- /react-text --><br data-reactid="1203"/><code class="sql hljs" data-reactid="1204"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1205"><code class="js hljs" data-reactid="1206">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1207"/><!-- react-text: 1208 -->Outputs:<!-- /react-text --><br data-reactid="1209"/><code class="sql hljs" data-reactid="1210"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-rightOuterJoin" data-reactid="1211"><b data-reactid="1212">rightOuterJoin</b><!-- react-text: 1213 --><!-- /react-text --><code data-reactid="1214">.rightOuterJoin(table, ~mixed~)</code><pre class="display" data-reactid="1215"><code class="js hljs" data-reactid="1216">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1217"/><!-- react-text: 1218 -->Outputs:<!-- /react-text --><br data-reactid="1219"/><code class="sql hljs" data-reactid="1220"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1221"><code class="js hljs" data-reactid="1222">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).rightOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1223"/><!-- react-text: 1224 -->Outputs:<!-- /react-text --><br data-reactid="1225"/><code class="sql hljs" data-reactid="1226"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">right</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-outerJoin" data-reactid="1227"><b data-reactid="1228">outerJoin</b><!-- react-text: 1229 --><!-- /react-text --><code data-reactid="1230">.outerJoin(table, ~mixed~)</code><pre class="display" data-reactid="1231"><code class="js hljs" data-reactid="1232">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).outerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1233"/><!-- react-text: 1234 -->Outputs:<!-- /react-text --><br data-reactid="1235"/><code class="sql hljs" data-reactid="1236"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1237"><code class="js hljs" data-reactid="1238">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).outerJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1239"/><!-- react-text: 1240 -->Outputs:<!-- /react-text --><br data-reactid="1241"/><code class="sql hljs" data-reactid="1242"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-fullOuterJoin" data-reactid="1243"><b data-reactid="1244">fullOuterJoin</b><!-- react-text: 1245 --><!-- /react-text --><code data-reactid="1246">.fullOuterJoin(table, ~mixed~)</code><pre class="display" data-reactid="1247"><code class="js hljs" data-reactid="1248">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).fullOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1249"/><!-- react-text: 1250 -->Outputs:<!-- /react-text --><br data-reactid="1251"/><code class="sql hljs" data-reactid="1252"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1253"><code class="js hljs" data-reactid="1254">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).fullOuterJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1255"/><!-- react-text: 1256 -->Outputs:<!-- /react-text --><br data-reactid="1257"/><code class="sql hljs" data-reactid="1258"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">outer</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-crossJoin" data-reactid="1259"><b data-reactid="1260">crossJoin</b><!-- react-text: 1261 --><!-- /react-text --><code data-reactid="1262">.crossJoin(table, ~mixed~)</code><p data-reactid="1263">Cross join conditions are only supported in MySQL and SQLite3. For join conditions rather use innerJoin.</p><pre class="display" data-reactid="1264"><code class="js hljs" data-reactid="1265">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).crossJoin(<span class="hljs-string">'accounts'</span>)</code><br data-reactid="1266"/><!-- react-text: 1267 -->Outputs:<!-- /react-text --><br data-reactid="1268"/><code class="sql hljs" data-reactid="1269"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">cross</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span></code></pre><pre class="display" data-reactid="1270"><code class="js hljs" data-reactid="1271">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).crossJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-string">'users.id'</span>, <span class="hljs-string">'accounts.user_id'</span>)</code><br data-reactid="1272"/><!-- react-text: 1273 -->Outputs:<!-- /react-text --><br data-reactid="1274"/><code class="sql hljs" data-reactid="1275"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">cross</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`user_id`</span></code></pre><pre class="display" data-reactid="1276"><code class="js hljs" data-reactid="1277">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).crossJoin(<span class="hljs-string">'accounts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'accounts.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.account_id'</span>).orOn(<span class="hljs-string">'accounts.owner_id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'users.id'</span>)
})</code><br data-reactid="1278"/><!-- react-text: 1279 -->Outputs:<!-- /react-text --><br data-reactid="1280"/><code class="sql hljs" data-reactid="1281"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">cross</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`account_id`</span> <span class="hljs-keyword">or</span> <span class="hljs-string">`accounts`</span>.<span class="hljs-string">`owner_id`</span> = <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span></code></pre></div><div id="Builder-joinRaw" data-reactid="1282"><b data-reactid="1283">joinRaw</b><!-- react-text: 1284 --><!-- /react-text --><code data-reactid="1285">.joinRaw(sql, [bindings])</code><pre class="display" data-reactid="1286"><code class="js hljs" data-reactid="1287">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).joinRaw(<span class="hljs-string">'natural full join table1'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="1288"/><!-- react-text: 1289 -->Outputs:<!-- /react-text --><br data-reactid="1290"/><code class="sql hljs" data-reactid="1291"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">natural</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">join</span> table1 <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre><pre class="display" data-reactid="1292"><code class="js hljs" data-reactid="1293">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).join(knex.raw(<span class="hljs-string">'natural full join table1'</span>)).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>)</code><br data-reactid="1294"/><!-- react-text: 1295 -->Outputs:<!-- /react-text --><br data-reactid="1296"/><code class="sql hljs" data-reactid="1297"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-keyword">natural</span> <span class="hljs-keyword">full</span> <span class="hljs-keyword">join</span> table1 <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span></code></pre></div><h3 id="Builder-on" data-reactid="1298">OnClauses</h3><div id="Builder-onIn" data-reactid="1299"><b data-reactid="1300">onIn</b><!-- react-text: 1301 --><!-- /react-text --><code data-reactid="1302">.onIn(column, values)</code><p data-reactid="1303">Adds a onIn clause to the query.</p><pre class="display" data-reactid="1304"><code class="js hljs" data-reactid="1305">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onIn(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>])
})</code><br data-reactid="1306"/><!-- react-text: 1307 -->Outputs:<!-- /react-text --><br data-reactid="1308"/><code class="sql hljs" data-reactid="1309"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>)</code></pre></div><div id="Builder-onNotIn" data-reactid="1310"><b data-reactid="1311">onNotIn</b><!-- react-text: 1312 --><!-- /react-text --><code data-reactid="1313">.onNotIn(column, values)</code><p data-reactid="1314">Adds a onNotIn clause to the query.</p><pre class="display" data-reactid="1315"><code class="js hljs" data-reactid="1316">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotIn(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>])
})</code><br data-reactid="1317"/><!-- react-text: 1318 -->Outputs:<!-- /react-text --><br data-reactid="1319"/><code class="sql hljs" data-reactid="1320"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">7</span>, <span class="hljs-number">15</span>, <span class="hljs-number">23</span>, <span class="hljs-number">41</span>)</code></pre></div><div id="Builder-onNull" data-reactid="1321"><b data-reactid="1322">onNull</b><!-- react-text: 1323 --><!-- /react-text --><code data-reactid="1324">.onNull(column)</code><p data-reactid="1325">Adds a onNull clause to the query.</p><pre class="display" data-reactid="1326"><code class="js hljs" data-reactid="1327">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNull(<span class="hljs-string">'contacts.email'</span>)
})</code><br data-reactid="1328"/><!-- react-text: 1329 -->Outputs:<!-- /react-text --><br data-reactid="1330"/><code class="sql hljs" data-reactid="1331"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-onNotNull" data-reactid="1332"><b data-reactid="1333">onNotNull</b><!-- react-text: 1334 --><!-- /react-text --><code data-reactid="1335">.onNotNull(column)</code><p data-reactid="1336">Adds a onNotNull clause to the query.</p><pre class="display" data-reactid="1337"><code class="js hljs" data-reactid="1338">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotNull(<span class="hljs-string">'contacts.email'</span>)
})</code><br data-reactid="1339"/><!-- react-text: 1340 -->Outputs:<!-- /react-text --><br data-reactid="1341"/><code class="sql hljs" data-reactid="1342"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-onExists" data-reactid="1343"><b data-reactid="1344">onExists</b><!-- react-text: 1345 --><!-- /react-text --><code data-reactid="1346">.onExists(builder | callback)</code><p data-reactid="1347">Adds a onExists clause to the query.</p><pre class="display" data-reactid="1348"><code class="js hljs" data-reactid="1349">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})
})</code><br data-reactid="1350"/><!-- react-text: 1351 -->Outputs:<!-- /react-text --><br data-reactid="1352"/><code class="sql hljs" data-reactid="1353"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-onNotExists" data-reactid="1354"><b data-reactid="1355">onNotExists</b><!-- react-text: 1356 --><!-- /react-text --><code data-reactid="1357">.onNotExists(builder | callback)</code><p data-reactid="1358">Adds a onNotExists clause to the query.</p><pre class="display" data-reactid="1359"><code class="js hljs" data-reactid="1360">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})
})</code><br data-reactid="1361"/><!-- react-text: 1362 -->Outputs:<!-- /react-text --><br data-reactid="1363"/><code class="sql hljs" data-reactid="1364"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-onBetween" data-reactid="1365"><b data-reactid="1366">onBetween</b><!-- react-text: 1367 --><!-- /react-text --><code data-reactid="1368">.onBetween(column, range)</code><p data-reactid="1369">Adds a onBetween clause to the query.</p><pre class="display" data-reactid="1370"><code class="js hljs" data-reactid="1371">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onBetween(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">30</span>])
})</code><br data-reactid="1372"/><!-- react-text: 1373 -->Outputs:<!-- /react-text --><br data-reactid="1374"/><code class="sql hljs" data-reactid="1375"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">30</span></code></pre></div><div id="Builder-onNotBetween" data-reactid="1376"><b data-reactid="1377">onNotBetween</b><!-- react-text: 1378 --><!-- /react-text --><code data-reactid="1379">.onNotBetween(column, range)</code><p data-reactid="1380">Adds a onNotBetween clause to the query.</p><pre class="display" data-reactid="1381"><code class="js hljs" data-reactid="1382">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).join(<span class="hljs-string">'contacts'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'users.id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-string">'contacts.id'</span>).onNotBetween(<span class="hljs-string">'contacts.id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">30</span>])
})</code><br data-reactid="1383"/><!-- react-text: 1384 -->Outputs:<!-- /react-text --><br data-reactid="1385"/><code class="sql hljs" data-reactid="1386"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">inner</span> <span class="hljs-keyword">join</span> <span class="hljs-string">`contacts`</span> <span class="hljs-keyword">on</span> <span class="hljs-string">`users`</span>.<span class="hljs-string">`id`</span> = <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`contacts`</span>.<span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">30</span></code></pre></div><h3 id="Builder-clear" data-reactid="1387">ClearClauses</h3><div id="Builder-clearSelect" data-reactid="1388"><b data-reactid="1389">clearSelect</b><!-- react-text: 1390 --><!-- /react-text --><code data-reactid="1391">.clearSelect()</code><p data-reactid="1392">Clears all select clauses from the query, excluding subqueries.</p><pre class="display" data-reactid="1393"><code class="js hljs" data-reactid="1394">knex.select(<span class="hljs-string">'email'</span>, <span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).clearSelect()</code><br data-reactid="1395"/><!-- react-text: 1396 -->Outputs:<!-- /react-text --><br data-reactid="1397"/><code class="sql hljs" data-reactid="1398"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-clearWhere" data-reactid="1399"><b data-reactid="1400">clearWhere</b><!-- react-text: 1401 --><!-- /react-text --><code data-reactid="1402">.clearWhere()</code><p data-reactid="1403">Clears all where clauses from the query, excluding subqueries.</p><pre class="display" data-reactid="1404"><code class="js hljs" data-reactid="1405">knex.select(<span class="hljs-string">'email'</span>, <span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).clearWhere()</code><br data-reactid="1406"/><!-- react-text: 1407 -->Outputs:<!-- /react-text --><br data-reactid="1408"/><code class="sql hljs" data-reactid="1409"><span class="hljs-keyword">select</span> <span class="hljs-string">`email`</span>, <span class="hljs-string">`name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-distinct" data-reactid="1410"><b data-reactid="1411">distinct</b><!-- react-text: 1412 --><!-- /react-text --><code data-reactid="1413">.distinct()</code><p data-reactid="1414">Sets a distinct clause on the query.</p><pre class="display" data-reactid="1415"><code class="js hljs" data-reactid="1416"><span class="hljs-comment">// select distinct 'first_name' from customers</span>
knex(<span class="hljs-string">'customers'</span>)
.distinct(<span class="hljs-string">'first_name'</span>, <span class="hljs-string">'last_name'</span>)
.select()</code><br data-reactid="1417"/><!-- react-text: 1418 -->Outputs:<!-- /react-text --><br data-reactid="1419"/><code class="sql hljs" data-reactid="1420"><span class="hljs-keyword">select</span> <span class="hljs-keyword">distinct</span> <span class="hljs-string">`first_name`</span>, <span class="hljs-string">`last_name`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`customers`</span></code></pre></div><div id="Builder-groupBy" data-reactid="1421"><b data-reactid="1422">groupBy</b><!-- react-text: 1423 --><!-- /react-text --><code data-reactid="1424">.groupBy(*names)</code><p data-reactid="1425">Adds a group by clause to the query.</p><pre class="display" data-reactid="1426"><code class="js hljs" data-reactid="1427">knex(<span class="hljs-string">'users'</span>).groupBy(<span class="hljs-string">'count'</span>)</code><br data-reactid="1428"/><!-- react-text: 1429 -->Outputs:<!-- /react-text --><br data-reactid="1430"/><code class="sql hljs" data-reactid="1431"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`count`</span></code></pre></div><div id="Builder-groupByRaw" data-reactid="1432"><b data-reactid="1433">groupByRaw</b><!-- react-text: 1434 --><!-- /react-text --><code data-reactid="1435">.groupByRaw(sql)</code><p data-reactid="1436">Adds a raw group by clause to the query.</p><pre class="display" data-reactid="1437"><code class="js hljs" data-reactid="1438">knex.select(<span class="hljs-string">'year'</span>, knex.raw(<span class="hljs-string">'SUM(profit)'</span>)).from(<span class="hljs-string">'sales'</span>).groupByRaw(<span class="hljs-string">'year WITH ROLLUP'</span>)</code><br data-reactid="1439"/><!-- react-text: 1440 -->Outputs:<!-- /react-text --><br data-reactid="1441"/><code class="sql hljs" data-reactid="1442"><span class="hljs-keyword">select</span> <span class="hljs-string">`year`</span>, <span class="hljs-keyword">SUM</span>(profit) <span class="hljs-keyword">from</span> <span class="hljs-string">`sales`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">year</span> <span class="hljs-keyword">WITH</span> <span class="hljs-keyword">ROLLUP</span></code></pre></div><div id="Builder-orderBy" data-reactid="1443"><b data-reactid="1444">orderBy</b><!-- react-text: 1445 --><!-- /react-text --><code data-reactid="1446">.orderBy(column, [direction])</code><p data-reactid="1447">Adds an order by clause to the query.</p><pre class="display" data-reactid="1448"><code class="js hljs" data-reactid="1449">knex(<span class="hljs-string">'users'</span>).orderBy(<span class="hljs-string">'name'</span>, <span class="hljs-string">'desc'</span>)</code><br data-reactid="1450"/><!-- react-text: 1451 -->Outputs:<!-- /react-text --><br data-reactid="1452"/><code class="sql hljs" data-reactid="1453"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">desc</span></code></pre></div><div id="Builder-orderByRaw" data-reactid="1454"><b data-reactid="1455">orderByRaw</b><!-- react-text: 1456 --><!-- /react-text --><code data-reactid="1457">.orderByRaw(sql)</code><p data-reactid="1458">Adds an order by raw clause to the query.</p><pre class="display" data-reactid="1459"><code class="js hljs" data-reactid="1460">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'table'</span>).orderByRaw(<span class="hljs-string">'col DESC NULLS LAST'</span>)</code><br data-reactid="1461"/><!-- react-text: 1462 -->Outputs:<!-- /react-text --><br data-reactid="1463"/><code class="sql hljs" data-reactid="1464"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`table`</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">col</span> <span class="hljs-keyword">DESC</span> <span class="hljs-keyword">NULLS</span> <span class="hljs-keyword">LAST</span></code></pre></div><h3 id="Builder-havings" data-reactid="1465">Having Clauses</h3><div id="Builder-having" data-reactid="1466"><b data-reactid="1467">having</b><!-- react-text: 1468 --><!-- /react-text --><code data-reactid="1469">.having(column, operator, value)</code><p data-reactid="1470">Adds a having clause to the query.</p><pre class="display" data-reactid="1471"><code class="js hljs" data-reactid="1472">knex(<span class="hljs-string">'users'</span>)
.groupBy(<span class="hljs-string">'count'</span>)
.orderBy(<span class="hljs-string">'name'</span>, <span class="hljs-string">'desc'</span>)
.having(<span class="hljs-string">'count'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">100</span>)</code><br data-reactid="1473"/><!-- react-text: 1474 -->Outputs:<!-- /react-text --><br data-reactid="1475"/><code class="sql hljs" data-reactid="1476"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`count`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`count`</span> &gt; <span class="hljs-number">100</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">desc</span></code></pre></div><div id="Builder-havingIn" data-reactid="1477"><b data-reactid="1478">havingIn</b><!-- react-text: 1479 --><!-- /react-text --><code data-reactid="1480">.havingIn(column, values)</code><p data-reactid="1481">Adds a havingIn clause to the query.</p><pre class="display" data-reactid="1482"><code class="js hljs" data-reactid="1483">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>])</code><br data-reactid="1484"/><!-- react-text: 1485 -->Outputs:<!-- /react-text --><br data-reactid="1486"/><code class="sql hljs" data-reactid="1487"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>)</code></pre></div><div id="Builder-havingNotIn" data-reactid="1488"><b data-reactid="1489">havingNotIn</b><!-- react-text: 1490 --><!-- /react-text --><code data-reactid="1491">.havingNotIn(column, values)</code><p data-reactid="1492">Adds a havingNotIn clause to the query.</p><pre class="display" data-reactid="1493"><code class="js hljs" data-reactid="1494">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotIn(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>])</code><br data-reactid="1495"/><!-- react-text: 1496 -->Outputs:<!-- /react-text --><br data-reactid="1497"/><code class="sql hljs" data-reactid="1498"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">17</span>)</code></pre></div><div id="Builder-havingNull" data-reactid="1499"><b data-reactid="1500">havingNull</b><!-- react-text: 1501 --><!-- /react-text --><code data-reactid="1502">.havingNull(column)</code><p data-reactid="1503">Adds a havingNull clause to the query.</p><pre class="display" data-reactid="1504"><code class="js hljs" data-reactid="1505">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNull(<span class="hljs-string">'email'</span>)</code><br data-reactid="1506"/><!-- react-text: 1507 -->Outputs:<!-- /react-text --><br data-reactid="1508"/><code class="sql hljs" data-reactid="1509"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-havingNotNull" data-reactid="1510"><b data-reactid="1511">havingNotNull</b><!-- react-text: 1512 --><!-- /react-text --><code data-reactid="1513">.havingNotNull(column)</code><p data-reactid="1514">Adds a havingNotNull clause to the query.</p><pre class="display" data-reactid="1515"><code class="js hljs" data-reactid="1516">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotNull(<span class="hljs-string">'email'</span>)</code><br data-reactid="1517"/><!-- react-text: 1518 -->Outputs:<!-- /react-text --><br data-reactid="1519"/><code class="sql hljs" data-reactid="1520"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`email`</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-havingExists" data-reactid="1521"><b data-reactid="1522">havingExists</b><!-- react-text: 1523 --><!-- /react-text --><code data-reactid="1524">.havingExists(builder | callback)</code><p data-reactid="1525">Adds a havingExists clause to the query.</p><pre class="display" data-reactid="1526"><code class="js hljs" data-reactid="1527">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="1528"/><!-- react-text: 1529 -->Outputs:<!-- /react-text --><br data-reactid="1530"/><code class="sql hljs" data-reactid="1531"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-havingNotExists" data-reactid="1532"><b data-reactid="1533">havingNotExists</b><!-- react-text: 1534 --><!-- /react-text --><code data-reactid="1535">.havingNotExists(builder | callback)</code><p data-reactid="1536">Adds a havingNotExists clause to the query.</p><pre class="display" data-reactid="1537"><code class="js hljs" data-reactid="1538">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotExists(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'accounts'</span>).whereRaw(<span class="hljs-string">'users.account_id = accounts.id'</span>);
})</code><br data-reactid="1539"/><!-- react-text: 1540 -->Outputs:<!-- /react-text --><br data-reactid="1541"/><code class="sql hljs" data-reactid="1542"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> (<span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> users.account_id = accounts.id)</code></pre></div><div id="Builder-havingBetween" data-reactid="1543"><b data-reactid="1544">havingBetween</b><!-- react-text: 1545 --><!-- /react-text --><code data-reactid="1546">.havingBetween(column, range)</code><p data-reactid="1547">Adds a havingBetween clause to the query.</p><pre class="display" data-reactid="1548"><code class="js hljs" data-reactid="1549">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingBetween(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">10</span>])</code><br data-reactid="1550"/><!-- react-text: 1551 -->Outputs:<!-- /react-text --><br data-reactid="1552"/><code class="sql hljs" data-reactid="1553"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">10</span></code></pre></div><div id="Builder-havingNotBetween" data-reactid="1554"><b data-reactid="1555">havingNotBetween</b><!-- react-text: 1556 --><!-- /react-text --><code data-reactid="1557">.havingNotBetween(column, range)</code><p data-reactid="1558">Adds a havingNotBetween clause to the query.</p><pre class="display" data-reactid="1559"><code class="js hljs" data-reactid="1560">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).havingNotBetween(<span class="hljs-string">'id'</span>, [<span class="hljs-number">5</span>, <span class="hljs-number">10</span>])</code><br data-reactid="1561"/><!-- react-text: 1562 -->Outputs:<!-- /react-text --><br data-reactid="1563"/><code class="sql hljs" data-reactid="1564"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">having</span> <span class="hljs-string">`id`</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">between</span> <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> <span class="hljs-number">10</span></code></pre></div><div id="Builder-havingRaw" data-reactid="1565"><b data-reactid="1566">havingRaw</b><!-- react-text: 1567 --><!-- /react-text --><code data-reactid="1568">.havingRaw(column, operator, value)</code><p data-reactid="1569">Adds a havingRaw clause to the query.</p><pre class="display" data-reactid="1570"><code class="js hljs" data-reactid="1571">knex(<span class="hljs-string">'users'</span>)
.groupBy(<span class="hljs-string">'count'</span>)
.orderBy(<span class="hljs-string">'name'</span>, <span class="hljs-string">'desc'</span>)
.havingRaw(<span class="hljs-string">'count &gt; ?'</span>, [<span class="hljs-number">100</span>])</code><br data-reactid="1572"/><!-- react-text: 1573 -->Outputs:<!-- /react-text --><br data-reactid="1574"/><code class="sql hljs" data-reactid="1575"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`count`</span> <span class="hljs-keyword">having</span> <span class="hljs-keyword">count</span> &gt; <span class="hljs-number">100</span> <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`name`</span> <span class="hljs-keyword">desc</span></code></pre></div><div id="Builder-offset" data-reactid="1576"><b data-reactid="1577">offset</b><!-- react-text: 1578 --><!-- /react-text --><code data-reactid="1579">.offset(value)</code><p data-reactid="1580">Adds an offset clause to the query.</p><pre class="display" data-reactid="1581"><code class="js hljs" data-reactid="1582">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).offset(<span class="hljs-number">10</span>)</code><br data-reactid="1583"/><!-- react-text: 1584 -->Outputs:<!-- /react-text --><br data-reactid="1585"/><code class="sql hljs" data-reactid="1586"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">limit</span> <span class="hljs-number">18446744073709551615</span> <span class="hljs-keyword">offset</span> <span class="hljs-number">10</span></code></pre></div><div id="Builder-limit" data-reactid="1587"><b data-reactid="1588">limit</b><!-- react-text: 1589 --><!-- /react-text --><code data-reactid="1590">.limit(value)</code><p data-reactid="1591">Adds a limit clause to the query.</p><pre class="display" data-reactid="1592"><code class="js hljs" data-reactid="1593">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).limit(<span class="hljs-number">10</span>).offset(<span class="hljs-number">30</span>)</code><br data-reactid="1594"/><!-- react-text: 1595 -->Outputs:<!-- /react-text --><br data-reactid="1596"/><code class="sql hljs" data-reactid="1597"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">limit</span> <span class="hljs-number">10</span> <span class="hljs-keyword">offset</span> <span class="hljs-number">30</span></code></pre></div><div id="Builder-union" data-reactid="1598"><b data-reactid="1599">union</b><!-- react-text: 1600 --><!-- /react-text --><code data-reactid="1601">.union([*queries], [wrap])</code><p data-reactid="1602">Creates a union query, taking an array or a list of callbacks to build the union statement, with optional boolean wrap. The queries will be individually wrapped in parentheses with a true wrap parameter.</p><pre class="display" data-reactid="1603"><code class="js hljs" data-reactid="1604">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'last_name'</span>).union(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'first_name'</span>);
})</code><br data-reactid="1605"/><!-- react-text: 1606 -->Outputs:<!-- /react-text --><br data-reactid="1607"/><code class="sql hljs" data-reactid="1608"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`last_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">union</span> <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`first_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-unionAll" data-reactid="1609"><b data-reactid="1610">unionAll</b><!-- react-text: 1611 --><!-- /react-text --><code data-reactid="1612">.unionAll(query)</code><p data-reactid="1613">Creates a union all query, with the same method signature as the union method.</p><pre class="display" data-reactid="1614"><code class="js hljs" data-reactid="1615">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'last_name'</span>).unionAll(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereNull(<span class="hljs-string">'first_name'</span>);
})</code><br data-reactid="1616"/><!-- react-text: 1617 -->Outputs:<!-- /react-text --><br data-reactid="1618"/><code class="sql hljs" data-reactid="1619"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`last_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">union</span> all <span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`first_name`</span> <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span></code></pre></div><div id="Builder-insert" data-reactid="1620"><b data-reactid="1621">insert</b><!-- react-text: 1622 --><!-- /react-text --><code data-reactid="1623">.insert(data, [returning])</code><p data-reactid="1624">Creates an insert query, taking either a hash of properties to be inserted into the row, or an array of inserts, to be executed as a single insert command. Resolves the promise / fulfills the callback with an array containing the first insert id of the inserted model, or an array containing all inserted ids for postgresql.</p><pre class="display" data-reactid="1625"><code class="js hljs" data-reactid="1626"><span class="hljs-comment">// Returns [1] in "mysql", "sqlite", "oracle"; [] in "postgresql" unless the 'returning' parameter is set.</span>
knex(<span class="hljs-string">'books'</span>).insert({<span class="hljs-attr">title</span>: <span class="hljs-string">'Slaughterhouse Five'</span>})</code><br data-reactid="1627"/><!-- react-text: 1628 -->Outputs:<!-- /react-text --><br data-reactid="1629"/><code class="sql hljs" data-reactid="1630"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Slaughterhouse Five'</span>)</code></pre><pre class="display" data-reactid="1631"><code class="js hljs" data-reactid="1632"><span class="hljs-comment">// Normalizes for empty keys on multi-row insert:</span>
knex(<span class="hljs-string">'coords'</span>).insert([{<span class="hljs-attr">x</span>: <span class="hljs-number">20</span>}, {<span class="hljs-attr">y</span>: <span class="hljs-number">30</span>}, {<span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span>}])</code><br data-reactid="1633"/><!-- react-text: 1634 -->Outputs:<!-- /react-text --><br data-reactid="1635"/><code class="sql hljs" data-reactid="1636"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`coords`</span> (<span class="hljs-string">`x`</span>, <span class="hljs-string">`y`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-number">20</span>, <span class="hljs-keyword">DEFAULT</span>), (<span class="hljs-keyword">DEFAULT</span>, <span class="hljs-number">30</span>), (<span class="hljs-number">10</span>, <span class="hljs-number">20</span>)</code></pre><pre class="display" data-reactid="1637"><code class="js hljs" data-reactid="1638"><span class="hljs-comment">// Returns [2] in "mysql", "sqlite"; [2, 3] in "postgresql"</span>
knex.insert([{<span class="hljs-attr">title</span>: <span class="hljs-string">'Great Gatsby'</span>}, {<span class="hljs-attr">title</span>: <span class="hljs-string">'Fahrenheit 451'</span>}], <span class="hljs-string">'id'</span>).into(<span class="hljs-string">'books'</span>)</code><br data-reactid="1639"/><!-- react-text: 1640 -->Outputs:<!-- /react-text --><br data-reactid="1641"/><code class="sql hljs" data-reactid="1642"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Great Gatsby'</span>), (<span class="hljs-string">'Fahrenheit 451'</span>)</code></pre></div><p data-reactid="1643">If one prefers that undefined keys are replaced with <code>NULL</code> instead of <code>DEFAULT</code> one may give <code>useNullAsDefault</code> configuration parameter in knex config.</p><pre data-reactid="1644"><code class="hljs js" data-reactid="1645"><span class="hljs-keyword">var</span> knex = <span class="hljs-built_in">require</span>(<span class="hljs-string">'knex'</span>)({
<span class="hljs-attr">client</span>: <span class="hljs-string">'mysql'</span>,
<span class="hljs-attr">connection</span>: {
<span class="hljs-attr">host</span> : <span class="hljs-string">'127.0.0.1'</span>,
<span class="hljs-attr">user</span> : <span class="hljs-string">'your_database_user'</span>,
<span class="hljs-attr">password</span> : <span class="hljs-string">'your_database_password'</span>,
<span class="hljs-attr">database</span> : <span class="hljs-string">'myapp_test'</span>
},
<span class="hljs-attr">useNullAsDefault</span>: <span class="hljs-literal">true</span>
});
knex(<span class="hljs-string">'coords'</span>).insert([{<span class="hljs-attr">x</span>: <span class="hljs-number">20</span>}, {<span class="hljs-attr">y</span>: <span class="hljs-number">30</span>}, {<span class="hljs-attr">x</span>: <span class="hljs-number">10</span>, <span class="hljs-attr">y</span>: <span class="hljs-number">20</span>}])
<span class="hljs-comment">// insert into `coords` (`x`, `y`) values (20, NULL), (NULL, 30), (10, 20)"</span></code></pre><div id="Builder-returning" data-reactid="1646"><b data-reactid="1647">returning</b><!-- react-text: 1648 --><!-- /react-text --><code data-reactid="1649">.returning(column) / .returning([column1, column2, ...])</code><p data-reactid="1650">Utilized by PostgreSQL, MSSQL, and Oracle databases, the returning method specifies which column should be returned by the insert and update methods. Passed column parameter may be a string or an array of strings. When passed in a string, makes the SQL result be reported as an array of values from the specified column. When passed in an array of strings, makes the SQL result be reported as an array of objects, each containing a single property for each of the specified columns.</p><pre class="display" data-reactid="1651"><code class="js hljs" data-reactid="1652"><span class="hljs-comment">// Returns [1]</span>
knex(<span class="hljs-string">'books'</span>)
.returning(<span class="hljs-string">'id'</span>)
.insert({<span class="hljs-attr">title</span>: <span class="hljs-string">'Slaughterhouse Five'</span>})</code><br data-reactid="1653"/><!-- react-text: 1654 -->Outputs:<!-- /react-text --><br data-reactid="1655"/><code class="sql hljs" data-reactid="1656"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Slaughterhouse Five'</span>)</code></pre><pre class="display" data-reactid="1657"><code class="js hljs" data-reactid="1658"><span class="hljs-comment">// Returns [2] in "mysql", "sqlite"; [2, 3] in "postgresql"</span>
knex(<span class="hljs-string">'books'</span>)
.returning(<span class="hljs-string">'id'</span>)
.insert([{<span class="hljs-attr">title</span>: <span class="hljs-string">'Great Gatsby'</span>}, {<span class="hljs-attr">title</span>: <span class="hljs-string">'Fahrenheit 451'</span>}])</code><br data-reactid="1659"/><!-- react-text: 1660 -->Outputs:<!-- /react-text --><br data-reactid="1661"/><code class="sql hljs" data-reactid="1662"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Great Gatsby'</span>), (<span class="hljs-string">'Fahrenheit 451'</span>)</code></pre><pre class="display" data-reactid="1663"><code class="js hljs" data-reactid="1664"><span class="hljs-comment">// Returns [ { id: 1, title: 'Slaughterhouse Five' } ]</span>
knex(<span class="hljs-string">'books'</span>)
.returning([<span class="hljs-string">'id'</span>,<span class="hljs-string">'title'</span>])
.insert({<span class="hljs-attr">title</span>: <span class="hljs-string">'Slaughterhouse Five'</span>})</code><br data-reactid="1665"/><!-- react-text: 1666 -->Outputs:<!-- /react-text --><br data-reactid="1667"/><code class="sql hljs" data-reactid="1668"><span class="hljs-keyword">insert</span> <span class="hljs-keyword">into</span> <span class="hljs-string">`books`</span> (<span class="hljs-string">`title`</span>) <span class="hljs-keyword">values</span> (<span class="hljs-string">'Slaughterhouse Five'</span>)</code></pre></div><div id="Builder-update" data-reactid="1669"><b data-reactid="1670">update</b><!-- react-text: 1671 --><!-- /react-text --><code data-reactid="1672">.update(data, [returning]) / .update(key, value, [returning])</code><p data-reactid="1673">Creates an update query, taking a hash of properties or a key/value pair to be updated based on the other query constraints. Resolves the promise / fulfills the callback with the number of affected rows for the query. If a key to be updated has value undefined it is ignored.</p><pre class="display" data-reactid="1674"><code class="js hljs" data-reactid="1675">knex(<span class="hljs-string">'books'</span>)
.where(<span class="hljs-string">'published_date'</span>, <span class="hljs-string">'&lt;'</span>, <span class="hljs-number">2000</span>)
.update({
<span class="hljs-attr">status</span>: <span class="hljs-string">'archived'</span>,
<span class="hljs-attr">thisKeyIsSkipped</span>: <span class="hljs-literal">undefined</span>
})</code><br data-reactid="1676"/><!-- react-text: 1677 -->Outputs:<!-- /react-text --><br data-reactid="1678"/><code class="sql hljs" data-reactid="1679"><span class="hljs-keyword">update</span> <span class="hljs-string">`books`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`status`</span> = <span class="hljs-string">'archived'</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`published_date`</span> &lt; <span class="hljs-number">2000</span></code></pre><pre class="display" data-reactid="1680"><code class="js hljs" data-reactid="1681"><span class="hljs-comment">// Returns [1] in "mysql", "sqlite", "oracle"; [] in "postgresql" unless the 'returning' parameter is set.</span>
knex(<span class="hljs-string">'books'</span>).update(<span class="hljs-string">'title'</span>, <span class="hljs-string">'Slaughterhouse Five'</span>)</code><br data-reactid="1682"/><!-- react-text: 1683 -->Outputs:<!-- /react-text --><br data-reactid="1684"/><code class="sql hljs" data-reactid="1685"><span class="hljs-keyword">update</span> <span class="hljs-string">`books`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`title`</span> = <span class="hljs-string">'Slaughterhouse Five'</span></code></pre></div><div id="Builder-del / delete" data-reactid="1686"><b data-reactid="1687">del / delete</b><!-- react-text: 1688 --><!-- /react-text --><code data-reactid="1689">.del()</code><p data-reactid="1690">Aliased to del as delete is a reserved word in JavaScript, this method deletes one or more rows, based on other conditions specified in the query. Resolves the promise / fulfills the callback with the number of affected rows for the query.</p><pre class="display" data-reactid="1691"><code class="js hljs" data-reactid="1692">knex(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'activated'</span>, <span class="hljs-literal">false</span>)
.del()</code><br data-reactid="1693"/><!-- react-text: 1694 -->Outputs:<!-- /react-text --><br data-reactid="1695"/><code class="sql hljs" data-reactid="1696"><span class="hljs-keyword">delete</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`activated`</span> = <span class="hljs-literal">false</span></code></pre></div><div id="Builder-transacting" data-reactid="1697"><b data-reactid="1698">transacting</b><!-- react-text: 1699 --><!-- /react-text --><code data-reactid="1700">.transacting(transactionObj)</code><p data-reactid="1701">Used by knex.transaction, the transacting method may be chained to any query and passed the object you wish to join the query as part of the transaction for.</p><pre data-reactid="1702"><code class="hljs js" data-reactid="1703"><span class="hljs-keyword">var</span> <span class="hljs-built_in">Promise</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bluebird'</span>);
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">trx</span>) </span>{
knex(<span class="hljs-string">'books'</span>).transacting(trx).insert({<span class="hljs-attr">name</span>: <span class="hljs-string">'Old Books'</span>})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) </span>{
<span class="hljs-keyword">var</span> id = resp[<span class="hljs-number">0</span>];
<span class="hljs-keyword">return</span> someExternalMethod(id, trx);
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Transaction complete.'</span>);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>) </span>{
<span class="hljs-built_in">console</span>.error(err);
});</code></pre></div><div id="Builder-forUpdate" data-reactid="1704"><b data-reactid="1705">forUpdate</b><!-- react-text: 1706 --><!-- /react-text --><code data-reactid="1707">.transacting(t).forUpdate()</code><p data-reactid="1708">Dynamically added after a transaction is specified, the forUpdate adds a FOR UPDATE in PostgreSQL and MySQL during a select statement.</p><pre class="display" data-reactid="1709"><code class="js hljs" data-reactid="1710">knex(<span class="hljs-string">'tableName'</span>)
.transacting(trx)
.forUpdate()
.select(<span class="hljs-string">'*'</span>)</code><br data-reactid="1711"/><!-- react-text: 1712 -->Outputs:<!-- /react-text --><br data-reactid="1713"/><code class="sql hljs" data-reactid="1714"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`tableName`</span> <span class="hljs-keyword">for</span> <span class="hljs-keyword">update</span></code></pre></div><div id="Builder-forShare" data-reactid="1715"><b data-reactid="1716">forShare</b><!-- react-text: 1717 --><!-- /react-text --><code data-reactid="1718">.transacting(t).forShare()</code><p data-reactid="1719">Dynamically added after a transaction is specified, the forShare adds a FOR SHARE in PostgreSQL and a LOCK IN SHARE MODE for MySQL during a select statement.</p><pre class="display" data-reactid="1720"><code class="js hljs" data-reactid="1721">knex(<span class="hljs-string">'tableName'</span>)
.transacting(trx)
.forShare()
.select(<span class="hljs-string">'*'</span>)</code><br data-reactid="1722"/><!-- react-text: 1723 -->Outputs:<!-- /react-text --><br data-reactid="1724"/><code class="sql hljs" data-reactid="1725"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`tableName`</span> <span class="hljs-keyword">lock</span> <span class="hljs-keyword">in</span> <span class="hljs-keyword">share</span> <span class="hljs-keyword">mode</span></code></pre></div><div id="Builder-count" data-reactid="1726"><b data-reactid="1727">count</b><!-- react-text: 1728 --><!-- /react-text --><code data-reactid="1729">.count(column|raw)</code><p data-reactid="1730">Performs a count on the specified column. Also accepts raw expressions. Note that in Postgres, count returns a bigint type which will be a String and not a Number (more info).</p><pre class="display" data-reactid="1731"><code class="js hljs" data-reactid="1732">knex(<span class="hljs-string">'users'</span>).count(<span class="hljs-string">'active'</span>)</code><br data-reactid="1733"/><!-- react-text: 1734 -->Outputs:<!-- /react-text --><br data-reactid="1735"/><code class="sql hljs" data-reactid="1736"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(<span class="hljs-string">`active`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1737"><code class="js hljs" data-reactid="1738">knex(<span class="hljs-string">'users'</span>).count(<span class="hljs-string">'active as a'</span>)</code><br data-reactid="1739"/><!-- react-text: 1740 -->Outputs:<!-- /react-text --><br data-reactid="1741"/><code class="sql hljs" data-reactid="1742"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(<span class="hljs-string">`active`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1743"><code class="js hljs" data-reactid="1744">knex(<span class="hljs-string">'users'</span>).count(knex.raw(<span class="hljs-string">'??'</span>, [<span class="hljs-string">'active'</span>]))</code><br data-reactid="1745"/><!-- react-text: 1746 -->Outputs:<!-- /react-text --><br data-reactid="1747"/><code class="sql hljs" data-reactid="1748"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(<span class="hljs-string">`active`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><p data-reactid="1749">Use <strong>countDistinct</strong> to add a distinct expression inside the aggregate function.</p><pre class="display" data-reactid="1750"><code class="js hljs" data-reactid="1751">knex(<span class="hljs-string">'users'</span>).countDistinct(<span class="hljs-string">'active'</span>)</code><br data-reactid="1752"/><!-- react-text: 1753 -->Outputs:<!-- /react-text --><br data-reactid="1754"/><code class="sql hljs" data-reactid="1755"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(<span class="hljs-keyword">distinct</span> <span class="hljs-string">`active`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><div id="Builder-min" data-reactid="1756"><b data-reactid="1757">min</b><!-- react-text: 1758 --><!-- /react-text --><code data-reactid="1759">.min(column|raw)</code><p data-reactid="1760">Gets the minimum value for the specified column. Also accepts raw expressions.</p><pre class="display" data-reactid="1761"><code class="js hljs" data-reactid="1762">knex(<span class="hljs-string">'users'</span>).min(<span class="hljs-string">'age'</span>)</code><br data-reactid="1763"/><!-- react-text: 1764 -->Outputs:<!-- /react-text --><br data-reactid="1765"/><code class="sql hljs" data-reactid="1766"><span class="hljs-keyword">select</span> <span class="hljs-keyword">min</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1767"><code class="js hljs" data-reactid="1768">knex(<span class="hljs-string">'users'</span>).min(<span class="hljs-string">'age as a'</span>)</code><br data-reactid="1769"/><!-- react-text: 1770 -->Outputs:<!-- /react-text --><br data-reactid="1771"/><code class="sql hljs" data-reactid="1772"><span class="hljs-keyword">select</span> <span class="hljs-keyword">min</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1773"><code class="js hljs" data-reactid="1774">knex(<span class="hljs-string">'users'</span>).min(knex.raw(<span class="hljs-string">'??'</span>, [<span class="hljs-string">'age'</span>]))</code><br data-reactid="1775"/><!-- react-text: 1776 -->Outputs:<!-- /react-text --><br data-reactid="1777"/><code class="sql hljs" data-reactid="1778"><span class="hljs-keyword">select</span> <span class="hljs-keyword">min</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-max" data-reactid="1779"><b data-reactid="1780">max</b><!-- react-text: 1781 --><!-- /react-text --><code data-reactid="1782">.max(column|raw)</code><p data-reactid="1783">Gets the maximum value for the specified column. Also accepts raw expressions.</p><pre class="display" data-reactid="1784"><code class="js hljs" data-reactid="1785">knex(<span class="hljs-string">'users'</span>).max(<span class="hljs-string">'age'</span>)</code><br data-reactid="1786"/><!-- react-text: 1787 -->Outputs:<!-- /react-text --><br data-reactid="1788"/><code class="sql hljs" data-reactid="1789"><span class="hljs-keyword">select</span> <span class="hljs-keyword">max</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1790"><code class="js hljs" data-reactid="1791">knex(<span class="hljs-string">'users'</span>).max(<span class="hljs-string">'age as a'</span>)</code><br data-reactid="1792"/><!-- react-text: 1793 -->Outputs:<!-- /react-text --><br data-reactid="1794"/><code class="sql hljs" data-reactid="1795"><span class="hljs-keyword">select</span> <span class="hljs-keyword">max</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1796"><code class="js hljs" data-reactid="1797">knex(<span class="hljs-string">'users'</span>).max(knex.raw(<span class="hljs-string">'??'</span>, [<span class="hljs-string">'age'</span>]))</code><br data-reactid="1798"/><!-- react-text: 1799 -->Outputs:<!-- /react-text --><br data-reactid="1800"/><code class="sql hljs" data-reactid="1801"><span class="hljs-keyword">select</span> <span class="hljs-keyword">max</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Builder-sum" data-reactid="1802"><b data-reactid="1803">sum</b><!-- react-text: 1804 --><!-- /react-text --><code data-reactid="1805">.sum(column|raw)</code><p data-reactid="1806">Retrieve the sum of the values of a given column. Also accepts raw expressions.</p><pre class="display" data-reactid="1807"><code class="js hljs" data-reactid="1808">knex(<span class="hljs-string">'users'</span>).sum(<span class="hljs-string">'products'</span>)</code><br data-reactid="1809"/><!-- react-text: 1810 -->Outputs:<!-- /react-text --><br data-reactid="1811"/><code class="sql hljs" data-reactid="1812"><span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-string">`products`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1813"><code class="js hljs" data-reactid="1814">knex(<span class="hljs-string">'users'</span>).sum(<span class="hljs-string">'products as p'</span>)</code><br data-reactid="1815"/><!-- react-text: 1816 -->Outputs:<!-- /react-text --><br data-reactid="1817"/><code class="sql hljs" data-reactid="1818"><span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-string">`products`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`p`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1819"><code class="js hljs" data-reactid="1820">knex(<span class="hljs-string">'users'</span>).sum(knex.raw(<span class="hljs-string">'??'</span>, [<span class="hljs-string">'products'</span>]))</code><br data-reactid="1821"/><!-- react-text: 1822 -->Outputs:<!-- /react-text --><br data-reactid="1823"/><code class="sql hljs" data-reactid="1824"><span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-string">`products`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><p data-reactid="1825">Use <strong>sumDistinct</strong> to add a distinct expression inside the aggregate function.</p><pre class="display" data-reactid="1826"><code class="js hljs" data-reactid="1827">knex(<span class="hljs-string">'users'</span>).sumDistinct(<span class="hljs-string">'products'</span>)</code><br data-reactid="1828"/><!-- react-text: 1829 -->Outputs:<!-- /react-text --><br data-reactid="1830"/><code class="sql hljs" data-reactid="1831"><span class="hljs-keyword">select</span> <span class="hljs-keyword">sum</span>(<span class="hljs-keyword">distinct</span> <span class="hljs-string">`products`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><div id="Builder-avg" data-reactid="1832"><b data-reactid="1833">avg</b><!-- react-text: 1834 --><!-- /react-text --><code data-reactid="1835">.avg(column|raw)</code><p data-reactid="1836">Retrieve the average of the values of a given column. Also accepts raw expressions.</p><pre class="display" data-reactid="1837"><code class="js hljs" data-reactid="1838">knex(<span class="hljs-string">'users'</span>).avg(<span class="hljs-string">'age'</span>)</code><br data-reactid="1839"/><!-- react-text: 1840 -->Outputs:<!-- /react-text --><br data-reactid="1841"/><code class="sql hljs" data-reactid="1842"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1843"><code class="js hljs" data-reactid="1844">knex(<span class="hljs-string">'users'</span>).avg(<span class="hljs-string">'age as a'</span>)</code><br data-reactid="1845"/><!-- react-text: 1846 -->Outputs:<!-- /react-text --><br data-reactid="1847"/><code class="sql hljs" data-reactid="1848"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">as</span> <span class="hljs-string">`a`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><pre class="display" data-reactid="1849"><code class="js hljs" data-reactid="1850">knex(<span class="hljs-string">'users'</span>).avg(knex.raw(<span class="hljs-string">'??'</span>, [<span class="hljs-string">'age'</span>]))</code><br data-reactid="1851"/><!-- react-text: 1852 -->Outputs:<!-- /react-text --><br data-reactid="1853"/><code class="sql hljs" data-reactid="1854"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre></div><p data-reactid="1855">Use <strong>avgDistinct</strong> to add a distinct expression inside the aggregate function.</p><pre class="display" data-reactid="1856"><code class="js hljs" data-reactid="1857">knex(<span class="hljs-string">'users'</span>).avgDistinct(<span class="hljs-string">'age'</span>)</code><br data-reactid="1858"/><!-- react-text: 1859 -->Outputs:<!-- /react-text --><br data-reactid="1860"/><code class="sql hljs" data-reactid="1861"><span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-keyword">distinct</span> <span class="hljs-string">`age`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span></code></pre><div id="Builder-increment" data-reactid="1862"><b data-reactid="1863">increment</b><!-- react-text: 1864 --><!-- /react-text --><code data-reactid="1865">.increment(column, amount)</code><p data-reactid="1866">Increments a column value by the specified amount.</p><pre class="display" data-reactid="1867"><code class="js hljs" data-reactid="1868">knex(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'userid'</span>, <span class="hljs-string">'='</span>, <span class="hljs-number">1</span>)
.increment(<span class="hljs-string">'balance'</span>, <span class="hljs-number">10</span>)</code><br data-reactid="1869"/><!-- react-text: 1870 -->Outputs:<!-- /react-text --><br data-reactid="1871"/><code class="sql hljs" data-reactid="1872"><span class="hljs-keyword">update</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`balance`</span> = <span class="hljs-string">`balance`</span> + <span class="hljs-number">10</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`userid`</span> = <span class="hljs-number">1</span></code></pre></div><div id="Builder-decrement" data-reactid="1873"><b data-reactid="1874">decrement</b><!-- react-text: 1875 --><!-- /react-text --><code data-reactid="1876">.decrement(column, amount)</code><p data-reactid="1877">Decrements a column value by the specified amount.</p><pre class="display" data-reactid="1878"><code class="js hljs" data-reactid="1879">knex(<span class="hljs-string">'accounts'</span>).where(<span class="hljs-string">'userid'</span>, <span class="hljs-string">'='</span>, <span class="hljs-number">1</span>).decrement(<span class="hljs-string">'balance'</span>, <span class="hljs-number">5</span>)</code><br data-reactid="1880"/><!-- react-text: 1881 -->Outputs:<!-- /react-text --><br data-reactid="1882"/><code class="sql hljs" data-reactid="1883"><span class="hljs-keyword">update</span> <span class="hljs-string">`accounts`</span> <span class="hljs-keyword">set</span> <span class="hljs-string">`balance`</span> = <span class="hljs-string">`balance`</span> - <span class="hljs-number">5</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`userid`</span> = <span class="hljs-number">1</span></code></pre></div><div id="Builder-truncate" data-reactid="1884"><b data-reactid="1885">truncate</b><!-- react-text: 1886 --><!-- /react-text --><code data-reactid="1887">.truncate()</code><p data-reactid="1888">Truncates the current table.</p><pre class="display" data-reactid="1889"><code class="js hljs" data-reactid="1890">knex(<span class="hljs-string">'accounts'</span>).truncate()</code><br data-reactid="1891"/><!-- react-text: 1892 -->Outputs:<!-- /react-text --><br data-reactid="1893"/><code class="sql hljs" data-reactid="1894"><span class="hljs-keyword">truncate</span> <span class="hljs-string">`accounts`</span></code></pre></div><div id="Builder-pluck" data-reactid="1895"><b data-reactid="1896">pluck</b><!-- react-text: 1897 --><!-- /react-text --><code data-reactid="1898">.pluck(id)</code><p data-reactid="1899">This will pluck the specified column from each row in your results, yielding a promise which resolves to the array of values selected.</p><pre data-reactid="1900"><code class="hljs js" data-reactid="1901">knex.table(<span class="hljs-string">'users'</span>).pluck(<span class="hljs-string">'id'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{ <span class="hljs-built_in">console</span>.log(ids); });</code></pre></div><div id="Builder-first" data-reactid="1902"><b data-reactid="1903">first</b><!-- react-text: 1904 --><!-- /react-text --><code data-reactid="1905">.first([columns])</code><p data-reactid="1906">Similar to select, but only retrieves &amp; resolves with the first record from the query.</p><pre data-reactid="1907"><code class="hljs js" data-reactid="1908">knex.table(<span class="hljs-string">'users'</span>).first(<span class="hljs-string">'id'</span>, <span class="hljs-string">'name'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">row</span>) </span>{ <span class="hljs-built_in">console</span>.log(row); });</code></pre></div><div id="Builder-clone" data-reactid="1909"><b data-reactid="1910">clone</b><!-- react-text: 1911 --><!-- /react-text --><code data-reactid="1912">.clone()</code><p data-reactid="1913">Clones the current query chain, useful for re-using partial query snippets in other queries without mutating the original.</p></div><div id="Builder-modify" data-reactid="1914"><b data-reactid="1915">modify</b><!-- react-text: 1916 --><!-- /react-text --><code data-reactid="1917">.modify(fn, *arguments)</code><p data-reactid="1918">Allows encapsulating and re-using query snippets and common behaviors as functions. The callback function should receive the query builder as its first argument, followed by the rest of the (optional) parameters passed to modify.</p><pre data-reactid="1919"><code class="hljs js" data-reactid="1920"><span class="hljs-keyword">var</span> withUserName = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">queryBuilder, foreignKey</span>) </span>{
queryBuilder.leftJoin(<span class="hljs-string">'users'</span>, foreignKey, <span class="hljs-string">'users.id'</span>).select(<span class="hljs-string">'users.user_name'</span>);
};
knex.table(<span class="hljs-string">'articles'</span>).select(<span class="hljs-string">'title'</span>, <span class="hljs-string">'body'</span>).modify(withUserName, <span class="hljs-string">'articles_user.id'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">article</span>) </span>{
<span class="hljs-built_in">console</span>.log(article.user_name);
});</code></pre></div><div id="Builder-columnInfo" data-reactid="1921"><b data-reactid="1922">columnInfo</b><!-- react-text: 1923 --><!-- /react-text --><code data-reactid="1924">.columnInfo([columnName])</code><p data-reactid="1925">Returns an object with the column info about the current table, or an individual column if one is passed, returning an object with the following keys:<em> <strong>defaultValue</strong>: the default value for the column</em> <strong>type</strong>: the column type<em> <strong>maxLength</strong>: the max length set for the column</em> <strong>nullable</strong>: whether the column may be null</p><pre data-reactid="1926"><code class="hljs js" data-reactid="1927">knex(<span class="hljs-string">'users'</span>).columnInfo().then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">info</span>) </span>{ <span class="hljs-comment">// ... });</span></code></pre></div><div id="Builder-debug" data-reactid="1928"><b data-reactid="1929">debug</b><!-- react-text: 1930 --><!-- /react-text --><code data-reactid="1931">.debug([enabled])</code><p data-reactid="1932">Overrides the global debug setting for the current query chain. If enabled is omitted, query debugging will be turned on.</p></div><div id="Builder-connection" data-reactid="1933"><b data-reactid="1934">connection</b><p data-reactid="1935"> <em> <strong>(incomplete)</strong> - This feature was incorrectly documented as functional.</em> <br/>If implemented, the method would set the db connection to use for the query without using the connection pool.</p></div><div id="Builder-options" data-reactid="1936"><b data-reactid="1937">options</b><!-- react-text: 1938 --><!-- /react-text --><code data-reactid="1939">.options()</code><p data-reactid="1940">Allows for mixing in additional options as defined by database client specific libraries:</p><pre data-reactid="1941"><code class="hljs js" data-reactid="1942">knex(<span class="hljs-string">'accounts as a1'</span>)
.leftJoin(<span class="hljs-string">'accounts as a2'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.on(<span class="hljs-string">'a1.email'</span>, <span class="hljs-string">'&lt;&gt;'</span>, <span class="hljs-string">'a2.email'</span>);
})
.select([<span class="hljs-string">'a1.email'</span>, <span class="hljs-string">'a2.email'</span>])
.where(knex.raw(<span class="hljs-string">'a1.id = 1'</span>))
.options({ <span class="hljs-attr">nestTables</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">rowMode</span>: <span class="hljs-string">'array'</span> })
.limit(<span class="hljs-number">2</span>)
.then(...</code></pre></div></div><div id="Transactions" data-reactid="1943"><h2 id="Transactions" data-reactid="1944">Transactions</h2><div data-reactid="1945"><p data-reactid="1946">Transactions are an important feature of relational databases, as they allow correct recovery from failures and keep a database consistent even in cases of system failure. All queries within a transaction are executed on the same database connection, and run the entire set of queries as a single unit of work. Any failure will mean the database will rollback any queries executed on that connection to the pre-transaction state.</p><p data-reactid="1947">Transactions are handled by passing a handler function into <code>knex.transaction</code>. The handler function accepts a single argument, an object which may be used in two ways:<ol>
<li>As the &quot;promise aware&quot; knex connection</li>
<li>As an object passed into a query with <a href="#Builder-transacting"></a>and eventually call commit or rollback.</li>
</ol>
Consider these two examples:</p></div><pre data-reactid="1948"><code class="hljs js" data-reactid="1949"><span class="hljs-keyword">var</span> <span class="hljs-built_in">Promise</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bluebird'</span>);
<span class="hljs-comment">// Using trx as a query builder:</span>
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">trx</span>) </span>{
<span class="hljs-keyword">var</span> books = [
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Canterbury Tales'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Moby Dick'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Hamlet'</span>}
];
<span class="hljs-keyword">return</span> trx
.insert({<span class="hljs-attr">name</span>: <span class="hljs-string">'Old Books'</span>}, <span class="hljs-string">'id'</span>)
.into(<span class="hljs-string">'catalogues'</span>)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.map(books, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">book</span>) </span>{
book.catalogue_id = ids[<span class="hljs-number">0</span>];
<span class="hljs-comment">// Some validation could take place here.</span>
<span class="hljs-keyword">return</span> trx.insert(info).into(<span class="hljs-string">'books'</span>);
});
});
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">inserts</span>) </span>{
<span class="hljs-built_in">console</span>.log(inserts.length + <span class="hljs-string">' new books saved.'</span>);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-comment">// If we get here, that means that neither the 'Old Books' catalogues insert,</span>
<span class="hljs-comment">// nor any of the books inserts will have taken place.</span>
<span class="hljs-built_in">console</span>.error(error);
});</code></pre><p data-reactid="1950">And then this example:</p><pre data-reactid="1951"><code class="hljs js" data-reactid="1952"><span class="hljs-keyword">var</span> <span class="hljs-built_in">Promise</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'bluebird'</span>);
<span class="hljs-comment">// Using trx as a transaction object:</span>
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">trx</span>) </span>{
<span class="hljs-keyword">var</span> books = [
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Canterbury Tales'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Moby Dick'</span>},
{<span class="hljs-attr">title</span>: <span class="hljs-string">'Hamlet'</span>}
];
knex.insert({<span class="hljs-attr">name</span>: <span class="hljs-string">'Old Books'</span>}, <span class="hljs-string">'id'</span>)
.into(<span class="hljs-string">'catalogues'</span>)
.transacting(trx)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.map(books, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">book</span>) </span>{
book.catalogue_id = ids[<span class="hljs-number">0</span>];
<span class="hljs-comment">// Some validation could take place here.</span>
<span class="hljs-keyword">return</span> knex.insert(info).into(<span class="hljs-string">'books'</span>).transacting(trx);
});
})
.then(trx.commit)
.catch(trx.rollback);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">inserts</span>) </span>{
<span class="hljs-built_in">console</span>.log(inserts.length + <span class="hljs-string">' new books saved.'</span>);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-comment">// If we get here, that means that neither the 'Old Books' catalogues insert,</span>
<span class="hljs-comment">// nor any of the books inserts will have taken place.</span>
<span class="hljs-built_in">console</span>.error(error);
});</code></pre><div data-reactid="1953"><p data-reactid="1954">Throwing an error directly from the transaction handler function automatically rolls back the transaction, same as returning a rejected promise.</p><p data-reactid="1955">Notice that if a promise is not returned within the handler, it is up to you to ensure <code>trx.commit</code>, or <code>trx.rollback</code> are called, otherwise the transaction connection will hang.</p></div></div><div id="Schema" data-reactid="1956"><h2 id="Schema" data-reactid="1957">Schema Builder</h2><p data-reactid="1958">The <code>knex.schema</code> is a <strong>getter function</strong>, which returns a stateful object containing the query. Therefore be sure to obtain a new instance of the <code>knex.schema</code> for every query. These methods return <a href="http://knexjs.org/#Interfaces-Promises">promises</a>.</p><div id="Schema-withSchema" data-reactid="1959"><b data-reactid="1960">withSchema</b><!-- react-text: 1961 --><!-- /react-text --><code data-reactid="1962">knex.schema.withSchema([schemaName])</code><p data-reactid="1963">Specifies the schema to be used when using the schema-building commands.</p><pre class="display" data-reactid="1964"><code class="js hljs" data-reactid="1965">knex.schema.withSchema(<span class="hljs-string">'public'</span>).createTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.increments();
})</code><br data-reactid="1966"/><!-- react-text: 1967 -->Outputs:<!-- /react-text --><br data-reactid="1968"/><code class="sql hljs" data-reactid="1969"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`public`</span>.<span class="hljs-string">`users`</span> (<span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> auto_increment primary <span class="hljs-keyword">key</span>)</code></pre></div><div id="Schema-createTable" data-reactid="1970"><b data-reactid="1971">createTable</b><!-- react-text: 1972 --><!-- /react-text --><code data-reactid="1973">knex.schema.createTable(tableName, callback)</code><p data-reactid="1974">Creates a new table on the database, with a callback function to modify the table&#39;s structure, using the schema-building commands.</p><pre class="display" data-reactid="1975"><code class="js hljs" data-reactid="1976">knex.schema.createTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.increments();
table.string(<span class="hljs-string">'name'</span>);
table.timestamps();
})</code><br data-reactid="1977"/><!-- react-text: 1978 -->Outputs:<!-- /react-text --><br data-reactid="1979"/><code class="sql hljs" data-reactid="1980"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> (<span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> auto_increment primary <span class="hljs-keyword">key</span>, <span class="hljs-string">`name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-string">`created_at`</span> datetime, <span class="hljs-string">`updated_at`</span> datetime)</code></pre></div><div id="Schema-createTableIfNotExists" data-reactid="1981"><b data-reactid="1982">createTableIfNotExists</b><!-- react-text: 1983 --><!-- /react-text --><code data-reactid="1984">knex.schema.createTableIfNotExists(tableName, callback)</code><p data-reactid="1985">Creates a new table on the database if it doesn&#39;t exists on database, with a callback function to modify the table&#39;s structure, using the schema-building commands.</p><pre class="display" data-reactid="1986"><code class="js hljs" data-reactid="1987">knex.schema.createTableIfNotExists(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.increments();
table.string(<span class="hljs-string">'name'</span>);
table.timestamps();
})</code><br data-reactid="1988"/><!-- react-text: 1989 -->Outputs:<!-- /react-text --><br data-reactid="1990"/><code class="sql hljs" data-reactid="1991"><span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">exists</span> <span class="hljs-string">`users`</span> (<span class="hljs-string">`id`</span> <span class="hljs-built_in">int</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">null</span> auto_increment primary <span class="hljs-keyword">key</span>, <span class="hljs-string">`name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-string">`created_at`</span> datetime, <span class="hljs-string">`updated_at`</span> datetime)</code></pre></div><div id="Schema-renameTable" data-reactid="1992"><b data-reactid="1993">renameTable</b><!-- react-text: 1994 --><!-- /react-text --><code data-reactid="1995">knex.schema.renameTable(from, to)</code><p data-reactid="1996">Renames a table from a current tableName to another.</p><pre class="display" data-reactid="1997"><code class="js hljs" data-reactid="1998">knex.schema.renameTable(<span class="hljs-string">'users'</span>, <span class="hljs-string">'old_users'</span>)</code><br data-reactid="1999"/><!-- react-text: 2000 -->Outputs:<!-- /react-text --><br data-reactid="2001"/><code class="sql hljs" data-reactid="2002"><span class="hljs-keyword">rename</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">to</span> <span class="hljs-string">`old_users`</span></code></pre></div><div id="Schema-dropTable" data-reactid="2003"><b data-reactid="2004">dropTable</b><!-- react-text: 2005 --><!-- /react-text --><code data-reactid="2006">knex.schema.dropTable(tableName)</code><p data-reactid="2007">Drops a table, specified by tableName.</p><pre class="display" data-reactid="2008"><code class="js hljs" data-reactid="2009">knex.schema.dropTable(<span class="hljs-string">'users'</span>)</code><br data-reactid="2010"/><!-- react-text: 2011 -->Outputs:<!-- /react-text --><br data-reactid="2012"/><code class="sql hljs" data-reactid="2013"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Schema-hasTable" data-reactid="2014"><b data-reactid="2015">hasTable</b><!-- react-text: 2016 --><!-- /react-text --><code data-reactid="2017">knex.schema.hasTable(tableName)</code><p data-reactid="2018">Checks for a table&#39;s existence by tableName, resolving with a boolean to signal if the table exists.</p><pre data-reactid="2019"><code class="hljs js" data-reactid="2020">knex.schema.hasTable(<span class="hljs-string">'users'</span>).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">exists</span>) </span>{
<span class="hljs-keyword">if</span> (!exists) {
<span class="hljs-keyword">return</span> knex.schema.createTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">t</span>) </span>{
t.increments(<span class="hljs-string">'id'</span>).primary();
t.string(<span class="hljs-string">'first_name'</span>, <span class="hljs-number">100</span>);
t.string(<span class="hljs-string">'last_name'</span>, <span class="hljs-number">100</span>);
t.text(<span class="hljs-string">'bio'</span>);
});
}
});</code></pre></div><div id="Schema-hasColumn" data-reactid="2021"><b data-reactid="2022">hasColumn</b><!-- react-text: 2023 --><!-- /react-text --><code data-reactid="2024">knex.schema.hasColumn(tableName, columnName)</code><p data-reactid="2025">Checks if a column exists in the current table, resolves the promise with a boolean, true if the column exists, false otherwise.</p></div><div id="Schema-dropTableIfExists" data-reactid="2026"><b data-reactid="2027">dropTableIfExists</b><!-- react-text: 2028 --><!-- /react-text --><code data-reactid="2029">knex.schema.dropTableIfExists(tableName)</code><p data-reactid="2030">Drops a table conditionally if the table exists, specified by tableName.</p><pre class="display" data-reactid="2031"><code class="js hljs" data-reactid="2032">knex.schema.dropTableIfExists(<span class="hljs-string">'users'</span>)</code><br data-reactid="2033"/><!-- react-text: 2034 -->Outputs:<!-- /react-text --><br data-reactid="2035"/><code class="sql hljs" data-reactid="2036"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">table</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">exists</span> <span class="hljs-string">`users`</span></code></pre></div><div id="Schema-table" data-reactid="2037"><b data-reactid="2038">table</b><!-- react-text: 2039 --><!-- /react-text --><code data-reactid="2040">knex.schema.table(tableName, callback)</code><p data-reactid="2041">Chooses a database table, and then modifies the table, using the Schema Building functions inside of the callback.</p><pre class="display" data-reactid="2042"><code class="js hljs" data-reactid="2043">knex.schema.table(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.dropColumn(<span class="hljs-string">'name'</span>);
table.string(<span class="hljs-string">'first_name'</span>);
table.string(<span class="hljs-string">'last_name'</span>);
})</code><br data-reactid="2044"/><!-- react-text: 2045 -->Outputs:<!-- /react-text --><br data-reactid="2046"/><code class="sql hljs" data-reactid="2047"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">add</span> <span class="hljs-string">`first_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-keyword">add</span> <span class="hljs-string">`last_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>);
<span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">drop</span> <span class="hljs-string">`name`</span></code></pre></div><div id="Schema-raw" data-reactid="2048"><b data-reactid="2049">raw</b><!-- react-text: 2050 --><!-- /react-text --><code data-reactid="2051">knex.schema.raw(statement)</code><p data-reactid="2052">Run an arbitrary sql query in the schema builder chain.</p><pre class="display" data-reactid="2053"><code class="js hljs" data-reactid="2054">knex.schema.raw(<span class="hljs-string">"SET sql_mode='TRADITIONAL'"</span>)
.table(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.dropColumn(<span class="hljs-string">'name'</span>);
table.string(<span class="hljs-string">'first_name'</span>);
table.string(<span class="hljs-string">'last_name'</span>);
})</code><br data-reactid="2055"/><!-- react-text: 2056 -->Outputs:<!-- /react-text --><br data-reactid="2057"/><code class="sql hljs" data-reactid="2058"><span class="hljs-keyword">SET</span> sql_mode=<span class="hljs-string">'TRADITIONAL'</span>;
<span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">add</span> <span class="hljs-string">`first_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>), <span class="hljs-keyword">add</span> <span class="hljs-string">`last_name`</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">255</span>);
<span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">drop</span> <span class="hljs-string">`name`</span></code></pre></div><h3 id="Schema-Building" data-reactid="2059">Schema Building:</h3><div id="Schema-dropColumn" data-reactid="2060"><b data-reactid="2061">dropColumn</b><!-- react-text: 2062 --><!-- /react-text --><code data-reactid="2063">table.dropColumn(name)</code><p data-reactid="2064">Drops a column, specified by the column&#39;s name</p></div><div id="Schema-dropColumns" data-reactid="2065"><b data-reactid="2066">dropColumns</b><!-- react-text: 2067 --><!-- /react-text --><code data-reactid="2068">table.dropColumns(*columns)</code><p data-reactid="2069">Drops multiple columns, taking a variable number of column names.</p></div><div id="Schema-renameColumn" data-reactid="2070"><b data-reactid="2071">renameColumn</b><!-- react-text: 2072 --><!-- /react-text --><code data-reactid="2073">table.renameColumn(from, to)</code><p data-reactid="2074">Renames a column from one name to another.</p></div><div id="Schema-increments" data-reactid="2075"><b data-reactid="2076">increments</b><!-- react-text: 2077 --><!-- /react-text --><code data-reactid="2078">table.increments(name)</code><p data-reactid="2079">Adds an auto incrementing column, in PostgreSQL this is a serial. This will be used as the primary key for the table. Also available is a bigIncrements if you wish to add a bigint incrementing number (in PostgreSQL bigserial).</p></div><div id="Schema-integer" data-reactid="2080"><b data-reactid="2081">integer</b><!-- react-text: 2082 --><!-- /react-text --><code data-reactid="2083">table.integer(name)</code><p data-reactid="2084">Adds an integer column.</p></div><div id="Schema-bigInteger" data-reactid="2085"><b data-reactid="2086">bigInteger</b><!-- react-text: 2087 --><!-- /react-text --><code data-reactid="2088">table.bigInteger(name)</code><p data-reactid="2089">In MySQL or PostgreSQL, adds a bigint column, otherwise adds a normal integer. Note that bigint data is returned as a string in queries because JavaScript may be unable to parse them without loss of precision.</p></div><div id="Schema-text" data-reactid="2090"><b data-reactid="2091">text</b><!-- react-text: 2092 --><!-- /react-text --><code data-reactid="2093">table.text(name, [textType])</code><p data-reactid="2094">Adds a text column, with optional textType for MySql text datatype preference. textType may be mediumtext or longtext, otherwise defaults to text.</p></div><div id="Schema-string" data-reactid="2095"><b data-reactid="2096">string</b><!-- react-text: 2097 --><!-- /react-text --><code data-reactid="2098">table.string(name, [length])</code><p data-reactid="2099">Adds a string column, with optional length defaulting to 255.</p></div><div id="Schema-float" data-reactid="2100"><b data-reactid="2101">float</b><!-- react-text: 2102 --><!-- /react-text --><code data-reactid="2103">table.float(column, [precision], [scale])</code><p data-reactid="2104">Adds a float column, with optional precision (defaults to 8) and scale (defaults to 2).</p></div><div id="Schema-decimal" data-reactid="2105"><b data-reactid="2106">decimal</b><!-- react-text: 2107 --><!-- /react-text --><code data-reactid="2108">table.decimal(column, [precision], [scale])</code><p data-reactid="2109">Adds a decimal column, with optional precision (defaults to 8) and scale (defaults to 2).</p></div><div id="Schema-boolean" data-reactid="2110"><b data-reactid="2111">boolean</b><!-- react-text: 2112 --><!-- /react-text --><code data-reactid="2113">table.boolean(name)</code><p data-reactid="2114">Adds a boolean column.</p></div><div id="Schema-date" data-reactid="2115"><b data-reactid="2116">date</b><!-- react-text: 2117 --><!-- /react-text --><code data-reactid="2118">table.date(name)</code><p data-reactid="2119">Adds a date column.</p></div><div id="Schema-dateTime" data-reactid="2120"><b data-reactid="2121">dateTime</b><!-- react-text: 2122 --><!-- /react-text --><code data-reactid="2123">table.dateTime(name)</code><p data-reactid="2124">Adds a dateTime column.</p></div><div id="Schema-time" data-reactid="2125"><b data-reactid="2126">time</b><!-- react-text: 2127 --><!-- /react-text --><code data-reactid="2128">table.time(name)</code><p data-reactid="2129">Adds a time column.</p></div><div id="Schema-timestamp" data-reactid="2130"><b data-reactid="2131">timestamp</b><!-- react-text: 2132 --><!-- /react-text --><code data-reactid="2133">table.timestamp(name, [standard])</code><p data-reactid="2134">Adds a timestamp column, defaults to timestamptz in PostgreSQL, unless true is passed as the second argument. For Example:</p><pre data-reactid="2135"><code class="hljs js" data-reactid="2136">table.timestamp(<span class="hljs-string">'created_at'</span>).defaultTo(knex.fn.now());</code></pre></div><div id="Schema-timestamps" data-reactid="2137"><b data-reactid="2138">timestamps</b><!-- react-text: 2139 --><!-- /react-text --><code data-reactid="2140">table.timestamps([useTimestamps], [defaultToNow])</code><p data-reactid="2141">Adds a created_at and updated_at column on the database, setting these each to dateTime types. When true is passed as the first argument a timestamp type is used. Both colums default to being not null and the current timestamp when true is passed as the second argument.</p></div><div id="Schema-dropTimestamps" data-reactid="2142"><b data-reactid="2143">dropTimestamps</b><!-- react-text: 2144 --><!-- /react-text --><code data-reactid="2145">table.dropTimestamps()</code><p data-reactid="2146">Drops the columns created_at and updated_at from the table, which can be created via timestamps.</p></div><div id="Schema-binary" data-reactid="2147"><b data-reactid="2148">binary</b><!-- react-text: 2149 --><!-- /react-text --><code data-reactid="2150">table.binary(name, [length])</code><p data-reactid="2151">Adds a binary column, with optional length argument for MySQL.</p></div><div id="Schema-enum / enu" data-reactid="2152"><b data-reactid="2153">enum / enu</b><!-- react-text: 2154 --><!-- /react-text --><code data-reactid="2155">table.enu(col, values)</code><p data-reactid="2156">Adds a enum column, (aliased to enu, as enum is a reserved word in JavaScript). Note that the second argument is an array of values. Example:</p><pre data-reactid="2157"><code class="hljs js" data-reactid="2158">table.enu(<span class="hljs-string">'column'</span>, [<span class="hljs-string">'value1'</span>, <span class="hljs-string">'value2'</span>])</code></pre></div><div id="Schema-json" data-reactid="2159"><b data-reactid="2160">json</b><!-- react-text: 2161 --><!-- /react-text --><code data-reactid="2162">table.json(name)</code><p data-reactid="2163">Adds a json column, using the built-in json type in postgresql, defaulting to a text column in older versions of postgresql or in unsupported databases. Note that when setting an array (or a value that could be an array) as the value of a json or jsonb column, you should use JSON.stringify() to convert your value to a string prior to passing it to the query builder, e.g.</p><pre data-reactid="2164"><code class="hljs js" data-reactid="2165">knex.table(<span class="hljs-string">'users'</span>)
.where({<span class="hljs-attr">id</span>: <span class="hljs-number">1</span>})
.update({<span class="hljs-attr">json_data</span>: <span class="hljs-built_in">JSON</span>.stringify(mightBeAnArray)});</code></pre><p data-reactid="2166">This is because postgresql has a native array type which uses a syntax incompatible with json; knex has no way of knowing which syntax to use, and calling JSON.stringify() forces json-style syntax.</p></div><div id="Schema-jsonb" data-reactid="2167"><b data-reactid="2168">jsonb</b><!-- react-text: 2169 --><!-- /react-text --><code data-reactid="2170">table.jsonb(name)</code><p data-reactid="2171">Adds a jsonb column. Works similar to table.json(), but uses native jsonb type if possible.</p></div><div id="Schema-uuid" data-reactid="2172"><b data-reactid="2173">uuid</b><!-- react-text: 2174 --><!-- /react-text --><code data-reactid="2175">table.uuid(name)</code><p data-reactid="2176">Adds a uuid column - this uses the built-in uuid type in postgresql, and falling back to a char(36) in other databases.</p></div><div id="Schema-comment" data-reactid="2177"><b data-reactid="2178">comment</b><!-- react-text: 2179 --><!-- /react-text --><code data-reactid="2180">table.comment(value)</code><p data-reactid="2181">Sets the comment for a table.</p></div><div id="Schema-engine" data-reactid="2182"><b data-reactid="2183">engine</b><!-- react-text: 2184 --><!-- /react-text --><code data-reactid="2185">table.engine(val)</code><p data-reactid="2186">Sets the engine for the database table, only available within a createTable call, and only applicable to MySQL.</p></div><div id="Schema-charset" data-reactid="2187"><b data-reactid="2188">charset</b><!-- react-text: 2189 --><!-- /react-text --><code data-reactid="2190">table.charset(val)</code><p data-reactid="2191">Sets the charset for the database table, only available within a createTable call, and only applicable to MySQL.</p></div><div id="Schema-collate" data-reactid="2192"><b data-reactid="2193">collate</b><!-- react-text: 2194 --><!-- /react-text --><code data-reactid="2195">table.collate(val)</code><p data-reactid="2196">Sets the collation for the database table, only available within a createTable call, and only applicable to MySQL.</p></div><div id="Schema-inherits" data-reactid="2197"><b data-reactid="2198">inherits</b><!-- react-text: 2199 --><!-- /react-text --><code data-reactid="2200">table.inherits(val)</code><p data-reactid="2201">Sets the tables that this table inherits, only available within a createTable call, and only applicable to PostgreSQL.</p></div><div id="Schema-specificType" data-reactid="2202"><b data-reactid="2203">specificType</b><!-- react-text: 2204 --><!-- /react-text --><code data-reactid="2205">table.specificType(name, type)</code><p data-reactid="2206">Sets a specific type for the column creation, if you&#39;d like to add a column type that isn&#39;t supported here.</p></div><div id="Schema-index" data-reactid="2207"><b data-reactid="2208">index</b><!-- react-text: 2209 --><!-- /react-text --><code data-reactid="2210">table.index(columns, [indexName], [indexType])</code><p data-reactid="2211">Adds an index to a table over the given columns. A default index name using the columns is used unless indexName is specified. The indexType can be optionally specified for PostgreSQL.</p></div><div id="Schema-dropIndex" data-reactid="2212"><b data-reactid="2213">dropIndex</b><!-- react-text: 2214 --><!-- /react-text --><code data-reactid="2215">table.dropIndex(columns, [indexName])</code><p data-reactid="2216">Drops an index from a table. A default index name using the columns is used unless indexName is specified (in which case columns is ignored).</p></div><div id="Schema-unique" data-reactid="2217"><b data-reactid="2218">unique</b><!-- react-text: 2219 --><!-- /react-text --><code data-reactid="2220">table.unique(columns, [indexName])</code><p data-reactid="2221">Adds an unique index to a table over the given <code>columns</code>. A default index name using the columns is used unless indexName is specified.</p><pre data-reactid="2222"><code class="hljs js" data-reactid="2223">knex.schema.alterTable(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">t</span>) </span>{
t.unique(<span class="hljs-string">'email'</span>)
})
knex.schema.alterTable(<span class="hljs-string">'job'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">t</span>) </span>{
t.unique([<span class="hljs-string">'account_id'</span>, <span class="hljs-string">'program_id'</span>])
})</code></pre></div><div id="Schema-foreign" data-reactid="2224"><b data-reactid="2225">foreign</b><!-- react-text: 2226 --><!-- /react-text --><code data-reactid="2227">table.foreign(columns, [foreignKeyName])</code><p data-reactid="2228">Adds a foreign key constraint to a table for an existing column using <code>table.foreign(column).references(column)</code> or multiple columns using <code>table.foreign(columns).references(columns)</code>. A default key name using the columns is used unless foreignKeyName is specified. You can also chain onDelete and/or onUpdate to set the reference option (RESTRICT, CASCADE, SET NULL, NO ACTION) for the operation. Note, this is the same as column.references(column) but works for existing columns.</p><pre data-reactid="2229"><code class="hljs js" data-reactid="2230">knex.schema.table(<span class="hljs-string">'users'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">table</span>) </span>{
table.integer(<span class="hljs-string">'user_id'</span>).unsigned()
table.foreign(<span class="hljs-string">'user_id'</span>).references(<span class="hljs-string">'Items.user_id_in_items'</span>)
})</code></pre></div><div id="Schema-dropForeign" data-reactid="2231"><b data-reactid="2232">dropForeign</b><!-- react-text: 2233 --><!-- /react-text --><code data-reactid="2234">table.dropForeign(columns, [foreignKeyName])</code><p data-reactid="2235">Drops a foreign key constraint from a table. A default foreign key name using the columns is used unless foreignKeyName is specified (in which case columns is ignored).</p></div><div id="Schema-dropUnique" data-reactid="2236"><b data-reactid="2237">dropUnique</b><!-- react-text: 2238 --><!-- /react-text --><code data-reactid="2239">table.dropUnique(columns, [indexName])</code><p data-reactid="2240">Drops a unique key constraint from a table. A default unique key name using the columns is used unless indexName is specified (in which case columns is ignored).</p></div><div id="Schema-dropPrimary" data-reactid="2241"><b data-reactid="2242">dropPrimary</b><!-- react-text: 2243 --><!-- /react-text --><code data-reactid="2244">table.dropPrimary([constraintName])</code><p data-reactid="2245">Drops the primary key constraint on a table. Defaults to tablename_pkey unless constraintName is specified.</p></div><h3 id="Chainable" data-reactid="2246">Chainable Methods:</h3><p data-reactid="2247">The following three methods may be chained on the schema building methods, as modifiers to the column.</p><div id="Schema-alter" data-reactid="2248"><b data-reactid="2249">alter</b><!-- react-text: 2250 --><!-- /react-text --><code data-reactid="2251">column.alter()</code><p data-reactid="2252">Marks the column as an alter / modify, instead of the default add. Note: This only works in .alterTable() and is not supported by SQlite. Alter is <em>not</em> done incrementally over older column type so if you like to add <code>notNull</code> and keep the old default value, the alter statement must contain both <code>.notNull().defaultTo(1).alter()</code>. If one just tries to add <code>.notNull().alter()</code> the old default value will be dropped.</p></div><pre data-reactid="2253"><code data-reactid="2254">knex.schema.alterTable('user', function(t) {
t.increments().primary(); // add
// drops previous default value from column, change type to string and add not nullable constraint
t.string('username', 35).notNullable().alter();
// drops both not null contraint and the default value
t.integer('age').alter();
});</code></pre><div id="Schema-index" data-reactid="2255"><b data-reactid="2256">index</b><!-- react-text: 2257 --><!-- /react-text --><code data-reactid="2258">column.index([indexName], [indexType])</code><p data-reactid="2259">Specifies a field as an index. If an indexName is specified, it is used in place of the standard index naming convention of tableName_columnName. The indexType can be optionally specified for PostgreSQL. No-op if this is chained off of a field that cannot be indexed.</p></div><div id="Schema-primary" data-reactid="2260"><b data-reactid="2261">primary</b><!-- react-text: 2262 --><!-- /react-text --><code data-reactid="2263">column.primary([constraintName])</code><p data-reactid="2264"><pre><code> When called on a single column it will set that column as the primary key for a table.
To create a compound primary key, pass an array of column names:
`table.primary([&#39;column1&#39;, &#39;column2&#39;])`.
Constraint name defaults to `tablename_pkey` unless `constraintName` is specified.
</code></pre></p></div><div id="Schema-unique" data-reactid="2265"><b data-reactid="2266">unique</b><!-- react-text: 2267 --><!-- /react-text --><code data-reactid="2268">column.unique()</code><p data-reactid="2269">Sets the column as unique.</p></div><div id="Schema-references" data-reactid="2270"><b data-reactid="2271">references</b><!-- react-text: 2272 --><!-- /react-text --><code data-reactid="2273">column.references(column)</code><p data-reactid="2274">Sets the &quot;column&quot; that the current column references as a foreign key. &quot;column&quot; can either be &quot;.<column>&quot; syntax, or just the column name followed up with a call to inTable to specify the table.</column><table></table></p></div><div id="Schema-inTable" data-reactid="2275"><b data-reactid="2276">inTable</b><!-- react-text: 2277 --><!-- /react-text --><code data-reactid="2278">column.inTable(table)</code><p data-reactid="2279">Sets the &quot;table&quot; where the foreign key column is located after calling column.references.</p></div><div id="Schema-onDelete" data-reactid="2280"><b data-reactid="2281">onDelete</b><!-- react-text: 2282 --><!-- /react-text --><code data-reactid="2283">column.onDelete(command)</code><p data-reactid="2284">Sets the SQL command to be run &quot;onDelete&quot;.</p></div><div id="Schema-onUpdate" data-reactid="2285"><b data-reactid="2286">onUpdate</b><!-- react-text: 2287 --><!-- /react-text --><code data-reactid="2288">column.onUpdate(command)</code><p data-reactid="2289">Sets the SQL command to be run &quot;onUpdate&quot;.</p></div><div id="Schema-defaultTo" data-reactid="2290"><b data-reactid="2291">defaultTo</b><!-- react-text: 2292 --><!-- /react-text --><code data-reactid="2293">column.defaultTo(value)</code><p data-reactid="2294">Sets the default value for the column on an insert.</p></div><div id="Schema-unsigned" data-reactid="2295"><b data-reactid="2296">unsigned</b><!-- react-text: 2297 --><!-- /react-text --><code data-reactid="2298">column.unsigned()</code><p data-reactid="2299">Specifies an integer as unsigned. No-op if this is chained off of a non-integer field.</p></div><div id="Schema-notNullable" data-reactid="2300"><b data-reactid="2301">notNullable</b><!-- react-text: 2302 --><!-- /react-text --><code data-reactid="2303">column.notNullable()</code><p data-reactid="2304">Adds a not null on the current column being created.</p></div><div id="Schema-nullable" data-reactid="2305"><b data-reactid="2306">nullable</b><!-- react-text: 2307 --><!-- /react-text --><code data-reactid="2308">column.nullable()</code><p data-reactid="2309">Default on column creation, this explicitly sets a field to be nullable.</p></div><div id="Schema-first" data-reactid="2310"><b data-reactid="2311">first</b><!-- react-text: 2312 --><!-- /react-text --><code data-reactid="2313">column.first()</code><p data-reactid="2314">Sets the column to be inserted on the first position, only used in MySQL alter tables.</p></div><div id="Schema-after" data-reactid="2315"><b data-reactid="2316">after</b><!-- react-text: 2317 --><!-- /react-text --><code data-reactid="2318">column.after(field)</code><p data-reactid="2319">Sets the column to be inserted after another, only used in MySQL alter tables.</p></div><div id="Schema-comment" data-reactid="2320"><b data-reactid="2321">comment</b><!-- react-text: 2322 --><!-- /react-text --><code data-reactid="2323">column.comment(value)</code><p data-reactid="2324">Sets the comment for a column.</p></div><pre data-reactid="2325"><code data-reactid="2326">knex.schema.createTable('accounts', function(t) {
t.increments().primary();
t.string('email').unique().comment('This is the email field');
});</code></pre><div id="Schema-collate" data-reactid="2327"><b data-reactid="2328">collate</b><!-- react-text: 2329 --><!-- /react-text --><code data-reactid="2330">column.collate(collation)</code><p data-reactid="2331">Sets the collation for a column (only works in MySQL). Here is a list of all available collations: <a href="https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html">https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html</a></p></div><pre data-reactid="2332"><code data-reactid="2333">knex.schema.createTable('users', function(t) {
t.increments();
t.string('email').unique().collate('utf8_unicode_ci');
});</code></pre></div><div id="Raw" data-reactid="2334"><h2 id="Raw" data-reactid="2335">Raw</h2><p data-reactid="2336">Sometimes you may need to use a raw expression in a query. Raw query object may be injected pretty much anywhere you want, and using proper bindings can ensure your values are escaped properly, preventing SQL-injection attacks.</p><h3 id="Raw-Bindings" data-reactid="2337">Raw Parameter Binding:</h3><p data-reactid="2338">One can paramterize sql given to <code>knex.raw(sql, bindings)</code>. Parameters can be positional named. One can also choose if parameter should be treated as value or as sql identifier e.g. in case of <code>&#39;TableName.ColumnName&#39;</code> reference.</p><pre class="display" data-reactid="2339"><code class="js hljs" data-reactid="2340">knex(<span class="hljs-string">'users'</span>)
.select(knex.raw(<span class="hljs-string">'count(*) as user_count, status'</span>))
.where(knex.raw(<span class="hljs-number">1</span>))
.orWhere(knex.raw(<span class="hljs-string">'status &lt;&gt; ?'</span>, [<span class="hljs-number">1</span>]))
.groupBy(<span class="hljs-string">'status'</span>)</code><br data-reactid="2341"/><!-- react-text: 2342 -->Outputs:<!-- /react-text --><br data-reactid="2343"/><code class="sql hljs" data-reactid="2344"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(*) <span class="hljs-keyword">as</span> user_count, <span class="hljs-keyword">status</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">status</span> &lt;&gt; <span class="hljs-number">1</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`status`</span></code></pre><p data-reactid="2345">Positional bindings <code>?</code> are interpreted as values and <code>??</code> are interpreted as identifiers.</p><pre class="display" data-reactid="2346"><code class="js hljs" data-reactid="2347">knex(<span class="hljs-string">'users'</span>).where(knex.raw(<span class="hljs-string">'?? = ?'</span>, [<span class="hljs-string">'user.name'</span>, <span class="hljs-number">1</span>]))</code><br data-reactid="2348"/><!-- react-text: 2349 -->Outputs:<!-- /react-text --><br data-reactid="2350"/><code class="sql hljs" data-reactid="2351"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`user`</span>.<span class="hljs-string">`name`</span> = <span class="hljs-number">1</span></code></pre><p data-reactid="2352">Named bindings such as <code>:name</code> are interpreted as values and <code>:name:</code> interpreted as identifiers. Named bindings are processed so long as the value is anything other than <code>undefined</code>.</p><pre class="display" data-reactid="2353"><code class="js hljs" data-reactid="2354">knex(<span class="hljs-string">'users'</span>)
.where(knex.raw(<span class="hljs-string">':name: = :thisGuy or :name: = :otherGuy or :name: = :undefinedBinding'</span>, {
<span class="hljs-attr">name</span>: <span class="hljs-string">'users.name'</span>,
<span class="hljs-attr">thisGuy</span>: <span class="hljs-string">'Bob'</span>,
<span class="hljs-attr">otherGuy</span>: <span class="hljs-string">'Jay'</span>,
<span class="hljs-attr">undefinedBinding</span>: <span class="hljs-literal">undefined</span>
}))</code><br data-reactid="2355"/><!-- react-text: 2356 -->Error:<!-- /react-text --><br data-reactid="2357"/><code class="sql hljs" data-reactid="2358">Undefined binding(s) detected when compiling RAW query: `users`.`name` = ? or `users`.`name` = ? or `users`.`name` = :undefinedBinding</code></pre><p data-reactid="2359">For simpler queries where one only has a single binding, <code>.raw</code> can accept said binding as its second parameter.</p><pre class="display" data-reactid="2360"><code class="js hljs" data-reactid="2361">knex(<span class="hljs-string">'users'</span>)
.where(
knex.raw(<span class="hljs-string">'LOWER("login") = ?'</span>, <span class="hljs-string">'knex'</span>)
)
.orWhere(
knex.raw(<span class="hljs-string">'accesslevel = ?'</span>, <span class="hljs-number">1</span>)
)
.orWhere(
knex.raw(<span class="hljs-string">'updtime = ?'</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>.UTC(<span class="hljs-string">'01-01-2016'</span>))
)</code><br data-reactid="2362"/><!-- react-text: 2363 -->Error:<!-- /react-text --><br data-reactid="2364"/><code class="sql hljs" data-reactid="2365">Date.UTC is not a constructor</code></pre><p data-reactid="2366">Note that due to ambiguity, arrays must be passed as arguments within a containing array.</p><pre class="display" data-reactid="2367"><code class="js hljs" data-reactid="2368">knex.raw(<span class="hljs-string">'select * from users where id in (?)'</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
<span class="hljs-comment">// Error: Expected 3 bindings, saw 1</span>
knex.raw(<span class="hljs-string">'select * from users where id in (?)'</span>, [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]])</code><br data-reactid="2369"/><!-- react-text: 2370 -->Outputs:<!-- /react-text --><br data-reactid="2371"/><code class="sql hljs" data-reactid="2372"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-keyword">users</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">id</span> <span class="hljs-keyword">in</span> (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)</code></pre><p data-reactid="2373">To prevent replacement of <code>?</code> one can use the escape sequence <code>\\?</code>.</p><pre class="display" data-reactid="2374"><code class="js hljs" data-reactid="2375">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'='</span>, <span class="hljs-number">1</span>).whereRaw(<span class="hljs-string">'?? \\? ?'</span>, [<span class="hljs-string">'jsonColumn'</span>, <span class="hljs-string">'jsonKey'</span>])</code><br data-reactid="2376"/><!-- react-text: 2377 -->Outputs:<!-- /react-text --><br data-reactid="2378"/><code class="sql hljs" data-reactid="2379"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`id`</span> = <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-string">`jsonColumn`</span> ? <span class="hljs-string">'jsonKey'</span></code></pre><p data-reactid="2380">To prevent replacement of named bindings one can use the escape sequence <code>\\:</code>.</p><pre class="display" data-reactid="2381"><code class="js hljs" data-reactid="2382">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).whereRaw(<span class="hljs-string">":property: = '\\:value' OR \\:property: = :value"</span>, {
<span class="hljs-attr">property</span>: <span class="hljs-string">'name'</span>,
<span class="hljs-attr">value</span>: <span class="hljs-string">'Bob'</span>
})</code><br data-reactid="2383"/><!-- react-text: 2384 -->Outputs:<!-- /react-text --><br data-reactid="2385"/><code class="sql hljs" data-reactid="2386"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-string">`name`</span> = <span class="hljs-string">':value'</span> <span class="hljs-keyword">OR</span> :property: = <span class="hljs-string">'Bob'</span></code></pre><h3 id="Raw-Expressions" data-reactid="2387">Raw Expressions:</h3><p data-reactid="2388">Raw expressions are created by using <code>knex.raw(sql, [bindings])</code> and passing this as a value for any value in the query chain.</p><pre class="display" data-reactid="2389"><code class="js hljs" data-reactid="2390">knex(<span class="hljs-string">'users'</span>)
.select(knex.raw(<span class="hljs-string">'count(*) as user_count, status'</span>))
.where(knex.raw(<span class="hljs-number">1</span>))
.orWhere(knex.raw(<span class="hljs-string">'status &lt;&gt; ?'</span>, [<span class="hljs-number">1</span>]))
.groupBy(<span class="hljs-string">'status'</span>)</code><br data-reactid="2391"/><!-- react-text: 2392 -->Outputs:<!-- /react-text --><br data-reactid="2393"/><code class="sql hljs" data-reactid="2394"><span class="hljs-keyword">select</span> <span class="hljs-keyword">count</span>(*) <span class="hljs-keyword">as</span> user_count, <span class="hljs-keyword">status</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`users`</span> <span class="hljs-keyword">where</span> <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">status</span> &lt;&gt; <span class="hljs-number">1</span> <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> <span class="hljs-string">`status`</span></code></pre><h3 id="Raw-Queries" data-reactid="2395">Raw Queries:</h3><p data-reactid="2396">The <code>knex.raw</code> may also be used to build a full query and execute it, as a standard query builder query would be executed. The benefit of this is that it uses the connection pool and provides a standard interface for the different client libraries.</p><pre data-reactid="2397"><code class="hljs js" data-reactid="2398">knex.raw(<span class="hljs-string">'select * from users where id = ?'</span>, [<span class="hljs-number">1</span>]).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) </span>{ ... });</code></pre><p class="info" data-reactid="2399">Note that the response will be whatever the underlying sql library would typically return on a normal query, so you may need to look at the documentation for the base library the queries are executing against to determine how to handle the response.</p><h3 id="Raw-queries-wrapped" data-reactid="2400">Wrapped Queries:</h3><p data-reactid="2401">The raw query builder also comes with a <code>wrap</code> method, which allows wrapping the query in a value:</p><pre class="display" data-reactid="2402"><code class="js hljs" data-reactid="2403"><span class="hljs-keyword">var</span> subcolumn = knex.raw(<span class="hljs-string">'select avg(salary) from employee where dept_no = e.dept_no'</span>)
.wrap(<span class="hljs-string">'('</span>, <span class="hljs-string">') avg_sal_dept'</span>);
knex.select(<span class="hljs-string">'e.lastname'</span>, <span class="hljs-string">'e.salary'</span>, subcolumn)
.from(<span class="hljs-string">'employee as e'</span>)
.whereRaw(<span class="hljs-string">'dept_no = e.dept_no'</span>)</code><br data-reactid="2404"/><!-- react-text: 2405 -->Outputs:<!-- /react-text --><br data-reactid="2406"/><code class="sql hljs" data-reactid="2407"><span class="hljs-keyword">select</span> <span class="hljs-string">`e`</span>.<span class="hljs-string">`lastname`</span>, <span class="hljs-string">`e`</span>.<span class="hljs-string">`salary`</span>, (<span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(salary) <span class="hljs-keyword">from</span> employee <span class="hljs-keyword">where</span> dept_no = e.dept_no) avg_sal_dept <span class="hljs-keyword">from</span> <span class="hljs-string">`employee`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`e`</span> <span class="hljs-keyword">where</span> dept_no = e.dept_no</code></pre><p data-reactid="2408">Note that the example above be achieved more easily using the <a href="#Builder-as">as</a> method.</p><pre class="display" data-reactid="2409"><code class="js hljs" data-reactid="2410"><span class="hljs-keyword">var</span> subcolumn = knex.avg(<span class="hljs-string">'salary'</span>)
.from(<span class="hljs-string">'employee'</span>)
.whereRaw(<span class="hljs-string">'dept_no = e.dept_no'</span>)
.as(<span class="hljs-string">'avg_sal_dept'</span>);
knex.select(<span class="hljs-string">'e.lastname'</span>, <span class="hljs-string">'e.salary'</span>, subcolumn)
.from(<span class="hljs-string">'employee as e'</span>)
.whereRaw(<span class="hljs-string">'dept_no = e.dept_no'</span>)</code><br data-reactid="2411"/><!-- react-text: 2412 -->Outputs:<!-- /react-text --><br data-reactid="2413"/><code class="sql hljs" data-reactid="2414"><span class="hljs-keyword">select</span> <span class="hljs-string">`e`</span>.<span class="hljs-string">`lastname`</span>, <span class="hljs-string">`e`</span>.<span class="hljs-string">`salary`</span>, (<span class="hljs-keyword">select</span> <span class="hljs-keyword">avg</span>(<span class="hljs-string">`salary`</span>) <span class="hljs-keyword">from</span> <span class="hljs-string">`employee`</span> <span class="hljs-keyword">where</span> dept_no = e.dept_no) <span class="hljs-keyword">as</span> <span class="hljs-string">`avg_sal_dept`</span> <span class="hljs-keyword">from</span> <span class="hljs-string">`employee`</span> <span class="hljs-keyword">as</span> <span class="hljs-string">`e`</span> <span class="hljs-keyword">where</span> dept_no = e.dept_no</code></pre></div><div id="Utility" data-reactid="2415"><h2 id="Utility" data-reactid="2416">Utility</h2><p data-reactid="2417">A collection of utilities that the knex library provides for convenience.</p><h3 id="Utility-BatchInsert" data-reactid="2418">Batch Insert</h3><div data-reactid="2419"><p data-reactid="2420">The <code>batchInsert</code> utility will insert a batch of rows wrapped inside a transaction <em>(which is automatically created unless explicitly given a transaction using <a href="#Builder-transacting">transacting</a>)</em>, at a given <code>chunkSize</code>.</p><p data-reactid="2421">It&#39;s primarily designed to be used when you have thousands of rows to insert into a table.</p><p data-reactid="2422">By default, the <code>chunkSize</code> is set to 1000.</p><p data-reactid="2423">BatchInsert also allows for <a href="#Builder-returning">returning values</a> and supplying transactions using <a href="#Builder-transacting">transacting</a>.</p></div><pre data-reactid="2424"><code class="hljs js" data-reactid="2425"><span class="hljs-keyword">var</span> rows = [{...}, {...}];
<span class="hljs-keyword">var</span> chunkSize = <span class="hljs-number">30</span>;
knex.batchInsert(<span class="hljs-string">'TableName'</span>, rows, chunkSize)
.returning(<span class="hljs-string">'id'</span>)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">ids</span>) </span>{ ... })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ ... });
knex.transaction(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">tr</span>) </span>{
<span class="hljs-keyword">return</span> knex.batchInsert(<span class="hljs-string">'TableName'</span>, rows, chunkSize)
.transacting(tr)
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ ... })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ ... });</code></pre></div><div id="Interfaces" data-reactid="2426"><h2 id="Interfaces" data-reactid="2427">Interfaces</h2><p data-reactid="2428">Knex.js provides several options to deal with query output. The following methods are present on the query builder, schema builder, and the raw builder:</p><h3 id="Interfaces-Promises" data-reactid="2429">Promises</h3><p data-reactid="2430"><a href="https://github.com/petkaantonov/bluebird#what-are-promises-and-why-should-i-use-them">Promises</a> are the preferred way of dealing with queries in knex, as they allow you to return values from a fulfillment handler, which in turn become the value of the promise. The main benefit of promises are the ability to catch thrown errors without crashing the node app, making your code behave like a <strong>.try / .catch / .finally</strong> in synchronous code.</p><pre data-reactid="2431"><code class="hljs js" data-reactid="2432">knex.select(<span class="hljs-string">'name'</span>)
.from(<span class="hljs-string">'users'</span>)
.where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">20</span>)
.andWhere(<span class="hljs-string">'id'</span>, <span class="hljs-string">'&lt;'</span>, <span class="hljs-number">200</span>)
.limit(<span class="hljs-number">10</span>)
.offset(x)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-keyword">return</span> _.pluck(rows, <span class="hljs-string">'name'</span>);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">names</span>) </span>{
<span class="hljs-keyword">return</span> knex.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'nicknames'</span>).whereIn(<span class="hljs-string">'nickname'</span>, names);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-built_in">console</span>.log(rows);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-built_in">console</span>.error(error)
});</code></pre><div id="Interfaces-then" data-reactid="2433"><b data-reactid="2434">then</b><!-- react-text: 2435 --><!-- /react-text --><code data-reactid="2436">.then(onFulfilled, [onRejected])</code><p data-reactid="2437">Coerces the current query builder chain into a promise state, accepting the resolve and reject handlers as specified by the Promises/A+ spec. As stated in the spec, more than one call to the then method for the current query chain will resolve with the same value, in the order they were called; the query will not be executed multiple times.</p><pre data-reactid="2438"><code class="hljs js" data-reactid="2439">knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'users'</span>)
.where({<span class="hljs-attr">name</span>: <span class="hljs-string">'Tim'</span>})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-keyword">return</span> knex.insert({<span class="hljs-attr">user_id</span>: rows[<span class="hljs-number">0</span>].id, <span class="hljs-attr">name</span>: <span class="hljs-string">'Test'</span>}, <span class="hljs-string">'id'</span>).into(<span class="hljs-string">'accounts'</span>);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">id</span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Inserted Account '</span> + id);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ <span class="hljs-built_in">console</span>.error(error); });</code></pre></div><div id="Interfaces-catch" data-reactid="2440"><b data-reactid="2441">catch</b><!-- react-text: 2442 --><!-- /react-text --><code data-reactid="2443">.catch(onRejected)</code><p data-reactid="2444">Coerces the current query builder into a promise state, catching any error thrown by the query, the same as calling .then(null, onRejected).</p><pre data-reactid="2445"><code class="hljs js" data-reactid="2446"><span class="hljs-keyword">return</span> knex.insert({<span class="hljs-attr">id</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">'Test'</span>}, <span class="hljs-string">'id'</span>)
.into(<span class="hljs-string">'accounts'</span>)
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-built_in">console</span>.error(error);
}).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'accounts'</span>)
.where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>);
}).then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">rows</span>) </span>{
<span class="hljs-built_in">console</span>.log(rows[<span class="hljs-number">0</span>]);
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-built_in">console</span>.error(error);
});</code></pre></div><div id="Interfaces-tap" data-reactid="2447"><b data-reactid="2448">tap</b><!-- react-text: 2449 --><!-- /react-text --><code data-reactid="2450">.tap(sideEffectHandler)</code><p data-reactid="2451">Executes side effects on the resolved response, ultimately returning a promise that fulfills with the original value. A thrown error or rejected promise will cause the promise to transition into a rejected state.</p><pre data-reactid="2452"><code class="hljs js" data-reactid="2453"><span class="hljs-comment">// Using only .then()</span>
query.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">x</span>) </span>{
doSideEffectsHere(x);
<span class="hljs-keyword">return</span> x;
});
<span class="hljs-comment">// Using .tap()</span>
promise.tap(doSideEffectsHere);</code></pre></div><div id="Interfaces-map" data-reactid="2454"><b data-reactid="2455">map</b><!-- react-text: 2456 --><!-- /react-text --><code data-reactid="2457">.map(mapper)</code><p data-reactid="2458">A passthrough to Bluebird&#39;s map implementation with the result set.</p><pre data-reactid="2459"><code class="hljs js" data-reactid="2460">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).limit(<span class="hljs-number">10</span>).map(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">row</span>) </span>{
<span class="hljs-keyword">return</span> row.name;
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">names</span>) </span>{ <span class="hljs-built_in">console</span>.log(names); })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{ <span class="hljs-built_in">console</span>.error(e); });</code></pre></div><div id="Interfaces-reduce" data-reactid="2461"><b data-reactid="2462">reduce</b><!-- react-text: 2463 --><!-- /react-text --><code data-reactid="2464">.reduce(reducer, [initialValue])</code><p data-reactid="2465">A passthrough to Bluebird&#39;s reduce implementation with the result set.</p><pre data-reactid="2466"><code class="hljs js" data-reactid="2467">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>).limit(<span class="hljs-number">10</span>).reduce(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">memo, row</span>) </span>{
memo.names.push(row.name);
memo.count++;
<span class="hljs-keyword">return</span> memo;
}, {<span class="hljs-attr">count</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">names</span>: []})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>) </span>{ <span class="hljs-built_in">console</span>.log(obj); })
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{ <span class="hljs-built_in">console</span>.error(e); });</code></pre></div><div id="Interfaces-bind" data-reactid="2468"><b data-reactid="2469">bind</b><!-- react-text: 2470 --><!-- /react-text --><code data-reactid="2471">.bind(context)</code><p data-reactid="2472">A passthrough to Bluebird&#39;s bind method which sets the context value (this) for the returned promise.</p><pre data-reactid="2473"><code class="hljs js" data-reactid="2474">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.limit(<span class="hljs-number">10</span>)
.bind(<span class="hljs-built_in">console</span>)
.then(<span class="hljs-built_in">console</span>.log)
.catch(<span class="hljs-built_in">console</span>.error)</code></pre></div><div id="Interfaces-return" data-reactid="2475"><b data-reactid="2476">return</b><!-- react-text: 2477 --><!-- /react-text --><code data-reactid="2478">.return(value)</code><p data-reactid="2479">Shorthand for calling .then(function() { return value }).</p><pre data-reactid="2480"><code class="hljs js" data-reactid="2481"><span class="hljs-comment">// Without return:</span>
knex.insert(values).into(<span class="hljs-string">'users'</span>)
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> {<span class="hljs-attr">inserted</span>: <span class="hljs-literal">true</span>};
});
knex.insert(values).into(<span class="hljs-string">'users'</span>).return({<span class="hljs-attr">inserted</span>: <span class="hljs-literal">true</span>});</code></pre></div><h3 id="Interfaces-Callbacks" data-reactid="2482">Callbacks</h3><div id="Interfaces-asCallback" data-reactid="2483"><b data-reactid="2484">asCallback</b><!-- react-text: 2485 --><!-- /react-text --><code data-reactid="2486">.asCallback(callback)</code><p data-reactid="2487">If you&#39;d prefer a callback interface over promises, the asCallback function accepts a standard node style callback for executing the query chain. Note that as with the then method, subsequent calls to the same query chain will return the same result.</p><pre data-reactid="2488"><code class="hljs js" data-reactid="2489">knex.select(<span class="hljs-string">'name'</span>).from(<span class="hljs-string">'users'</span>)
.where(<span class="hljs-string">'id'</span>, <span class="hljs-string">'&gt;'</span>, <span class="hljs-number">20</span>)
.andWhere(<span class="hljs-string">'id'</span>, <span class="hljs-string">'&lt;'</span>, <span class="hljs-number">200</span>)
.limit(<span class="hljs-number">10</span>)
.offset(x)
.asCallback(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err, rows</span>) </span>{
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">return</span> <span class="hljs-built_in">console</span>.error(err);
knex.select(<span class="hljs-string">'id'</span>).from(<span class="hljs-string">'nicknames'</span>)
.whereIn(<span class="hljs-string">'nickname'</span>, _.pluck(rows, <span class="hljs-string">'name'</span>))
.asCallback(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err, rows</span>) </span>{
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">return</span> <span class="hljs-built_in">console</span>.error(err);
<span class="hljs-built_in">console</span>.log(rows);
});
});</code></pre></div><h3 id="Interfaces-Streams" data-reactid="2490">Streams</h3><p data-reactid="2491">Streams are a powerful way of piping data through as it comes in, rather than all at once. You can read more about streams <a href="https://github.com/substack/stream-handbook">here at substack&#39;s stream handbook</a>. See the following for example uses of stream &amp; pipe. If you wish to use streams with PostgreSQL, you must also install the <a href="https://github.com/brianc/node-pg-query-stream">pg-query-stream</a> module. On an HTTP server, make sure to <a href="https://github.com/tgriesser/knex/wiki/Manually-Closing-Streams">manually close your streams</a> if a request is aborted.</p><div id="Interfaces-stream" data-reactid="2492"><b data-reactid="2493">stream</b><!-- react-text: 2494 --><!-- /react-text --><code data-reactid="2495">.stream([options], [callback])</code><p data-reactid="2496">If called with a callback, the callback is passed the stream and a promise is returned. Otherwise, the readable stream is returned.</p><pre data-reactid="2497"><code class="hljs js" data-reactid="2498"><span class="hljs-comment">// Retrieve the stream:</span>
<span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).stream();
stream.pipe(writableStream);
<span class="hljs-comment">// With options:</span>
<span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).stream({<span class="hljs-attr">highWaterMark</span>: <span class="hljs-number">5</span>});
stream.pipe(writableStream);
<span class="hljs-comment">// Use as a promise:</span>
<span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)
.where(knex.raw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>]))
.stream(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">stream</span>) </span>{
stream.pipe(writableStream);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// ... })</span>
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{ <span class="hljs-built_in">console</span>.error(e); });</code></pre></div><div id="Interfaces-pipe" data-reactid="2499"><b data-reactid="2500">pipe</b><!-- react-text: 2501 --><!-- /react-text --><code data-reactid="2502">.pipe(writableStream)</code><p data-reactid="2503">Pipe a stream for the current query to a writableStream.</p><pre data-reactid="2504"><code class="hljs js" data-reactid="2505"><span class="hljs-keyword">var</span> stream = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).pipe(writableStream);</code></pre></div><h3 id="Interfaces-Events" data-reactid="2506">Events</h3><div id="Interfaces-query" data-reactid="2507"><b data-reactid="2508">query</b><p data-reactid="2509">A query event is fired just before a query takes place, providing data about the query, including the connection&#39;s __cid property and any other information about the query as described in toSQL. Useful for logging all queries throughout your application.</p><pre data-reactid="2510"><code class="hljs js" data-reactid="2511">knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'users'</span>)
.on(<span class="hljs-string">'query'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) </span>{
app.log(data);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-comment">// ...</span>
});</code></pre></div><div id="Interfaces-query-error" data-reactid="2512"><b data-reactid="2513">query-error</b><p data-reactid="2514">A query-error event is fired when an error occurs when running a query, providing the error object and data about the query, including the connection&#39;s __cid property and any other information about the query as described in toSQL. Useful for logging all query errors throughout your application.</p><pre data-reactid="2515"><code class="hljs js" data-reactid="2516">knex.select([<span class="hljs-string">'NonExistentColumn'</span>])
.from(<span class="hljs-string">'users'</span>)
.on(<span class="hljs-string">'query-error'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error, obj</span>) </span>{
app.log(error);
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// ... })</span>
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{
<span class="hljs-comment">// Same error object as the query-error event provides.</span>
});</code></pre></div><div id="Interfaces-query-response" data-reactid="2517"><b data-reactid="2518">query-response</b><p data-reactid="2519">A query-response event is fired when a successful query has been run, providing the response of the query and data about the query, including the connection&#39;s __cid property and any other information about the query as described in toSQL, and finally the query builder used for the query.</p><pre data-reactid="2520"><code class="hljs js" data-reactid="2521">knex.select(<span class="hljs-string">'*'</span>)
.from(<span class="hljs-string">'users'</span>)
.on(<span class="hljs-string">'query-response'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response, obj, builder</span>) </span>{
<span class="hljs-comment">// ...</span>
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">response</span>) </span>{
<span class="hljs-comment">// Same response as the emitted event</span>
})
.catch(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">error</span>) </span>{ });</code></pre></div><h3 id="Interfaces-Other" data-reactid="2522">Other</h3><div id="Interfaces-toString" data-reactid="2523"><b data-reactid="2524">toString</b><!-- react-text: 2525 --><!-- /react-text --><code data-reactid="2526">.toString()</code><p data-reactid="2527">Returns an array of query strings filled out with the correct values based on bindings, etc. Useful for debugging, but should not be used to create queries for running them against DB.</p><pre data-reactid="2528"><code class="hljs js" data-reactid="2529"><span class="hljs-keyword">var</span> toStringQuery = knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>).where(<span class="hljs-string">'id'</span>, <span class="hljs-number">1</span>).toString();
<span class="hljs-comment">// Outputs: console.log(toStringQuery); </span>
<span class="hljs-comment">// select * from "users" where "id" = 1</span></code></pre></div><div id="Interfaces-toSQL" data-reactid="2530"><b data-reactid="2531">toSQL</b><!-- react-text: 2532 --><!-- /react-text --><code data-reactid="2533">.toSQL() and toSQL().toNative()</code><p data-reactid="2534">Returns an array of query strings filled out with the correct values based on bindings, etc. Useful for debugging and building queries for running them manually with DB driver. <code>.toSQL().toNative()</code> outputs object with sql string and bindings in a dialects format in the same way that knex internally sends them to unterlying DB driver.</p><pre data-reactid="2535"><code class="hljs js" data-reactid="2536">knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)
.where(knex.raw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>]))
.toSQL()
<span class="hljs-comment">// Outputs:</span>
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// bindings: [1],</span>
<span class="hljs-comment">// method: 'select',</span>
<span class="hljs-comment">// sql: 'select * from "users" where id = ?',</span>
<span class="hljs-comment">// options: undefined,</span>
<span class="hljs-comment">// toNative: function () {}</span>
<span class="hljs-comment">// }</span>
knex.select(<span class="hljs-string">'*'</span>).from(<span class="hljs-string">'users'</span>)
.where(knex.raw(<span class="hljs-string">'id = ?'</span>, [<span class="hljs-number">1</span>]))
.toSQL().toNative()
<span class="hljs-comment">// Outputs for postgresql dialect:</span>
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// bindings: [1],</span>
<span class="hljs-comment">// sql: 'select * from "users" where id = $1',</span>
<span class="hljs-comment">// }</span></code></pre></div></div><div id="Migrations" data-reactid="2537"><h2 id="Migrations" data-reactid="2538">Migrations</h2><p data-reactid="2539">Migrations allow for you to define sets of schema changes so upgrading a database is a breeze.</p><h3 id="Migrations-CLI" data-reactid="2540">Migration CLI</h3><p data-reactid="2541">The migration CLI is bundled with the knex install, and is driven by the <a href="https://github.com/tkellen/node-liftoff">node-liftoff</a> module. To install globally, run:</p><pre data-reactid="2542"><code data-reactid="2543">$ npm install knex -g</code></pre><p data-reactid="2544">Migrations use a <strong>knexfile</strong>, which specify various configuration settings for the module. To create a new knexfile, run the following:</p><pre data-reactid="2545"><code data-reactid="2546">$ knex init
# or for .coffee
$ knex init -x coffee</code></pre><p data-reactid="2547">will create a sample knexfile.js - the file which contains our various database configurations. Once you have a knexfile.js, you can use the migration tool to create migration files to the specified directory (default migrations). Creating new migration files can be achieved by running:</p><pre data-reactid="2548"><code data-reactid="2549">$ knex migrate:make migration_name</code></pre><p data-reactid="2550">Once you have finished writing the migrations, you can update the database matching your <code>NODE_ENV</code> by running:</p><pre data-reactid="2551"><code data-reactid="2552">$ knex migrate:latest</code></pre><p data-reactid="2553">You can also pass the <code>--env</code> flag or set <code>NODE_ENV</code> to select an alternative environment:</p><pre data-reactid="2554"><code data-reactid="2555">$ knex migrate:latest --env production
# or
$ NODE_ENV=production knex migrate:latest</code></pre><p data-reactid="2556">To rollback the last batch of migrations:</p><pre data-reactid="2557"><code data-reactid="2558">$ knex migrate:rollback</code></pre><h2 id="Seeds" data-reactid="2559">Seed files</h2><p data-reactid="2560">Seed files allow you to populate your database with test or seed data independent of your migration files.</p><h3 id="Seeds-CLI" data-reactid="2561">Seed CLI</h3><p data-reactid="2562">To create a seed file, run:</p><pre data-reactid="2563"><code data-reactid="2564">$ knex seed:make seed_name</code></pre><p data-reactid="2565">Seed files are created in the directory specified in your knexfile.js for the current environment. A sample seed configuration looks like:</p><pre data-reactid="2566"><code data-reactid="2567">development: {
client: ...,
connection: { ... },
seeds: {
directory: './seeds/dev'
}
}</code></pre><div data-reactid="2568"><p data-reactid="2569">If no <code>seeds.directory</code> is defined, files are created in <code>./seeds</code>. Note that the seed directory needs to be a relative path. Absolute paths are not supported (nor is it good practice).</p><p data-reactid="2570">To run seed files, execute:</p></div><pre data-reactid="2571"><code data-reactid="2572">$ knex seed:run</code></pre><p data-reactid="2573">Seed files are executed in alphabetical order. Unlike migrations, <em>every</em> seed file will be executed when you run the command. You should design your seed files to reset tables as needed before inserting data.</p><h3 id="knexfile" data-reactid="2574">knexfile.js</h3><p data-reactid="2575">A knexfile.js or knexfile.coffee generally contains all of the configuration for your database. It can optionally provide different configuration for different environments. You may pass a <code>--knexfile</code> option to any of the command line statements to specify an alternate path to your knexfile.</p><h4 data-reactid="2576">Basic configuration:</h4><pre data-reactid="2577"><code class="hljs js" data-reactid="2578"><span class="hljs-built_in">module</span>.exports = {
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: process.env.DATABASE_URL || { <span class="hljs-attr">user</span>: <span class="hljs-string">'me'</span>, <span class="hljs-attr">database</span>: <span class="hljs-string">'my_app'</span> }
};</code></pre><h4 data-reactid="2579">Environment configuration:</h4><pre data-reactid="2580"><code class="hljs js" data-reactid="2581"><span class="hljs-built_in">module</span>.exports = {
<span class="hljs-attr">development</span>: {
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">connection</span>: { <span class="hljs-attr">user</span>: <span class="hljs-string">'me'</span>, <span class="hljs-attr">database</span>: <span class="hljs-string">'my_app'</span> }
},
<span class="hljs-attr">production</span>: { <span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>, <span class="hljs-attr">connection</span>: process.env.DATABASE_URL }
};</code></pre><h4 data-reactid="2582">Custom migration:</h4><p data-reactid="2583">You may provide a custom migration stub to be used in place of the default option.</p><pre data-reactid="2584"><code class="hljs js" data-reactid="2585"><span class="hljs-built_in">module</span>.exports = {
<span class="hljs-attr">client</span>: <span class="hljs-string">'pg'</span>,
<span class="hljs-attr">migration</span>: {
<span class="hljs-attr">stub</span>: <span class="hljs-string">'migration.stub'</span>
}
};</code></pre><h3 id="Migrations-API" data-reactid="2586">Migration API</h3><div data-reactid="2587"><p data-reactid="2588"><code>knex.migrate</code> is the class utilized by the knex migrations cli.</p><p data-reactid="2589">Each method takes an optional <code>config</code> object, which may specify the following properties:</p></div><ul data-reactid="2590"><li data-reactid="2591"><code>directory</code>: a relative path to the directory containing the migration files (default <code>./migrations</code>)</li><li data-reactid="2592"><code>extension</code>: the file extension used for the generated migration files (default <code>js</code>)</li><li data-reactid="2593"><code>tableName</code>: the table name used for storing the migration state (default <code>knex_migrations</code>)</li><li data-reactid="2594"><code>disableTransactions</code>: don&#39;t run migrations inside transactions (default <code>false</code>)</li><li data-reactid="2595"><code>loadExtensions</code>: array of file extensions which knex will treat as migrations. For example, if you have typescript transpiled into javascript in the same folder, you want to execute only javascript migrations. In this case, set <code>loadExtensions</code> to <code>[&#39;.js&#39;]</code> (Notice the dot!) (default <code>[&#39;.co&#39;, &#39;.coffee&#39;, &#39;.eg&#39;, &#39;.iced&#39;, &#39;.js&#39;, &#39;.litcoffee&#39;, &#39;.ls&#39;, &#39;.ts&#39;]</code>)</li></ul><h4 id="Migrations-API-transactions" data-reactid="2596">Transactions in migrations</h4><p data-reactid="2597">By default, each migration is run inside a transaction. Whenever needed, one can disable transactions for all migrations via the common migration config option <code>config.disableTransactions</code> or per-migration, via exposing a boolean property <code>config.transaction</code> from a migration file:</p><pre data-reactid="2598"><code class="hljs js" data-reactid="2599">exports.up = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">knex, Promise</span>) </span>{ <span class="hljs-comment">/* ... */</span> };
exports.down = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">knex, Promise</span>) </span>{ <span class="hljs-comment">/* ... */</span> };
exports.config = { <span class="hljs-attr">transaction</span>: <span class="hljs-literal">false</span> };</code></pre><p data-reactid="2600">The same config property can be used for enabling transaction per-migration in case the common configuration has <code>disableTransactions: true</code>.</p><div id="Migrations-make" data-reactid="2601"><b data-reactid="2602">make</b><!-- react-text: 2603 --><!-- /react-text --><code data-reactid="2604">knex.migrate.make(name, [config])</code><p data-reactid="2605">Creates a new migration, with the name of the migration being added.</p></div><div id="Migrations-latest" data-reactid="2606"><b data-reactid="2607">latest</b><!-- react-text: 2608 --><!-- /react-text --><code data-reactid="2609">knex.migrate.latest([config])</code><p data-reactid="2610">Runs all migrations that have not yet been run.</p></div><p data-reactid="2611">If you need to run something only after all migrations have finished their execution, you can do something like this:</p><pre data-reactid="2612"><code class="hljs js" data-reactid="2613">knex.migrate.latest()
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> knex.seed.run();
})
.then(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-comment">// migrations are finished</span>
});</code></pre><div id="Migrations-rollback" data-reactid="2614"><b data-reactid="2615">rollback</b><!-- react-text: 2616 --><!-- /react-text --><code data-reactid="2617">knex.migrate.rollback([config])</code><p data-reactid="2618">Rolls back the latest migration group.</p></div><div id="Migrations-currentVersion" data-reactid="2619"><b data-reactid="2620">currentVersion</b><!-- react-text: 2621 --><!-- /react-text --><code data-reactid="2622">knex.migrate.currentVersion([config])</code><p data-reactid="2623">Retrieves and returns the current migration version, as a promise. If there aren&#39;t any migrations run yet, returns &quot;none&quot; as the value for the currentVersion.</p></div><h3 id="Notes-about-locks" data-reactid="2624">Notes about locks</h3><p data-reactid="2625">A lock system is there to prevent multiple processes from running the same migration batch in the same time. When a batch of migrations is about to be run, the migration system first tries to get a lock using a <code>SELECT ... FOR UPDATE</code> statement (preventing race conditions from happening). If it can get a lock, the migration batch will run. If it can&#39;t, it will wait until the lock is released.</p><p data-reactid="2626">Please note that if your process unfortunately crashes, the lock will have to be <em>manually</em> removed in order to let migrations run again. The locks are saved in a table called &quot;<code>tableName</code>_lock&quot;; it has single one column called <code>is_locked</code> that you need to set to <code>0</code> in order to release the lock.</p></div><div id="Seeds" data-reactid="2627"><h3 id="Seeds-API" data-reactid="2628">Seed API</h3><div data-reactid="2629"><p data-reactid="2630"><code>knex.seed</code> is the class utilized by the knex seed CLI.</p><p data-reactid="2631">Each method takes an optional <code>config</code> object, which may specify the following properties:</p></div><ul data-reactid="2632"><li data-reactid="2633"><code>directory</code>: a relative path to the directory containing the seed files (default <code>./seeds</code>)</li><li data-reactid="2634"><code>loadExtensions</code>: array of file extensions which knex will treat as seeds. For example, if you have typescript transpiled into javascript in the same folder, you want to execute only javascript seeds. In this case, set <code>loadExtensions</code> to <code>[&#39;.js&#39;]</code> (Notice the dot!) (default <code>[&#39;.co&#39;, &#39;.coffee&#39;, &#39;.eg&#39;, &#39;.iced&#39;, &#39;.js&#39;, &#39;.litcoffee&#39;, &#39;.ls&#39;, &#39;.ts&#39;]</code>)</li></ul><h4 id="Seeds-API-methods" data-reactid="2635">Methods</h4><div id="Seeds-make" data-reactid="2636"><b data-reactid="2637">make</b><!-- react-text: 2638 --><!-- /react-text --><code data-reactid="2639">knex.seed.make(name, [config])</code><p data-reactid="2640">Creates a new seed file, with the name of the seed file being added.</p></div><div id="Seeds-run" data-reactid="2641"><b data-reactid="2642">run</b><!-- react-text: 2643 --><!-- /react-text --><code data-reactid="2644">knex.seed.run([config])</code><p data-reactid="2645">Runs all seed files for the current environment.</p></div></div><div id="Support" data-reactid="2646"><h2 id="support" data-reactid="2647">Support</h2><p data-reactid="2648">Have questions about the library? Come join us in the <a href="http://webchat.freenode.net/?channels=bookshelf">#bookshelf freenode IRC</a> channel for support on knex.js and <a href="http://bookshelfjs.org">bookshelf.js</a>, or post an issue on <a href="http://stackoverflow.com/questions/tagged/knex.js">Stack Overflow</a> or in the GitHub <a href="https://github.com/tgriesser/knex/issues">issue tracker</a>.</p></div><div id="Faq" data-reactid="2649"><h2 id="faq" data-reactid="2650">F.A.Q.</h2><div data-reactid="2651"><p data-reactid="2652"><strong>How do I help contribute?</strong><br>Glad you ask! Pull requests, or feature requests, though not always implemented, are a great way to help make Knex even better than it is now. If you&#39;re looking for something specific to help out with, there&#39;s a number of unit tests that aren&#39;t implemented yet, the library could never have too many of those. If you want to submit a fix or feature, take a look at the <a href="https://github.com/tgriesser/knex/blob/master/CONTRIBUTING.md">Contributing</a> readme in the Github and go ahead and open a ticket.</p><p data-reactid="2653"><strong>How do I debug?</strong><br>Knex is beginning to make use of the <a href="https://github.com/visionmedia/debug">debug</a> module internally, so you can set the <code>DEBUG</code> environment variable to <code>knex:*</code> to see all debugging, or select individual namespaces <code>DEBUG=knex:query,knex:tx</code> to constrain a bit.</p><p data-reactid="2654">If you pass <code>{debug: true}</code> as one of the options in your initialize settings, you can see all of the query calls being made. Sometimes you need to dive a bit further into the various calls and see what all is going on behind the scenes. I&#39;d recommend <a href="https://github.com/dannycoates/node-inspector">node-inspector</a>, which allows you to debug code with <code>debugger</code> statements like you would in the browser.</p><p data-reactid="2655">At the start of your application code will catch any errors not otherwise caught in the normal promise chain handlers, which is very helpful in debugging.</p><p data-reactid="2656"><strong>How do I run the test suite?</strong><br>The test suite looks for an environment variable called <code>KNEX_TEST</code> for the path to the database configuration. If you run the following command:</p></div><pre data-reactid="2657"><code data-reactid="2658">$ export KNEX_TEST='/path/to/your/knex_config.js'
$ npm test</code></pre><div data-reactid="2659"><p data-reactid="2660">replacing with the path to your config file, and the config file is valid, the test suite should run properly.</p><p data-reactid="2661"><strong>My tests are failing because slow DB connection and short test timeouts! How to extend test timeouts?</strong><br>Sometimes, e.g. when running CI on travis, test suite&#39;s default timeout of 5 seconds might be too short. In such cases an alternative test timeout value in milliseconds can be specified using the <code>KNEX_TEST_TIMEOUT</code> environment variable.</p></div><pre data-reactid="2662"><code data-reactid="2663">$ export KNEX_TEST_TIMEOUT=30000
$ npm test</code></pre><p data-reactid="2664"><strong>Can I use Knex outside of Node.js</strong><br>Yes. While the WebSQL spec is deprecated, there is still an adapter that provides support. You will need to use a build tool like browserify or webpack for a browser build.</p></div><div id="changelog" data-reactid="2665"><h2 data-reactid="2666">Change Log</h2><div data-reactid="2667"><b class="header" data-reactid="2668">0.14.2</b><!-- react-text: 2669 --><!-- /react-text --><small data-reactid="2670"><i data-reactid="2671">24 Nov, 2017</i></small><br data-reactid="2672"/><b data-reactid="2673">Bug fixes:</b><ul data-reactid="2674"><li data-reactid="2675">Fix sqlite3 truncate method to work again <a href="https://github.com/tgriesser/knex/issues/2348">#2348</a></li></ul></div><div data-reactid="2676"><b class="header" data-reactid="2677">0.14.1</b><!-- react-text: 2678 --><!-- /react-text --><small data-reactid="2679"><i data-reactid="2680">19 Nov, 2017</i></small><br data-reactid="2681"/><b data-reactid="2682">Bug fixes:</b><ul data-reactid="2683"><li data-reactid="2684">Fix support for multiple schema names in in postgres <code>searchPath</code> <a href="https://github.com/tgriesser/knex/issues/2340">#2340</a></li><li data-reactid="2685">Fix create new connection to pass errors to query instead of retry loop <a href="https://github.com/tgriesser/knex/issues/2336">#2336</a></li><li data-reactid="2686">Fix recognition of connections closed by server <a href="https://github.com/tgriesser/knex/issues/2341">#2341</a></li></ul></div><div data-reactid="2687"><b class="header" data-reactid="2688">0.14.0</b><!-- react-text: 2689 --><!-- /react-text --><small data-reactid="2690"><i data-reactid="2691">6 Nov, 2017</i></small><br data-reactid="2692"/><b data-reactid="2693">Breaking Changes:</b><ul data-reactid="2694"><li data-reactid="2695">Remove sorting of statements from update queries <a href="https://github.com/tgriesser/knex/issues/2171">#2171</a></li><li data-reactid="2696">Updated allowed operator list with some missing operators and make all to lower case <a href="https://github.com/tgriesser/knex/issues/2239">#2239</a></li><li data-reactid="2697">Use node-mssql 4.0.0 <a href="https://github.com/tgriesser/knex/issues/2029">#2029</a></li><li data-reactid="2698">Support for enum columns to SQlite3 dialect <a href="https://github.com/tgriesser/knex/issues/2055">#2055</a></li><li data-reactid="2699">Better identifier quoting in Sqlite3 <a href="https://github.com/tgriesser/knex/issues/2087">#2087</a></li><li data-reactid="2700">Migration Errors - Display filename of of failed migration <a href="https://github.com/tgriesser/knex/issues/2272">#2272</a></li></ul><b data-reactid="2701">Other Features:</b><ul data-reactid="2702"><li data-reactid="2703">Post processing hook for query result <a href="https://github.com/tgriesser/knex/issues/2261">#2261</a></li><li data-reactid="2704">Build native SQL where binding parameters are dialect specific <a href="https://github.com/tgriesser/knex/issues/2237">#2237</a></li><li data-reactid="2705">Configuration option to allow override identifier wrapping <a href="https://github.com/tgriesser/knex/issues/2217">#2217</a></li><li data-reactid="2706">Implemented select syntax: select({ alias: &#39;column&#39; }) <a href="https://github.com/tgriesser/knex/issues/2227">#2227</a></li><li data-reactid="2707">Allows to filter seeds and migrations by extensions <a href="https://github.com/tgriesser/knex/issues/2168">#2168</a></li><li data-reactid="2708">Reconnecting after database server disconnect/reconnect + tests <a href="https://github.com/tgriesser/knex/issues/2017">#2017</a></li><li data-reactid="2709">Removed filering from allowed configuration settings of mysql2 <a href="https://github.com/tgriesser/knex/issues/2040">#2040</a></li><li data-reactid="2710">Allow raw expressions in query builder aggregate methods <a href="https://github.com/tgriesser/knex/issues/2257">#2257</a></li><li data-reactid="2711">Throw error on non-string table comment <a href="https://github.com/tgriesser/knex/issues/2126">#2126</a></li><li data-reactid="2712">Support for mysql stream query options <a href="https://github.com/tgriesser/knex/issues/2301">#2301</a></li></ul><b data-reactid="2713">Bug fixes:</b><ul data-reactid="2714"><li data-reactid="2715">Allow update queries and passing query builder to with statements <a href="https://github.com/tgriesser/knex/issues/2298">#2298</a></li><li data-reactid="2716">Fix escape table name in SQLite columnInfo call <a href="https://github.com/tgriesser/knex/issues/2281">#2281</a></li><li data-reactid="2717">Preventing containsUndefined from going to recursion loop <a href="https://github.com/tgriesser/knex/issues/1711">#1711</a></li><li data-reactid="2718">Fix error caused by call to knex.migrate.currentVersion <a href="https://github.com/tgriesser/knex/issues/2123">#2123</a></li><li data-reactid="2719">Upgraded generic-pool to 3.1.7 (did resolve some memory issues) <a href="https://github.com/tgriesser/knex/issues/2208">#2208</a></li><li data-reactid="2720">Allow using NOT ILIKE operator <a href="https://github.com/tgriesser/knex/issues/2195">#2195</a></li><li data-reactid="2721">Fix postgres searchPath to be case-sensitive <a href="https://github.com/tgriesser/knex/issues/2172">#2172</a></li><li data-reactid="2722">Fix drop of multiple columns in sqlite3 <a href="https://github.com/tgriesser/knex/issues/2107">#2107</a></li><li data-reactid="2723">Fix adding multiple columns in Oracle <a href="https://github.com/tgriesser/knex/issues/2115">#2115</a></li><li data-reactid="2724">Use selected schema when dropping indices in Postgres. <a href="https://github.com/tgriesser/knex/issues/2105">#2105</a></li><li data-reactid="2725">Fix hasTable for MySQL to not do partial matches <a href="https://github.com/tgriesser/knex/issues/2097">#2097</a></li><li data-reactid="2726">Fix setting autoTransaction in batchInsert <a href="https://github.com/tgriesser/knex/issues/2113">#2113</a></li><li data-reactid="2727">Fix connection error propagation when streaming <a href="https://github.com/tgriesser/knex/issues/2199">#2199</a></li><li data-reactid="2728">Fix comments not being applied to increments columns <a href="https://github.com/tgriesser/knex/issues/2243">#2243</a></li><li data-reactid="2729">Fix mssql wrong binding order of queries that combine a limit with select raw or update <a href="https://github.com/tgriesser/knex/issues/2066">#2066</a></li><li data-reactid="2730">Fixed mysql alter table attributes order <a href="https://github.com/tgriesser/knex/issues/2062">#2062</a></li></ul><b data-reactid="2731">Test / internal changes</b><ul data-reactid="2732"><li data-reactid="2733">Update each out-of-date dependency according to david-dm.org <a href="https://github.com/tgriesser/knex/issues/2297">#2297</a></li><li data-reactid="2734">Update v8flags to version 3.0.0 <a href="https://github.com/tgriesser/knex/issues/2288">#2288</a></li><li data-reactid="2735">Update interpret version <a href="https://github.com/tgriesser/knex/issues/2283">#2283</a></li><li data-reactid="2736">Fix debug output typo <a href="https://github.com/tgriesser/knex/issues/2187">#2187</a></li><li data-reactid="2737">Docker CI tests <a href="https://github.com/tgriesser/knex/issues/2164">#2164</a></li><li data-reactid="2738">Unit test for right/rightOuterJoin combination <a href="https://github.com/tgriesser/knex/issues/2117">#2117</a></li><li data-reactid="2739">Unit test for fullOuterJoin <a href="https://github.com/tgriesser/knex/issues/2118">#2118</a></li><li data-reactid="2740">Unit tests for table comment <a href="https://github.com/tgriesser/knex/issues/2098">#2098</a></li><li data-reactid="2741">Test referencing non-existent column with sqlite3 <a href="https://github.com/tgriesser/knex/issues/2104">#2104</a></li><li data-reactid="2742">Unit test for renaming column in postgresql <a href="https://github.com/tgriesser/knex/issues/2099">#2099</a></li><li data-reactid="2743">Unit test for cross-join <a href="https://github.com/tgriesser/knex/issues/2102">#2102</a></li><li data-reactid="2744">Fix incorrect parameter name <a href="https://github.com/tgriesser/knex/issues/2068">#2068</a></li></ul></div><div data-reactid="2745"><a href="#" data-reactid="2746">Show Full Changelog</a><div class="more-changelog" data-reactid="2747"><div data-reactid="2748"><b class="header" data-reactid="2749">0.13.0</b><!-- react-text: 2750 --><!-- /react-text --><small data-reactid="2751"><i data-reactid="2752">29 Apr, 2017</i></small><br data-reactid="2753"/><b data-reactid="2754">Breaking Changes:</b><ul data-reactid="2755"><li data-reactid="2756">Multiple concurrent migration runners blocks instead of throwing error when possible <a href="https://github.com/tgriesser/knex/issues/1962">#1962</a></li><li data-reactid="2757">Fixed transaction promise mutation issue <a href="https://github.com/tgriesser/knex/issues/1991">#1991</a></li></ul><b data-reactid="2758">Other Changes:</b><ul data-reactid="2759"><li data-reactid="2760">Allow passing version of connected db in configuration file <a href="https://github.com/tgriesser/knex/issues/1993">#1993</a></li><li data-reactid="2761">Bugfixes on batchInsert and transactions for mysql/maria <a href="https://github.com/tgriesser/knex/issues/1992">#1992</a></li><li data-reactid="2762">Add fetchAsString optional parameter to oracledb dialect <a href="https://github.com/tgriesser/knex/issues/1998">#1998</a></li><li data-reactid="2763">fix: escapeObject parameter order for Postgres dialect. <a href="https://github.com/tgriesser/knex/issues/2003">#2003</a></li></ul></div><div data-reactid="2764"><b class="header" data-reactid="2765">0.12.9</b><!-- react-text: 2766 --><!-- /react-text --><small data-reactid="2767"><i data-reactid="2768">23 Mar, 2017</i></small><br data-reactid="2769"/><p data-reactid="2770">Fixed unhandled exception in batchInsert when the rows to be inserted resulted in duplicate key violation <a href="https://github.com/tgriesser/knex/issues/1880">#1880</a></p></div><div data-reactid="2771"><b class="header" data-reactid="2772">0.12.8</b><!-- react-text: 2773 --><!-- /react-text --><small data-reactid="2774"><i data-reactid="2775">15 Mar, 2017</i></small><br data-reactid="2776"/><ul data-reactid="2777"><li data-reactid="2778">Added clearSelect and clearWhere to query builder <a href="https://github.com/tgriesser/knex/issues/1912">#1912</a></li><li data-reactid="2779">Properly close Postgres query streams on error <a href="https://github.com/tgriesser/knex/issues/1935">#1935</a></li><li data-reactid="2780">Transactions should never reject with undefined <a href="https://github.com/tgriesser/knex/issues/1970">#1970</a></li><li data-reactid="2781">Clear acquireConnectionTimeout if an error occurs when acquiring a connection <a href="https://github.com/tgriesser/knex/issues/1973">#1973</a></li></ul></div><div data-reactid="2782"><b class="header" data-reactid="2783">0.12.7</b><!-- react-text: 2784 --><!-- /react-text --><small data-reactid="2785"><i data-reactid="2786">17 Feb, 2017</i></small><br data-reactid="2787"/><b data-reactid="2788">Accidental Breaking Change:</b><ul data-reactid="2789"><li data-reactid="2790">Ensure that &#39;client&#39; is provided in knex config object <a href="https://github.com/tgriesser/knex/issues/1822">#1822</a></li></ul><b data-reactid="2791">Other Changes:</b><ul data-reactid="2792"><li data-reactid="2793">Support custom foreign key names <a href="https://github.com/tgriesser/knex/issues/1311">#1311</a>, <a href="https://github.com/tgriesser/knex/issues/1726">#1726</a></li><li data-reactid="2794">Fixed named bindings to work with queries containing <code>:</code>-chars <a href="https://github.com/tgriesser/knex/issues/1890">#1890</a></li><li data-reactid="2795">Exposed more promise functions <a href="https://github.com/tgriesser/knex/issues/1896">#1896</a></li><li data-reactid="2796">Pass rollback errors to transaction promise in mssql <a href="https://github.com/tgriesser/knex/issues/1885">#1885</a></li><li data-reactid="2797">ONLY keyword support for PostgreSQL (for table inheritance) <a href="https://github.com/tgriesser/knex/issues/1874">#1874</a></li><li data-reactid="2798">Fixed Mssql update with join syntax <a href="https://github.com/tgriesser/knex/issues/1777">#1777</a></li><li data-reactid="2799">Replace migrations and seed for react-native packager <a href="https://github.com/tgriesser/knex/issues/1813">#1813</a></li><li data-reactid="2800">Support knexfile, migration and seeds in TypeScript <a href="https://github.com/tgriesser/knex/issues/1769">#1769</a></li><li data-reactid="2801">Fix float to integer conversion of decimal fields in MSSQL <a href="https://github.com/tgriesser/knex/issues/1781">#1781</a></li><li data-reactid="2802">External authentication capability when using oracledb driver <a href="https://github.com/tgriesser/knex/issues/1716">#1716</a></li><li data-reactid="2803">Fixed MSSQL incorect query build when locks are used <a href="https://github.com/tgriesser/knex/issues/1707">#1707</a></li><li data-reactid="2804">Allow to use <code>first</code> method as aliased select <a href="https://github.com/tgriesser/knex/issues/1784">#1784</a></li><li data-reactid="2805">Alter column for nullability, type and default value <a href="https://github.com/tgriesser/knex/issues/46">#46</a>, <a href="https://github.com/tgriesser/knex/issues/1759">#1759</a></li><li data-reactid="2806">Add more having<em> methods / join clause on</em> methods <a href="https://github.com/tgriesser/knex/issues/1674">#1674</a></li><li data-reactid="2807">Compatibility fixes and cleanups <a href="https://github.com/tgriesser/knex/issues/1788">#1788</a>, <a href="https://github.com/tgriesser/knex/issues/1792">#1792</a>, <a href="https://github.com/tgriesser/knex/issues/1794">#1794</a>, <a href="https://github.com/tgriesser/knex/issues/1814">#1814</a>, <a href="https://github.com/tgriesser/knex/issues/1857">#1857</a>, <a href="https://github.com/tgriesser/knex/issues/1649">#1649</a></li></ul></div><div data-reactid="2808"><b class="header" data-reactid="2809">0.12.6</b><!-- react-text: 2810 --><!-- /react-text --><small data-reactid="2811"><i data-reactid="2812">19 Oct, 2016</i></small><br data-reactid="2813"/><ul data-reactid="2814"><li data-reactid="2815">Address warnings mentioned in <a href="https://github.com/tgriesser/knex/issues/1388">#1388</a> (<a href="https://github.com/tgriesser/knex/issues/1740">#1740</a>)</li><li data-reactid="2816">Remove postinstall script (<a href="https://github.com/tgriesser/knex/issues/1746">#1746</a>)</li></ul></div><div data-reactid="2817"><b class="header" data-reactid="2818">0.12.5</b><!-- react-text: 2819 --><!-- /react-text --><small data-reactid="2820"><i data-reactid="2821">12 Oct, 2016</i></small><br data-reactid="2822"/><ul data-reactid="2823"><li data-reactid="2824">Fix broken 0.12.4 build (removed from npm)</li><li data-reactid="2825">Fix <a href="https://github.com/tgriesser/knex/issues/1733">#1733</a>, <a href="https://github.com/tgriesser/knex/issues/920">#920</a>, incorrect postgres array bindings</li></ul></div><div data-reactid="2826"><b class="header" data-reactid="2827">0.12.3</b><!-- react-text: 2828 --><!-- /react-text --><small data-reactid="2829"><i data-reactid="2830">9 Oct, 2016</i></small><br data-reactid="2831"/><ul data-reactid="2832"><li data-reactid="2833">Fix <a href="https://github.com/tgriesser/knex/issues/1703">#1703</a>, <a href="https://github.com/tgriesser/knex/issues/1694">#1694</a> - connections should be returned to pool if acquireConnectionTimeout is triggered</li><li data-reactid="2834">Fix <a href="https://github.com/tgriesser/knex/issues/1710">#1710</a> regression in postgres array escaping</li></ul></div><div data-reactid="2835"><b class="header" data-reactid="2836">0.12.2</b><!-- react-text: 2837 --><!-- /react-text --><small data-reactid="2838"><i data-reactid="2839">27 Sep, 2016</i></small><br data-reactid="2840"/><ul data-reactid="2841"><li data-reactid="2842">Restore pool min: 1 for sqlite3, <a href="https://github.com/tgriesser/knex/issues/1701">#1701</a></li><li data-reactid="2843">Fix for connection error after it&#39;s closed / released, <a href="https://github.com/tgriesser/knex/issues/1691">#1691</a></li><li data-reactid="2844">Fix oracle prefetchRowCount setting, <a href="https://github.com/tgriesser/knex/issues/1675">#1675</a></li></ul></div><div data-reactid="2845"><b class="header" data-reactid="2846">0.12.1</b><!-- react-text: 2847 --><!-- /react-text --><small data-reactid="2848"><i data-reactid="2849">16 Sep, 2016</i></small><br data-reactid="2850"/><ul data-reactid="2851"><li data-reactid="2852">Fix MSSQL sql execution error, <a href="https://github.com/tgriesser/knex/issues/1669">#1669</a></li><li data-reactid="2853">Added DEBUG=knex:bindings for debugging query bindings, <a href="https://github.com/tgriesser/knex/issues/1557">#1557</a></li></ul></div><div data-reactid="2854"><b class="header" data-reactid="2855">0.12.0</b><!-- react-text: 2856 --><!-- /react-text --><small data-reactid="2857"><i data-reactid="2858">13 Sep, 2016</i></small><br data-reactid="2859"/><ul data-reactid="2860"><li data-reactid="2861">Remove build / built files, <a href="https://github.com/tgriesser/knex/issues/1616">#1616</a></li><li data-reactid="2862">Upgrade to Babel 6, <a href="https://github.com/tgriesser/knex/issues/1617">#1617</a></li><li data-reactid="2863">Reference Bluebird module directly, remove deprecated .exec method, <a href="https://github.com/tgriesser/knex/issues/1618">#1618</a></li><li data-reactid="2864">Remove documentation files from main repo</li><li data-reactid="2865">Fix broken behavior on WebSQL build, <a href="https://github.com/tgriesser/knex/issues/1638">#1638</a></li><li data-reactid="2866">Oracle id sequence now handles manual inserts, <a href="https://github.com/tgriesser/knex/issues/906">#906</a></li><li data-reactid="2867">Cleanup PG escaping, fix <a href="https://github.com/tgriesser/knex/issues/1602">#1602</a>, <a href="https://github.com/tgriesser/knex/issues/1548">#1548</a></li><li data-reactid="2868">Added <a href="#Builder-with"><code>with</code></a> to builder for <a href="https://www.postgresql.org/docs/9.4/static/queries-with.html">common table expressions</a>, <a href="https://github.com/tgriesser/knex/issues/1599">#1599</a></li><li data-reactid="2869">Fix <a href="https://github.com/tgriesser/knex/issues/1619">#1619</a>, pluck with explicit column names</li><li data-reactid="2870">Switching back to <a href="https://github.com/coopernurse/node-pool">generic-pool</a> for pooling resource management</li><li data-reactid="2871">Removed index.html, please direct all PR&#39;s for docs against the files in <a href="https://github.com/knex/documentation">knex/documentation</a></li></ul></div><div data-reactid="2872"><b class="header" data-reactid="2873">0.11.10</b><!-- react-text: 2874 --><!-- /react-text --><small data-reactid="2875"><i data-reactid="2876">9 Aug, 2016</i></small><br data-reactid="2877"/><ul data-reactid="2878"><li data-reactid="2879">Added CHANGELOG.md for a <a href="https://github.com/knex/documentation">new documentation</a> builder coming soon, <a href="https://github.com/tgriesser/knex/issues/1615">#1615</a></li><li data-reactid="2880">Minor documentation tweaks</li><li data-reactid="2881">PG: Fix Uint8Array being considered undefined, <a href="https://github.com/tgriesser/knex/issues/1601">#1601</a></li><li data-reactid="2882">MSSQL: Make columnInfo schema dynamic, <a href="https://github.com/tgriesser/knex/issues/1585">#1585</a></li></ul></div><div data-reactid="2883"><b class="header" data-reactid="2884">0.11.9</b><!-- react-text: 2885 --><!-- /react-text --><small data-reactid="2886"><i data-reactid="2887">21 Jul, 2016</i></small><br data-reactid="2888"/><p data-reactid="2889">Reverted knex client breaking change (commit b74cd69e906), fixes <a href="https://github.com/tgriesser/knex/issues/1587">#1587</a></p></div><div data-reactid="2890"><b class="header" data-reactid="2891">0.11.8</b><!-- react-text: 2892 --><!-- /react-text --><small data-reactid="2893"><i data-reactid="2894">21 Jul, 2016</i></small><br data-reactid="2895"/><ul data-reactid="2896"><li data-reactid="2897">Oracledb dialect <a href="https://github.com/tgriesser/knex/issues/990">#990</a></li><li data-reactid="2898">Documentation fix <a href="https://github.com/tgriesser/knex/issues/1532">#1532</a></li><li data-reactid="2899">Allow named bindings to be escaped. <a href="https://github.com/tgriesser/knex/issues/1576">#1576</a></li><li data-reactid="2900">Several bugs with MS SQL schema creation and installing from gihub fix <a href="https://github.com/tgriesser/knex/issues/1577">#1577</a></li><li data-reactid="2901">Fix incorrect escaping of backslashes in SqlString.escape <a href="https://github.com/tgriesser/knex/issues/1545">#1545</a></li></ul></div><div data-reactid="2902"><b class="header" data-reactid="2903">0.11.7</b><!-- react-text: 2904 --><!-- /react-text --><small data-reactid="2905"><i data-reactid="2906">19 Jun, 2016</i></small><br data-reactid="2907"/><p data-reactid="2908">Add missing dependency. <a href="https://github.com/tgriesser/knex/issues/1516">#1516</a></p></div><div data-reactid="2909"><b class="header" data-reactid="2910">0.11.6</b><!-- react-text: 2911 --><!-- /react-text --><small data-reactid="2912"><i data-reactid="2913">18 Jun, 2016</i></small><br data-reactid="2914"/><ul data-reactid="2915"><li data-reactid="2916">Allow cancellation on timeout (MySQL) <a href="https://github.com/tgriesser/knex/issues/1454">#1454</a></li><li data-reactid="2917">Better bigint support. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1445">#1445</a></li><li data-reactid="2918">More consistent handling of <code>undefined</code> values in <code>QueryBuilder#where</code> and <code>Raw</code>. <a href="https://github.com/tgriesser/knex/issues/1459">#1459</a></li><li data-reactid="2919">Fix Webpack build. <a href="https://github.com/tgriesser/knex/issues/1447">#1447</a></li><li data-reactid="2920">Fix code that triggered Bluebird warnings. <a href="https://github.com/tgriesser/knex/issues/1460">#1460</a>, <a href="https://github.com/tgriesser/knex/issues/1489">#1489</a></li><li data-reactid="2921">Fix <code>ping</code> function. (Oracle) <a href="https://github.com/tgriesser/knex/issues/1486">#1486</a></li><li data-reactid="2922">Fix <code>columnInfo</code>. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1464">#1464</a></li><li data-reactid="2923">Fix <code>ColumnCompiler#binary</code>. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1464">#1464</a></li><li data-reactid="2924">Allow connection strings that do not contain a password. <a href="https://github.com/tgriesser/knex/issues/1473">#1473</a></li><li data-reactid="2925">Fix race condition in seed stubs. <a href="https://github.com/tgriesser/knex/issues/1493">#1493</a></li><li data-reactid="2926">Give each query a UUID. <a href="https://github.com/tgriesser/knex/issues/1510">#1510</a></li></ul></div><div data-reactid="2927"><b class="header" data-reactid="2928">0.11.5</b><!-- react-text: 2929 --><!-- /react-text --><small data-reactid="2930"><i data-reactid="2931">26 May, 2016</i></small><br data-reactid="2932"/><p data-reactid="2933">Bugfix: Using <code>Raw</code> or <code>QueryBuilder</code> as a binding to <code>Raw</code> now works as intended</p></div><div data-reactid="2934"><b class="header" data-reactid="2935">0.11.4</b><!-- react-text: 2936 --><!-- /react-text --><small data-reactid="2937"><i data-reactid="2938">22 May, 2016</i></small><br data-reactid="2939"/><ul data-reactid="2940"><li data-reactid="2941">Bugfix: Inconsistency of <code>.primary()</code> and <code>.dropPrimary()</code> between dialects <a href="https://github.com/tgriesser/knex/issues/1430">#1430</a></li><li data-reactid="2942">Feature: Allow using custom Client/Dialect (you can pass your own client in knex config) <a href="https://github.com/tgriesser/knex/issues/1428">#1428</a></li><li data-reactid="2943">Docs: Add documentation for .dropTimestamps <a href="https://github.com/tgriesser/knex/issues/1432">#1432</a></li><li data-reactid="2944">Bugfix: Fixed passing undefined fields for insert/update inside transaction <a href="https://github.com/tgriesser/knex/issues/1423">#1423</a></li><li data-reactid="2945">Feature: <code>batchInsert</code> with existing transaction <a href="https://github.com/tgriesser/knex/issues/1354">#1354</a></li><li data-reactid="2946">Build: eslint instead of jshint <a href="https://github.com/tgriesser/knex/issues/1416">#1416</a></li><li data-reactid="2947">Bugfix: Pooled connections not releasing <a href="https://github.com/tgriesser/knex/issues/1382">#1382</a></li><li data-reactid="2948">Bugfix: Support passing <code>knex.raw</code> to <code>.whereNot</code> <a href="https://github.com/tgriesser/knex/issues/1402">#1402</a></li><li data-reactid="2949">Docs: Fixed list of dialects which supports <code>.returning</code> <a href="https://github.com/tgriesser/knex/issues/1398">#1398</a></li><li data-reactid="2950">Bugfix: rename table does not fail anymore even with schema defined <a href="https://github.com/tgriesser/knex/issues/1403">#1403</a></li></ul></div><div data-reactid="2951"><b class="header" data-reactid="2952">0.11.3</b><!-- react-text: 2953 --><!-- /react-text --><small data-reactid="2954"><i data-reactid="2955">14 May, 2016</i></small><br data-reactid="2956"/><p data-reactid="2957">Support nested joins. <a href="https://github.com/tgriesser/knex/issues/1397">#1397</a></p></div><div data-reactid="2958"><b class="header" data-reactid="2959">0.11.2</b><!-- react-text: 2960 --><!-- /react-text --><small data-reactid="2961"><i data-reactid="2962">14 May, 2016</i></small><br data-reactid="2963"/><ul data-reactid="2964"><li data-reactid="2965">Prevent crash on <code>knex seed:make</code>. <a href="https://github.com/tgriesser/knex/issues/1389">#1389</a></li><li data-reactid="2966">Improvements to <code>batchInsert</code>. <a href="https://github.com/tgriesser/knex/issues/1391">#1391</a></li><li data-reactid="2967">Improvements to inserting <code>DEFAULT</code> with <code>undefined</code> binding. <a href="https://github.com/tgriesser/knex/issues/1396">#1396</a></li><li data-reactid="2968">Correct generated code for adding/dropping multiple columns. (MSSQL) <a href="https://github.com/tgriesser/knex/issues/1401">#1401</a></li></ul></div><div data-reactid="2969"><b class="header" data-reactid="2970">0.11.1</b><!-- react-text: 2971 --><!-- /react-text --><small data-reactid="2972"><i data-reactid="2973">6 May, 2016</i></small><br data-reactid="2974"/><p data-reactid="2975">Fix error in CLI command <code>migrate:make</code>. <a href="https://github.com/tgriesser/knex/issues/1386">#1386</a></p></div><div data-reactid="2976"><b class="header" data-reactid="2977">0.11.0</b><!-- react-text: 2978 --><!-- /react-text --><small data-reactid="2979"><i data-reactid="2980">5 May, 2016</i></small><br data-reactid="2981"/><b data-reactid="2982">Breaking Changes:</b><ul data-reactid="2983"><li data-reactid="2984"><code>QueryBuilder#orWhere</code> joins multiple arguments with <code>AND</code>. <a href="https://github.com/tgriesser/knex/issues/1164">#1164</a></li></ul><b data-reactid="2985">Other Changes:</b><ul data-reactid="2986"><li data-reactid="2987">Collate for columns. (MySQL) <a href="https://github.com/tgriesser/knex/issues/1147">#1147</a></li><li data-reactid="2988">Add <code>QueryBuilder#timeout</code>, <code>Raw#timeout</code>. <a href="https://github.com/tgriesser/knex/issues/1201">#1201</a> <a href="https://github.com/tgriesser/knex/issues/1260">#1260</a></li><li data-reactid="2989">Exit with error code when appropriate. <a href="https://github.com/tgriesser/knex/issues/1238">#1238</a></li><li data-reactid="2990">MSSQL connection accepts <code>host</code> as an alias for <code>server</code> in accordance with other dialects. <a href="https://github.com/tgriesser/knex/issues/1239">#1239</a></li><li data-reactid="2991">Add <code>query-response</code> event. <a href="https://github.com/tgriesser/knex/issues/1231">#1231</a></li><li data-reactid="2992">Correct behaviour of sibling nested transactions. <a href="https://github.com/tgriesser/knex/issues/1226">#1226</a></li><li data-reactid="2993">Support <code>RETURNING</code> with <code>UPDATE</code>. (Oracle) <a href="https://github.com/tgriesser/knex/issues/1253">#1253</a></li><li data-reactid="2994">Throwing callbacks from transactions automatically rolls them back. <a href="https://github.com/tgriesser/knex/issues/1257">#1257</a></li><li data-reactid="2995">Fixes to named <code>Raw</code> bindings. <a href="https://github.com/tgriesser/knex/issues/1251">#1251</a></li><li data-reactid="2996"><code>timestamps</code> accepts an argument to set <code>NOT NULL</code> and default to current timestamp.</li><li data-reactid="2997">Add <code>TableBuilder#inherits</code> for PostgreSQL. <a href="https://github.com/tgriesser/knex/issues/601">#601</a></li><li data-reactid="2998">Wrap index names. <a href="https://github.com/tgriesser/knex/issues/1289">#1289</a></li><li data-reactid="2999">Restore coffeescript knexfiles and configurations. <a href="https://github.com/tgriesser/knex/issues/1292">#1292</a></li><li data-reactid="3000">Add <code>andWhereBetween</code> and <code>andWhereNotBetween</code> <a href="https://github.com/tgriesser/knex/issues/1132">#1132</a></li><li data-reactid="3001">Fix <code>valueForUndefined</code> failure. <a href="https://github.com/tgriesser/knex/issues/1269">#1269</a></li><li data-reactid="3002"><code>renameColumn</code> no longer drops default value or nullability. <a href="https://github.com/tgriesser/knex/issues/1326">#1326</a></li><li data-reactid="3003">Correct MySQL2 error handling. <a href="https://github.com/tgriesser/knex/issues/1315">#1315</a></li><li data-reactid="3004">Fix MSSQL <code>createTableIfNotExists</code>. <a href="https://github.com/tgriesser/knex/issues/1362">#1362</a></li><li data-reactid="3005">Fix MSSQL URL parsing. <a href="https://github.com/tgriesser/knex/issues/1342">#1342</a></li><li data-reactid="3006">Update Lodash to 4.6.0 <a href="https://github.com/tgriesser/knex/issues/1242">#1242</a></li><li data-reactid="3007">Update Bluebird to 3.3.4 <a href="https://github.com/tgriesser/knex/issues/1279">#1279</a></li></ul></div><div data-reactid="3008"><b class="header" data-reactid="3009">0.10.0</b><!-- react-text: 3010 --><!-- /react-text --><small data-reactid="3011"><i data-reactid="3012">15 Feb, 2016</i></small><br data-reactid="3013"/><b data-reactid="3014">Breaking Changes:</b><ul data-reactid="3015"><li data-reactid="3016"><code>insert</code> and <code>update</code> now ignore <code>undefined</code> values. Back compatibility is provided through the option <code>useNullAsDefault</code>. <a href="https://github.com/tgriesser/knex/issues/1174">#1174</a>, <a href="https://github.com/tgriesser/knex/issues/1043">#1043</a></li></ul><b data-reactid="3017">Other Changes:</b><ul data-reactid="3018"><li data-reactid="3019">Add <a href="#Builder-countDistinct"><code>countDistinct</code></a>, <a href="#Builder-avgDistinct"><code>avgDistinct</code></a> and <a href="#Builder-sumDistinct"><code>sumDistinct</code></a>. <a href="https://github.com/tgriesser/knex/issues/1046">#1046</a></li><li data-reactid="3020">Add <a href="#Schema-jsonb"><code>schema.jsonb</code></a>. Deprecated <code>schema.json(column, true)</code>. <a href="https://github.com/tgriesser/knex/issues/991">#991</a></li><li data-reactid="3021">Support binding identifiers with <code>??</code>. <a href="https://github.com/tgriesser/knex/issues/1103">#1103</a></li><li data-reactid="3022">Restore <code>query</code> event when triggered by transactions. <a href="https://github.com/tgriesser/knex/issues/855">#855</a></li><li data-reactid="3023">Correct question mark escaping in rendered queries. <a href="https://github.com/tgriesser/knex/issues/519">#519</a>, <a href="https://github.com/tgriesser/knex/issues/1058">#1058</a></li><li data-reactid="3024">Add per-dialect escaping, allowing quotes to be escaped correctly. <a href="https://github.com/tgriesser/knex/issues/886">#886</a>, <a href="https://github.com/tgriesser/knex/issues/1095">#1095</a></li><li data-reactid="3025">Add MSSQL support. <a href="https://github.com/tgriesser/knex/issues/1090">#1090</a></li><li data-reactid="3026">Add migration locking. <a href="https://github.com/tgriesser/knex/issues/1094">#1094</a></li><li data-reactid="3027">Allow column aliases to contain <code>.</code>. <a href="https://github.com/tgriesser/knex/issues/1181">#1181</a></li><li data-reactid="3028">Add <code>batchInsert</code>. <a href="https://github.com/tgriesser/knex/issues/1182">#1182</a></li><li data-reactid="3029">Support non-array arguments to <a href="#Raw-Bindings"><code>knex.raw</code></a>.</li><li data-reactid="3030">Global <code>query-error</code> event. <a href="https://github.com/tgriesser/knex/issues/1163">#1163</a></li><li data-reactid="3031">Add <code>batchInsert</code>. <a href="https://github.com/tgriesser/knex/issues/1182">#1182</a></li><li data-reactid="3032">Better support for Mysql2 dialect options. <a href="https://github.com/tgriesser/knex/issues/980">#980</a></li><li data-reactid="3033">Support for <code>acquireConnectionTimeout</code> default 60 seconds preventing <a href="https://github.com/tgriesser/knex/issues/1040">#1040</a> from happening. <a href="https://github.com/tgriesser/knex/issues/1177">#1177</a></li><li data-reactid="3034">Fixed constraint name escaping when dropping a constraint. <a href="https://github.com/tgriesser/knex/issues/1177">#1177</a></li><li data-reactid="3035">Show also <code>.raw</code> queries in debug output. <a href="https://github.com/tgriesser/knex/issues/1169">#1169</a></li><li data-reactid="3036">Support for <code>cli</code> to use basic configuration without specific environment set. <a href="https://github.com/tgriesser/knex/issues/1101">#1101</a></li></ul></div><div data-reactid="3037"><b class="header" data-reactid="3038">0.9.0</b><!-- react-text: 3039 --><!-- /react-text --><small data-reactid="3040"><i data-reactid="3041">Nov 2, 2015</i></small><br data-reactid="3042"/><ul data-reactid="3043"><li data-reactid="3044">Fix error when merging <code>knex.raw</code> instances without arguments. <a href="https://github.com/tgriesser/knex/issues/853">#853</a></li><li data-reactid="3045">Fix error that caused the connection to time out while streaming. <a href="https://github.com/tgriesser/knex/issues/849">#849</a></li><li data-reactid="3046">Correctly parse SSL query parameter for PostgreSQL. <a href="https://github.com/tgriesser/knex/issues/852">#852</a></li><li data-reactid="3047">Pass <code>compress</code> option to MySQL2. <a href="https://github.com/tgriesser/knex/issues/843">#843</a></li><li data-reactid="3048">Schema: Use <code>timestamp with timezone</code> by default for <code>time</code>, <code>datetime</code> and <code>timestamp</code> for Oracle. <a href="https://github.com/tgriesser/knex/issues/876">#876</a></li><li data-reactid="3049">Add <a href="#Builder-modify"><code>QueryBuilder#modify</code></a> <a href="https://github.com/tgriesser/knex/issues/881">#881</a></li><li data-reactid="3050">Add LiveScript and Early Gray support for seeds and migrations.</li><li data-reactid="3051">Add <a href="#Builder-withSchema"><code>QueryBuilder#withSchema</code></a> <a href="https://github.com/tgriesser/knex/issues/518">#518</a></li><li data-reactid="3052">Allow escaping of <code>?</code> in <code>knex.raw</code> queries. <a href="https://github.com/tgriesser/knex/issues/946">#946</a></li><li data-reactid="3053">Allow <code>0</code> in join clause. <a href="https://github.com/tgriesser/knex/issues/953">#953</a></li><li data-reactid="3054">Add migration config to allow disabling/enabling transactions per migration. <a href="https://github.com/tgriesser/knex/issues/834">#834</a></li></ul></div><div data-reactid="3055"><b class="header" data-reactid="3056">0.8.6</b><!-- react-text: 3057 --><!-- /react-text --><small data-reactid="3058"><i data-reactid="3059">May 20, 2015</i></small><br data-reactid="3060"/><p data-reactid="3061">Fix for several transaction / migration issues, <a href="https://github.com/tgriesser/knex/issues/832">#832</a>, <a href="https://github.com/tgriesser/knex/issues/833">#833</a>, <a href="https://github.com/tgriesser/knex/issues/834">#834</a>, <a href="https://github.com/tgriesser/knex/issues/835">#835</a></p></div><div data-reactid="3062"><b class="header" data-reactid="3063">0.8.5</b><!-- react-text: 3064 --><!-- /react-text --><small data-reactid="3065"><i data-reactid="3066">May 14, 2015</i></small><br data-reactid="3067"/><p data-reactid="3068">Pool should be initialized if no pool options are specified</p></div><div data-reactid="3069"><b class="header" data-reactid="3070">0.8.4</b><!-- react-text: 3071 --><!-- /react-text --><small data-reactid="3072"><i data-reactid="3073">May 13, 2015</i></small><br data-reactid="3074"/><p data-reactid="3075">Pool should not be initialized if {max: 0} is sent in config options</p></div><div data-reactid="3076"><b class="header" data-reactid="3077">0.8.3</b><!-- react-text: 3078 --><!-- /react-text --><small data-reactid="3079"><i data-reactid="3080">May 2, 2015</i></small><br data-reactid="3081"/><p data-reactid="3082">Alias postgresql -&gt; postgres in connection config options</p></div><div data-reactid="3083"><b class="header" data-reactid="3084">0.8.2</b><!-- react-text: 3085 --><!-- /react-text --><small data-reactid="3086"><i data-reactid="3087">May 1, 2015</i></small><br data-reactid="3088"/><p data-reactid="3089">Fix regression in using query string in connection config</p></div><div data-reactid="3090"><b class="header" data-reactid="3091">0.8.1</b><!-- react-text: 3092 --><!-- /react-text --><small data-reactid="3093"><i data-reactid="3094">May 1, 2015</i></small><br data-reactid="3095"/><ul data-reactid="3096"><li data-reactid="3097">Warn rather than error when implicit commits wipe out savepoints in mysql / mariadb, <a href="https://github.com/tgriesser/knex/issues/805">#805</a>.</li><li data-reactid="3098">Fix for incorrect seed config reference, <a href="https://github.com/tgriesser/knex/issues/804">#804</a></li></ul></div><div data-reactid="3099"><b class="header" data-reactid="3100">0.8.0</b><!-- react-text: 3101 --><!-- /react-text --><small data-reactid="3102"><i data-reactid="3103">Apr 30, 2015</i></small><br data-reactid="3104"/><b data-reactid="3105">New Features:</b><ul data-reactid="3106"><li data-reactid="3107">Fixes several major outstanding bugs with the connection pool, switching to <a href="https://github.com/myndzi/pool2">Pool2</a> in place of generic-pool-redux</li><li data-reactid="3108">strong-oracle module support</li><li data-reactid="3109">Nested transactions automatically become savepoints, with <code>commit</code> &amp; <code>rollback</code> releasing or rolling back the current savepoint.</li><li data-reactid="3110">Database seed file support, <a href="https://github.com/tgriesser/knex/issues/391">#391</a></li><li data-reactid="3111">Improved support for sub-raw queries within raw statements</li><li data-reactid="3112">Migrations are now wrapped in transactions where possible</li><li data-reactid="3113">Subqueries supported in insert statements, <a href="https://github.com/tgriesser/knex/issues/627">#627</a></li><li data-reactid="3114">Support for nested having, <a href="https://github.com/tgriesser/knex/issues/572">#572</a></li><li data-reactid="3115">Support object syntax for joins, similar to &quot;where&quot; <a href="https://github.com/tgriesser/knex/issues/743">#743</a></li></ul><b data-reactid="3116">Major Changes:</b><ul data-reactid="3117"><li data-reactid="3118">Transactions are immediately invoked as A+ promises, <a href="https://github.com/tgriesser/knex/issues/470">#470</a> (this is a feature and should not actually break anything in practice)</li><li data-reactid="3119">Heavy refactoring internal APIs (public APIs should not be affected)</li></ul><b data-reactid="3120">&quot;Other Changes:</b><ul data-reactid="3121"><li data-reactid="3122">Allow mysql2 to use non-default port, <a href="https://github.com/tgriesser/knex/issues/588">#588</a></li><li data-reactid="3123">Support creating &amp; dropping extensions in PostgreSQL, <a href="https://github.com/tgriesser/knex/issues/540">#540</a></li><li data-reactid="3124">CLI support for knexfiles that do not provide environment keys, <a href="https://github.com/tgriesser/knex/issues/527">#527</a></li><li data-reactid="3125">Added sqlite3 dialect version of whereRaw/andWhereRaw (<a href="https://github.com/tgriesser/knex/issues/477">#477</a>)</li></ul></div><div data-reactid="3126"><b class="header" data-reactid="3127">0.7.5</b><!-- react-text: 3128 --><!-- /react-text --><small data-reactid="3129"><i data-reactid="3130">Mar 9, 2015</i></small><br data-reactid="3131"/><p data-reactid="3132">Fix bug in validateMigrationList, (<a href="https://github.com/tgriesser/knex/issues/697">#697</a>)</p></div><div data-reactid="3133"><b class="header" data-reactid="3134">0.7.4</b><!-- react-text: 3135 --><!-- /react-text --><small data-reactid="3136"><i data-reactid="3137">Feb 25, 2015</i></small><br data-reactid="3138"/><ul data-reactid="3139"><li data-reactid="3140">Fix incorrect order of query parameters when using subqueries, <a href="https://github.com/tgriesser/knex/issues/704">#704</a></li><li data-reactid="3141">Properly handle limit 0, (<a href="https://github.com/tgriesser/knex/issues/655">#655</a>)</li><li data-reactid="3142">Apply promise args from then instead of <a href="https://github.com/petkaantonov/bluebird/issues/482">explicitly passing</a>.</li><li data-reactid="3143">Respect union parameter as last argument (<a href="https://github.com/tgriesser/knex/issues/660">#660</a>).</li><li data-reactid="3144">Added sqlite3 dialect version of whereRaw/andWhereRaw (<a href="https://github.com/tgriesser/knex/issues/477">#477</a>).</li><li data-reactid="3145">Fix SQLite dropColumn doesn&#39;t work for last column (<a href="https://github.com/tgriesser/knex/issues/544">#544</a>).</li><li data-reactid="3146">Add POSIX operator support for Postgres (<a href="https://github.com/tgriesser/knex/issues/562">#562</a>)</li><li data-reactid="3147">Sample seed files now correctly (<a href="https://github.com/tgriesser/knex/issues/391">#391</a>)</li></ul></div><div data-reactid="3148"><b class="header" data-reactid="3149">0.7.3</b><!-- react-text: 3150 --><!-- /react-text --><small data-reactid="3151"><i data-reactid="3152">Oct 3, 2014</i></small><br data-reactid="3153"/><ul data-reactid="3154"><li data-reactid="3155">Support for <code>join(table, rawOrBuilder)</code> syntax.</li><li data-reactid="3156">Fix for regression in PostgreSQL connection (<a href="https://github.com/tgriesser/knex/issues/516">#516</a>)</li></ul></div><div data-reactid="3157"><b class="header" data-reactid="3158">0.7.2</b><!-- react-text: 3159 --><!-- /react-text --><small data-reactid="3160"><i data-reactid="3161">Oct 1, 2014</i></small><br data-reactid="3162"/><p data-reactid="3163">Fix for regression in migrations</p></div><div data-reactid="3164"><b class="header" data-reactid="3165">0.7.1</b><!-- react-text: 3166 --><!-- /react-text --><small data-reactid="3167"><i data-reactid="3168">Oct 1, 2014</i></small><br data-reactid="3169"/><p data-reactid="3170">Better disconnect handling &amp; pool removal for MySQL clients, <a href="https://github.com/tgriesser/knex/issues/452">#452</a></p></div><div data-reactid="3171"><b class="header" data-reactid="3172">0.7.0</b><!-- react-text: 3173 --><!-- /react-text --><small data-reactid="3174"><i data-reactid="3175">Oct 1, 2014</i></small><br data-reactid="3176"/><b data-reactid="3177">New Features</b><ul data-reactid="3178"><li data-reactid="3179">Oracle support, <a href="https://github.com/tgriesser/knex/issues/419">#419</a></li><li data-reactid="3180">Database seed file support, <a href="https://github.com/tgriesser/knex/issues/391">#391</a></li><li data-reactid="3181">Improved support for sub-raw queries within raw statements</li></ul><b data-reactid="3182">Breaking Changes</b><ul data-reactid="3183"><li data-reactid="3184">&quot;collate nocase&quot; no longer used by default in sqlite3 <a href="https://github.com/tgriesser/knex/issues/396">#396</a></li></ul><b data-reactid="3185">Other Changes</b><ul data-reactid="3186"><li data-reactid="3187">Bumping Bluebird to ^2.x</li><li data-reactid="3188">Transactions in websql are now a no-op (unsupported) <a href="https://github.com/tgriesser/knex/issues/375">#375</a></li><li data-reactid="3189">Improved test suite</li><li data-reactid="3190">knex.fn namespace as function helper (knex.fn.now), <a href="https://github.com/tgriesser/knex/issues/372">#372</a></li><li data-reactid="3191">Better handling of disconnect errors</li><li data-reactid="3192">Support for offset without limit, <a href="https://github.com/tgriesser/knex/issues/446">#446</a></li><li data-reactid="3193">Chainable first method for mysql schema, <a href="https://github.com/tgriesser/knex/issues/406">#406</a></li><li data-reactid="3194">Support for empty array in <code>whereIn</code></li><li data-reactid="3195">Create/drop schema for postgres, <a href="https://github.com/tgriesser/knex/issues/511">#511</a></li><li data-reactid="3196">Inserting multiple rows with default values, <a href="https://github.com/tgriesser/knex/issues/468">#468</a></li><li data-reactid="3197">Join columns are optional for cross-join, <a href="https://github.com/tgriesser/knex/issues/508">#508</a></li><li data-reactid="3198">Flag for creating jsonb columns in Postgresql, <a href="https://github.com/tgriesser/knex/issues/500">#500</a></li></ul></div><div data-reactid="3199"><b class="header" data-reactid="3200">0.6.22</b><!-- react-text: 3201 --><!-- /react-text --><small data-reactid="3202"><i data-reactid="3203">July 10, 2014</i></small><br data-reactid="3204"/><p data-reactid="3205">Bug fix for properly binding postgresql streaming queries, (<a href="https://github.com/tgriesser/knex/issues/363">#363</a>)</p></div><div data-reactid="3206"><b class="header" data-reactid="3207">0.6.21</b><!-- react-text: 3208 --><!-- /react-text --><small data-reactid="3209"><i data-reactid="3210">July 9, 2014</i></small><br data-reactid="3211"/><ul data-reactid="3212"><li data-reactid="3213">Bug fix for raw queries not being transaction context aware, (<a href="https://github.com/tgriesser/knex/issues/351">#351</a>).</li><li data-reactid="3214">Properly forward stream errors in sqlite3 runner, (<a href="https://github.com/tgriesser/knex/issues/359">#359</a>)</li></ul></div><div data-reactid="3215"><b class="header" data-reactid="3216">0.6.20</b><!-- react-text: 3217 --><!-- /react-text --><small data-reactid="3218"><i data-reactid="3219">June 30, 2014</i></small><br data-reactid="3220"/><p data-reactid="3221">Allow case insensitive operators in sql clauses, (<a href="https://github.com/tgriesser/knex/issues/344">#344</a>)</p></div><div data-reactid="3222"><b class="header" data-reactid="3223">0.6.19</b><!-- react-text: 3224 --><!-- /react-text --><small data-reactid="3225"><i data-reactid="3226">June 27, 2014</i></small><br data-reactid="3227"/><ul data-reactid="3228"><li data-reactid="3229">Add <code>groupByRaw</code> / <code>orderByRaw</code> methods, better support for raw statements in group / order (<a href="https://github.com/tgriesser/knex/issues/282">#282</a>).</li><li data-reactid="3230">Support more config options for node-mysql2 dialect (<a href="https://github.com/tgriesser/knex/issues/341">#341</a>).</li><li data-reactid="3231">CLI help text fix, (<a href="https://github.com/tgriesser/knex/issues/342">#342</a>)</li></ul></div><div data-reactid="3232"><b class="header" data-reactid="3233">0.6.18</b><!-- react-text: 3234 --><!-- /react-text --><small data-reactid="3235"><i data-reactid="3236">June 25, 2014</i></small><br data-reactid="3237"/><p data-reactid="3238">Patch for the method, calling without a handler should return the stream, not a promise (<a href="https://github.com/tgriesser/knex/issues/337">#337</a>)</p></div><div data-reactid="3239"><b class="header" data-reactid="3240">0.6.17</b><!-- react-text: 3241 --><!-- /react-text --><small data-reactid="3242"><i data-reactid="3243">June 23, 2014</i></small><br data-reactid="3244"/><p data-reactid="3245">Adding missing map / reduce proxies to bluebird&#39;s implementation</p></div><div data-reactid="3246"><b class="header" data-reactid="3247">0.6.16</b><!-- react-text: 3248 --><!-- /react-text --><small data-reactid="3249"><i data-reactid="3250">June 18, 2014</i></small><br data-reactid="3251"/><ul data-reactid="3252"><li data-reactid="3253">Increment / decrement returns the number of affectedRows (<a href="https://github.com/tgriesser/knex/issues/330">#330</a>).</li><li data-reactid="3254">Allow --cwd option flag to be passed to CLI tool (<a href="https://github.com/tgriesser/knex/issues/326">#326</a>)</li></ul></div><div data-reactid="3255"><b class="header" data-reactid="3256">0.6.15</b><!-- react-text: 3257 --><!-- /react-text --><small data-reactid="3258"><i data-reactid="3259">June 14, 2014</i></small><br data-reactid="3260"/><p data-reactid="3261">Added the as method for aliasing subqueries</p></div><div data-reactid="3262"><b class="header" data-reactid="3263">0.6.14</b><!-- react-text: 3264 --><!-- /react-text --><small data-reactid="3265"><i data-reactid="3266">June 14, 2014</i></small><br data-reactid="3267"/><p data-reactid="3268">whereExists / whereNotExists may now take a query builder instance as well as a callback</p></div><div data-reactid="3269"><b class="header" data-reactid="3270">0.6.13</b><!-- react-text: 3271 --><!-- /react-text --><small data-reactid="3272"><i data-reactid="3273">June 12, 2014</i></small><br data-reactid="3274"/><ul data-reactid="3275"><li data-reactid="3276">Fix regression with onUpdate / onDelete in PostgreSQL, (<a href="https://github.com/tgriesser/knex/issues/308">#308</a>).</li><li data-reactid="3277">Add missing <code>Promise</code> require to knex.js, unit test for knex.destroy (<a href="https://github.com/tgriesser/knex/issues/314">#314</a>)</li></ul></div><div data-reactid="3278"><b class="header" data-reactid="3279">0.6.12</b><!-- react-text: 3280 --><!-- /react-text --><small data-reactid="3281"><i data-reactid="3282">June 10, 2014</i></small><br data-reactid="3283"/><p data-reactid="3284">Fix for regression with boolean default types in PostgreSQL</p></div><div data-reactid="3285"><b class="header" data-reactid="3286">0.6.11</b><!-- react-text: 3287 --><!-- /react-text --><small data-reactid="3288"><i data-reactid="3289">June 10, 2014</i></small><br data-reactid="3290"/><p data-reactid="3291">Fix for regression with queries containing multiple order by statements in sqlite3</p></div><div data-reactid="3292"><b class="header" data-reactid="3293">0.6.10</b><!-- react-text: 3294 --><!-- /react-text --><small data-reactid="3295"><i data-reactid="3296">June 10, 2014</i></small><br data-reactid="3297"/><p data-reactid="3298">Fix for big regression in memoization of column names from 0.5 -&gt; 0.6</p></div><div data-reactid="3299"><b class="header" data-reactid="3300">0.6.9</b><!-- react-text: 3301 --><!-- /react-text --><small data-reactid="3302"><i data-reactid="3303">June 9, 2014</i></small><br data-reactid="3304"/><p data-reactid="3305">Fix for regression in specificType method</p></div><div data-reactid="3306"><b class="header" data-reactid="3307">0.6.8</b><!-- react-text: 3308 --><!-- /react-text --><small data-reactid="3309"><i data-reactid="3310">June 9, 2014</i></small><br data-reactid="3311"/><p data-reactid="3312">Package.json fix for CLI</p></div><div data-reactid="3313"><b class="header" data-reactid="3314">0.6.7</b><!-- react-text: 3315 --><!-- /react-text --><small data-reactid="3316"><i data-reactid="3317">June 9, 2014</i></small><br data-reactid="3318"/><ul data-reactid="3319"><li data-reactid="3320">Adds support for <a href="https://github.com/sidorares/node-mysql2">node-mysql2</a> library.</li><li data-reactid="3321">Bundles CLI with the knex install, various related migrate CLI fixes</li></ul></div><div data-reactid="3322"><b class="header" data-reactid="3323">0.6.6</b><!-- react-text: 3324 --><!-- /react-text --><small data-reactid="3325"><i data-reactid="3326">June 9, 2014</i></small><br data-reactid="3327"/><ul data-reactid="3328"><li data-reactid="3329">console.warn rather than throw when adding foreignKeys in SQLite3.</li><li data-reactid="3330">Add support for dropColumn in SQLite3.</li><li data-reactid="3331">Document <code>raw.wrap</code></li></ul></div><div data-reactid="3332"><b class="header" data-reactid="3333">0.6.5</b><!-- react-text: 3334 --><!-- /react-text --><small data-reactid="3335"><i data-reactid="3336">June 9, 2014</i></small><br data-reactid="3337"/><p data-reactid="3338">Add missing _ require to WebSQL builds</p></div><div data-reactid="3339"><b class="header" data-reactid="3340">0.6.4</b><!-- react-text: 3341 --><!-- /react-text --><small data-reactid="3342"><i data-reactid="3343">June 9, 2014</i></small><br data-reactid="3344"/><p data-reactid="3345">Fix &amp; document schema.raw method</p></div><div data-reactid="3346"><b class="header" data-reactid="3347">0.6.3</b><!-- react-text: 3348 --><!-- /react-text --><small data-reactid="3349"><i data-reactid="3350">June 6, 2014</i></small><br data-reactid="3351"/><ul data-reactid="3352"><li data-reactid="3353">Schema methods on transaction object are now transaction aware (<a href="https://github.com/tgriesser/knex/issues/301">#301</a>).</li><li data-reactid="3354">Fix for resolved value from transactions, (<a href="https://github.com/tgriesser/knex/issues/298">#298</a>).</li><li data-reactid="3355">Undefined columns are not added to builder</li></ul></div><div data-reactid="3356"><b class="header" data-reactid="3357">0.6.2</b><!-- react-text: 3358 --><!-- /react-text --><small data-reactid="3359"><i data-reactid="3360">June 4, 2014</i></small><br data-reactid="3361"/><ul data-reactid="3362"><li data-reactid="3363">Fix regression in raw query output, (<a href="https://github.com/tgriesser/knex/issues/297">#297</a>).</li><li data-reactid="3364">Fix regression in &quot;pluck&quot; method (<a href="https://github.com/tgriesser/knex/issues/296">#296</a>).</li><li data-reactid="3365">Document <a href="#Builder-first">first</a> method</li></ul></div><div data-reactid="3366"><b class="header" data-reactid="3367">0.6.1</b><!-- react-text: 3368 --><!-- /react-text --><small data-reactid="3369"><i data-reactid="3370">June 4, 2014</i></small><br data-reactid="3371"/><p data-reactid="3372">Reverting to using .npmignore, the &quot;files&quot; syntax forgot the knex.js file</p></div><div data-reactid="3373"><b class="header" data-reactid="3374">0.6.0</b><!-- react-text: 3375 --><!-- /react-text --><small data-reactid="3376"><i data-reactid="3377">June 4, 2014</i></small><br data-reactid="3378"/><b data-reactid="3379">Major Library refactor:</b><ul data-reactid="3380"><li data-reactid="3381">Major internal overhaul to clean up the various dialect code.</li><li data-reactid="3382">Improved unit test suite.</li><li data-reactid="3383">Support for the <a href="https://github.com/mscdex/node-mariasql">mariasql</a> driver.</li><li data-reactid="3384">More consistent use of raw query bindings throughout the library.</li><li data-reactid="3385">Queries are more composable, may be injected in various points throughout the builder.</li><li data-reactid="3386">Added <a href="#Interfaces-Streams">streaming</a> interface</li><li data-reactid="3387">Deprecated 5 argument <a href="#Builder-join">join</a> in favor of additional join methods.</li><li data-reactid="3388">The wrapValue function to allow for array column operations in PostgreSQL (<a href="https://github.com/tgriesser/knex/issues/287">#287</a>).</li><li data-reactid="3389">An explicit connection can be passed for any query (<a href="https://github.com/tgriesser/knex/issues/56">#56</a>).</li><li data-reactid="3390">Drop column support for sqlite3</li><li data-reactid="3391">All schema actions are run sequentially on the same connection if chained.</li><li data-reactid="3392">Schema actions can now be wrapped in a transaction</li><li data-reactid="3393"><code>.references(tableName.columnName)</code> as shorthand for <code>.references(columnName).inTable(tableName)</code></li><li data-reactid="3394"><code>.join(&#39;table.column&#39;, &#39;otherTable.column&#39;)</code> as shorthand for .join(&#39;table.column&#39;, &#39;=&#39;, &#39;otherTable.column&#39;)</li><li data-reactid="3395">Streams are supported for selects, passing through to the streaming capabilities of node-mysql and node-postgres</li><li data-reactid="3396">For More information, see this <a href="https://github.com/tgriesser/knex/pull/252">pull-request</a></li></ul></div><div data-reactid="3397"><b class="header" data-reactid="3398">0.5.15</b><!-- react-text: 3399 --><!-- /react-text --><small data-reactid="3400"><i data-reactid="3401">June 4, 2014</i></small><br data-reactid="3402"/><p data-reactid="3403">Dropped indexes feature now functions correctly, (<a href="https://github.com/tgriesser/knex/issues/278">#278</a>)</p></div><div data-reactid="3404"><b class="header" data-reactid="3405">0.5.14</b><!-- react-text: 3406 --><!-- /react-text --><small data-reactid="3407"><i data-reactid="3408">May 6, 2014</i></small><br data-reactid="3409"/><p data-reactid="3410">Remove the charset encoding if it&#39;s utf8 for mysql, as it&#39;s the default but also currently causes some issues in recent versions of node-mysql</p></div><div data-reactid="3411"><b class="header" data-reactid="3412">0.5.13</b><!-- react-text: 3413 --><!-- /react-text --><small data-reactid="3414"><i data-reactid="3415">April 2, 2014</i></small><br data-reactid="3416"/><p data-reactid="3417">Fix regression in array bindings for postgresql (<a href="https://github.com/tgriesser/knex/issues/228">#228</a>)</p></div><div data-reactid="3418"><b class="header" data-reactid="3419">0.5.12</b><!-- react-text: 3420 --><!-- /react-text --><small data-reactid="3421"><i data-reactid="3422">Mar 31, 2014</i></small><br data-reactid="3423"/><p data-reactid="3424">Add more operators for where clauses, including &amp;&amp; (<a href="https://github.com/tgriesser/knex/issues/226">#226</a>)</p></div><div data-reactid="3425"><b class="header" data-reactid="3426">0.5.11</b><!-- react-text: 3427 --><!-- /react-text --><small data-reactid="3428"><i data-reactid="3429">Mar 25, 2014</i></small><br data-reactid="3430"/><ul data-reactid="3431"><li data-reactid="3432"><code>.where(col, &#39;is&#39;, null)</code> or <code>.where(col, &#39;is not&#39;, null)</code> are not supported (<a href="https://github.com/tgriesser/knex/issues/221">#221</a>).</li><li data-reactid="3433">Case insensitive <code>where</code> operators now allowed (<a href="https://github.com/tgriesser/knex/issues/212">#212</a>).</li><li data-reactid="3434">Fix bug in increment/decrement truncating to an integer (<a href="https://github.com/tgriesser/knex/issues/210">#210</a>).</li><li data-reactid="3435">Disconnected connections are now properly handled &amp; removed from the pool (<a href="https://github.com/tgriesser/knex/issues/206">#206</a>).</li><li data-reactid="3436">Internal tweaks to binding concatenations for performance (<a href="https://github.com/tgriesser/knex/issues/207">#207</a>)</li></ul></div><div data-reactid="3437"><b class="header" data-reactid="3438">0.5.10</b><!-- react-text: 3439 --><!-- /react-text --><small data-reactid="3440"><i data-reactid="3441">Mar 19, 2014</i></small><br data-reactid="3442"/><p data-reactid="3443">Add the .exec method to the internal promise shim</p></div><div data-reactid="3444"><b class="header" data-reactid="3445">0.5.9</b><!-- react-text: 3446 --><!-- /react-text --><small data-reactid="3447"><i data-reactid="3448">Mar 18, 2014</i></small><br data-reactid="3449"/><p data-reactid="3450">Remove error&#39;ed connections from the connection pool (<a href="https://github.com/tgriesser/knex/issues/206">#206</a>), added support for node-postgres-pure (pg.js) (<a href="https://github.com/tgriesser/knex/issues/200">#200</a>)</p></div><div data-reactid="3451"><b class="header" data-reactid="3452">0.5.8</b><!-- react-text: 3453 --><!-- /react-text --><small data-reactid="3454"><i data-reactid="3455">Feb 27, 2014</i></small><br data-reactid="3456"/><p data-reactid="3457">Fix for chaining on forUpdate / forShare, adding map &amp; reduce from bluebird</p></div><div data-reactid="3458"><b class="header" data-reactid="3459">0.5.7</b><!-- react-text: 3460 --><!-- /react-text --><small data-reactid="3461"><i data-reactid="3462">Feb 18, 2014</i></small><br data-reactid="3463"/><p data-reactid="3464">Fix for a null limit / offset breaking query chain (<a href="https://github.com/tgriesser/knex/issues/182">#182</a>)</p></div><div data-reactid="3465"><b class="header" data-reactid="3466">0.5.6</b><!-- react-text: 3467 --><!-- /react-text --><small data-reactid="3468"><i data-reactid="3469">Feb 5, 2014</i></small><br data-reactid="3470"/><p data-reactid="3471">Bump bluebird dependency to ~1.0.0, fixing regression in Bluebird 1.0.2 (<a href="https://github.com/tgriesser/knex/issues/176">#176</a>)</p></div><div data-reactid="3472"><b class="header" data-reactid="3473">0.5.5</b><!-- react-text: 3474 --><!-- /react-text --><small data-reactid="3475"><i data-reactid="3476">Jan 28, 2014</i></small><br data-reactid="3477"/><ul data-reactid="3478"><li data-reactid="3479">Fix for the exit code on the migrations cli (<a href="https://github.com/tgriesser/knex/issues/151">#151</a>).</li><li data-reactid="3480">The <code>init</code> method in <code>knex.migrate</code> now uses <code>this.config</code> if one isn&#39;t passed in (<a href="https://github.com/tgriesser/knex/issues/156">#156</a>)</li></ul></div><div data-reactid="3481"><b class="header" data-reactid="3482">0.5.4</b><!-- react-text: 3483 --><!-- /react-text --><small data-reactid="3484"><i data-reactid="3485">Jan 7, 2014</i></small><br data-reactid="3486"/><p data-reactid="3487">Fix for using raw statements in defaultTo schema builder methods (<a href="https://github.com/tgriesser/knex/issues/146">#146</a>)</p></div><div data-reactid="3488"><b class="header" data-reactid="3489">0.5.3</b><!-- react-text: 3490 --><!-- /react-text --><small data-reactid="3491"><i data-reactid="3492">Jan 2, 2014</i></small><br data-reactid="3493"/><p data-reactid="3494">Fix for incorrectly formed sql when aggregates are used with columns (<a href="https://github.com/tgriesser/knex/issues/144">#144</a>)</p></div><div data-reactid="3495"><b class="header" data-reactid="3496">0.5.2</b><!-- react-text: 3497 --><!-- /react-text --><small data-reactid="3498"><i data-reactid="3499">Dec 18, 2013</i></small><br data-reactid="3500"/><p data-reactid="3501">Adding passthrough &quot;catch&quot;, &quot;finally&quot; to bluebird implementations, use bluebird&#39;s &quot;nodeify&quot; internally for exec</p></div><div data-reactid="3502"><b class="header" data-reactid="3503">0.5.1</b><!-- react-text: 3504 --><!-- /react-text --><small data-reactid="3505"><i data-reactid="3506">Dec 12, 2013</i></small><br data-reactid="3507"/><ul data-reactid="3508"><li data-reactid="3509">The <a href="#Builder-returning">returning</a> in PostgreSQL may now accept * or an array of columns to return. If either of these are passed, the response will be an array of objects rather than an array of values. Updates may also now use a <code>returning</code> value. (<a href="https://github.com/tgriesser/knex/issues/132">#132</a>)</li><li data-reactid="3510">Added <code>bigint</code> and <code>bigserial</code> type to PostgreSQL. (<a href="https://github.com/tgriesser/knex/issues/111">#111</a>)</li><li data-reactid="3511">Fix for the <a href="#Schema-specificType">specificType</a> schema call (<a href="https://github.com/tgriesser/knex/issues/118">#118</a>)</li><li data-reactid="3512">Several fixes for migrations, including migration file path fixes, passing a Promise constructor to the migration <code>up</code> and <code>down</code> methods, allowing the &quot;knex&quot; module to be used globally, file ordering on migrations, and other small improvements. (<a href="https://github.com/tgriesser/knex/issues/112">#112</a>-115, <a href="https://github.com/tgriesser/knex/issues/125">#125</a>, <a href="https://github.com/tgriesser/knex/issues/135">#135</a>)</li></ul></div><div data-reactid="3513"><b class="header" data-reactid="3514">0.5.0</b><!-- react-text: 3515 --><!-- /react-text --><small data-reactid="3516"><i data-reactid="3517">Nov 25, 2013</i></small><br data-reactid="3518"/><ul data-reactid="3519"><li data-reactid="3520">Initial pass at a <a href="#Migrations">migration</a> api.</li><li data-reactid="3521">Aggregate methods are no longer aliased as &quot;aggregate&quot;, but may now be aliased and have more than one aggregate in a query (<a href="https://github.com/tgriesser/knex/issues/108">#108</a>, <a href="https://github.com/tgriesser/knex/issues/110">#110</a>).</li><li data-reactid="3522">Adding bigint and bigserial to PostgreSQL (<a href="https://github.com/tgriesser/knex/issues/111">#111</a>).</li><li data-reactid="3523">Bugfix on increment/decrement values (<a href="https://github.com/tgriesser/knex/issues/100">#100</a>).</li><li data-reactid="3524">Bugfix with having method (<a href="https://github.com/tgriesser/knex/issues/107">#107</a>).</li><li data-reactid="3525">Switched from when.js to <a href="https://github.com/petkaantonov/bluebird">bluebird</a> for promise implementation, with shim for backward compatibility.</li><li data-reactid="3526">Switched from underscore to lodash, for semver reliability</li></ul></div><div data-reactid="3527"><b class="header" data-reactid="3528">0.4.13</b><!-- react-text: 3529 --><!-- /react-text --><small data-reactid="3530"><i data-reactid="3531">Oct 31, 2013</i></small><br data-reactid="3532"/><p data-reactid="3533">Fix for aggregate methods on toString and clone, (<a href="https://github.com/tgriesser/knex/issues/98">#98</a>)</p></div><div data-reactid="3534"><b class="header" data-reactid="3535">0.4.12</b><!-- react-text: 3536 --><!-- /react-text --><small data-reactid="3537"><i data-reactid="3538">Oct 29, 2013</i></small><br data-reactid="3539"/><p data-reactid="3540">Fix incorrect values passed to float in MySQL and decimal in PostgreSQL</p></div><div data-reactid="3541"><b class="header" data-reactid="3542">0.4.11</b><!-- react-text: 3543 --><!-- /react-text --><small data-reactid="3544"><i data-reactid="3545">Oct 15, 2013</i></small><br data-reactid="3546"/><p data-reactid="3547">Fix potential sql injection vulnerability in orderBy, thanks to @sebgie</p></div><div data-reactid="3548"><b class="header" data-reactid="3549">0.4.10</b><!-- react-text: 3550 --><!-- /react-text --><small data-reactid="3551"><i data-reactid="3552">Oct 14, 2013</i></small><br data-reactid="3553"/><ul data-reactid="3554"><li data-reactid="3555">Added <a href="#Builder-forUpdate">forUpdate</a> and <a href="#Builder-forShare">forShare</a> for select modes in transactions. (<a href="https://github.com/tgriesser/knex/issues/84">#84</a>)</li><li data-reactid="3556">Fix bug where current query chain type is not copied on <a href="#Builder-clone">clone</a>. (<a href="https://github.com/tgriesser/knex/issues/90">#90</a>)</li><li data-reactid="3557">Charset and collate are now added as methods on the schema builder. (<a href="https://github.com/tgriesser/knex/issues/89">#89</a>)</li><li data-reactid="3558">Added <code>into</code> as an alias of <a href="#Builder-from">from</a>, for builder syntax of: <code>insert(value).into(tableName)</code></li><li data-reactid="3559">Internal pool fixes. (<a href="https://github.com/tgriesser/knex/issues/90">#90</a>)</li></ul></div><div data-reactid="3560"><b class="header" data-reactid="3561">0.4.9</b><!-- react-text: 3562 --><!-- /react-text --><small data-reactid="3563"><i data-reactid="3564">Oct 7, 2013</i></small><br data-reactid="3565"/><ul data-reactid="3566"><li data-reactid="3567">Fix for documentation of <a href="#Schema-hasColumn">hasColumn</a>, ensure that <code>hasColumn</code> works with MySQL (<a href="https://github.com/tgriesser/knex/issues/87">#87</a>).</li><li data-reactid="3568">More cleanup of error messages, showing the original error message concatenated with the sql and bindings</li></ul></div><div data-reactid="3569"><b class="header" data-reactid="3570">0.4.8</b><!-- react-text: 3571 --><!-- /react-text --><small data-reactid="3572"><i data-reactid="3573">Oct 2, 2013</i></small><br data-reactid="3574"/><p data-reactid="3575">Connections are no longer pushed back into the pool if they never existed to begin with (<a href="https://github.com/tgriesser/knex/issues/85">#85</a>)</p></div><div data-reactid="3576"><b class="header" data-reactid="3577">0.4.7</b><!-- react-text: 3578 --><!-- /react-text --><small data-reactid="3579"><i data-reactid="3580">Sep 27, 2013</i></small><br data-reactid="3581"/><p data-reactid="3582">The column is now a documented method on the builder api, and takes either an individual column or an array of columns to select</p></div><div data-reactid="3583"><b class="header" data-reactid="3584">0.4.6</b><!-- react-text: 3585 --><!-- /react-text --><small data-reactid="3586"><i data-reactid="3587">Sep 25, 2013</i></small><br data-reactid="3588"/><p data-reactid="3589">Standardizing handling of errors for easier debugging, as noted in (<a href="https://github.com/tgriesser/knex/issues/39">#39</a>)</p></div><div data-reactid="3590"><b class="header" data-reactid="3591">0.4.5</b><!-- react-text: 3592 --><!-- /react-text --><small data-reactid="3593"><i data-reactid="3594">Sep 24, 2013</i></small><br data-reactid="3595"/><p data-reactid="3596">Fix for hasTable always returning true in MySQL (<a href="https://github.com/tgriesser/knex/issues/82">#82</a>), fix where sql queries were duplicated with multiple calls on toSql with the schema builder</p></div><div data-reactid="3597"><b class="header" data-reactid="3598">0.4.4</b><!-- react-text: 3599 --><!-- /react-text --><small data-reactid="3600"><i data-reactid="3601">Sep 22, 2013</i></small><br data-reactid="3602"/><p data-reactid="3603">Fix for debug method not properly debugging individual queries</p></div><div data-reactid="3604"><b class="header" data-reactid="3605">0.4.3</b><!-- react-text: 3606 --><!-- /react-text --><small data-reactid="3607"><i data-reactid="3608">Sep 18, 2013</i></small><br data-reactid="3609"/><p data-reactid="3610">Fix for underscore not being defined in various grammar files</p></div><div data-reactid="3611"><b class="header" data-reactid="3612">0.4.2</b><!-- react-text: 3613 --><!-- /react-text --><small data-reactid="3614"><i data-reactid="3615">Sep 17, 2013</i></small><br data-reactid="3616"/><p data-reactid="3617">Fix for an error being thrown when an initialized ClientBase instance was passed into Knex.initialize. pool.destroy now optionally accepts a callback to notify when it has completed draining and destroying all connections</p></div><div data-reactid="3618"><b class="header" data-reactid="3619">0.4.1</b><!-- react-text: 3620 --><!-- /react-text --><small data-reactid="3621"><i data-reactid="3622">Sep 16, 2013</i></small><br data-reactid="3623"/><p data-reactid="3624">Cleanup from the 0.4.0 release, fix a potential exploit in &quot;where&quot; clauses pointed out by Andri Möll, fix for clients not being properly released from the pool <a href="https://github.com/tgriesser/knex/issues/70">#70</a>, fix for where(&quot;foo&quot;, &quot;&lt;&gt;&quot;, null) doing an &quot;IS NULL&quot; statement</p></div><div data-reactid="3625"><b class="header" data-reactid="3626">0.4.0</b><!-- react-text: 3627 --><!-- /react-text --><small data-reactid="3628"><i data-reactid="3629">Sep 13, 2013</i></small><br data-reactid="3630"/><b data-reactid="3631">Breaking Changes:</b><ul data-reactid="3632"><li data-reactid="3633">Global state is no longer stored in the library, an instance is returned from <code>Knex.initialize</code>, so you will need to call this once and then reference this <code>knex</code> client elsewhere in your application.</li><li data-reactid="3634">Lowercasing of <code>knex.raw</code>, <code>knex.transaction</code>, and <code>knex.schema</code>.</li><li data-reactid="3635">Created columns are now nullable by default, unless <code>notNullable</code> is chained as an option.</li><li data-reactid="3636">Keys created with <code>increments</code> are now assumed to be unsigned (MySQL) by default.</li><li data-reactid="3637">The <code>destroyAllNow</code> is no longer called by the library on <code>process.exit</code> event. If you need to call it explicitly yourself, you may use <code>knex.client.destroyPool</code></li></ul></div><div data-reactid="3638"><b class="header" data-reactid="3639">0.2.6</b><!-- react-text: 3640 --><!-- /react-text --><small data-reactid="3641"><i data-reactid="3642">Aug 29, 2013</i></small><br data-reactid="3643"/><p data-reactid="3644">Reject the transaction promise if the transaction &quot;commit&quot; fails, (<a href="https://github.com/tgriesser/knex/issues/50">#50</a>)</p></div><div data-reactid="3645"><b class="header" data-reactid="3646">0.2.5</b><!-- react-text: 3647 --><!-- /react-text --><small data-reactid="3648"><i data-reactid="3649">Aug 25, 2013</i></small><br data-reactid="3650"/><p data-reactid="3651">Fix error if a callback isn&#39;t specified for exec, (<a href="https://github.com/tgriesser/knex/issues/49">#49</a>)</p></div><div data-reactid="3652"><b class="header" data-reactid="3653">0.2.4</b><!-- react-text: 3654 --><!-- /react-text --><small data-reactid="3655"><i data-reactid="3656">Aug 22, 2013</i></small><br data-reactid="3657"/><p data-reactid="3658">Fix SQLite3 delete not returning affected row count, (<a href="https://github.com/tgriesser/knex/issues/45">#45</a>)</p></div><div data-reactid="3659"><b class="header" data-reactid="3660">0.2.3</b><!-- react-text: 3661 --><!-- /react-text --><small data-reactid="3662"><i data-reactid="3663">Aug 22, 2013</i></small><br data-reactid="3664"/><p data-reactid="3665">Fix insert with default values in PostgreSQL and SQLite3, (<a href="https://github.com/tgriesser/knex/issues/44">#44</a>)</p></div><div data-reactid="3666"><b class="header" data-reactid="3667">0.2.2</b><!-- react-text: 3668 --><!-- /react-text --><small data-reactid="3669"><i data-reactid="3670">Aug 20, 2013</i></small><br data-reactid="3671"/><p data-reactid="3672">Allowing Raw queries to be passed as the primary table names</p></div><div data-reactid="3673"><b class="header" data-reactid="3674">0.2.1</b><!-- react-text: 3675 --><!-- /react-text --><small data-reactid="3676"><i data-reactid="3677">Aug 13, 2013</i></small><br data-reactid="3678"/><p data-reactid="3679">Fix for an array passed to insert being mutated</p></div><div data-reactid="3680"><b class="header" data-reactid="3681">0.2.0</b><!-- react-text: 3682 --><!-- /react-text --><small data-reactid="3683"><i data-reactid="3684">Aug 7, 2013</i></small><br data-reactid="3685"/><b data-reactid="3686">Breaking changes:</b><ul data-reactid="3687"><li data-reactid="3688"><a href="#Schema-hasTable">hasTable</a> now returns a boolean rather than a failed promise.</li><li data-reactid="3689">Changed syntax for insert in postgresql, where the <code>id</code> is not assumed on inserts (<a href="https://github.com/tgriesser/knex/issues/18">#18</a>). The second parameter of <a href="#Builder-insert">insert</a> is now required to return an array of insert id&#39;s for the last insert.</li><li data-reactid="3690">The <a href="#Schema-timestamp">timestamp</a> method on the schema builder now uses a <code>dateTime</code> rather than a <code>timestamp</code></li></ul></div><div data-reactid="3691"><b class="header" data-reactid="3692">0.1.8</b><!-- react-text: 3693 --><!-- /react-text --><small data-reactid="3694"><i data-reactid="3695">July 7, 2013</i></small><br data-reactid="3696"/><p data-reactid="3697">Somehow missing the != operator. Using <em>.find rather than </em>.where in getCommandsByName(<a href="https://github.com/tgriesser/knex/issues/22">#22</a>)</p></div><div data-reactid="3698"><b class="header" data-reactid="3699">0.1.7</b><!-- react-text: 3700 --><!-- /react-text --><small data-reactid="3701"><i data-reactid="3702">June 12, 2013</i></small><br data-reactid="3703"/><p data-reactid="3704">Ensures unhandled errors in the exec callback interface are re-thrown</p></div><div data-reactid="3705"><b class="header" data-reactid="3706">0.1.6</b><!-- react-text: 3707 --><!-- /react-text --><small data-reactid="3708"><i data-reactid="3709">June 9, 2013</i></small><br data-reactid="3710"/><p data-reactid="3711">Renaming beforeCreate to afterCreate. Better handling of errors in the connection pooling</p></div><div data-reactid="3712"><b class="header" data-reactid="3713">0.1.5</b><!-- react-text: 3714 --><!-- /react-text --><small data-reactid="3715"><i data-reactid="3716">June 9, 2013</i></small><br data-reactid="3717"/><p data-reactid="3718">Added the ability to specify beforeCreate and beforeDestroy hooks on the initialize&#39;s options.pool to perform any necessary database setup/teardown on connections before use (<a href="https://github.com/tgriesser/knex/issues/14">#14</a>). where and having may now accept Knex.Raw instances, for consistency (<a href="https://github.com/tgriesser/knex/issues/15">#15</a>). Added an orHaving method to the builder. The ability to specify bindings on Raw queries has been removed</p></div><div data-reactid="3719"><b class="header" data-reactid="3720">0.1.4</b><!-- react-text: 3721 --><!-- /react-text --><small data-reactid="3722"><i data-reactid="3723">May 22, 2013</i></small><br data-reactid="3724"/><p data-reactid="3725">defaultTo now accepts &quot;false&quot; for boolean columns, allows for empty strings as default values</p></div><div data-reactid="3726"><b class="header" data-reactid="3727">0.1.3</b><!-- react-text: 3728 --><!-- /react-text --><small data-reactid="3729"><i data-reactid="3730">May 18, 2013</i></small><br data-reactid="3731"/><p data-reactid="3732">Enabling table aliases (<a href="https://github.com/tgriesser/knex/issues/11">#11</a>). Fix for issues with transactions not functioning (<a href="https://github.com/tgriesser/knex/issues/12">#12</a>)</p></div><div data-reactid="3733"><b class="header" data-reactid="3734">0.1.2</b><!-- react-text: 3735 --><!-- /react-text --><small data-reactid="3736"><i data-reactid="3737">May 15, 2013</i></small><br data-reactid="3738"/><p data-reactid="3739">Bug fixes for groupBy (<a href="https://github.com/tgriesser/knex/issues/7">#7</a>). Mysql using collation, charset config settings in createTable. Added engine on schemaBuilder specifier (<a href="https://github.com/tgriesser/knex/issues/6">#6</a>). Other doc fixes, tests</p></div><div data-reactid="3740"><b class="header" data-reactid="3741">0.1.1</b><!-- react-text: 3742 --><!-- /react-text --><small data-reactid="3743"><i data-reactid="3744">May 14, 2013</i></small><br data-reactid="3745"/><p data-reactid="3746">Bug fixes for sub-queries, minor changes to initializing &quot;main&quot; instance, adding &quot;pg&quot; as a valid parameter for the client name in the connection settings</p></div><div data-reactid="3747"><b class="header" data-reactid="3748">0.1.0</b><!-- react-text: 3749 --><!-- /react-text --><small data-reactid="3750"><i data-reactid="3751">May 13, 2013</i></small><br data-reactid="3752"/><p data-reactid="3753">Initial Knex release</p></div></div></div></div></div></div></div>
</body>
<script type="text/javascript" src="build/bundle.js"></script>
<script type="text/javascript" src="assets/ga.js"></script>
</html>