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

<!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"><!--
# 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
<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 f
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-
<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>])</c
.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-reacti
<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">`tab
.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 cla
<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>) <
.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
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: 2
.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 j
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 speci
$ 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
</body>
<script type="text/javascript" src="build/bundle.js"></script>
<script type="text/javascript" src="assets/ga.js"></script>
</html>