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
  6a5af19b_779e_e705_f0b0_c71ea6c700fd["buildSelectQuery()"]
  f80973ef_149c_bd36_66ee_1e7e9024a9d5["GelDialect"]
  6a5af19b_779e_e705_f0b0_c71ea6c700fd -->|defined in| f80973ef_149c_bd36_66ee_1e7e9024a9d5
  548001e7_3776_357f_1960_62423198f20f["buildRelationalQueryWithoutPK()"]
  548001e7_3776_357f_1960_62423198f20f -->|calls| 6a5af19b_779e_e705_f0b0_c71ea6c700fd
  508f5cc1_d856_0ba4_7831_119c8e91ea8f["buildWithCTE()"]
  6a5af19b_779e_e705_f0b0_c71ea6c700fd -->|calls| 508f5cc1_d856_0ba4_7831_119c8e91ea8f
  e2fb541e_5197_4994_9f97_bb47abf659ff["buildSelection()"]
  6a5af19b_779e_e705_f0b0_c71ea6c700fd -->|calls| e2fb541e_5197_4994_9f97_bb47abf659ff
  3dcc15aa_317d_ff74_31aa_fc51c58a0b1c["buildFromTable()"]
  6a5af19b_779e_e705_f0b0_c71ea6c700fd -->|calls| 3dcc15aa_317d_ff74_31aa_fc51c58a0b1c
  e14f6d94_0730_204f_0331_852853023bf3["buildJoins()"]
  6a5af19b_779e_e705_f0b0_c71ea6c700fd -->|calls| e14f6d94_0730_204f_0331_852853023bf3
  896a4bfc_a0fe_c924_90a8_862a42b92dd8["buildSetOperations()"]
  6a5af19b_779e_e705_f0b0_c71ea6c700fd -->|calls| 896a4bfc_a0fe_c924_90a8_862a42b92dd8
  style 6a5af19b_779e_e705_f0b0_c71ea6c700fd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/gel-core/dialect.ts lines 336–443

	buildSelectQuery(
		{
			withList,
			fields,
			fieldsFlat,
			where,
			having,
			table,
			joins,
			orderBy,
			groupBy,
			limit,
			offset,
			lockingClause,
			distinct,
			setOperators,
		}: GelSelectConfig,
	): SQL {
		const fieldsList = fieldsFlat ?? orderSelectedFields<GelColumn>(fields);
		for (const f of fieldsList) {
			if (
				is(f.field, Column)
				&& getTableName(f.field.table)
					!== (is(table, Subquery)
						? table._.alias
						: is(table, GelViewBase)
						? 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/gel-core/dialect.ts.
Where is buildSelectQuery() defined?
buildSelectQuery() is defined in drizzle-orm/src/gel-core/dialect.ts at line 336.
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