Home / Class/ SQL Class — drizzle-orm Architecture

SQL Class — drizzle-orm Architecture

Architecture documentation for the SQL class in sql.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  2361fb30_407d_7604_384a_a24acc5652f6["SQL"]
  99347ab2_b1a1_faf4_e37c_7643e4b2eb8a["sql.ts"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|defined in| 99347ab2_b1a1_faf4_e37c_7643e4b2eb8a
  f883c79a_2de4_75b6_8527_638d774e21e8["constructor()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| f883c79a_2de4_75b6_8527_638d774e21e8
  368977b8_bef8_9ef2_255c_cda2b970c14a["append()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| 368977b8_bef8_9ef2_255c_cda2b970c14a
  254ea348_1614_ce5d_9d75_64a14048644e["toQuery()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| 254ea348_1614_ce5d_9d75_64a14048644e
  f5a982b1_e6b9_5bf8_12b4_3d305d59c2ae["buildQueryFromSourceParams()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| f5a982b1_e6b9_5bf8_12b4_3d305d59c2ae
  46489340_6e04_faec_9b9b_56050d0e0439["mapInlineParam()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| 46489340_6e04_faec_9b9b_56050d0e0439
  ccfdd8be_83d3_c28f_35db_9346eb6c1dfb["getSQL()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| ccfdd8be_83d3_c28f_35db_9346eb6c1dfb
  0978f951_3ba0_da9c_c53c_2033c8b98ee2["as()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| 0978f951_3ba0_da9c_c53c_2033c8b98ee2
  932d6924_b2b0_6352_42f7_220855d521b7["mapWith()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| 932d6924_b2b0_6352_42f7_220855d521b7
  f844e442_f94b_ac0a_70d1_892845daf9e9["inlineParams()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| f844e442_f94b_ac0a_70d1_892845daf9e9
  edf7c708_00a8_8534_6f5b_4941d3bf7424["if()"]
  2361fb30_407d_7604_384a_a24acc5652f6 -->|method| edf7c708_00a8_8534_6f5b_4941d3bf7424

Relationship Graph

Source Code

drizzle-orm/src/sql/sql.ts lines 103–372

export class SQL<T = unknown> implements SQLWrapper {
	static readonly [entityKind]: string = 'SQL';

	declare _: {
		brand: 'SQL';
		type: T;
	};

	/** @internal */
	decoder: DriverValueDecoder<T, any> = noopDecoder;
	private shouldInlineParams = false;

	/** @internal */
	usedTables: string[] = [];

	constructor(readonly queryChunks: SQLChunk[]) {
		for (const chunk of queryChunks) {
			if (is(chunk, Table)) {
				const schemaName = chunk[Table.Symbol.Schema];

				this.usedTables.push(
					schemaName === undefined
						? chunk[Table.Symbol.Name]
						: schemaName + '.' + chunk[Table.Symbol.Name],
				);
			}
		}
	}

	append(query: SQL): this {
		this.queryChunks.push(...query.queryChunks);
		return this;
	}

	toQuery(config: BuildQueryConfig): QueryWithTypings {
		return tracer.startActiveSpan('drizzle.buildSQL', (span) => {
			const query = this.buildQueryFromSourceParams(this.queryChunks, config);
			span?.setAttributes({
				'drizzle.query.text': query.sql,
				'drizzle.query.params': JSON.stringify(query.params),
			});
			return query;
		});
	}

	buildQueryFromSourceParams(chunks: SQLChunk[], _config: BuildQueryConfig): Query {
		const config = Object.assign({}, _config, {
			inlineParams: _config.inlineParams || this.shouldInlineParams,
			paramStartIndex: _config.paramStartIndex || { value: 0 },
		});

		const {
			casing,
			escapeName,
			escapeParam,
			prepareTyping,
			inlineParams,
			paramStartIndex,
		} = config;

		return mergeQueries(chunks.map((chunk): QueryWithTypings => {
			if (is(chunk, StringChunk)) {
				return { sql: chunk.value.join(''), params: [] };
			}

			if (is(chunk, Name)) {
				return { sql: escapeName(chunk.value), params: [] };
			}

			if (chunk === undefined) {
				return { sql: '', params: [] };
			}

			if (Array.isArray(chunk)) {
				const result: SQLChunk[] = [new StringChunk('(')];
				for (const [i, p] of chunk.entries()) {
					result.push(p);
					if (i < chunk.length - 1) {
						result.push(new StringChunk(', '));
					}
				}

Domain

Frequently Asked Questions

What is the SQL class?
SQL is a class in the drizzle-orm codebase, defined in drizzle-orm/src/sql/sql.ts.
Where is SQL defined?
SQL is defined in drizzle-orm/src/sql/sql.ts at line 103.

Analyze Your Own Codebase

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

Try Supermodel Free