(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[401],{1940:function(s,e,n){(window.__NEXT_P=window.__NEXT_P||[]).push(["/docs/walkthrough/order-by",function(){return n(2037)}])},2037:function(s,e,n){"use strict";n.r(e),n.d(e,{__toc:function(){return i}});var r=n(1527),l=n(7682),o=n(6727);let i=[{depth:3,value:"Order by ID",id:"order-by-id"},{depth:4,value:"Order by latest created",id:"order-by-latest-created"},{depth:4,value:"Combine with pagination",id:"combine-with-pagination"}];function t(s){let e=Object.assign({h1:"h1",p:"p",pre:"pre",code:"code",span:"span",h3:"h3",h4:"h4"},(0,o.a)(),s.components);return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(e.h1,{children:"Order By"}),"\n",(0,r.jsx)(e.p,{children:"The examples use the following prisma schema:"}),"\n",(0,r.jsx)(e.pre,{"data-language":"prisma","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"prisma","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"model"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Post"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" {"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  id        "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"String"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"   "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"@id"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"@default"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"cuid"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"())"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  createdAt "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"DateTime"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"@default"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"now"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"())"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  updatedAt "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"DateTime"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"@updatedAt"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  published "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"Boolean"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  title     "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"String"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  content   "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"String"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:"?"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:" "}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-comment)"},children:"// add an index to be able to order by created_at"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"@@index"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(["}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"createdAt"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"])"})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"}"})})]})}),"\n",(0,r.jsx)(e.h3,{id:"order-by-id",children:"Order by ID"}),"\n",(0,r.jsx)(e.p,{children:"The following example would equal to the default behaviour of ordering by ID in ascending order:"}),"\n",(0,r.jsx)(e.pre,{"data-language":"go","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"go","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"posts, err "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:":="}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" client.Post."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"FindMany"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"()."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"OrderBy"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  db.Post.ID."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Order"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(db.SortOrderAsc),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Exec"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(ctx)"})]})]})}),"\n",(0,r.jsx)(e.p,{children:"You can order by any field ein either direction, but it's recommended to use an index on fields you order."}),"\n",(0,r.jsx)(e.h4,{id:"order-by-latest-created",children:"Order by latest created"}),"\n",(0,r.jsx)(e.pre,{"data-language":"go","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"go","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"posts, err "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:":="}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" client.Post."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"FindMany"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"()."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"OrderBy"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  db.Post.CreatedAt."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Order"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(db.SortOrderDesc),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Exec"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(ctx)"})]})]})}),"\n",(0,r.jsx)(e.h4,{id:"combine-with-pagination",children:"Combine with pagination"}),"\n",(0,r.jsx)(e.pre,{"data-language":"go","data-theme":"default",children:(0,r.jsxs)(e.code,{"data-language":"go","data-theme":"default",children:[(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"posts, err "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-keyword)"},children:":="}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:" client."})]}),"\n",(0,r.jsx)(e.span,{className:"line",children:(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  Post."})}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"FindMany"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"()."})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Take"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-constant)"},children:"5"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:")."})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Cursor"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(db.Post.ID."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Cursor"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-string-expression)"},children:'"abc"'}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"))."})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  "}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"OrderBy"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"("})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"    db.Post.CreatedAt."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Order"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(db.SortOrderDesc),"})]}),"\n",(0,r.jsxs)(e.span,{className:"line",children:[(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"  )."}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-token-function)"},children:"Exec"}),(0,r.jsx)(e.span,{style:{color:"var(--shiki-color-text)"},children:"(ctx)"})]})]})})]})}e.default=(0,l.j)({MDXContent:function(){let s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},{wrapper:e}=Object.assign({},(0,o.a)(),s.components);return e?(0,r.jsx)(e,{...s,children:(0,r.jsx)(t,{...s})}):t(s)},pageOpts:{filePath:"pages/docs/walkthrough/order-by.md",route:"/docs/walkthrough/order-by",title:"Order By",headings:i},pageNextRoute:"/docs/walkthrough/order-by"})}},function(s){s.O(0,[682,888,774,179],function(){return s(s.s=1940)}),_N_E=s.O()}]);