Home / Class/ SelectionProxyHandler Class — drizzle-orm Architecture

SelectionProxyHandler Class — drizzle-orm Architecture

Architecture documentation for the SelectionProxyHandler class in selection-proxy.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  dd80b7b3_f33f_d059_9164_4d580deca2d7["SelectionProxyHandler"]
  a54a11d6_4422_4e60_3d09_6c04afd8f1eb["selection-proxy.ts"]
  dd80b7b3_f33f_d059_9164_4d580deca2d7 -->|defined in| a54a11d6_4422_4e60_3d09_6c04afd8f1eb
  c028152e_8efe_a4e0_46df_fd251cee705b["constructor()"]
  dd80b7b3_f33f_d059_9164_4d580deca2d7 -->|method| c028152e_8efe_a4e0_46df_fd251cee705b
  a40f58bf_d603_3d63_ca91_0e1365452bad["get()"]
  dd80b7b3_f33f_d059_9164_4d580deca2d7 -->|method| a40f58bf_d603_3d63_ca91_0e1365452bad

Relationship Graph

Source Code

drizzle-orm/src/selection-proxy.ts lines 8–121

export class SelectionProxyHandler<T extends Subquery | Record<string, unknown> | View>
	implements ProxyHandler<Subquery | Record<string, unknown> | View>
{
	static readonly [entityKind]: string = 'SelectionProxyHandler';

	private config: {
		/**
		 * Table alias for the columns
		 */
		alias?: string;
		/**
		 * What to do when a field is an instance of `SQL.Aliased` and it's not a selection field (from a subquery)
		 *
		 * `sql` - return the underlying SQL expression
		 *
		 * `alias` - return the field alias
		 */
		sqlAliasedBehavior: 'sql' | 'alias';
		/**
		 * What to do when a field is an instance of `SQL` and it doesn't have an alias declared
		 *
		 * `sql` - return the underlying SQL expression
		 *
		 * `error` - return a DrizzleTypeError on type level and throw an error on runtime
		 */
		sqlBehavior: 'sql' | 'error';

		/**
		 * Whether to replace the original name of the column with the alias
		 * Should be set to `true` for views creation
		 * @default false
		 */
		replaceOriginalName?: boolean;
	};

	constructor(config: SelectionProxyHandler<T>['config']) {
		this.config = { ...config };
	}

	get(subquery: T, prop: string | symbol): any {
		if (prop === '_') {
			return {
				...subquery['_' as keyof typeof subquery],
				selectedFields: new Proxy(
					(subquery as Subquery)._.selectedFields,
					this as ProxyHandler<Record<string, unknown>>,
				),
			};
		}

		if (prop === ViewBaseConfig) {
			return {
				...subquery[ViewBaseConfig as keyof typeof subquery],
				selectedFields: new Proxy(
					(subquery as View)[ViewBaseConfig].selectedFields,
					this as ProxyHandler<Record<string, unknown>>,
				),
			};
		}

		if (typeof prop === 'symbol') {
			return subquery[prop as keyof typeof subquery];
		}

		const columns = is(subquery, Subquery)
			? subquery._.selectedFields
			: is(subquery, View)
			? subquery[ViewBaseConfig].selectedFields
			: subquery;
		const value: unknown = columns[prop as keyof typeof columns];

		if (is(value, SQL.Aliased)) {
			// Never return the underlying SQL expression for a field previously selected in a subquery
			if (this.config.sqlAliasedBehavior === 'sql' && !value.isSelectionField) {
				return value.sql;
			}

			const newValue = value.clone();
			newValue.isSelectionField = true;
			return newValue;
		}

Domain

Frequently Asked Questions

What is the SelectionProxyHandler class?
SelectionProxyHandler is a class in the drizzle-orm codebase, defined in drizzle-orm/src/selection-proxy.ts.
Where is SelectionProxyHandler defined?
SelectionProxyHandler is defined in drizzle-orm/src/selection-proxy.ts at line 8.

Analyze Your Own Codebase

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

Try Supermodel Free