select.d.cts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. import type { CacheConfig, WithCacheConfig } from "../../cache/core/types.cjs";
  2. import { entityKind } from "../../entity.cjs";
  3. import type { PgColumn } from "../columns/index.cjs";
  4. import type { PgDialect } from "../dialect.cjs";
  5. import type { PgSession } from "../session.cjs";
  6. import type { SubqueryWithSelection } from "../subquery.cjs";
  7. import type { PgTable } from "../table.cjs";
  8. import { PgViewBase } from "../view-base.cjs";
  9. import { TypedQueryBuilder } from "../../query-builders/query-builder.cjs";
  10. import type { BuildSubquerySelection, GetSelectTableName, GetSelectTableSelection, JoinNullability, SelectMode, SelectResult } from "../../query-builders/select.types.cjs";
  11. import { QueryPromise } from "../../query-promise.cjs";
  12. import type { RunnableQuery } from "../../runnable-query.cjs";
  13. import { SQL } from "../../sql/sql.cjs";
  14. import type { ColumnsSelection, Placeholder, Query, SQLWrapper } from "../../sql/sql.cjs";
  15. import { Subquery } from "../../subquery.cjs";
  16. import { type DrizzleTypeError, type ValueOrArray } from "../../utils.cjs";
  17. import type { CreatePgSelectFromBuilderMode, GetPgSetOperators, LockConfig, LockStrength, PgCreateSetOperatorFn, PgSelectConfig, PgSelectCrossJoinFn, PgSelectDynamic, PgSelectHKT, PgSelectHKTBase, PgSelectJoinFn, PgSelectPrepare, PgSelectWithout, PgSetOperatorExcludedMethods, PgSetOperatorWithResult, SelectedFields, SetOperatorRightSelect, TableLikeHasEmptySelection } from "./select.types.cjs";
  18. export declare class PgSelectBuilder<TSelection extends SelectedFields | undefined, TBuilderMode extends 'db' | 'qb' = 'db'> {
  19. static readonly [entityKind]: string;
  20. private fields;
  21. private session;
  22. private dialect;
  23. private withList;
  24. private distinct;
  25. constructor(config: {
  26. fields: TSelection;
  27. session: PgSession | undefined;
  28. dialect: PgDialect;
  29. withList?: Subquery[];
  30. distinct?: boolean | {
  31. on: (PgColumn | SQLWrapper)[];
  32. };
  33. });
  34. private authToken?;
  35. /**
  36. * Specify the table, subquery, or other target that you're
  37. * building a select query against.
  38. *
  39. * {@link https://www.postgresql.org/docs/current/sql-select.html#SQL-FROM | Postgres from documentation}
  40. */
  41. from<TFrom extends PgTable | Subquery | PgViewBase | SQL>(source: TableLikeHasEmptySelection<TFrom> extends true ? DrizzleTypeError<"Cannot reference a data-modifying statement subquery if it doesn't contain a `returning` clause"> : TFrom): CreatePgSelectFromBuilderMode<TBuilderMode, GetSelectTableName<TFrom>, TSelection extends undefined ? GetSelectTableSelection<TFrom> : TSelection, TSelection extends undefined ? 'single' : 'partial'>;
  42. }
  43. export declare abstract class PgSelectQueryBuilderBase<THKT extends PgSelectHKTBase, TTableName extends string | undefined, TSelection extends ColumnsSelection, TSelectMode extends SelectMode, TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string ? Record<TTableName, 'not-null'> : {}, TDynamic extends boolean = false, TExcludedMethods extends string = never, TResult extends any[] = SelectResult<TSelection, TSelectMode, TNullabilityMap>[], TSelectedFields extends ColumnsSelection = BuildSubquerySelection<TSelection, TNullabilityMap>> extends TypedQueryBuilder<TSelectedFields, TResult> {
  44. static readonly [entityKind]: string;
  45. readonly _: {
  46. readonly dialect: 'pg';
  47. readonly hkt: THKT;
  48. readonly tableName: TTableName;
  49. readonly selection: TSelection;
  50. readonly selectMode: TSelectMode;
  51. readonly nullabilityMap: TNullabilityMap;
  52. readonly dynamic: TDynamic;
  53. readonly excludedMethods: TExcludedMethods;
  54. readonly result: TResult;
  55. readonly selectedFields: TSelectedFields;
  56. readonly config: PgSelectConfig;
  57. };
  58. protected config: PgSelectConfig;
  59. protected joinsNotNullableMap: Record<string, boolean>;
  60. protected tableName: string | undefined;
  61. private isPartialSelect;
  62. protected session: PgSession | undefined;
  63. protected dialect: PgDialect;
  64. protected cacheConfig?: WithCacheConfig;
  65. protected usedTables: Set<string>;
  66. constructor({ table, fields, isPartialSelect, session, dialect, withList, distinct }: {
  67. table: PgSelectConfig['table'];
  68. fields: PgSelectConfig['fields'];
  69. isPartialSelect: boolean;
  70. session: PgSession | undefined;
  71. dialect: PgDialect;
  72. withList: Subquery[];
  73. distinct: boolean | {
  74. on: (PgColumn | SQLWrapper)[];
  75. } | undefined;
  76. });
  77. private createJoin;
  78. /**
  79. * Executes a `left join` operation by adding another table to the current query.
  80. *
  81. * Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
  82. *
  83. * See docs: {@link https://orm.drizzle.team/docs/joins#left-join}
  84. *
  85. * @param table the table to join.
  86. * @param on the `on` clause.
  87. *
  88. * @example
  89. *
  90. * ```ts
  91. * // Select all users and their pets
  92. * const usersWithPets: { user: User; pets: Pet | null; }[] = await db.select()
  93. * .from(users)
  94. * .leftJoin(pets, eq(users.id, pets.ownerId))
  95. *
  96. * // Select userId and petId
  97. * const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
  98. * userId: users.id,
  99. * petId: pets.id,
  100. * })
  101. * .from(users)
  102. * .leftJoin(pets, eq(users.id, pets.ownerId))
  103. * ```
  104. */
  105. leftJoin: PgSelectJoinFn<this, TDynamic, "left", false>;
  106. /**
  107. * Executes a `left join lateral` operation by adding subquery to the current query.
  108. *
  109. * A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
  110. *
  111. * Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
  112. *
  113. * See docs: {@link https://orm.drizzle.team/docs/joins#left-join-lateral}
  114. *
  115. * @param table the subquery to join.
  116. * @param on the `on` clause.
  117. */
  118. leftJoinLateral: PgSelectJoinFn<this, TDynamic, "left", true>;
  119. /**
  120. * Executes a `right join` operation by adding another table to the current query.
  121. *
  122. * Calling this method associates each row of the joined table with the corresponding row from the main table, if a match is found. If no matching row exists, it sets all columns of the main table to null.
  123. *
  124. * See docs: {@link https://orm.drizzle.team/docs/joins#right-join}
  125. *
  126. * @param table the table to join.
  127. * @param on the `on` clause.
  128. *
  129. * @example
  130. *
  131. * ```ts
  132. * // Select all users and their pets
  133. * const usersWithPets: { user: User | null; pets: Pet; }[] = await db.select()
  134. * .from(users)
  135. * .rightJoin(pets, eq(users.id, pets.ownerId))
  136. *
  137. * // Select userId and petId
  138. * const usersIdsAndPetIds: { userId: number | null; petId: number; }[] = await db.select({
  139. * userId: users.id,
  140. * petId: pets.id,
  141. * })
  142. * .from(users)
  143. * .rightJoin(pets, eq(users.id, pets.ownerId))
  144. * ```
  145. */
  146. rightJoin: PgSelectJoinFn<this, TDynamic, "right", false>;
  147. /**
  148. * Executes an `inner join` operation, creating a new table by combining rows from two tables that have matching values.
  149. *
  150. * Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
  151. *
  152. * See docs: {@link https://orm.drizzle.team/docs/joins#inner-join}
  153. *
  154. * @param table the table to join.
  155. * @param on the `on` clause.
  156. *
  157. * @example
  158. *
  159. * ```ts
  160. * // Select all users and their pets
  161. * const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
  162. * .from(users)
  163. * .innerJoin(pets, eq(users.id, pets.ownerId))
  164. *
  165. * // Select userId and petId
  166. * const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
  167. * userId: users.id,
  168. * petId: pets.id,
  169. * })
  170. * .from(users)
  171. * .innerJoin(pets, eq(users.id, pets.ownerId))
  172. * ```
  173. */
  174. innerJoin: PgSelectJoinFn<this, TDynamic, "inner", false>;
  175. /**
  176. * Executes an `inner join lateral` operation, creating a new table by combining rows from two queries that have matching values.
  177. *
  178. * A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
  179. *
  180. * Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
  181. *
  182. * See docs: {@link https://orm.drizzle.team/docs/joins#inner-join-lateral}
  183. *
  184. * @param table the subquery to join.
  185. * @param on the `on` clause.
  186. */
  187. innerJoinLateral: PgSelectJoinFn<this, TDynamic, "inner", true>;
  188. /**
  189. * Executes a `full join` operation by combining rows from two tables into a new table.
  190. *
  191. * Calling this method retrieves all rows from both main and joined tables, merging rows with matching values and filling in `null` for non-matching columns.
  192. *
  193. * See docs: {@link https://orm.drizzle.team/docs/joins#full-join}
  194. *
  195. * @param table the table to join.
  196. * @param on the `on` clause.
  197. *
  198. * @example
  199. *
  200. * ```ts
  201. * // Select all users and their pets
  202. * const usersWithPets: { user: User | null; pets: Pet | null; }[] = await db.select()
  203. * .from(users)
  204. * .fullJoin(pets, eq(users.id, pets.ownerId))
  205. *
  206. * // Select userId and petId
  207. * const usersIdsAndPetIds: { userId: number | null; petId: number | null; }[] = await db.select({
  208. * userId: users.id,
  209. * petId: pets.id,
  210. * })
  211. * .from(users)
  212. * .fullJoin(pets, eq(users.id, pets.ownerId))
  213. * ```
  214. */
  215. fullJoin: PgSelectJoinFn<this, TDynamic, "full", false>;
  216. /**
  217. * Executes a `cross join` operation by combining rows from two tables into a new table.
  218. *
  219. * Calling this method retrieves all rows from both main and joined tables, merging all rows from each table.
  220. *
  221. * See docs: {@link https://orm.drizzle.team/docs/joins#cross-join}
  222. *
  223. * @param table the table to join.
  224. *
  225. * @example
  226. *
  227. * ```ts
  228. * // Select all users, each user with every pet
  229. * const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
  230. * .from(users)
  231. * .crossJoin(pets)
  232. *
  233. * // Select userId and petId
  234. * const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
  235. * userId: users.id,
  236. * petId: pets.id,
  237. * })
  238. * .from(users)
  239. * .crossJoin(pets)
  240. * ```
  241. */
  242. crossJoin: PgSelectCrossJoinFn<this, TDynamic, false>;
  243. /**
  244. * Executes a `cross join lateral` operation by combining rows from two queries into a new table.
  245. *
  246. * A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
  247. *
  248. * Calling this method retrieves all rows from both main and joined queries, merging all rows from each query.
  249. *
  250. * See docs: {@link https://orm.drizzle.team/docs/joins#cross-join-lateral}
  251. *
  252. * @param table the query to join.
  253. */
  254. crossJoinLateral: PgSelectCrossJoinFn<this, TDynamic, true>;
  255. private createSetOperator;
  256. /**
  257. * Adds `union` set operator to the query.
  258. *
  259. * Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.
  260. *
  261. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union}
  262. *
  263. * @example
  264. *
  265. * ```ts
  266. * // Select all unique names from customers and users tables
  267. * await db.select({ name: users.name })
  268. * .from(users)
  269. * .union(
  270. * db.select({ name: customers.name }).from(customers)
  271. * );
  272. * // or
  273. * import { union } from 'drizzle-orm/pg-core'
  274. *
  275. * await union(
  276. * db.select({ name: users.name }).from(users),
  277. * db.select({ name: customers.name }).from(customers)
  278. * );
  279. * ```
  280. */
  281. union: <TValue extends PgSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetPgSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => PgSelectWithout<this, TDynamic, PgSetOperatorExcludedMethods, true>;
  282. /**
  283. * Adds `union all` set operator to the query.
  284. *
  285. * Calling this method will combine the result-set of the `select` statements and keep all duplicate rows that appear across them.
  286. *
  287. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union-all}
  288. *
  289. * @example
  290. *
  291. * ```ts
  292. * // Select all transaction ids from both online and in-store sales
  293. * await db.select({ transaction: onlineSales.transactionId })
  294. * .from(onlineSales)
  295. * .unionAll(
  296. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  297. * );
  298. * // or
  299. * import { unionAll } from 'drizzle-orm/pg-core'
  300. *
  301. * await unionAll(
  302. * db.select({ transaction: onlineSales.transactionId }).from(onlineSales),
  303. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  304. * );
  305. * ```
  306. */
  307. unionAll: <TValue extends PgSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetPgSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => PgSelectWithout<this, TDynamic, PgSetOperatorExcludedMethods, true>;
  308. /**
  309. * Adds `intersect` set operator to the query.
  310. *
  311. * Calling this method will retain only the rows that are present in both result sets and eliminate duplicates.
  312. *
  313. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect}
  314. *
  315. * @example
  316. *
  317. * ```ts
  318. * // Select course names that are offered in both departments A and B
  319. * await db.select({ courseName: depA.courseName })
  320. * .from(depA)
  321. * .intersect(
  322. * db.select({ courseName: depB.courseName }).from(depB)
  323. * );
  324. * // or
  325. * import { intersect } from 'drizzle-orm/pg-core'
  326. *
  327. * await intersect(
  328. * db.select({ courseName: depA.courseName }).from(depA),
  329. * db.select({ courseName: depB.courseName }).from(depB)
  330. * );
  331. * ```
  332. */
  333. intersect: <TValue extends PgSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetPgSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => PgSelectWithout<this, TDynamic, PgSetOperatorExcludedMethods, true>;
  334. /**
  335. * Adds `intersect all` set operator to the query.
  336. *
  337. * Calling this method will retain only the rows that are present in both result sets including all duplicates.
  338. *
  339. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect-all}
  340. *
  341. * @example
  342. *
  343. * ```ts
  344. * // Select all products and quantities that are ordered by both regular and VIP customers
  345. * await db.select({
  346. * productId: regularCustomerOrders.productId,
  347. * quantityOrdered: regularCustomerOrders.quantityOrdered
  348. * })
  349. * .from(regularCustomerOrders)
  350. * .intersectAll(
  351. * db.select({
  352. * productId: vipCustomerOrders.productId,
  353. * quantityOrdered: vipCustomerOrders.quantityOrdered
  354. * })
  355. * .from(vipCustomerOrders)
  356. * );
  357. * // or
  358. * import { intersectAll } from 'drizzle-orm/pg-core'
  359. *
  360. * await intersectAll(
  361. * db.select({
  362. * productId: regularCustomerOrders.productId,
  363. * quantityOrdered: regularCustomerOrders.quantityOrdered
  364. * })
  365. * .from(regularCustomerOrders),
  366. * db.select({
  367. * productId: vipCustomerOrders.productId,
  368. * quantityOrdered: vipCustomerOrders.quantityOrdered
  369. * })
  370. * .from(vipCustomerOrders)
  371. * );
  372. * ```
  373. */
  374. intersectAll: <TValue extends PgSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetPgSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => PgSelectWithout<this, TDynamic, PgSetOperatorExcludedMethods, true>;
  375. /**
  376. * Adds `except` set operator to the query.
  377. *
  378. * Calling this method will retrieve all unique rows from the left query, except for the rows that are present in the result set of the right query.
  379. *
  380. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except}
  381. *
  382. * @example
  383. *
  384. * ```ts
  385. * // Select all courses offered in department A but not in department B
  386. * await db.select({ courseName: depA.courseName })
  387. * .from(depA)
  388. * .except(
  389. * db.select({ courseName: depB.courseName }).from(depB)
  390. * );
  391. * // or
  392. * import { except } from 'drizzle-orm/pg-core'
  393. *
  394. * await except(
  395. * db.select({ courseName: depA.courseName }).from(depA),
  396. * db.select({ courseName: depB.courseName }).from(depB)
  397. * );
  398. * ```
  399. */
  400. except: <TValue extends PgSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetPgSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => PgSelectWithout<this, TDynamic, PgSetOperatorExcludedMethods, true>;
  401. /**
  402. * Adds `except all` set operator to the query.
  403. *
  404. * Calling this method will retrieve all rows from the left query, except for the rows that are present in the result set of the right query.
  405. *
  406. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except-all}
  407. *
  408. * @example
  409. *
  410. * ```ts
  411. * // Select all products that are ordered by regular customers but not by VIP customers
  412. * await db.select({
  413. * productId: regularCustomerOrders.productId,
  414. * quantityOrdered: regularCustomerOrders.quantityOrdered,
  415. * })
  416. * .from(regularCustomerOrders)
  417. * .exceptAll(
  418. * db.select({
  419. * productId: vipCustomerOrders.productId,
  420. * quantityOrdered: vipCustomerOrders.quantityOrdered,
  421. * })
  422. * .from(vipCustomerOrders)
  423. * );
  424. * // or
  425. * import { exceptAll } from 'drizzle-orm/pg-core'
  426. *
  427. * await exceptAll(
  428. * db.select({
  429. * productId: regularCustomerOrders.productId,
  430. * quantityOrdered: regularCustomerOrders.quantityOrdered
  431. * })
  432. * .from(regularCustomerOrders),
  433. * db.select({
  434. * productId: vipCustomerOrders.productId,
  435. * quantityOrdered: vipCustomerOrders.quantityOrdered
  436. * })
  437. * .from(vipCustomerOrders)
  438. * );
  439. * ```
  440. */
  441. exceptAll: <TValue extends PgSetOperatorWithResult<TResult>>(rightSelection: ((setOperators: GetPgSetOperators) => SetOperatorRightSelect<TValue, TResult>) | SetOperatorRightSelect<TValue, TResult>) => PgSelectWithout<this, TDynamic, PgSetOperatorExcludedMethods, true>;
  442. /**
  443. * Adds a `where` clause to the query.
  444. *
  445. * Calling this method will select only those rows that fulfill a specified condition.
  446. *
  447. * See docs: {@link https://orm.drizzle.team/docs/select#filtering}
  448. *
  449. * @param where the `where` clause.
  450. *
  451. * @example
  452. * You can use conditional operators and `sql function` to filter the rows to be selected.
  453. *
  454. * ```ts
  455. * // Select all cars with green color
  456. * await db.select().from(cars).where(eq(cars.color, 'green'));
  457. * // or
  458. * await db.select().from(cars).where(sql`${cars.color} = 'green'`)
  459. * ```
  460. *
  461. * You can logically combine conditional operators with `and()` and `or()` operators:
  462. *
  463. * ```ts
  464. * // Select all BMW cars with a green color
  465. * await db.select().from(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
  466. *
  467. * // Select all cars with the green or blue color
  468. * await db.select().from(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
  469. * ```
  470. */
  471. where(where: ((aliases: this['_']['selection']) => SQL | undefined) | SQL | undefined): PgSelectWithout<this, TDynamic, 'where'>;
  472. /**
  473. * Adds a `having` clause to the query.
  474. *
  475. * Calling this method will select only those rows that fulfill a specified condition. It is typically used with aggregate functions to filter the aggregated data based on a specified condition.
  476. *
  477. * See docs: {@link https://orm.drizzle.team/docs/select#aggregations}
  478. *
  479. * @param having the `having` clause.
  480. *
  481. * @example
  482. *
  483. * ```ts
  484. * // Select all brands with more than one car
  485. * await db.select({
  486. * brand: cars.brand,
  487. * count: sql<number>`cast(count(${cars.id}) as int)`,
  488. * })
  489. * .from(cars)
  490. * .groupBy(cars.brand)
  491. * .having(({ count }) => gt(count, 1));
  492. * ```
  493. */
  494. having(having: ((aliases: this['_']['selection']) => SQL | undefined) | SQL | undefined): PgSelectWithout<this, TDynamic, 'having'>;
  495. /**
  496. * Adds a `group by` clause to the query.
  497. *
  498. * Calling this method will group rows that have the same values into summary rows, often used for aggregation purposes.
  499. *
  500. * See docs: {@link https://orm.drizzle.team/docs/select#aggregations}
  501. *
  502. * @example
  503. *
  504. * ```ts
  505. * // Group and count people by their last names
  506. * await db.select({
  507. * lastName: people.lastName,
  508. * count: sql<number>`cast(count(*) as int)`
  509. * })
  510. * .from(people)
  511. * .groupBy(people.lastName);
  512. * ```
  513. */
  514. groupBy(builder: (aliases: this['_']['selection']) => ValueOrArray<PgColumn | SQL | SQL.Aliased>): PgSelectWithout<this, TDynamic, 'groupBy'>;
  515. groupBy(...columns: (PgColumn | SQL | SQL.Aliased)[]): PgSelectWithout<this, TDynamic, 'groupBy'>;
  516. /**
  517. * Adds an `order by` clause to the query.
  518. *
  519. * Calling this method will sort the result-set in ascending or descending order. By default, the sort order is ascending.
  520. *
  521. * See docs: {@link https://orm.drizzle.team/docs/select#order-by}
  522. *
  523. * @example
  524. *
  525. * ```
  526. * // Select cars ordered by year
  527. * await db.select().from(cars).orderBy(cars.year);
  528. * ```
  529. *
  530. * You can specify whether results are in ascending or descending order with the `asc()` and `desc()` operators.
  531. *
  532. * ```ts
  533. * // Select cars ordered by year in descending order
  534. * await db.select().from(cars).orderBy(desc(cars.year));
  535. *
  536. * // Select cars ordered by year and price
  537. * await db.select().from(cars).orderBy(asc(cars.year), desc(cars.price));
  538. * ```
  539. */
  540. orderBy(builder: (aliases: this['_']['selection']) => ValueOrArray<PgColumn | SQL | SQL.Aliased>): PgSelectWithout<this, TDynamic, 'orderBy'>;
  541. orderBy(...columns: (PgColumn | SQL | SQL.Aliased)[]): PgSelectWithout<this, TDynamic, 'orderBy'>;
  542. /**
  543. * Adds a `limit` clause to the query.
  544. *
  545. * Calling this method will set the maximum number of rows that will be returned by this query.
  546. *
  547. * See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
  548. *
  549. * @param limit the `limit` clause.
  550. *
  551. * @example
  552. *
  553. * ```ts
  554. * // Get the first 10 people from this query.
  555. * await db.select().from(people).limit(10);
  556. * ```
  557. */
  558. limit(limit: number | Placeholder): PgSelectWithout<this, TDynamic, 'limit'>;
  559. /**
  560. * Adds an `offset` clause to the query.
  561. *
  562. * Calling this method will skip a number of rows when returning results from this query.
  563. *
  564. * See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
  565. *
  566. * @param offset the `offset` clause.
  567. *
  568. * @example
  569. *
  570. * ```ts
  571. * // Get the 10th-20th people from this query.
  572. * await db.select().from(people).offset(10).limit(10);
  573. * ```
  574. */
  575. offset(offset: number | Placeholder): PgSelectWithout<this, TDynamic, 'offset'>;
  576. /**
  577. * Adds a `for` clause to the query.
  578. *
  579. * Calling this method will specify a lock strength for this query that controls how strictly it acquires exclusive access to the rows being queried.
  580. *
  581. * See docs: {@link https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE}
  582. *
  583. * @param strength the lock strength.
  584. * @param config the lock configuration.
  585. */
  586. for(strength: LockStrength, config?: LockConfig): PgSelectWithout<this, TDynamic, 'for'>;
  587. toSQL(): Query;
  588. as<TAlias extends string>(alias: TAlias): SubqueryWithSelection<this['_']['selectedFields'], TAlias>;
  589. $dynamic(): PgSelectDynamic<this>;
  590. $withCache(config?: {
  591. config?: CacheConfig;
  592. tag?: string;
  593. autoInvalidate?: boolean;
  594. } | false): this;
  595. }
  596. export interface PgSelectBase<TTableName extends string | undefined, TSelection extends ColumnsSelection, TSelectMode extends SelectMode, TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string ? Record<TTableName, 'not-null'> : {}, TDynamic extends boolean = false, TExcludedMethods extends string = never, TResult extends any[] = SelectResult<TSelection, TSelectMode, TNullabilityMap>[], TSelectedFields extends ColumnsSelection = BuildSubquerySelection<TSelection, TNullabilityMap>> extends PgSelectQueryBuilderBase<PgSelectHKT, TTableName, TSelection, TSelectMode, TNullabilityMap, TDynamic, TExcludedMethods, TResult, TSelectedFields>, QueryPromise<TResult>, SQLWrapper {
  597. }
  598. export declare class PgSelectBase<TTableName extends string | undefined, TSelection extends ColumnsSelection, TSelectMode extends SelectMode, TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string ? Record<TTableName, 'not-null'> : {}, TDynamic extends boolean = false, TExcludedMethods extends string = never, TResult = SelectResult<TSelection, TSelectMode, TNullabilityMap>[], TSelectedFields = BuildSubquerySelection<TSelection, TNullabilityMap>> extends PgSelectQueryBuilderBase<PgSelectHKT, TTableName, TSelection, TSelectMode, TNullabilityMap, TDynamic, TExcludedMethods, TResult, TSelectedFields> implements RunnableQuery<TResult, 'pg'>, SQLWrapper {
  599. static readonly [entityKind]: string;
  600. /**
  601. * Create a prepared statement for this query. This allows
  602. * the database to remember this query for the given session
  603. * and call it by name, rather than specifying the full query.
  604. *
  605. * {@link https://www.postgresql.org/docs/current/sql-prepare.html | Postgres prepare documentation}
  606. */
  607. prepare(name: string): PgSelectPrepare<this>;
  608. private authToken?;
  609. execute: ReturnType<this['prepare']>['execute'];
  610. }
  611. /**
  612. * Adds `union` set operator to the query.
  613. *
  614. * Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.
  615. *
  616. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union}
  617. *
  618. * @example
  619. *
  620. * ```ts
  621. * // Select all unique names from customers and users tables
  622. * import { union } from 'drizzle-orm/pg-core'
  623. *
  624. * await union(
  625. * db.select({ name: users.name }).from(users),
  626. * db.select({ name: customers.name }).from(customers)
  627. * );
  628. * // or
  629. * await db.select({ name: users.name })
  630. * .from(users)
  631. * .union(
  632. * db.select({ name: customers.name }).from(customers)
  633. * );
  634. * ```
  635. */
  636. export declare const union: PgCreateSetOperatorFn;
  637. /**
  638. * Adds `union all` set operator to the query.
  639. *
  640. * Calling this method will combine the result-set of the `select` statements and keep all duplicate rows that appear across them.
  641. *
  642. * See docs: {@link https://orm.drizzle.team/docs/set-operations#union-all}
  643. *
  644. * @example
  645. *
  646. * ```ts
  647. * // Select all transaction ids from both online and in-store sales
  648. * import { unionAll } from 'drizzle-orm/pg-core'
  649. *
  650. * await unionAll(
  651. * db.select({ transaction: onlineSales.transactionId }).from(onlineSales),
  652. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  653. * );
  654. * // or
  655. * await db.select({ transaction: onlineSales.transactionId })
  656. * .from(onlineSales)
  657. * .unionAll(
  658. * db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
  659. * );
  660. * ```
  661. */
  662. export declare const unionAll: PgCreateSetOperatorFn;
  663. /**
  664. * Adds `intersect` set operator to the query.
  665. *
  666. * Calling this method will retain only the rows that are present in both result sets and eliminate duplicates.
  667. *
  668. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect}
  669. *
  670. * @example
  671. *
  672. * ```ts
  673. * // Select course names that are offered in both departments A and B
  674. * import { intersect } from 'drizzle-orm/pg-core'
  675. *
  676. * await intersect(
  677. * db.select({ courseName: depA.courseName }).from(depA),
  678. * db.select({ courseName: depB.courseName }).from(depB)
  679. * );
  680. * // or
  681. * await db.select({ courseName: depA.courseName })
  682. * .from(depA)
  683. * .intersect(
  684. * db.select({ courseName: depB.courseName }).from(depB)
  685. * );
  686. * ```
  687. */
  688. export declare const intersect: PgCreateSetOperatorFn;
  689. /**
  690. * Adds `intersect all` set operator to the query.
  691. *
  692. * Calling this method will retain only the rows that are present in both result sets including all duplicates.
  693. *
  694. * See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect-all}
  695. *
  696. * @example
  697. *
  698. * ```ts
  699. * // Select all products and quantities that are ordered by both regular and VIP customers
  700. * import { intersectAll } from 'drizzle-orm/pg-core'
  701. *
  702. * await intersectAll(
  703. * db.select({
  704. * productId: regularCustomerOrders.productId,
  705. * quantityOrdered: regularCustomerOrders.quantityOrdered
  706. * })
  707. * .from(regularCustomerOrders),
  708. * db.select({
  709. * productId: vipCustomerOrders.productId,
  710. * quantityOrdered: vipCustomerOrders.quantityOrdered
  711. * })
  712. * .from(vipCustomerOrders)
  713. * );
  714. * // or
  715. * await db.select({
  716. * productId: regularCustomerOrders.productId,
  717. * quantityOrdered: regularCustomerOrders.quantityOrdered
  718. * })
  719. * .from(regularCustomerOrders)
  720. * .intersectAll(
  721. * db.select({
  722. * productId: vipCustomerOrders.productId,
  723. * quantityOrdered: vipCustomerOrders.quantityOrdered
  724. * })
  725. * .from(vipCustomerOrders)
  726. * );
  727. * ```
  728. */
  729. export declare const intersectAll: PgCreateSetOperatorFn;
  730. /**
  731. * Adds `except` set operator to the query.
  732. *
  733. * Calling this method will retrieve all unique rows from the left query, except for the rows that are present in the result set of the right query.
  734. *
  735. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except}
  736. *
  737. * @example
  738. *
  739. * ```ts
  740. * // Select all courses offered in department A but not in department B
  741. * import { except } from 'drizzle-orm/pg-core'
  742. *
  743. * await except(
  744. * db.select({ courseName: depA.courseName }).from(depA),
  745. * db.select({ courseName: depB.courseName }).from(depB)
  746. * );
  747. * // or
  748. * await db.select({ courseName: depA.courseName })
  749. * .from(depA)
  750. * .except(
  751. * db.select({ courseName: depB.courseName }).from(depB)
  752. * );
  753. * ```
  754. */
  755. export declare const except: PgCreateSetOperatorFn;
  756. /**
  757. * Adds `except all` set operator to the query.
  758. *
  759. * Calling this method will retrieve all rows from the left query, except for the rows that are present in the result set of the right query.
  760. *
  761. * See docs: {@link https://orm.drizzle.team/docs/set-operations#except-all}
  762. *
  763. * @example
  764. *
  765. * ```ts
  766. * // Select all products that are ordered by regular customers but not by VIP customers
  767. * import { exceptAll } from 'drizzle-orm/pg-core'
  768. *
  769. * await exceptAll(
  770. * db.select({
  771. * productId: regularCustomerOrders.productId,
  772. * quantityOrdered: regularCustomerOrders.quantityOrdered
  773. * })
  774. * .from(regularCustomerOrders),
  775. * db.select({
  776. * productId: vipCustomerOrders.productId,
  777. * quantityOrdered: vipCustomerOrders.quantityOrdered
  778. * })
  779. * .from(vipCustomerOrders)
  780. * );
  781. * // or
  782. * await db.select({
  783. * productId: regularCustomerOrders.productId,
  784. * quantityOrdered: regularCustomerOrders.quantityOrdered,
  785. * })
  786. * .from(regularCustomerOrders)
  787. * .exceptAll(
  788. * db.select({
  789. * productId: vipCustomerOrders.productId,
  790. * quantityOrdered: vipCustomerOrders.quantityOrdered,
  791. * })
  792. * .from(vipCustomerOrders)
  793. * );
  794. * ```
  795. */
  796. export declare const exceptAll: PgCreateSetOperatorFn;