[{"data":1,"prerenderedAt":2030},["ShallowReactive",2],{"navigation_docs":3,"-models-retrieving":74,"-models-retrieving-surround":2025},[4,20,65],{"title":5,"path":6,"stem":7,"children":8,"page":19},"Getting Started","/getting-started","1.getting-started",[9,14],{"title":10,"path":11,"stem":12,"icon":13},"Introduction","/getting-started/introduction","1.getting-started/1.introduction","i-lucide-house",{"title":15,"path":16,"stem":17,"icon":18},"Installation","/getting-started/installation","1.getting-started/2.installation","i-lucide-download",false,{"title":21,"path":22,"stem":23,"children":24,"page":19},"Models","/models","2.models",[25,30,35,40,45,50,55,60],{"title":26,"path":27,"stem":28,"icon":29},"Defining Models","/models/defining-models","2.models/1.defining-models","i-lucide-database",{"title":31,"path":32,"stem":33,"icon":34},"Retrieving Models","/models/retrieving","2.models/2.retrieving","i-lucide-database-search",{"title":36,"path":37,"stem":38,"icon":39},"Inserting & Updating Models","/models/inserting-and-updating","2.models/3.inserting-and-updating","i-lucide-between-horizontal-start",{"title":41,"path":42,"stem":43,"icon":44},"Deleting Models","/models/deleting","2.models/4.deleting","i-lucide-trash",{"title":46,"path":47,"stem":48,"icon":49},"Relationships","/models/relationships","2.models/5.relationships","i-lucide-share-2",{"title":51,"path":52,"stem":53,"icon":54},"Events","/models/events","2.models/6.events","i-lucide-bell",{"title":56,"path":57,"stem":58,"icon":59},"Migrations & Seeders","/models/migrations-and-seeders","2.models/7.migrations-and-seeders","i-lucide-wrench",{"title":61,"path":62,"stem":63,"icon":64},"Transactions","/models/transactions","2.models/8.transactions","i-lucide-git-commit-horizontal",{"title":66,"path":67,"stem":68,"children":69,"page":19},"Contributing","/contributing","3.contributing",[70],{"title":71,"path":72,"stem":73},"Local Development","/contributing/local-development","3.contributing/1.local-development",{"id":75,"title":31,"body":76,"description":2017,"extension":2018,"links":2019,"meta":2020,"navigation":2021,"path":32,"seo":2022,"stem":33,"__hash__":2024},"docs/2.models/2.retrieving.md",{"type":77,"value":78,"toc":1994},"minimark",[79,83,88,100,145,149,154,164,205,208,255,262,296,300,306,355,365,414,418,422,427,430,475,478,637,641,648,703,707,716,756,760,773,821,825,832,868,873,907,911,932,1128,1149,1153,1163,1289,1293,1300,1348,1352,1372,1382,1386,1394,1576,1580,1589,1679,1683,1686,1857,1864,1990],[80,81,82],"p",{},"Once you have defined your models, you can use them to retrieve data from your database.",[84,85,87],"h2",{"id":86},"retrieving-all-models","Retrieving All Models",[80,89,90,91,95,96,99],{},"To retrieve all models from a table, you can use the ",[92,93,94],"code",{},"all"," or ",[92,97,98],{},"get"," methods directly on the class.",[101,102,107],"pre",{"className":103,"code":104,"language":105,"meta":106,"style":106},"language-ts shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","const users = await User.all();\n","ts","",[92,108,109],{"__ignoreMap":106},[110,111,114,118,122,126,130,133,136,139,142],"span",{"class":112,"line":113},"line",1,[110,115,117],{"class":116},"spNyl","const",[110,119,121],{"class":120},"sTEyZ"," users ",[110,123,125],{"class":124},"sMK4o","=",[110,127,129],{"class":128},"s7zQu"," await",[110,131,132],{"class":120}," User",[110,134,135],{"class":124},".",[110,137,94],{"class":138},"s2Zo4",[110,140,141],{"class":120},"()",[110,143,144],{"class":124},";\n",[84,146,148],{"id":147},"retrieving-single-models","Retrieving Single Models",[150,151,153],"h3",{"id":152},"find","Find",[80,155,156,157,159,160,163],{},"If you know the primary key of the model you want to retrieve, you can use the ",[92,158,152],{}," method. This method returns the model instance if found, or ",[92,161,162],{},"undefined"," otherwise.",[101,165,167],{"className":103,"code":166,"language":105,"meta":106,"style":106},"// Returns User | undefined\nconst user = await User.find(1);\n",[92,168,169,175],{"__ignoreMap":106},[110,170,171],{"class":112,"line":113},[110,172,174],{"class":173},"sHwdD","// Returns User | undefined\n",[110,176,178,180,183,185,187,189,191,193,196,200,203],{"class":112,"line":177},2,[110,179,117],{"class":116},[110,181,182],{"class":120}," user ",[110,184,125],{"class":124},[110,186,129],{"class":128},[110,188,132],{"class":120},[110,190,135],{"class":124},[110,192,152],{"class":138},[110,194,195],{"class":120},"(",[110,197,199],{"class":198},"sbssI","1",[110,201,202],{"class":120},")",[110,204,144],{"class":124},[80,206,207],{},"You can also pass an array of primary keys to retrieve multiple records.",[101,209,211],{"className":103,"code":210,"language":105,"meta":106,"style":106},"// Returns User[]\nconst users = await User.find([1, 2, 3]);\n",[92,212,213,218],{"__ignoreMap":106},[110,214,215],{"class":112,"line":113},[110,216,217],{"class":173},"// Returns User[]\n",[110,219,220,222,224,226,228,230,232,234,237,239,242,245,247,250,253],{"class":112,"line":177},[110,221,117],{"class":116},[110,223,121],{"class":120},[110,225,125],{"class":124},[110,227,129],{"class":128},[110,229,132],{"class":120},[110,231,135],{"class":124},[110,233,152],{"class":138},[110,235,236],{"class":120},"([",[110,238,199],{"class":198},[110,240,241],{"class":124},",",[110,243,244],{"class":198}," 2",[110,246,241],{"class":124},[110,248,249],{"class":198}," 3",[110,251,252],{"class":120},"])",[110,254,144],{"class":124},[80,256,257,258,261],{},"The ",[92,259,260],{},"findOrFail"," method works similarly but throws an error if the model is not found.",[101,263,265],{"className":103,"code":264,"language":105,"meta":106,"style":106},"// Returns User, Throws Error if not found\nconst user = await User.findOrFail(1);\n",[92,266,267,272],{"__ignoreMap":106},[110,268,269],{"class":112,"line":113},[110,270,271],{"class":173},"// Returns User, Throws Error if not found\n",[110,273,274,276,278,280,282,284,286,288,290,292,294],{"class":112,"line":177},[110,275,117],{"class":116},[110,277,182],{"class":120},[110,279,125],{"class":124},[110,281,129],{"class":128},[110,283,132],{"class":120},[110,285,135],{"class":124},[110,287,260],{"class":138},[110,289,195],{"class":120},[110,291,199],{"class":198},[110,293,202],{"class":120},[110,295,144],{"class":124},[150,297,299],{"id":298},"first","First",[80,301,302,303,305],{},"To retrieve the first record matching your query constraints, use the ",[92,304,298],{}," method.",[101,307,309],{"className":103,"code":308,"language":105,"meta":106,"style":106},"const user = await User.where(\"active\", true).first();\n",[92,310,311],{"__ignoreMap":106},[110,312,313,315,317,319,321,323,325,328,330,333,337,339,341,345,347,349,351,353],{"class":112,"line":113},[110,314,117],{"class":116},[110,316,182],{"class":120},[110,318,125],{"class":124},[110,320,129],{"class":128},[110,322,132],{"class":120},[110,324,135],{"class":124},[110,326,327],{"class":138},"where",[110,329,195],{"class":120},[110,331,332],{"class":124},"\"",[110,334,336],{"class":335},"sfazB","active",[110,338,332],{"class":124},[110,340,241],{"class":124},[110,342,344],{"class":343},"sfNiH"," true",[110,346,202],{"class":120},[110,348,135],{"class":124},[110,350,298],{"class":138},[110,352,141],{"class":120},[110,354,144],{"class":124},[80,356,357,358,360,361,364],{},"Just like ",[92,359,152],{},", there is a ",[92,362,363],{},"firstOrFail"," method that throws an error if no matching record is found.",[101,366,368],{"className":103,"code":367,"language":105,"meta":106,"style":106},"// Returns User, Throws Error if no active user is found\nconst activeUser = await User.where(\"active\", true).firstOrFail();\n",[92,369,370,375],{"__ignoreMap":106},[110,371,372],{"class":112,"line":113},[110,373,374],{"class":173},"// Returns User, Throws Error if no active user is found\n",[110,376,377,379,382,384,386,388,390,392,394,396,398,400,402,404,406,408,410,412],{"class":112,"line":177},[110,378,117],{"class":116},[110,380,381],{"class":120}," activeUser ",[110,383,125],{"class":124},[110,385,129],{"class":128},[110,387,132],{"class":120},[110,389,135],{"class":124},[110,391,327],{"class":138},[110,393,195],{"class":120},[110,395,332],{"class":124},[110,397,336],{"class":335},[110,399,332],{"class":124},[110,401,241],{"class":124},[110,403,344],{"class":343},[110,405,202],{"class":120},[110,407,135],{"class":124},[110,409,363],{"class":138},[110,411,141],{"class":120},[110,413,144],{"class":124},[84,415,417],{"id":416},"filtering-results","Filtering Results",[150,419,421],{"id":420},"where-clauses","Where Clauses",[80,423,424,425,305],{},"You can filter results using the ",[92,426,327],{},[80,428,429],{},"The most basic call requires two arguments: the name of the column and the value to match against.",[101,431,433],{"className":103,"code":432,"language":105,"meta":106,"style":106},"const user = await User.where(\"votes\", 100).get();\n",[92,434,435],{"__ignoreMap":106},[110,436,437,439,441,443,445,447,449,451,453,455,458,460,462,465,467,469,471,473],{"class":112,"line":113},[110,438,117],{"class":116},[110,440,182],{"class":120},[110,442,125],{"class":124},[110,444,129],{"class":128},[110,446,132],{"class":120},[110,448,135],{"class":124},[110,450,327],{"class":138},[110,452,195],{"class":120},[110,454,332],{"class":124},[110,456,457],{"class":335},"votes",[110,459,332],{"class":124},[110,461,241],{"class":124},[110,463,464],{"class":198}," 100",[110,466,202],{"class":120},[110,468,135],{"class":124},[110,470,98],{"class":138},[110,472,141],{"class":120},[110,474,144],{"class":124},[80,476,477],{},"You may also pass a comparison operator as the second argument and the value as the third argument.",[101,479,481],{"className":103,"code":480,"language":105,"meta":106,"style":106},"const user = await User.where(\"votes\", \"=\", 100).get();\n\nconst user = await User.where(\"votes\", \">=\", 100).get();\n\nconst user = await User.where(\"votes\", \"\u003C>\", 100).get();\n",[92,482,483,530,536,584,589],{"__ignoreMap":106},[110,484,485,487,489,491,493,495,497,499,501,503,505,507,509,512,514,516,518,520,522,524,526,528],{"class":112,"line":113},[110,486,117],{"class":116},[110,488,182],{"class":120},[110,490,125],{"class":124},[110,492,129],{"class":128},[110,494,132],{"class":120},[110,496,135],{"class":124},[110,498,327],{"class":138},[110,500,195],{"class":120},[110,502,332],{"class":124},[110,504,457],{"class":335},[110,506,332],{"class":124},[110,508,241],{"class":124},[110,510,511],{"class":124}," \"",[110,513,125],{"class":335},[110,515,332],{"class":124},[110,517,241],{"class":124},[110,519,464],{"class":198},[110,521,202],{"class":120},[110,523,135],{"class":124},[110,525,98],{"class":138},[110,527,141],{"class":120},[110,529,144],{"class":124},[110,531,532],{"class":112,"line":177},[110,533,535],{"emptyLinePlaceholder":534},true,"\n",[110,537,539,541,543,545,547,549,551,553,555,557,559,561,563,565,568,570,572,574,576,578,580,582],{"class":112,"line":538},3,[110,540,117],{"class":116},[110,542,182],{"class":120},[110,544,125],{"class":124},[110,546,129],{"class":128},[110,548,132],{"class":120},[110,550,135],{"class":124},[110,552,327],{"class":138},[110,554,195],{"class":120},[110,556,332],{"class":124},[110,558,457],{"class":335},[110,560,332],{"class":124},[110,562,241],{"class":124},[110,564,511],{"class":124},[110,566,567],{"class":335},">=",[110,569,332],{"class":124},[110,571,241],{"class":124},[110,573,464],{"class":198},[110,575,202],{"class":120},[110,577,135],{"class":124},[110,579,98],{"class":138},[110,581,141],{"class":120},[110,583,144],{"class":124},[110,585,587],{"class":112,"line":586},4,[110,588,535],{"emptyLinePlaceholder":534},[110,590,592,594,596,598,600,602,604,606,608,610,612,614,616,618,621,623,625,627,629,631,633,635],{"class":112,"line":591},5,[110,593,117],{"class":116},[110,595,182],{"class":120},[110,597,125],{"class":124},[110,599,129],{"class":128},[110,601,132],{"class":120},[110,603,135],{"class":124},[110,605,327],{"class":138},[110,607,195],{"class":120},[110,609,332],{"class":124},[110,611,457],{"class":335},[110,613,332],{"class":124},[110,615,241],{"class":124},[110,617,511],{"class":124},[110,619,620],{"class":335},"\u003C>",[110,622,332],{"class":124},[110,624,241],{"class":124},[110,626,464],{"class":198},[110,628,202],{"class":120},[110,630,135],{"class":124},[110,632,98],{"class":138},[110,634,141],{"class":120},[110,636,144],{"class":124},[150,638,640],{"id":639},"where-in","Where In",[80,642,643,644,647],{},"You can use the ",[92,645,646],{},"whereIn"," method to verify that a given column's value is contained within the given array of values.",[101,649,651],{"className":103,"code":650,"language":105,"meta":106,"style":106},"const users = await User.whereIn(\"id\", [1, 2, 3]).get();\n",[92,652,653],{"__ignoreMap":106},[110,654,655,657,659,661,663,665,667,669,671,673,676,678,680,683,685,687,689,691,693,695,697,699,701],{"class":112,"line":113},[110,656,117],{"class":116},[110,658,121],{"class":120},[110,660,125],{"class":124},[110,662,129],{"class":128},[110,664,132],{"class":120},[110,666,135],{"class":124},[110,668,646],{"class":138},[110,670,195],{"class":120},[110,672,332],{"class":124},[110,674,675],{"class":335},"id",[110,677,332],{"class":124},[110,679,241],{"class":124},[110,681,682],{"class":120}," [",[110,684,199],{"class":198},[110,686,241],{"class":124},[110,688,244],{"class":198},[110,690,241],{"class":124},[110,692,249],{"class":198},[110,694,252],{"class":120},[110,696,135],{"class":124},[110,698,98],{"class":138},[110,700,141],{"class":120},[110,702,144],{"class":124},[150,704,706],{"id":705},"where-not-null","Where Not Null",[80,708,257,709,712,713,135],{},[92,710,711],{},"whereNotNull"," method verifies that the column's value is not ",[92,714,715],{},"NULL",[101,717,719],{"className":103,"code":718,"language":105,"meta":106,"style":106},"const users = await User.whereNotNull(\"updated_at\").get();\n",[92,720,721],{"__ignoreMap":106},[110,722,723,725,727,729,731,733,735,737,739,741,744,746,748,750,752,754],{"class":112,"line":113},[110,724,117],{"class":116},[110,726,121],{"class":120},[110,728,125],{"class":124},[110,730,129],{"class":128},[110,732,132],{"class":120},[110,734,135],{"class":124},[110,736,711],{"class":138},[110,738,195],{"class":120},[110,740,332],{"class":124},[110,742,743],{"class":335},"updated_at",[110,745,332],{"class":124},[110,747,202],{"class":120},[110,749,135],{"class":124},[110,751,98],{"class":138},[110,753,141],{"class":120},[110,755,144],{"class":124},[84,757,759],{"id":758},"ordering","Ordering",[80,761,762,763,766,767,95,770,135],{},"To sort the results of the query, you may use the ",[92,764,765],{},"orderBy"," method. The first argument should be the column you wish to sort by, while the second argument controls the direction of the sort and may be either ",[92,768,769],{},"asc",[92,771,772],{},"desc",[101,774,776],{"className":103,"code":775,"language":105,"meta":106,"style":106},"const user = await User.orderBy(\"name\", \"desc\").get();\n",[92,777,778],{"__ignoreMap":106},[110,779,780,782,784,786,788,790,792,794,796,798,801,803,805,807,809,811,813,815,817,819],{"class":112,"line":113},[110,781,117],{"class":116},[110,783,182],{"class":120},[110,785,125],{"class":124},[110,787,129],{"class":128},[110,789,132],{"class":120},[110,791,135],{"class":124},[110,793,765],{"class":138},[110,795,195],{"class":120},[110,797,332],{"class":124},[110,799,800],{"class":335},"name",[110,802,332],{"class":124},[110,804,241],{"class":124},[110,806,511],{"class":124},[110,808,772],{"class":335},[110,810,332],{"class":124},[110,812,202],{"class":120},[110,814,135],{"class":124},[110,816,98],{"class":138},[110,818,141],{"class":120},[110,820,144],{"class":124},[84,822,824],{"id":823},"pagination","Pagination",[80,826,827,828,831],{},"Vasta's paginator is integrated with the query builder and provides a convenient, easy-to-use way of paginating database results. The ",[92,829,830],{},"paginate"," method accepts the number of items per page as its first argument and the current page number as its second argument.",[101,833,835],{"className":103,"code":834,"language":105,"meta":106,"style":106},"const result = await User.paginate(15, 1);\n",[92,836,837],{"__ignoreMap":106},[110,838,839,841,844,846,848,850,852,854,856,859,861,864,866],{"class":112,"line":113},[110,840,117],{"class":116},[110,842,843],{"class":120}," result ",[110,845,125],{"class":124},[110,847,129],{"class":128},[110,849,132],{"class":120},[110,851,135],{"class":124},[110,853,830],{"class":138},[110,855,195],{"class":120},[110,857,858],{"class":198},"15",[110,860,241],{"class":124},[110,862,863],{"class":198}," 1",[110,865,202],{"class":120},[110,867,144],{"class":124},[80,869,257,870,872],{},[92,871,830],{}," method returns an object with the following properties:",[874,875,876,883,889,895,901],"ul",{},[877,878,879,882],"li",{},[92,880,881],{},"data",": An array of model instances for the current page.",[877,884,885,888],{},[92,886,887],{},"total",": The total number of records matching the query.",[877,890,891,894],{},[92,892,893],{},"perPage",": The number of items per page.",[877,896,897,900],{},[92,898,899],{},"currentPage",": The current page number.",[877,902,903,906],{},[92,904,905],{},"lastPage",": The last page number.",[84,908,910],{"id":909},"aggregates","Aggregates",[80,912,913,914,917,918,917,921,917,924,927,928,931],{},"The query builder also provides a variety of aggregate methods: ",[92,915,916],{},"count",", ",[92,919,920],{},"max",[92,922,923],{},"min",[92,925,926],{},"avg",", and ",[92,929,930],{},"sum",". Each of these executes the query immediately and can be combined with any other query constraints.",[101,933,935],{"className":103,"code":934,"language":105,"meta":106,"style":106},"const count = await User.count();\n\nconst max = await User.max(\"price\");\n\nconst min = await User.min(\"price\");\n\nconst avg = await User.avg(\"price\");\n\nconst sum = await Order.where(\"status\", \"=\", \"paid\").sum(\"total\");\n",[92,936,937,958,962,992,996,1025,1030,1060,1065],{"__ignoreMap":106},[110,938,939,941,944,946,948,950,952,954,956],{"class":112,"line":113},[110,940,117],{"class":116},[110,942,943],{"class":120}," count ",[110,945,125],{"class":124},[110,947,129],{"class":128},[110,949,132],{"class":120},[110,951,135],{"class":124},[110,953,916],{"class":138},[110,955,141],{"class":120},[110,957,144],{"class":124},[110,959,960],{"class":112,"line":177},[110,961,535],{"emptyLinePlaceholder":534},[110,963,964,966,969,971,973,975,977,979,981,983,986,988,990],{"class":112,"line":538},[110,965,117],{"class":116},[110,967,968],{"class":120}," max ",[110,970,125],{"class":124},[110,972,129],{"class":128},[110,974,132],{"class":120},[110,976,135],{"class":124},[110,978,920],{"class":138},[110,980,195],{"class":120},[110,982,332],{"class":124},[110,984,985],{"class":335},"price",[110,987,332],{"class":124},[110,989,202],{"class":120},[110,991,144],{"class":124},[110,993,994],{"class":112,"line":586},[110,995,535],{"emptyLinePlaceholder":534},[110,997,998,1000,1003,1005,1007,1009,1011,1013,1015,1017,1019,1021,1023],{"class":112,"line":591},[110,999,117],{"class":116},[110,1001,1002],{"class":120}," min ",[110,1004,125],{"class":124},[110,1006,129],{"class":128},[110,1008,132],{"class":120},[110,1010,135],{"class":124},[110,1012,923],{"class":138},[110,1014,195],{"class":120},[110,1016,332],{"class":124},[110,1018,985],{"class":335},[110,1020,332],{"class":124},[110,1022,202],{"class":120},[110,1024,144],{"class":124},[110,1026,1028],{"class":112,"line":1027},6,[110,1029,535],{"emptyLinePlaceholder":534},[110,1031,1033,1035,1038,1040,1042,1044,1046,1048,1050,1052,1054,1056,1058],{"class":112,"line":1032},7,[110,1034,117],{"class":116},[110,1036,1037],{"class":120}," avg ",[110,1039,125],{"class":124},[110,1041,129],{"class":128},[110,1043,132],{"class":120},[110,1045,135],{"class":124},[110,1047,926],{"class":138},[110,1049,195],{"class":120},[110,1051,332],{"class":124},[110,1053,985],{"class":335},[110,1055,332],{"class":124},[110,1057,202],{"class":120},[110,1059,144],{"class":124},[110,1061,1063],{"class":112,"line":1062},8,[110,1064,535],{"emptyLinePlaceholder":534},[110,1066,1068,1070,1073,1075,1077,1080,1082,1084,1086,1088,1091,1093,1095,1097,1099,1101,1103,1105,1108,1110,1112,1114,1116,1118,1120,1122,1124,1126],{"class":112,"line":1067},9,[110,1069,117],{"class":116},[110,1071,1072],{"class":120}," sum ",[110,1074,125],{"class":124},[110,1076,129],{"class":128},[110,1078,1079],{"class":120}," Order",[110,1081,135],{"class":124},[110,1083,327],{"class":138},[110,1085,195],{"class":120},[110,1087,332],{"class":124},[110,1089,1090],{"class":335},"status",[110,1092,332],{"class":124},[110,1094,241],{"class":124},[110,1096,511],{"class":124},[110,1098,125],{"class":335},[110,1100,332],{"class":124},[110,1102,241],{"class":124},[110,1104,511],{"class":124},[110,1106,1107],{"class":335},"paid",[110,1109,332],{"class":124},[110,1111,202],{"class":120},[110,1113,135],{"class":124},[110,1115,930],{"class":138},[110,1117,195],{"class":120},[110,1119,332],{"class":124},[110,1121,887],{"class":335},[110,1123,332],{"class":124},[110,1125,202],{"class":120},[110,1127,144],{"class":124},[80,1129,1130,1132,1133,1135,1136,1139,1140,917,1142,927,1144,1135,1146,135],{},[92,1131,916],{}," and ",[92,1134,930],{}," return ",[92,1137,1138],{},"0"," when no records match, while ",[92,1141,920],{},[92,1143,923],{},[92,1145,926],{},[92,1147,1148],{},"null",[150,1150,1152],{"id":1151},"determining-if-records-exist","Determining if Records Exist",[80,1154,1155,1156,1132,1159,1162],{},"Instead of counting records, you can use the ",[92,1157,1158],{},"exists",[92,1160,1161],{},"doesntExist"," methods to efficiently check whether any records match the query's constraints:",[101,1164,1166],{"className":103,"code":1165,"language":105,"meta":106,"style":106},"if (await Order.where(\"status\", \"=\", \"pending\").exists()) {\n  // ...\n}\n\nif (await Order.where(\"user_id\", \"=\", 1).doesntExist()) {\n  // ...\n}\n",[92,1167,1168,1222,1227,1232,1236,1281,1285],{"__ignoreMap":106},[110,1169,1170,1173,1176,1179,1181,1183,1185,1187,1189,1191,1193,1195,1197,1199,1201,1203,1205,1208,1210,1212,1214,1216,1219],{"class":112,"line":113},[110,1171,1172],{"class":128},"if",[110,1174,1175],{"class":120}," (",[110,1177,1178],{"class":128},"await",[110,1180,1079],{"class":120},[110,1182,135],{"class":124},[110,1184,327],{"class":138},[110,1186,195],{"class":120},[110,1188,332],{"class":124},[110,1190,1090],{"class":335},[110,1192,332],{"class":124},[110,1194,241],{"class":124},[110,1196,511],{"class":124},[110,1198,125],{"class":335},[110,1200,332],{"class":124},[110,1202,241],{"class":124},[110,1204,511],{"class":124},[110,1206,1207],{"class":335},"pending",[110,1209,332],{"class":124},[110,1211,202],{"class":120},[110,1213,135],{"class":124},[110,1215,1158],{"class":138},[110,1217,1218],{"class":120},"()) ",[110,1220,1221],{"class":124},"{\n",[110,1223,1224],{"class":112,"line":177},[110,1225,1226],{"class":173},"  // ...\n",[110,1228,1229],{"class":112,"line":538},[110,1230,1231],{"class":124},"}\n",[110,1233,1234],{"class":112,"line":586},[110,1235,535],{"emptyLinePlaceholder":534},[110,1237,1238,1240,1242,1244,1246,1248,1250,1252,1254,1257,1259,1261,1263,1265,1267,1269,1271,1273,1275,1277,1279],{"class":112,"line":591},[110,1239,1172],{"class":128},[110,1241,1175],{"class":120},[110,1243,1178],{"class":128},[110,1245,1079],{"class":120},[110,1247,135],{"class":124},[110,1249,327],{"class":138},[110,1251,195],{"class":120},[110,1253,332],{"class":124},[110,1255,1256],{"class":335},"user_id",[110,1258,332],{"class":124},[110,1260,241],{"class":124},[110,1262,511],{"class":124},[110,1264,125],{"class":335},[110,1266,332],{"class":124},[110,1268,241],{"class":124},[110,1270,863],{"class":198},[110,1272,202],{"class":120},[110,1274,135],{"class":124},[110,1276,1161],{"class":138},[110,1278,1218],{"class":120},[110,1280,1221],{"class":124},[110,1282,1283],{"class":112,"line":1027},[110,1284,1226],{"class":173},[110,1286,1287],{"class":112,"line":1032},[110,1288,1231],{"class":124},[84,1290,1292],{"id":1291},"selecting-columns","Selecting Columns",[80,1294,1295,1296,1299],{},"You may not always want to select all columns from a database table. Using the ",[92,1297,1298],{},"select"," method, you can specify a custom \"select\" clause for the query:",[101,1301,1303],{"className":103,"code":1302,"language":105,"meta":106,"style":106},"const users = await User.select([\"name\", \"email\"]).get();\n",[92,1304,1305],{"__ignoreMap":106},[110,1306,1307,1309,1311,1313,1315,1317,1319,1321,1323,1325,1327,1329,1331,1333,1336,1338,1340,1342,1344,1346],{"class":112,"line":113},[110,1308,117],{"class":116},[110,1310,121],{"class":120},[110,1312,125],{"class":124},[110,1314,129],{"class":128},[110,1316,132],{"class":120},[110,1318,135],{"class":124},[110,1320,1298],{"class":138},[110,1322,236],{"class":120},[110,1324,332],{"class":124},[110,1326,800],{"class":335},[110,1328,332],{"class":124},[110,1330,241],{"class":124},[110,1332,511],{"class":124},[110,1334,1335],{"class":335},"email",[110,1337,332],{"class":124},[110,1339,252],{"class":120},[110,1341,135],{"class":124},[110,1343,98],{"class":138},[110,1345,141],{"class":120},[110,1347,144],{"class":124},[84,1349,1351],{"id":1350},"expressions-subqueries","Expressions & Subqueries",[80,1353,1354,1355,1358,1359,1362,1363,917,1365,917,1367,927,1369,1371],{},"Because Vasta is built on top of Kysely, it natively supports Kysely's expression and subquery system for advanced queries. You can pass an ",[92,1356,1357],{},"ExpressionBuilder"," callback—or standard Kysely ",[92,1360,1361],{},"Expression"," objects—directly into your ",[92,1364,1298],{},[92,1366,327],{},[92,1368,646],{},[92,1370,765],{}," clauses.",[80,1373,1374,1375,135],{},"For detailed information on writing expressions, please refer to the ",[1376,1377,1381],"a",{"href":1378,"rel":1379},"https://kysely.dev/docs/recipes/expressions",[1380],"nofollow","official Kysely documentation about expressions",[150,1383,1385],{"id":1384},"select-expressions","Select Expressions",[80,1387,643,1388,1390,1391,1393],{},[92,1389,1357],{}," callback in the ",[92,1392,1298],{}," method to utilize database functions, aliasing, and raw values. Vasta provides strict typings for aliased selections right out of the box:",[101,1395,1397],{"className":103,"code":1396,"language":105,"meta":106,"style":106},"const users = await User.select((eb) => [\n  \"id\",\n  \"email\",\n  eb.fn(\"upper\", [\"name\"]).as(\"upper_name\"), // SQL: upper(\"name\") as \"upper_name\"\n]).get();\n\n// Typed access is natively supported on the returned model instance!\nconsole.log(users[0].upper_name);\nconsole.log(users[0].attributes.upper_name);\n",[92,1398,1399,1431,1443,1453,1505,1517,1521,1526,1551],{"__ignoreMap":106},[110,1400,1401,1403,1405,1407,1409,1411,1413,1415,1417,1419,1423,1425,1428],{"class":112,"line":113},[110,1402,117],{"class":116},[110,1404,121],{"class":120},[110,1406,125],{"class":124},[110,1408,129],{"class":128},[110,1410,132],{"class":120},[110,1412,135],{"class":124},[110,1414,1298],{"class":138},[110,1416,195],{"class":120},[110,1418,195],{"class":124},[110,1420,1422],{"class":1421},"sHdIc","eb",[110,1424,202],{"class":124},[110,1426,1427],{"class":116}," =>",[110,1429,1430],{"class":120}," [\n",[110,1432,1433,1436,1438,1440],{"class":112,"line":177},[110,1434,1435],{"class":124},"  \"",[110,1437,675],{"class":335},[110,1439,332],{"class":124},[110,1441,1442],{"class":124},",\n",[110,1444,1445,1447,1449,1451],{"class":112,"line":538},[110,1446,1435],{"class":124},[110,1448,1335],{"class":335},[110,1450,332],{"class":124},[110,1452,1442],{"class":124},[110,1454,1455,1458,1460,1463,1465,1467,1470,1472,1474,1476,1478,1480,1482,1484,1486,1489,1491,1493,1496,1498,1500,1502],{"class":112,"line":586},[110,1456,1457],{"class":120},"  eb",[110,1459,135],{"class":124},[110,1461,1462],{"class":138},"fn",[110,1464,195],{"class":120},[110,1466,332],{"class":124},[110,1468,1469],{"class":335},"upper",[110,1471,332],{"class":124},[110,1473,241],{"class":124},[110,1475,682],{"class":120},[110,1477,332],{"class":124},[110,1479,800],{"class":335},[110,1481,332],{"class":124},[110,1483,252],{"class":120},[110,1485,135],{"class":124},[110,1487,1488],{"class":138},"as",[110,1490,195],{"class":120},[110,1492,332],{"class":124},[110,1494,1495],{"class":335},"upper_name",[110,1497,332],{"class":124},[110,1499,202],{"class":120},[110,1501,241],{"class":124},[110,1503,1504],{"class":173}," // SQL: upper(\"name\") as \"upper_name\"\n",[110,1506,1507,1509,1511,1513,1515],{"class":112,"line":591},[110,1508,252],{"class":120},[110,1510,135],{"class":124},[110,1512,98],{"class":138},[110,1514,141],{"class":120},[110,1516,144],{"class":124},[110,1518,1519],{"class":112,"line":1027},[110,1520,535],{"emptyLinePlaceholder":534},[110,1522,1523],{"class":112,"line":1032},[110,1524,1525],{"class":173},"// Typed access is natively supported on the returned model instance!\n",[110,1527,1528,1531,1533,1536,1539,1541,1544,1546,1549],{"class":112,"line":1062},[110,1529,1530],{"class":120},"console",[110,1532,135],{"class":124},[110,1534,1535],{"class":138},"log",[110,1537,1538],{"class":120},"(users[",[110,1540,1138],{"class":198},[110,1542,1543],{"class":120},"]",[110,1545,135],{"class":124},[110,1547,1548],{"class":120},"upper_name)",[110,1550,144],{"class":124},[110,1552,1553,1555,1557,1559,1561,1563,1565,1567,1570,1572,1574],{"class":112,"line":1067},[110,1554,1530],{"class":120},[110,1556,135],{"class":124},[110,1558,1535],{"class":138},[110,1560,1538],{"class":120},[110,1562,1138],{"class":198},[110,1564,1543],{"class":120},[110,1566,135],{"class":124},[110,1568,1569],{"class":120},"attributes",[110,1571,135],{"class":124},[110,1573,1548],{"class":120},[110,1575,144],{"class":124},[150,1577,1579],{"id":1578},"where-expressions","Where Expressions",[80,1581,1582,1583,1585,1586,1588],{},"You can use the same ",[92,1584,1357],{}," context in ",[92,1587,327],{}," clauses to filter against database functions or computed columns without needing to drop down into raw SQL strings:",[101,1590,1592],{"className":103,"code":1591,"language":105,"meta":106,"style":106},"// Using database functions in constraints\nconst users = await User.where((eb) => eb.fn(\"upper\", [\"name\"]), \"=\", \"DAVID\").get();\n",[92,1593,1594,1599],{"__ignoreMap":106},[110,1595,1596],{"class":112,"line":113},[110,1597,1598],{"class":173},"// Using database functions in constraints\n",[110,1600,1601,1603,1605,1607,1609,1611,1613,1615,1617,1619,1621,1623,1625,1628,1630,1632,1634,1636,1638,1640,1642,1644,1646,1648,1650,1652,1654,1656,1658,1660,1662,1664,1667,1669,1671,1673,1675,1677],{"class":112,"line":177},[110,1602,117],{"class":116},[110,1604,121],{"class":120},[110,1606,125],{"class":124},[110,1608,129],{"class":128},[110,1610,132],{"class":120},[110,1612,135],{"class":124},[110,1614,327],{"class":138},[110,1616,195],{"class":120},[110,1618,195],{"class":124},[110,1620,1422],{"class":1421},[110,1622,202],{"class":124},[110,1624,1427],{"class":116},[110,1626,1627],{"class":120}," eb",[110,1629,135],{"class":124},[110,1631,1462],{"class":138},[110,1633,195],{"class":120},[110,1635,332],{"class":124},[110,1637,1469],{"class":335},[110,1639,332],{"class":124},[110,1641,241],{"class":124},[110,1643,682],{"class":120},[110,1645,332],{"class":124},[110,1647,800],{"class":335},[110,1649,332],{"class":124},[110,1651,252],{"class":120},[110,1653,241],{"class":124},[110,1655,511],{"class":124},[110,1657,125],{"class":335},[110,1659,332],{"class":124},[110,1661,241],{"class":124},[110,1663,511],{"class":124},[110,1665,1666],{"class":335},"DAVID",[110,1668,332],{"class":124},[110,1670,202],{"class":120},[110,1672,135],{"class":124},[110,1674,98],{"class":138},[110,1676,141],{"class":120},[110,1678,144],{"class":124},[150,1680,1682],{"id":1681},"subqueries","Subqueries",[80,1684,1685],{},"You can also pass Kysely subquery expressions directly into your constraints. This is highly useful for matching values against a separate query result:",[101,1687,1689],{"className":103,"code":1688,"language":105,"meta":106,"style":106},"import { db } from \"./database\";\n\n// Create a subquery (resolves to a single value)\nconst latestPostSubquery = db.selectFrom(\"posts\").select(\"user_id\").orderBy(\"created_at\", \"desc\").limit(1);\n\n// Find the user who made the latest post matching the subquery\nconst user = await User.where(\"id\", \"=\", latestPostSubquery).first();\n",[92,1690,1691,1717,1721,1726,1803,1807,1812],{"__ignoreMap":106},[110,1692,1693,1696,1699,1702,1705,1708,1710,1713,1715],{"class":112,"line":113},[110,1694,1695],{"class":128},"import",[110,1697,1698],{"class":124}," {",[110,1700,1701],{"class":120}," db",[110,1703,1704],{"class":124}," }",[110,1706,1707],{"class":128}," from",[110,1709,511],{"class":124},[110,1711,1712],{"class":335},"./database",[110,1714,332],{"class":124},[110,1716,144],{"class":124},[110,1718,1719],{"class":112,"line":177},[110,1720,535],{"emptyLinePlaceholder":534},[110,1722,1723],{"class":112,"line":538},[110,1724,1725],{"class":173},"// Create a subquery (resolves to a single value)\n",[110,1727,1728,1730,1733,1735,1737,1739,1742,1744,1746,1749,1751,1753,1755,1757,1759,1761,1763,1765,1767,1769,1771,1773,1775,1778,1780,1782,1784,1786,1788,1790,1792,1795,1797,1799,1801],{"class":112,"line":586},[110,1729,117],{"class":116},[110,1731,1732],{"class":120}," latestPostSubquery ",[110,1734,125],{"class":124},[110,1736,1701],{"class":120},[110,1738,135],{"class":124},[110,1740,1741],{"class":138},"selectFrom",[110,1743,195],{"class":120},[110,1745,332],{"class":124},[110,1747,1748],{"class":335},"posts",[110,1750,332],{"class":124},[110,1752,202],{"class":120},[110,1754,135],{"class":124},[110,1756,1298],{"class":138},[110,1758,195],{"class":120},[110,1760,332],{"class":124},[110,1762,1256],{"class":335},[110,1764,332],{"class":124},[110,1766,202],{"class":120},[110,1768,135],{"class":124},[110,1770,765],{"class":138},[110,1772,195],{"class":120},[110,1774,332],{"class":124},[110,1776,1777],{"class":335},"created_at",[110,1779,332],{"class":124},[110,1781,241],{"class":124},[110,1783,511],{"class":124},[110,1785,772],{"class":335},[110,1787,332],{"class":124},[110,1789,202],{"class":120},[110,1791,135],{"class":124},[110,1793,1794],{"class":138},"limit",[110,1796,195],{"class":120},[110,1798,199],{"class":198},[110,1800,202],{"class":120},[110,1802,144],{"class":124},[110,1804,1805],{"class":112,"line":591},[110,1806,535],{"emptyLinePlaceholder":534},[110,1808,1809],{"class":112,"line":1027},[110,1810,1811],{"class":173},"// Find the user who made the latest post matching the subquery\n",[110,1813,1814,1816,1818,1820,1822,1824,1826,1828,1830,1832,1834,1836,1838,1840,1842,1844,1846,1849,1851,1853,1855],{"class":112,"line":1032},[110,1815,117],{"class":116},[110,1817,182],{"class":120},[110,1819,125],{"class":124},[110,1821,129],{"class":128},[110,1823,132],{"class":120},[110,1825,135],{"class":124},[110,1827,327],{"class":138},[110,1829,195],{"class":120},[110,1831,332],{"class":124},[110,1833,675],{"class":335},[110,1835,332],{"class":124},[110,1837,241],{"class":124},[110,1839,511],{"class":124},[110,1841,125],{"class":335},[110,1843,332],{"class":124},[110,1845,241],{"class":124},[110,1847,1848],{"class":120}," latestPostSubquery)",[110,1850,135],{"class":124},[110,1852,298],{"class":138},[110,1854,141],{"class":120},[110,1856,144],{"class":124},[80,1858,1859,1860,1863],{},"You can use arrays and ",[92,1861,1862],{},"in"," clauses in exactly the same way to match multiple results from a subquery:",[101,1865,1867],{"className":103,"code":1866,"language":105,"meta":106,"style":106},"// Query returning an array of IDs\nconst activeUsersSubquery = db.selectFrom(\"sessions\").select(\"user_id\").where(\"is_active\", \"=\", true);\n\n// Pass the subquery properly to the \"in\" operator\nconst activeUsers = await User.whereIn(\"id\", activeUsersSubquery).get();\n",[92,1868,1869,1874,1943,1947,1952],{"__ignoreMap":106},[110,1870,1871],{"class":112,"line":113},[110,1872,1873],{"class":173},"// Query returning an array of IDs\n",[110,1875,1876,1878,1881,1883,1885,1887,1889,1891,1893,1896,1898,1900,1902,1904,1906,1908,1910,1912,1914,1916,1918,1920,1922,1925,1927,1929,1931,1933,1935,1937,1939,1941],{"class":112,"line":177},[110,1877,117],{"class":116},[110,1879,1880],{"class":120}," activeUsersSubquery ",[110,1882,125],{"class":124},[110,1884,1701],{"class":120},[110,1886,135],{"class":124},[110,1888,1741],{"class":138},[110,1890,195],{"class":120},[110,1892,332],{"class":124},[110,1894,1895],{"class":335},"sessions",[110,1897,332],{"class":124},[110,1899,202],{"class":120},[110,1901,135],{"class":124},[110,1903,1298],{"class":138},[110,1905,195],{"class":120},[110,1907,332],{"class":124},[110,1909,1256],{"class":335},[110,1911,332],{"class":124},[110,1913,202],{"class":120},[110,1915,135],{"class":124},[110,1917,327],{"class":138},[110,1919,195],{"class":120},[110,1921,332],{"class":124},[110,1923,1924],{"class":335},"is_active",[110,1926,332],{"class":124},[110,1928,241],{"class":124},[110,1930,511],{"class":124},[110,1932,125],{"class":335},[110,1934,332],{"class":124},[110,1936,241],{"class":124},[110,1938,344],{"class":343},[110,1940,202],{"class":120},[110,1942,144],{"class":124},[110,1944,1945],{"class":112,"line":538},[110,1946,535],{"emptyLinePlaceholder":534},[110,1948,1949],{"class":112,"line":586},[110,1950,1951],{"class":173},"// Pass the subquery properly to the \"in\" operator\n",[110,1953,1954,1956,1959,1961,1963,1965,1967,1969,1971,1973,1975,1977,1979,1982,1984,1986,1988],{"class":112,"line":591},[110,1955,117],{"class":116},[110,1957,1958],{"class":120}," activeUsers ",[110,1960,125],{"class":124},[110,1962,129],{"class":128},[110,1964,132],{"class":120},[110,1966,135],{"class":124},[110,1968,646],{"class":138},[110,1970,195],{"class":120},[110,1972,332],{"class":124},[110,1974,675],{"class":335},[110,1976,332],{"class":124},[110,1978,241],{"class":124},[110,1980,1981],{"class":120}," activeUsersSubquery)",[110,1983,135],{"class":124},[110,1985,98],{"class":138},[110,1987,141],{"class":120},[110,1989,144],{"class":124},[1991,1992,1993],"style",{},"html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .sfNiH, html code.shiki .sfNiH{--shiki-light:#FF5370;--shiki-default:#FF9CAC;--shiki-dark:#FF9CAC}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}",{"title":106,"searchDepth":177,"depth":177,"links":1995},[1996,1997,2001,2006,2007,2008,2011,2012],{"id":86,"depth":177,"text":87},{"id":147,"depth":177,"text":148,"children":1998},[1999,2000],{"id":152,"depth":538,"text":153},{"id":298,"depth":538,"text":299},{"id":416,"depth":177,"text":417,"children":2002},[2003,2004,2005],{"id":420,"depth":538,"text":421},{"id":639,"depth":538,"text":640},{"id":705,"depth":538,"text":706},{"id":758,"depth":177,"text":759},{"id":823,"depth":177,"text":824},{"id":909,"depth":177,"text":910,"children":2009},[2010],{"id":1151,"depth":538,"text":1152},{"id":1291,"depth":177,"text":1292},{"id":1350,"depth":177,"text":1351,"children":2013},[2014,2015,2016],{"id":1384,"depth":538,"text":1385},{"id":1578,"depth":538,"text":1579},{"id":1681,"depth":538,"text":1682},"Retrieving models with Vasta.","md",null,{},{"icon":34},{"description":2023,"title":31},"Learn how to retrieve models with Vasta.","N6NRA6mYI9OS_JXEGQEjzaYNffKoDZmeZpsZTflIHLw",[2026,2028],{"title":26,"path":27,"stem":28,"description":2027,"icon":29,"children":-1},"Building models with Vasta.",{"title":36,"path":37,"stem":38,"description":2029,"icon":39,"children":-1},"Inserting and updating models with Vasta.",1784320847711]