Home / Function/ buildSelectQuery() — drizzle-orm Function Reference

buildSelectQuery() — drizzle-orm Function Reference

Architecture documentation for the buildSelectQuery() function in dialect.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0["buildSelectQuery()"]
  3977a3fd_2f08_73b1_4df4_3d0fd1858514["PgDialect"]
  9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0 -->|defined in| 3977a3fd_2f08_73b1_4df4_3d0fd1858514
  4fe5b8c4_3d15_8881_602b_e044ec70f270["buildRelationalQueryWithoutPK()"]
  4fe5b8c4_3d15_8881_602b_e044ec70f270 -->|calls| 9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0
  bcbd4f74_c6c7_2989_e283_a503bf2039fa["buildWithCTE()"]
  9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0 -->|calls| bcbd4f74_c6c7_2989_e283_a503bf2039fa
  e700cd38_c0c8_fbeb_26a9_7fbf17216117["buildSelection()"]
  9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0 -->|calls| e700cd38_c0c8_fbeb_26a9_7fbf17216117
  a3d42ee8_7394_7230_8590_b17469f33bad["buildFromTable()"]
  9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0 -->|calls| a3d42ee8_7394_7230_8590_b17469f33bad
  b947243c_21ad_f757_2719_e0aeb7980c05["buildJoins()"]
  9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0 -->|calls| b947243c_21ad_f757_2719_e0aeb7980c05
  b2ea6cdf_c2fc_2a55_86dc_57e47a8deb11["buildSetOperations()"]
  9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0 -->|calls| b2ea6cdf_c2fc_2a55_86dc_57e47a8deb11
  style 9a7f35b1_0b1e_f3ac_68cb_233ef6bc00a0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/pg-core/dialect.ts lines 341–448

	buildSelectQuery(
		{
			withList,
			fields,
			fieldsFlat,
			where,
			having,
			table,
			joins,
			orderBy,
			groupBy,
			limit,
			offset,
			lockingClause,
			distinct,
			setOperators,
		}: PgSelectConfig,
	): SQL {
		const fieldsList = fieldsFlat ?? orderSelectedFields<PgColumn>(fields);
		for (const f of fieldsList) {
			if (
				is(f.field, Column)
				&& getTableName(f.field.table)
					!== (is(table, Subquery)
						? table._.alias
						: is(table, PgViewBase)
						? table[ViewBaseConfig].name
						: is(table, SQL)
						? undefined
						: getTableName(table))
				&& !((table) =>
					joins?.some(({ alias }) =>
						alias === (table[Table.Symbol.IsAlias] ? getTableName(table) : table[Table.Symbol.BaseName])
					))(f.field.table)
			) {
				const tableName = getTableName(f.field.table);
				throw new Error(
					`Your "${
						f.path.join('->')
					}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?`,
				);
			}
		}

		const isSingleTable = !joins || joins.length === 0;

		const withSql = this.buildWithCTE(withList);

		let distinctSql: SQL | undefined;
		if (distinct) {
			distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
		}

		const selection = this.buildSelection(fieldsList, { isSingleTable });

		const tableSql = this.buildFromTable(table);

		const joinsSql = this.buildJoins(joins);

		const whereSql = where ? sql` where ${where}` : undefined;

		const havingSql = having ? sql` having ${having}` : undefined;

		let orderBySql;
		if (orderBy && orderBy.length > 0) {
			orderBySql = sql` order by ${sql.join(orderBy, sql`, `)}`;
		}

		let groupBySql;
		if (groupBy && groupBy.length > 0) {
			groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`;
		}

		const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
			? sql` limit ${limit}`
			: undefined;

		const offsetSql = offset ? sql` offset ${offset}` : undefined;

		const lockingClauseSql = sql.empty();
		if (lockingClause) {

Domain

Subdomains

Frequently Asked Questions

What does buildSelectQuery() do?
buildSelectQuery() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/pg-core/dialect.ts.
Where is buildSelectQuery() defined?
buildSelectQuery() is defined in drizzle-orm/src/pg-core/dialect.ts at line 341.
What does buildSelectQuery() call?
buildSelectQuery() calls 5 function(s): buildFromTable, buildJoins, buildSelection, buildSetOperations, buildWithCTE.
What calls buildSelectQuery()?
buildSelectQuery() is called by 1 function(s): buildRelationalQueryWithoutPK.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free