with.ts — drizzle-orm Source File
Architecture documentation for with.ts, a typescript file in the drizzle-orm codebase. 5 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 6f234cee_b294_8c79_89a4_fce75dd869b5["with.ts"] 9c290d96_894a_8e33_3455_d9f3a1fa1518["db.ts"] 6f234cee_b294_8c79_89a4_fce75dd869b5 --> 9c290d96_894a_8e33_3455_d9f3a1fa1518 25b05299_f48f_bceb_01ca_1343d330a8f7["utils.ts"] 6f234cee_b294_8c79_89a4_fce75dd869b5 --> 25b05299_f48f_bceb_01ca_1343d330a8f7 cf530c77_bff8_0def_5c6c_6cf067e6c757["index.ts"] 6f234cee_b294_8c79_89a4_fce75dd869b5 --> cf530c77_bff8_0def_5c6c_6cf067e6c757 5c46beaf_8b59_d2e3_def3_8af6daf1fccd["index.ts"] 6f234cee_b294_8c79_89a4_fce75dd869b5 --> 5c46beaf_8b59_d2e3_def3_8af6daf1fccd be483a7f_d5d7_7a9b_9a13_44a4a6aafbbd["sql.ts"] 6f234cee_b294_8c79_89a4_fce75dd869b5 --> be483a7f_d5d7_7a9b_9a13_44a4a6aafbbd style 6f234cee_b294_8c79_89a4_fce75dd869b5 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 { int, serial, singlestoreTable, text } from '~/singlestore-core/index.ts';
import { gt, inArray } from '~/sql/expressions/index.ts';
import { sql } from '~/sql/sql.ts';
import { db } from './db.ts';
const orders = singlestoreTable('orders', {
id: serial('id').primaryKey(),
region: text('region').notNull(),
product: text('product').notNull(),
amount: int('amount').notNull(),
quantity: int('quantity').notNull(),
/* generated: text('generatedText').generatedAlwaysAs(sql``), */
});
{
const regionalSales = db
.$with('regional_sales')
.as(
db
.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(
db
.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 providers = singlestoreTable('providers', {
id: serial().primaryKey(),
providerName: text().notNull(),
});
const sq1 = db.$with('providers_sq', {
name: providers.providerName,
}).as(sql`select provider_name as name from providers`);
const q1 = await db.with(sq1).select().from(sq1);
Expect<Equal<typeof q1, { name: string }[]>>;
const sq2 = db.$with('providers_sq', {
nested: {
id: providers.id,
},
}).as(() => sql`select id from providers`);
const q2 = await db.with(sq2).select().from(sq2);
Expect<Equal<typeof q2, { nested: { id: number } }[]>>;
// @ts-expect-error
db.$with('providers_sq', { name: providers.providerName }).as(db.select().from(providers));
// @ts-expect-error
db.$with('providers_sq', { name: providers.providerName }).as((qb) => qb.select().from(providers));
}
Domain
Subdomains
Functions
Dependencies
- db.ts
- index.ts
- index.ts
- sql.ts
- utils.ts
Source
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, QueryBuilders subdomain.
What functions are defined in with.ts?
with.ts defines 1 function(s): sq2.
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/singlestore/with.ts (domain: DrizzleORM, subdomain: QueryBuilders, directory: drizzle-orm/type-tests/singlestore).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free