Home / Function/ withReplicas() — drizzle-orm Function Reference

withReplicas() — drizzle-orm Function Reference

Architecture documentation for the withReplicas() function in db.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81["withReplicas()"]
  99d4b8a7_e046_2f16_afff_9a25e87a12c1["db.ts"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|defined in| 99d4b8a7_e046_2f16_afff_9a25e87a12c1
  8995ca38_e96f_9e8c_c35e_cdf5ac90572c["select()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| 8995ca38_e96f_9e8c_c35e_cdf5ac90572c
  ca0e6ee4_ee25_74d1_36f1_48337059053f["selectDistinct()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| ca0e6ee4_ee25_74d1_36f1_48337059053f
  927861bc_81ae_46e9_c8b8_1f8444803d6c["selectDistinctOn()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| 927861bc_81ae_46e9_c8b8_1f8444803d6c
  c31c8ee9_821b_1478_1429_c044db2ed0f2["update()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| c31c8ee9_821b_1478_1429_c044db2ed0f2
  31164af1_b812_4e89_c9b6_11fac44d336d["insert()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| 31164af1_b812_4e89_c9b6_11fac44d336d
  0a6826ec_d3e6_da20_68f4_2ee2bebd305d["execute()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| 0a6826ec_d3e6_da20_68f4_2ee2bebd305d
  f371ea12_9edf_f7a5_91e9_963c68942a56["transaction()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| f371ea12_9edf_f7a5_91e9_963c68942a56
  9b772e40_24ac_dce2_2d47_3465eee14485["refreshMaterializedView()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| 9b772e40_24ac_dce2_2d47_3465eee14485
  41d4d9ab_8f73_38df_b085_0e6bc0f6afee["$count()"]
  99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 -->|calls| 41d4d9ab_8f73_38df_b085_0e6bc0f6afee
  style 99f2c80b_960e_3f9d_a9fe_c8c1b4967d81 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/pg-core/db.ts lines 646–695

export const withReplicas = <
	HKT extends PgQueryResultHKT,
	TFullSchema extends Record<string, unknown>,
	TSchema extends TablesRelationalConfig,
	Q extends PgDatabase<
		HKT,
		TFullSchema,
		TSchema extends Record<string, unknown> ? ExtractTablesWithRelations<TFullSchema> : TSchema
	>,
>(
	primary: Q,
	replicas: [Q, ...Q[]],
	getReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,
): PgWithReplicas<Q> => {
	const select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);
	const selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);
	const selectDistinctOn: Q['selectDistinctOn'] = (...args: [any]) => getReplica(replicas).selectDistinctOn(...args);
	const $count: Q['$count'] = (...args: [any]) => getReplica(replicas).$count(...args);
	const _with: Q['with'] = (...args: any) => getReplica(replicas).with(...args);
	const $with: Q['$with'] = (arg: any) => getReplica(replicas).$with(arg) as any;

	const update: Q['update'] = (...args: [any]) => primary.update(...args);
	const insert: Q['insert'] = (...args: [any]) => primary.insert(...args);
	const $delete: Q['delete'] = (...args: [any]) => primary.delete(...args);
	const execute: Q['execute'] = (...args: [any]) => primary.execute(...args);
	const transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);
	const refreshMaterializedView: Q['refreshMaterializedView'] = (...args: [any]) =>
		primary.refreshMaterializedView(...args);

	return {
		...primary,
		update,
		insert,
		delete: $delete,
		execute,
		transaction,
		refreshMaterializedView,
		$primary: primary,
		$replicas: replicas,
		select,
		selectDistinct,
		selectDistinctOn,
		$count,
		$with,
		with: _with,
		get query() {
			return getReplica(replicas).query;
		},
	};
};

Domain

Subdomains

Frequently Asked Questions

What does withReplicas() do?
withReplicas() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/pg-core/db.ts.
Where is withReplicas() defined?
withReplicas() is defined in drizzle-orm/src/pg-core/db.ts at line 646.
What does withReplicas() call?
withReplicas() calls 9 function(s): $count, execute, insert, refreshMaterializedView, select, selectDistinct, selectDistinctOn, transaction, and 1 more.

Analyze Your Own Codebase

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

Try Supermodel Free