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

withSelect() — drizzle-orm Function Reference

Architecture documentation for the withSelect() function in index.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  ab82a4f8_8d8d_af60_85ce_15b456a7e2b1["withSelect()"]
  34f01008_7c45_3804_1fad_b8b59cd8a413["MyDurableObject"]
  ab82a4f8_8d8d_af60_85ce_15b456a7e2b1 -->|defined in| 34f01008_7c45_3804_1fad_b8b59cd8a413
  7c7acbfe_6328_c8df_feba_552ee62a84d9["default.fetch()"]
  7c7acbfe_6328_c8df_feba_552ee62a84d9 -->|calls| ab82a4f8_8d8d_af60_85ce_15b456a7e2b1
  b19b108f_def7_3a32_99a3_c8be51ce2fbf["beforeEach()"]
  ab82a4f8_8d8d_af60_85ce_15b456a7e2b1 -->|calls| b19b108f_def7_3a32_99a3_c8be51ce2fbf
  style ab82a4f8_8d8d_af60_85ce_15b456a7e2b1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

integration-tests/tests/sqlite/durable-objects/index.ts lines 1493–1578

	async withSelect(): Promise<void> {
		try {
			await this.beforeEach();
			this.db
				.insert(orders)
				.values([
					{ region: 'Europe', product: 'A', amount: 10, quantity: 1 },
					{ region: 'Europe', product: 'A', amount: 20, quantity: 2 },
					{ region: 'Europe', product: 'B', amount: 20, quantity: 2 },
					{ region: 'Europe', product: 'B', amount: 30, quantity: 3 },
					{ region: 'US', product: 'A', amount: 30, quantity: 3 },
					{ region: 'US', product: 'A', amount: 40, quantity: 4 },
					{ region: 'US', product: 'B', amount: 40, quantity: 4 },
					{ region: 'US', product: 'B', amount: 50, quantity: 5 },
				])
				.run();

			const regionalSales = this.db.$with('regional_sales').as(
				this.db
					.select({
						region: orders.region,
						totalSales: sql<number>`sum(${orders.amount})`.as('total_sales'),
					})
					.from(orders)
					.groupBy(orders.region),
			);

			const topRegions = this.db.$with('top_regions').as(
				this.db
					.select({
						region: regionalSales.region,
					})
					.from(regionalSales)
					.where(
						gt(
							regionalSales.totalSales,
							this.db.select({ sales: sql`sum(${regionalSales.totalSales})/10` }).from(regionalSales),
						),
					),
			);

			const result = this.db
				.with(regionalSales, topRegions)
				.select({
					region: orders.region,
					product: orders.product,
					productUnits: sql<number>`cast(sum(${orders.quantity}) as int)`,
					productSales: sql<number>`cast(sum(${orders.amount}) as int)`,
				})
				.from(orders)
				.where(inArray(orders.region, this.db.select({ region: topRegions.region }).from(topRegions)))
				.groupBy(orders.region, orders.product)
				.orderBy(orders.region, orders.product)
				.all();

			expect(result).deep.equal([
				{
					region: 'Europe',
					product: 'A',
					productUnits: 3,
					productSales: 30,
				},
				{
					region: 'Europe',
					product: 'B',
					productUnits: 5,
					productSales: 50,
				},
				{
					region: 'US',
					product: 'A',
					productUnits: 7,
					productSales: 70,
				},
				{
					region: 'US',
					product: 'B',
					productUnits: 9,
					productSales: 90,
				},
			]);

Domain

Subdomains

Calls

Called By

Frequently Asked Questions

What does withSelect() do?
withSelect() is a function in the drizzle-orm codebase, defined in integration-tests/tests/sqlite/durable-objects/index.ts.
Where is withSelect() defined?
withSelect() is defined in integration-tests/tests/sqlite/durable-objects/index.ts at line 1493.
What does withSelect() call?
withSelect() calls 1 function(s): beforeEach.
What calls withSelect()?
withSelect() is called by 1 function(s): default.fetch.

Analyze Your Own Codebase

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

Try Supermodel Free