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

aggregate.ts — drizzle-orm Source File

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

File typescript DrizzleORM SQLDialects 4 imports 8 functions

Entity Profile

Dependency Diagram

graph LR
  ddea600c_c886_0da9_1354_46365d17b567["aggregate.ts"]
  99347ab2_b1a1_faf4_e37c_7643e4b2eb8a["sql.ts"]
  ddea600c_c886_0da9_1354_46365d17b567 --> 99347ab2_b1a1_faf4_e37c_7643e4b2eb8a
  eb432528_5e12_9f5b_6ed4_4aeab03024d8["sql"]
  ddea600c_c886_0da9_1354_46365d17b567 --> eb432528_5e12_9f5b_6ed4_4aeab03024d8
  05f0a280_d0c9_693a_a4bf_83cc671012d2["column.ts"]
  ddea600c_c886_0da9_1354_46365d17b567 --> 05f0a280_d0c9_693a_a4bf_83cc671012d2
  27705a9d_afe9_57dd_8c97_e52d8a67d426["entity.ts"]
  ddea600c_c886_0da9_1354_46365d17b567 --> 27705a9d_afe9_57dd_8c97_e52d8a67d426
  style ddea600c_c886_0da9_1354_46365d17b567 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { type AnyColumn, Column } from '~/column.ts';
import { is } from '~/entity.ts';
import { type SQL, sql, type SQLWrapper } from '../sql.ts';

/**
 * Returns the number of values in `expression`.
 *
 * ## Examples
 *
 * ```ts
 * // Number employees with null values
 * db.select({ value: count() }).from(employees)
 * // Number of employees where `name` is not null
 * db.select({ value: count(employees.name) }).from(employees)
 * ```
 *
 * @see countDistinct to get the number of non-duplicate values in `expression`
 */
export function count(expression?: SQLWrapper): SQL<number> {
	return sql`count(${expression || sql.raw('*')})`.mapWith(Number);
}

/**
 * Returns the number of non-duplicate values in `expression`.
 *
 * ## Examples
 *
 * ```ts
 * // Number of employees where `name` is distinct
 * db.select({ value: countDistinct(employees.name) }).from(employees)
 * ```
 *
 * @see count to get the number of values in `expression`, including duplicates
 */
export function countDistinct(expression: SQLWrapper): SQL<number> {
	return sql`count(distinct ${expression})`.mapWith(Number);
}

/**
 * Returns the average (arithmetic mean) of all non-null values in `expression`.
 *
 * ## Examples
 *
 * ```ts
 * // Average salary of an employee
 * db.select({ value: avg(employees.salary) }).from(employees)
 * ```
 *
 * @see avgDistinct to get the average of all non-null and non-duplicate values in `expression`
 */
export function avg(expression: SQLWrapper): SQL<string | null> {
	return sql`avg(${expression})`.mapWith(String);
}

/**
 * Returns the average (arithmetic mean) of all non-null and non-duplicate values in `expression`.
 *
 * ## Examples
 *
 * ```ts
// ... (70 more lines)

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does aggregate.ts do?
aggregate.ts is a source file in the drizzle-orm codebase, written in typescript. It belongs to the DrizzleORM domain, SQLDialects subdomain.
What functions are defined in aggregate.ts?
aggregate.ts defines 8 function(s): avg, avgDistinct, count, countDistinct, max, min, sum, sumDistinct.
What does aggregate.ts depend on?
aggregate.ts imports 4 module(s): column.ts, entity.ts, sql, sql.ts.
Where is aggregate.ts in the architecture?
aggregate.ts is located at drizzle-orm/src/sql/functions/aggregate.ts (domain: DrizzleORM, subdomain: SQLDialects, directory: drizzle-orm/src/sql/functions).

Analyze Your Own Codebase

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

Try Supermodel Free