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
  d2020b13_06e0_e575_3703_4a78bd11a479["withReplicas()"]
  9b087225_652d_6b65_62a9_87af0bff69c9["db.ts"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|defined in| 9b087225_652d_6b65_62a9_87af0bff69c9
  8b6fe780_2735_4d40_58d5_5812a013740c["select()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| 8b6fe780_2735_4d40_58d5_5812a013740c
  c3eded38_37aa_4fe4_bd2c_4904f6aec4e4["selectDistinct()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| c3eded38_37aa_4fe4_bd2c_4904f6aec4e4
  629dd302_a3f5_ca4b_2314_108ea327f807["selectDistinctOn()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| 629dd302_a3f5_ca4b_2314_108ea327f807
  530dda0f_a6c2_24bc_0de1_af7772e2b801["update()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| 530dda0f_a6c2_24bc_0de1_af7772e2b801
  2dfa5289_723e_ca21_62bb_5086546fc2fc["insert()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| 2dfa5289_723e_ca21_62bb_5086546fc2fc
  e8e29963_fbb6_348d_5909_b77cb7286be4["execute()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| e8e29963_fbb6_348d_5909_b77cb7286be4
  27f51009_7960_cf02_6403_fcd1154c9b3d["transaction()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| 27f51009_7960_cf02_6403_fcd1154c9b3d
  de5b0f24_d478_a1db_8df5_4c89acbc2b82["$with()"]
  d2020b13_06e0_e575_3703_4a78bd11a479 -->|calls| de5b0f24_d478_a1db_8df5_4c89acbc2b82
  style d2020b13_06e0_e575_3703_4a78bd11a479 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/gel-core/db.ts lines 627–674

export const withReplicas = <
	HKT extends GelQueryResultHKT,
	TFullSchema extends Record<string, unknown>,
	TSchema extends TablesRelationalConfig,
	Q extends GelDatabase<
		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)]!,
): GelWithReplicas<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 _with: Q['with'] = (...args: any) => getReplica(replicas).with(...args);
	const $with: Q['$with'] = (arg: any) => getReplica(replicas).$with(arg);

	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,
		$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/gel-core/db.ts.
Where is withReplicas() defined?
withReplicas() is defined in drizzle-orm/src/gel-core/db.ts at line 627.
What does withReplicas() call?
withReplicas() calls 8 function(s): $with, execute, insert, select, selectDistinct, selectDistinctOn, transaction, update.

Analyze Your Own Codebase

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

Try Supermodel Free