Home / Class/ PgSelectBuilder Class — drizzle-orm Architecture

PgSelectBuilder Class — drizzle-orm Architecture

Architecture documentation for the PgSelectBuilder class in select.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  6821233b_77bc_4757_b1f2_448aa2353293["PgSelectBuilder"]
  50a440a3_419f_74a4_a0ef_a8f58a685eb0["select.ts"]
  6821233b_77bc_4757_b1f2_448aa2353293 -->|defined in| 50a440a3_419f_74a4_a0ef_a8f58a685eb0
  5fca8f13_7d69_a30b_05af_90a34b1b2301["constructor()"]
  6821233b_77bc_4757_b1f2_448aa2353293 -->|method| 5fca8f13_7d69_a30b_05af_90a34b1b2301
  1905dbab_b1dc_6d27_7ad3_1045fff8afbe["setToken()"]
  6821233b_77bc_4757_b1f2_448aa2353293 -->|method| 1905dbab_b1dc_6d27_7ad3_1045fff8afbe
  8c473e54_8e6c_6b52_8f38_fe9a8bc71637["from()"]
  6821233b_77bc_4757_b1f2_448aa2353293 -->|method| 8c473e54_8e6c_6b52_8f38_fe9a8bc71637

Relationship Graph

Source Code

drizzle-orm/src/pg-core/query-builders/select.ts lines 62–151

export class PgSelectBuilder<
	TSelection extends SelectedFields | undefined,
	TBuilderMode extends 'db' | 'qb' = 'db',
> {
	static readonly [entityKind]: string = 'PgSelectBuilder';

	private fields: TSelection;
	private session: PgSession | undefined;
	private dialect: PgDialect;
	private withList: Subquery[] = [];
	private distinct: boolean | {
		on: (PgColumn | SQLWrapper)[];
	} | undefined;

	constructor(
		config: {
			fields: TSelection;
			session: PgSession | undefined;
			dialect: PgDialect;
			withList?: Subquery[];
			distinct?: boolean | {
				on: (PgColumn | SQLWrapper)[];
			};
		},
	) {
		this.fields = config.fields;
		this.session = config.session;
		this.dialect = config.dialect;
		if (config.withList) {
			this.withList = config.withList;
		}
		this.distinct = config.distinct;
	}

	private authToken?: NeonAuthToken;
	/** @internal */
	setToken(token?: NeonAuthToken) {
		this.authToken = token;
		return this;
	}

	/**
	 * Specify the table, subquery, or other target that you're
	 * building a select query against.
	 *
	 * {@link https://www.postgresql.org/docs/current/sql-select.html#SQL-FROM | Postgres from documentation}
	 */
	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'
	> {
		const isPartialSelect = !!this.fields;
		const src = source as TFrom;

		let fields: SelectedFields;
		if (this.fields) {
			fields = this.fields;
		} else if (is(src, Subquery)) {
			// This is required to use the proxy handler to get the correct field values from the subquery
			fields = Object.fromEntries(
				Object.keys(src._.selectedFields).map((
					key,
				) => [key, src[key as unknown as keyof typeof src] as unknown as SelectedFields[string]]),
			);
		} else if (is(src, PgViewBase)) {
			fields = src[ViewBaseConfig].selectedFields as SelectedFields;
		} else if (is(src, SQL)) {
			fields = {};
		} else {
			fields = getTableColumns<PgTable>(src);
		}

		return (new PgSelectBase({
			table: src,

Domain

Frequently Asked Questions

What is the PgSelectBuilder class?
PgSelectBuilder is a class in the drizzle-orm codebase, defined in drizzle-orm/src/pg-core/query-builders/select.ts.
Where is PgSelectBuilder defined?
PgSelectBuilder is defined in drizzle-orm/src/pg-core/query-builders/select.ts at line 62.

Analyze Your Own Codebase

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

Try Supermodel Free