Home / Class/ D1PreparedQuery Class — drizzle-orm Architecture

D1PreparedQuery Class — drizzle-orm Architecture

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

Entity Profile

Dependency Diagram

graph TD
  40aea31b_a504_408a_306d_c180ac0ece23["D1PreparedQuery"]
  74ae4fc7_01d3_a587_15aa_476edeee51c0["session.ts"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|defined in| 74ae4fc7_01d3_a587_15aa_476edeee51c0
  8e4136bd_758a_4f1a_5c8c_ce0dbd6c559b["constructor()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| 8e4136bd_758a_4f1a_5c8c_ce0dbd6c559b
  1d5afe2c_0cde_87b6_9e0e_5be634ac18de["run()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| 1d5afe2c_0cde_87b6_9e0e_5be634ac18de
  87d7d387_bee1_e6f0_646e_f45078893a2c["all()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| 87d7d387_bee1_e6f0_646e_f45078893a2c
  577fd81a_46a8_df11_08fb_ad758aa8a167["mapAllResult()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| 577fd81a_46a8_df11_08fb_ad758aa8a167
  e176c6d5_5c23_b5d8_4689_6ed9e0a9bf9f["get()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| e176c6d5_5c23_b5d8_4689_6ed9e0a9bf9f
  fc299b5b_b02d_ff84_c29f_41dab0f8c97f["mapGetResult()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| fc299b5b_b02d_ff84_c29f_41dab0f8c97f
  393e2983_eb79_4fb0_cae1_ff7dd5ad9280["values()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| 393e2983_eb79_4fb0_cae1_ff7dd5ad9280
  a0fbb787_7086_2250_6396_ee1e84d232f1["isResponseInArrayMode()"]
  40aea31b_a504_408a_306d_c180ac0ece23 -->|method| a0fbb787_7086_2250_6396_ee1e84d232f1

Relationship Graph

Source Code

drizzle-orm/src/d1/session.ts lines 164–289

export class D1PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends SQLitePreparedQuery<
	{ type: 'async'; run: D1Response; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
	static override readonly [entityKind]: string = 'D1PreparedQuery';

	/** @internal */
	customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown;

	/** @internal */
	fields?: SelectedFieldsOrdered;

	/** @internal */
	stmt: D1PreparedStatement;

	constructor(
		stmt: D1PreparedStatement,
		query: Query,
		private logger: Logger,
		cache: Cache,
		queryMetadata: {
			type: 'select' | 'update' | 'delete' | 'insert';
			tables: string[];
		} | undefined,
		cacheConfig: WithCacheConfig | undefined,
		fields: SelectedFieldsOrdered | undefined,
		executeMethod: SQLiteExecuteMethod,
		private _isResponseInArrayMode: boolean,
		customResultMapper?: (rows: unknown[][]) => unknown,
	) {
		super('async', executeMethod, query, cache, queryMetadata, cacheConfig);
		this.customResultMapper = customResultMapper;
		this.fields = fields;
		this.stmt = stmt;
	}

	async run(placeholderValues?: Record<string, unknown>): Promise<D1Response> {
		const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
		this.logger.logQuery(this.query.sql, params);
		return await this.queryWithCache(this.query.sql, params, async () => {
			return this.stmt.bind(...params).run();
		});
	}

	async all(placeholderValues?: Record<string, unknown>): Promise<T['all']> {
		const { fields, query, logger, stmt, customResultMapper } = this;
		if (!fields && !customResultMapper) {
			const params = fillPlaceholders(query.params, placeholderValues ?? {});
			logger.logQuery(query.sql, params);
			return await this.queryWithCache(query.sql, params, async () => {
				return stmt.bind(...params).all().then(({ results }) => this.mapAllResult(results!));
			});
		}

		const rows = await this.values(placeholderValues);

		return this.mapAllResult(rows);
	}

	override mapAllResult(rows: unknown, isFromBatch?: boolean): unknown {
		if (isFromBatch) {
			rows = d1ToRawMapping((rows as D1Result).results);
		}

		if (!this.fields && !this.customResultMapper) {
			return rows;
		}

		if (this.customResultMapper) {
			return this.customResultMapper(rows as unknown[][]);
		}

		return (rows as unknown[][]).map((row) => mapResultRow(this.fields!, row, this.joinsNotNullableMap));
	}

	async get(placeholderValues?: Record<string, unknown>): Promise<T['get']> {
		const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;
		if (!fields && !customResultMapper) {
			const params = fillPlaceholders(query.params, placeholderValues ?? {});
			logger.logQuery(query.sql, params);
			return await this.queryWithCache(query.sql, params, async () => {
				return stmt.bind(...params).all().then(({ results }) => results![0]);

Domain

Frequently Asked Questions

What is the D1PreparedQuery class?
D1PreparedQuery is a class in the drizzle-orm codebase, defined in drizzle-orm/src/d1/session.ts.
Where is D1PreparedQuery defined?
D1PreparedQuery is defined in drizzle-orm/src/d1/session.ts at line 164.

Analyze Your Own Codebase

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

Try Supermodel Free