Home / Class/ PgDatabase Class — drizzle-orm Architecture

PgDatabase Class — drizzle-orm Architecture

Architecture documentation for the PgDatabase class in db.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  7b170b6b_8109_4580_dc12_4da0d59d8d8d["PgDatabase"]
  99d4b8a7_e046_2f16_afff_9a25e87a12c1["db.ts"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|defined in| 99d4b8a7_e046_2f16_afff_9a25e87a12c1
  56154db1_5e3f_c59a_0fe1_49dee20b0f87["constructor()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 56154db1_5e3f_c59a_0fe1_49dee20b0f87
  41d4d9ab_8f73_38df_b085_0e6bc0f6afee["$count()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 41d4d9ab_8f73_38df_b085_0e6bc0f6afee
  9373d797_a688_263a_ed31_d047e2450a06["with()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 9373d797_a688_263a_ed31_d047e2450a06
  8995ca38_e96f_9e8c_c35e_cdf5ac90572c["select()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 8995ca38_e96f_9e8c_c35e_cdf5ac90572c
  ca0e6ee4_ee25_74d1_36f1_48337059053f["selectDistinct()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| ca0e6ee4_ee25_74d1_36f1_48337059053f
  927861bc_81ae_46e9_c8b8_1f8444803d6c["selectDistinctOn()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 927861bc_81ae_46e9_c8b8_1f8444803d6c
  c31c8ee9_821b_1478_1429_c044db2ed0f2["update()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| c31c8ee9_821b_1478_1429_c044db2ed0f2
  31164af1_b812_4e89_c9b6_11fac44d336d["insert()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 31164af1_b812_4e89_c9b6_11fac44d336d
  b2c03690_83dd_a35b_9fed_84a90844602d["delete()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| b2c03690_83dd_a35b_9fed_84a90844602d
  9b772e40_24ac_dce2_2d47_3465eee14485["refreshMaterializedView()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 9b772e40_24ac_dce2_2d47_3465eee14485
  0a6826ec_d3e6_da20_68f4_2ee2bebd305d["execute()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| 0a6826ec_d3e6_da20_68f4_2ee2bebd305d
  f371ea12_9edf_f7a5_91e9_963c68942a56["transaction()"]
  7b170b6b_8109_4580_dc12_4da0d59d8d8d -->|method| f371ea12_9edf_f7a5_91e9_963c68942a56

Relationship Graph

Source Code

drizzle-orm/src/pg-core/db.ts lines 36–642

export class PgDatabase<
	TQueryResult extends PgQueryResultHKT,
	TFullSchema extends Record<string, unknown> = Record<string, never>,
	TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>,
> {
	static readonly [entityKind]: string = 'PgDatabase';

	declare readonly _: {
		readonly schema: TSchema | undefined;
		readonly fullSchema: TFullSchema;
		readonly tableNamesMap: Record<string, string>;
		readonly session: PgSession<TQueryResult, TFullSchema, TSchema>;
	};

	query: TFullSchema extends Record<string, never>
		? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>
		: {
			[K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;
		};

	constructor(
		/** @internal */
		readonly dialect: PgDialect,
		/** @internal */
		readonly session: PgSession<any, any, any>,
		schema: RelationalSchemaConfig<TSchema> | undefined,
	) {
		this._ = schema
			? {
				schema: schema.schema,
				fullSchema: schema.fullSchema as TFullSchema,
				tableNamesMap: schema.tableNamesMap,
				session,
			}
			: {
				schema: undefined,
				fullSchema: {} as TFullSchema,
				tableNamesMap: {},
				session,
			};
		this.query = {} as typeof this['query'];
		if (this._.schema) {
			for (const [tableName, columns] of Object.entries(this._.schema)) {
				(this.query as PgDatabase<TQueryResult, Record<string, any>>['query'])[tableName] = new RelationalQueryBuilder(
					schema!.fullSchema,
					this._.schema,
					this._.tableNamesMap,
					schema!.fullSchema[tableName] as PgTable,
					columns,
					dialect,
					session,
				);
			}
		}
		this.$cache = { invalidate: async (_params: any) => {} };
	}

	/**
	 * Creates a subquery that defines a temporary named result set as a CTE.
	 *
	 * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
	 *
	 * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
	 *
	 * @param alias The alias for the subquery.
	 *
	 * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
	 *
	 * @example
	 *
	 * ```ts
	 * // Create a subquery with alias 'sq' and use it in the select query
	 * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
	 *
	 * const result = await db.with(sq).select().from(sq);
	 * ```
	 *
	 * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
	 *
	 * ```ts
	 * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query

Domain

Frequently Asked Questions

What is the PgDatabase class?
PgDatabase is a class in the drizzle-orm codebase, defined in drizzle-orm/src/pg-core/db.ts.
Where is PgDatabase defined?
PgDatabase is defined in drizzle-orm/src/pg-core/db.ts at line 36.

Analyze Your Own Codebase

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

Try Supermodel Free