Home / Class/ NeonSession Class — drizzle-orm Architecture

NeonSession Class — drizzle-orm Architecture

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

Entity Profile

Dependency Diagram

graph TD
  bef17a4b_1799_4883_f38d_851173aa1ff4["NeonSession"]
  bd67ed5b_8d2f_60fd_725e_a8b2ea44664c["session.ts"]
  bef17a4b_1799_4883_f38d_851173aa1ff4 -->|defined in| bd67ed5b_8d2f_60fd_725e_a8b2ea44664c
  54a0d1f6_ff3e_da78_252a_101463b79e2d["constructor()"]
  bef17a4b_1799_4883_f38d_851173aa1ff4 -->|method| 54a0d1f6_ff3e_da78_252a_101463b79e2d
  9d3081d8_b5a6_3044_e2bf_ac49e7ab3506["prepareQuery()"]
  bef17a4b_1799_4883_f38d_851173aa1ff4 -->|method| 9d3081d8_b5a6_3044_e2bf_ac49e7ab3506
  88f3cd0b_32e9_d2ff_604d_a5259bc10c9e["query()"]
  bef17a4b_1799_4883_f38d_851173aa1ff4 -->|method| 88f3cd0b_32e9_d2ff_604d_a5259bc10c9e
  22cbffac_96fd_c141_cb07_5bdc63518bad["queryObjects()"]
  bef17a4b_1799_4883_f38d_851173aa1ff4 -->|method| 22cbffac_96fd_c141_cb07_5bdc63518bad
  cbe1851e_cb24_472c_c668_c6b84ba44ab8["count()"]
  bef17a4b_1799_4883_f38d_851173aa1ff4 -->|method| cbe1851e_cb24_472c_c668_c6b84ba44ab8
  0df49818_fd3d_7eaf_026f_4bb7ca00ab17["transaction()"]
  bef17a4b_1799_4883_f38d_851173aa1ff4 -->|method| 0df49818_fd3d_7eaf_026f_4bb7ca00ab17

Relationship Graph

Source Code

drizzle-orm/src/neon-serverless/session.ts lines 188–282

export class NeonSession<
	TFullSchema extends Record<string, unknown>,
	TSchema extends TablesRelationalConfig,
> extends PgSession<NeonQueryResultHKT, TFullSchema, TSchema> {
	static override readonly [entityKind]: string = 'NeonSession';

	private logger: Logger;
	private cache: Cache;

	constructor(
		private client: NeonClient,
		dialect: PgDialect,
		private schema: RelationalSchemaConfig<TSchema> | undefined,
		private options: NeonSessionOptions = {},
	) {
		super(dialect);
		this.logger = options.logger ?? new NoopLogger();
		this.cache = options.cache ?? new NoopCache();
	}

	prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
		query: Query,
		fields: SelectedFieldsOrdered | undefined,
		name: string | undefined,
		isResponseInArrayMode: boolean,
		customResultMapper?: (rows: unknown[][]) => T['execute'],
		queryMetadata?: {
			type: 'select' | 'update' | 'delete' | 'insert';
			tables: string[];
		},
		cacheConfig?: WithCacheConfig,
	): PgPreparedQuery<T> {
		return new NeonPreparedQuery(
			this.client,
			query.sql,
			query.params,
			this.logger,
			this.cache,
			queryMetadata,
			cacheConfig,
			fields,
			name,
			isResponseInArrayMode,
			customResultMapper,
		);
	}

	async query(query: string, params: unknown[]): Promise<QueryResult> {
		this.logger.logQuery(query, params);
		const result = await this.client.query({
			rowMode: 'array',
			text: query,
			values: params,
		});
		return result;
	}

	async queryObjects<T extends QueryResultRow>(
		query: string,
		params: unknown[],
	): Promise<QueryResult<T>> {
		return this.client.query<T>(query, params);
	}

	override async count(sql: SQL): Promise<number> {
		const res = await this.execute<{ rows: [{ count: string }] }>(sql);

		return Number(
			res['rows'][0]['count'],
		);
	}

	override async transaction<T>(
		transaction: (tx: NeonTransaction<TFullSchema, TSchema>) => Promise<T>,
		config: PgTransactionConfig = {},
	): Promise<T> {
		const session = this.client instanceof Pool // eslint-disable-line no-instanceof/no-instanceof
			? new NeonSession(await this.client.connect(), this.dialect, this.schema, this.options)
			: this;
		const tx = new NeonTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
		await tx.execute(sql`begin ${tx.getTransactionConfigSQL(config)}`);

Domain

Frequently Asked Questions

What is the NeonSession class?
NeonSession is a class in the drizzle-orm codebase, defined in drizzle-orm/src/neon-serverless/session.ts.
Where is NeonSession defined?
NeonSession is defined in drizzle-orm/src/neon-serverless/session.ts at line 188.

Analyze Your Own Codebase

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

Try Supermodel Free