All files / third-parties/drizzle-adapter/src/ydb-core/query-builders builtins.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 13/13
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72            1x       1x       1x       3x 4x                     1x       1x       1x             2x 1x   1x                     3x 1x               2x 1x    
import { SQL, type SQLWrapper, sql as yql } from 'drizzle-orm/sql/sql'
 
// Current UTC clock readings. These are constant within a query — every call
// site receives the same value — so they're safe to use multiple times in one
// statement without diverging.
export function currentUtcDate(): SQL<Date> {
	return yql`CurrentUtcDate()`
}
 
export function currentUtcDatetime(): SQL<Date> {
	return yql`CurrentUtcDatetime()`
}
 
export function currentUtcTimestamp(): SQL<Date> {
	return yql`CurrentUtcTimestamp()`
}
 
function joinKeys(keys: readonly SQLWrapper[]): SQL {
	return yql.join(
		keys.map((key) => yql`${key}`),
		yql`, `
	)
}
 
// `Random()` / `RandomNumber()` / `RandomUuid()` are evaluated once per call
// site per query unless they receive cache keys — meaning the no-arg form
// returns the *same* value for every row, which is almost never what you want.
// Requiring at least one key (typically a column reference) forces per-row
// re-evaluation and prevents the silent foot-gun.
export function random(key: SQLWrapper, ...rest: SQLWrapper[]): SQL<number> {
	return yql`Random(${joinKeys([key, ...rest])})`
}
 
export function randomNumber(key: SQLWrapper, ...rest: SQLWrapper[]): SQL<bigint> {
	return yql`RandomNumber(${joinKeys([key, ...rest])})`
}
 
export function randomUuid(key: SQLWrapper, ...rest: SQLWrapper[]): SQL<string> {
	return yql`RandomUuid(${joinKeys([key, ...rest])})`
}
 
// `Unwrap(value, [message])` strips an `Optional<T>` to `T`, failing the query
// with `message` (or a generic error) if the value is NULL. Useful any time a
// value is statically nullable but the destination column or expression is not.
export function unwrap<T>(value: SQLWrapper, message?: string): SQL<T> {
	if (message === undefined) {
		return yql`Unwrap(${value})`
	}
	return yql`Unwrap(${value}, ${message})`
}
 
// `MAX_OF(a, b, ...)` / `MIN_OF(a, b, ...)` are N-ary scalar extrema across
// the arguments (not aggregates). Standard SQL `GREATEST`/`LEAST` aren't part
// of YQL; this is the idiomatic replacement.
export function maxOf<T>(
	first: SQLWrapper | T,
	second: SQLWrapper | T,
	...rest: Array<SQLWrapper | T>
): SQL<T> {
	let args = [first, second, ...rest].map((arg) => yql`${arg}`)
	return yql`MAX_OF(${yql.join(args, yql`, `)})`
}
 
export function minOf<T>(
	first: SQLWrapper | T,
	second: SQLWrapper | T,
	...rest: Array<SQLWrapper | T>
): SQL<T> {
	let args = [first, second, ...rest].map((arg) => yql`${arg}`)
	return yql`MIN_OF(${yql.join(args, yql`, `)})`
}