Home / File/ with.ts — drizzle-orm Source File

with.ts — drizzle-orm Source File

Architecture documentation for with.ts, a typescript file in the drizzle-orm codebase. 5 imports, 0 dependents.

File typescript DrizzleORM RelationalQuery 5 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  75f2c353_40af_2ce9_31a7_9e2237405a87["with.ts"]
  02ee7a70_490f_3b80_fa60_d59aaa62cd1d["db.ts"]
  75f2c353_40af_2ce9_31a7_9e2237405a87 --> 02ee7a70_490f_3b80_fa60_d59aaa62cd1d
  25b05299_f48f_bceb_01ca_1343d330a8f7["utils.ts"]
  75f2c353_40af_2ce9_31a7_9e2237405a87 --> 25b05299_f48f_bceb_01ca_1343d330a8f7
  076448f5_e30d_7700_bbe9_f5d1e76b579a["index.ts"]
  75f2c353_40af_2ce9_31a7_9e2237405a87 --> 076448f5_e30d_7700_bbe9_f5d1e76b579a
  5c46beaf_8b59_d2e3_def3_8af6daf1fccd["index.ts"]
  75f2c353_40af_2ce9_31a7_9e2237405a87 --> 5c46beaf_8b59_d2e3_def3_8af6daf1fccd
  be483a7f_d5d7_7a9b_9a13_44a4a6aafbbd["sql.ts"]
  75f2c353_40af_2ce9_31a7_9e2237405a87 --> be483a7f_d5d7_7a9b_9a13_44a4a6aafbbd
  style 75f2c353_40af_2ce9_31a7_9e2237405a87 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { Equal } from 'type-tests/utils.ts';
import { Expect } from 'type-tests/utils.ts';
import { gelTable, integer, text } from '~/gel-core/index.ts';
import { gt, inArray, like } from '~/sql/expressions/index.ts';
import { sql } from '~/sql/sql.ts';
import { db } from './db.ts';

const orders = gelTable('orders', {
	id: integer('id').primaryKey(),
	region: text('region').notNull(),
	product: text('product').notNull(),
	amount: integer('amount').notNull(),
	quantity: integer('quantity').notNull(),
	generated: text('generatedText').generatedAlwaysAs(sql``),
});

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

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

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

	Expect<
		Equal<{
			region: string;
			product: string;
			productUnits: number;
			productSales: number;
		}[], typeof result>
	>;

	const allOrdersWith = db.$with('all_orders_with').as(db.select().from(orders));
	const allFromWith = await db.with(allOrdersWith).select().from(allOrdersWith);

	Expect<
		Equal<{
			id: number;
			region: string;
			product: string;
			amount: number;
			quantity: number;
			generated: string | null;
		}[], typeof allFromWith>
	>;

	const regionalSalesWith = db.$with('regional_sales_with').as(db.select().from(regionalSales));
	db.with(regionalSalesWith).select().from(regionalSalesWith).where(like(regionalSalesWith.totalSales, 'abc'));
}

Domain

Subdomains

Dependencies

  • db.ts
  • index.ts
  • index.ts
  • sql.ts
  • utils.ts

Frequently Asked Questions

What does with.ts do?
with.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the DrizzleORM domain, RelationalQuery subdomain.
What functions are defined in with.ts?
with.ts defines 2 function(s): regionalSales, topRegions.
What does with.ts depend on?
with.ts imports 5 module(s): db.ts, index.ts, index.ts, sql.ts, utils.ts.
Where is with.ts in the architecture?
with.ts is located at drizzle-orm/type-tests/geldb/with.ts (domain: DrizzleORM, subdomain: RelationalQuery, directory: drizzle-orm/type-tests/geldb).

Analyze Your Own Codebase

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

Try Supermodel Free