1"use strict";(globalThis.webpackChunkkysely_site=globalThis.webpackChunkkysely_site||[]).push([[5984],{8759(e,n,s){s.r(n),s.d(n,{assets:()=>c,contentTitle:()=>a,default:()=>u,frontMatter:()=>o,metadata:()=>r,toc:()=>h});const r=JSON.parse('{"id":"examples/where/conditional-where-calls","title":"Conditional where calls","description":"You can add expressions conditionally like this:","source":"@site/docs/examples/where/0050-conditional-where-calls.mdx","sourceDirName":"examples/where","slug":"/examples/where/conditional-where-calls","permalink":"/docs/examples/where/conditional-where-calls","draft":false,"unlisted":false,"editUrl":"https://github.com/kysely-org/kysely/tree/master/site/docs/examples/where/0050-conditional-where-calls.mdx","tags":[],"version":"current","sidebarPosition":50,"frontMatter":{"title":"Conditional where calls"},"sidebar":"tutorialSidebar","previous":{"title":"OR where","permalink":"/docs/examples/where/or-where"},"next":{"title":"Complex where clause","permalink":"/docs/examples/where/complex-where-clause"}}');var i=s(1058),t=s(4801),l=s(2360);const o={title:"Conditional where calls"},a="Conditional where calls",c={},h=[];function d(e){const n={a:"a",admonition:"admonition",h1:"h1",header:"header",li:"li",p:"p",ul:"ul",...(0,t.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.header,{children:(0,i.jsx)(n.h1,{id:"conditional-where-calls",children:"Conditional where calls"})}),"\n",(0,i.jsx)(n.p,{children:"You can add expressions conditionally like this:"}),"\n","\n","\n",(0,i.jsx)("div",{style:{marginBottom:"1em"},children:(0,i.jsx)(l.Q,{code:"import { Expression, SqlBool } from 'kysely'\n\nconst firstName: string | undefined = 'Jennifer'\nconst lastName: string | undefined = 'Aniston'\nconst under18 = true\nconst over60 = true\n\nlet query = db\n .selectFrom('person')\n .selectAll()\n\nif (firstName) {\n // The query builder is immutable. Remember to reassign\n // the result back to the query variable.\n query = query.where('first_name', '=', firstName)\n}\n\nif (lastName) {\n query = query.where('last_name', '=', lastName)\n}\n\nif (under18 || over60) {\n // Conditional OR expressions can be added like this.\n query = query.where((eb) => {\n const ors: Expression<SqlBool>[] = []\n\n if (under18) {\n ors.push(eb('age', '<', 18))\n }\n\n if (over60) {\n ors.push(eb('age', '>', 60))\n }\n\n return eb.or(ors)\n })\n}\n\nconst persons = await query.execute()"})}),"\n",(0,i.jsxs)(n.admonition,{title:"More examples",type:"info",children:[(0,i.jsxs)(n.p,{children:["The API documentation is packed with examples. The API docs are hosted ",(0,i.jsx)(n.a,{href:"https://kysely-org.github.io/kysely-apidoc/",children:"here"}),",\nbut you can access the same documentation by hovering over functions/methods/classes in your IDE. The examples are always\njust one hover away!"]}),(0,i.jsx)(n.p,{children:"For example, check out these sections:"}),(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#where",children:"where method"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#whereRef",children:"whereRef method"})}),"\n"]})]})]})}function u(e={}){const{wrapper:n}={...(0,t.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.