Home / Class/ PreparedQuery Class — drizzle-orm Architecture

PreparedQuery Class — drizzle-orm Architecture

Architecture documentation for the PreparedQuery class in session.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  e722a0dc_424f_e268_b60c_f7637b7e64fb["PreparedQuery"]
  058d4164_8a6e_e307_11eb_23f5c52611d5["session.ts"]
  e722a0dc_424f_e268_b60c_f7637b7e64fb -->|defined in| 058d4164_8a6e_e307_11eb_23f5c52611d5
  afefe345_f889_6a3e_35a3_f0fc5fb3f0d0["constructor()"]
  e722a0dc_424f_e268_b60c_f7637b7e64fb -->|method| afefe345_f889_6a3e_35a3_f0fc5fb3f0d0
  a8b9aa76_08ef_960b_7cba_6f7e5c867df3["run()"]
  e722a0dc_424f_e268_b60c_f7637b7e64fb -->|method| a8b9aa76_08ef_960b_7cba_6f7e5c867df3
  aeaeb9a8_8f44_2e12_6c95_c00b1bd0a997["all()"]
  e722a0dc_424f_e268_b60c_f7637b7e64fb -->|method| aeaeb9a8_8f44_2e12_6c95_c00b1bd0a997
  b77391e5_131a_4f3c_614d_65fffc9bed0b["get()"]
  e722a0dc_424f_e268_b60c_f7637b7e64fb -->|method| b77391e5_131a_4f3c_614d_65fffc9bed0b
  98d16be4_f92a_9508_7d74_e7b5e2a8d9ce["values()"]
  e722a0dc_424f_e268_b60c_f7637b7e64fb -->|method| 98d16be4_f92a_9508_7d74_e7b5e2a8d9ce
  860dd8e6_999d_f422_3a73_8327a5733ce8["isResponseInArrayMode()"]
  e722a0dc_424f_e268_b60c_f7637b7e64fb -->|method| 860dd8e6_999d_f422_3a73_8327a5733ce8

Relationship Graph

Source Code

drizzle-orm/src/sql-js/session.ts lines 89–195

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
	{ type: 'sync'; run: void; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
	static override readonly [entityKind]: string = 'SQLJsPreparedQuery';

	constructor(
		private client: Database,
		query: Query,
		private logger: Logger,
		private fields: SelectedFieldsOrdered | undefined,
		executeMethod: SQLiteExecuteMethod,
		private _isResponseInArrayMode: boolean,
		private customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown,
	) {
		super('sync', executeMethod, query);
	}

	run(placeholderValues?: Record<string, unknown>): void {
		const stmt = this.client.prepare(this.query.sql);

		const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
		this.logger.logQuery(this.query.sql, params);
		const result = stmt.run(params as BindParams);

		stmt.free();

		return result;
	}

	all(placeholderValues?: Record<string, unknown>): T['all'] {
		const stmt = this.client.prepare(this.query.sql);

		const { fields, joinsNotNullableMap, logger, query, customResultMapper } = this;
		if (!fields && !customResultMapper) {
			const params = fillPlaceholders(query.params, placeholderValues ?? {});
			logger.logQuery(query.sql, params);
			stmt.bind(params as BindParams);
			const rows: unknown[] = [];
			while (stmt.step()) {
				rows.push(stmt.getAsObject());
			}

			stmt.free();

			return rows;
		}

		const rows = this.values(placeholderValues) as unknown[][];

		if (customResultMapper) {
			return customResultMapper(rows, normalizeFieldValue) as T['all'];
		}

		return rows.map((row) => mapResultRow(fields!, row.map((v) => normalizeFieldValue(v)), joinsNotNullableMap));
	}

	get(placeholderValues?: Record<string, unknown>): T['get'] {
		const stmt = this.client.prepare(this.query.sql);

		const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
		this.logger.logQuery(this.query.sql, params);

		const { fields, joinsNotNullableMap, customResultMapper } = this;
		if (!fields && !customResultMapper) {
			const result = stmt.getAsObject(params as BindParams);

			stmt.free();

			return result;
		}

		const row = stmt.get(params as BindParams);

		stmt.free();

		if (!row || (row.length === 0 && fields!.length > 0)) {
			return undefined;
		}

		if (customResultMapper) {
			return customResultMapper([row], normalizeFieldValue) as T['get'];

Domain

Frequently Asked Questions

What is the PreparedQuery class?
PreparedQuery is a class in the drizzle-orm codebase, defined in drizzle-orm/src/sql-js/session.ts.
Where is PreparedQuery defined?
PreparedQuery is defined in drizzle-orm/src/sql-js/session.ts at line 89.

Analyze Your Own Codebase

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

Try Supermodel Free