Home / Class/ NodePgSession Class — drizzle-orm Architecture

NodePgSession Class — drizzle-orm Architecture

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

Entity Profile

Dependency Diagram

graph TD
  c379869d_eae7_28c1_b5ff_21c36a0fef11["NodePgSession"]
  0507ccca_3cb1_384d_6315_fc19900c69b1["session.ts"]
  c379869d_eae7_28c1_b5ff_21c36a0fef11 -->|defined in| 0507ccca_3cb1_384d_6315_fc19900c69b1
  892458e5_c727_756d_48cf_9ce867f8280a["constructor()"]
  c379869d_eae7_28c1_b5ff_21c36a0fef11 -->|method| 892458e5_c727_756d_48cf_9ce867f8280a
  7d451f5d_508f_7e96_c779_14628245f496["prepareQuery()"]
  c379869d_eae7_28c1_b5ff_21c36a0fef11 -->|method| 7d451f5d_508f_7e96_c779_14628245f496
  2aecbcfe_6a09_c4c7_c8af_f142cff742c0["transaction()"]
  c379869d_eae7_28c1_b5ff_21c36a0fef11 -->|method| 2aecbcfe_6a09_c4c7_c8af_f142cff742c0
  808c0ea0_29b0_a4d9_5543_66392d0a0e36["count()"]
  c379869d_eae7_28c1_b5ff_21c36a0fef11 -->|method| 808c0ea0_29b0_a4d9_5543_66392d0a0e36

Relationship Graph

Source Code

drizzle-orm/src/node-postgres/session.ts lines 201–276

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

	private logger: Logger;
	private cache: Cache;

	constructor(
		private client: NodePgClient,
		dialect: PgDialect,
		private schema: RelationalSchemaConfig<TSchema> | undefined,
		private options: NodePgSessionOptions = {},
	) {
		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 NodePgPreparedQuery(
			this.client,
			query.sql,
			query.params,
			this.logger,
			this.cache,
			queryMetadata,
			cacheConfig,
			fields,
			name,
			isResponseInArrayMode,
			customResultMapper,
		);
	}

	override async transaction<T>(
		transaction: (tx: NodePgTransaction<TFullSchema, TSchema>) => Promise<T>,
		config?: PgTransactionConfig | undefined,
	): Promise<T> {
		const isPool = this.client instanceof Pool || Object.getPrototypeOf(this.client).constructor.name.includes('Pool'); // eslint-disable-line no-instanceof/no-instanceof
		const session = isPool
			? new NodePgSession(await (<pg.Pool> this.client).connect(), this.dialect, this.schema, this.options)
			: this;
		const tx = new NodePgTransaction<TFullSchema, TSchema>(this.dialect, session, this.schema);
		await tx.execute(sql`begin${config ? sql` ${tx.getTransactionConfigSQL(config)}` : undefined}`);
		try {
			const result = await transaction(tx);
			await tx.execute(sql`commit`);
			return result;
		} catch (error) {
			await tx.execute(sql`rollback`);
			throw error;
		} finally {
			if (isPool) (session.client as PoolClient).release();
		}
	}

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

Domain

Frequently Asked Questions

What is the NodePgSession class?
NodePgSession is a class in the drizzle-orm codebase, defined in drizzle-orm/src/node-postgres/session.ts.
Where is NodePgSession defined?
NodePgSession is defined in drizzle-orm/src/node-postgres/session.ts at line 201.

Analyze Your Own Codebase

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

Try Supermodel Free