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
  22556be1_3c4d_bb6a_b1d6_687082441712["withReplicas()"]
  e9188303_2815_1371_46b3_eb6061e52537["db.ts"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|defined in| e9188303_2815_1371_46b3_eb6061e52537
  b0a10589_f90f_2f86_cfda_0887e40c5f4f["select()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| b0a10589_f90f_2f86_cfda_0887e40c5f4f
  4ad96ef6_a777_f318_7f1e_4f43a66fcf91["selectDistinct()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| 4ad96ef6_a777_f318_7f1e_4f43a66fcf91
  fc16af3e_3100_a7f6_55e0_a4f8b741a096["update()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| fc16af3e_3100_a7f6_55e0_a4f8b741a096
  ef603885_d0e2_bb44_930e_01cdc05b63df["insert()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| ef603885_d0e2_bb44_930e_01cdc05b63df
  7aa1e133_803c_8614_dde4_b88e3702b5a8["run()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| 7aa1e133_803c_8614_dde4_b88e3702b5a8
  e5881113_8325_4fad_1edf_507c769fa31f["all()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| e5881113_8325_4fad_1edf_507c769fa31f
  4127d8f5_0011_a277_4962_2970d4765de0["get()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| 4127d8f5_0011_a277_4962_2970d4765de0
  00f24df6_58eb_5e6d_d5b6_f743143ee711["values()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| 00f24df6_58eb_5e6d_d5b6_f743143ee711
  00de2587_dd19_ed53_b8aa_23f027479330["transaction()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| 00de2587_dd19_ed53_b8aa_23f027479330
  fc54b5d4_2bd3_18fc_c29b_5f41dbf5decc["$count()"]
  22556be1_3c4d_bb6a_b1d6_687082441712 -->|calls| fc54b5d4_2bd3_18fc_c29b_5f41dbf5decc
  style 22556be1_3c4d_bb6a_b1d6_687082441712 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/sqlite-core/db.ts lines 599–649

export const withReplicas = <
	TResultKind extends 'sync' | 'async',
	TRunResult,
	TFullSchema extends Record<string, unknown>,
	TSchema extends TablesRelationalConfig,
	Q extends BaseSQLiteDatabase<
		TResultKind,
		TRunResult,
		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)]!,
): SQLiteWithReplicas<Q> => {
	const select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);
	const selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);
	const $count: Q['$count'] = (...args: [any]) => getReplica(replicas).$count(...args);
	const $with: Q['with'] = (...args: []) => getReplica(replicas).with(...args);

	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 run: Q['run'] = (...args: [any]) => primary.run(...args);
	const all: Q['all'] = (...args: [any]) => primary.all(...args);
	const get: Q['get'] = (...args: [any]) => primary.get(...args);
	const values: Q['values'] = (...args: [any]) => primary.values(...args);
	const transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);

	return {
		...primary,
		update,
		insert,
		delete: $delete,
		run,
		all,
		get,
		values,
		transaction,
		$primary: primary,
		$replicas: replicas,
		select,
		selectDistinct,
		$count,
		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/sqlite-core/db.ts.
Where is withReplicas() defined?
withReplicas() is defined in drizzle-orm/src/sqlite-core/db.ts at line 599.
What does withReplicas() call?
withReplicas() calls 10 function(s): $count, all, get, insert, run, select, selectDistinct, transaction, and 2 more.

Analyze Your Own Codebase

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

Try Supermodel Free