1"use strict";(globalThis.webpackChunkkysely_site=globalThis.webpackChunkkysely_site||[]).push([[5629],{3868(e,t,s){s.r(t),s.d(t,{assets:()=>c,contentTitle:()=>a,default:()=>p,frontMatter:()=>l,metadata:()=>n,toc:()=>d});const n=JSON.parse('{"id":"examples/select/nested-object","title":"Nested object","description":"While kysely is not an ORM and it doesn\'t have the concept of relations, we do provide","source":"@site/docs/examples/select/0120-nested-object.mdx","sourceDirName":"examples/select","slug":"/examples/select/nested-object","permalink":"/docs/examples/select/nested-object","draft":false,"unlisted":false,"editUrl":"https://github.com/kysely-org/kysely/tree/master/site/docs/examples/select/0120-nested-object.mdx","tags":[],"version":"current","sidebarPosition":120,"frontMatter":{"title":"Nested object"},"sidebar":"tutorialSidebar","previous":{"title":"Nested array","permalink":"/docs/examples/select/nested-array"},"next":{"title":"Generic find query","permalink":"/docs/examples/select/generic-find-query"}}');var r=s(1058),o=s(4801),i=s(2360);const l={title:"Nested object"},a="Nested object",c={},d=[];function h(e){const t={a:"a",admonition:"admonition",code:"code",h1:"h1",header:"header",li:"li",p:"p",ul:"ul",...(0,o.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(t.header,{children:(0,r.jsx)(t.h1,{id:"nested-object",children:"Nested object"})}),"\n",(0,r.jsxs)(t.p,{children:["While kysely is not an ORM and it doesn't have the concept of relations, we do provide\nhelpers for fetching nested objects and arrays in a single query. In this example we\nuse the ",(0,r.jsx)(t.code,{children:"jsonObjectFrom"})," helper to fetch person's favorite pet along with the person's id."]}),"\n",(0,r.jsxs)(t.p,{children:["Please keep in mind that the helpers under the ",(0,r.jsx)(t.code,{children:"kysely/helpers"})," folder, including\n",(0,r.jsx)(t.code,{children:"jsonObjectFrom"}),", are not guaranteed to work with third-party dialects. In order for\nthem to work, the dialect must automatically parse the ",(0,r.jsx)(t.code,{children:"json"})," data type into\nJavaScript JSON values like objects and arrays. Some dialects might simply return\nthe data as a JSON string. In these cases you can use the built in ",(0,r.jsx)(t.code,{children:"ParseJSONResultsPlugin"}),"\nto parse the results."]}),"\n","\n","\n",(0,r.jsx)("div",{style:{marginBottom:"1em"},children:(0,r.jsx)(i.Q,{code:"import { jsonObjectFrom } from 'kysely/helpers/postgres'\n\nconst result = await db\n .selectFrom('person')\n .select((eb) => [\n 'id',\n jsonObjectFrom(\n eb.selectFrom('pet')\n .select(['pet.id as pet_id', 'pet.name'])\n .whereRef('pet.owner_id', '=', 'person.id')\n .where('pet.is_favorite', '=', true)\n ).as('favorite_pet')\n ])\n .execute()"})}),"\n",(0,r.jsxs)(t.admonition,{title:"More examples",type:"info",children:[(0,r.jsxs)(t.p,{children:["The API documentation is packed with examples. The API docs are hosted ",(0,r.jsx)(t.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,r.jsx)(t.p,{children:"For example, check out these sections:"}),(0,r.jsxs)(t.ul,{children:["\n",(0,r.jsx)(t.li,{children:(0,r.jsx)(t.a,{href:"https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#select",children:"select method"})}),"\n",(0,r.jsx)(t.li,{children:(0,r.jsx)(t.a,{href:"https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#selectAll",children:"selectAll method"})}),"\n",(0,r.jsx)(t.li,{children:(0,r.jsx)(t.a,{href:"https://kysely-org.github.io/kysely-apidoc/classes/Kysely.html#selectFrom",children:"selectFrom method"})}),"\n"]})]})]})}function p(e={}){const{wrapper:t}={...(0,o.R)(),...e.components};return t?(0,r.jsx)(t,{...e,children:(0,r.jsx)(h,{...e})}):h(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.