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

81.81% Statements 18/22
75% Branches 15/20
100% Functions 6/6
81.81% Lines 18/22

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 73 74                        4x       4x       121x 2x     119x       119x 108x     11x       32x 2x 4x             30x       30x       30x       16x 2x     14x       3x                
import { is } from 'drizzle-orm/entity'
import { SelectionProxyHandler } from 'drizzle-orm/selection-proxy'
import { SQL, type SQLWrapper, View } from 'drizzle-orm/sql/sql'
import { Subquery } from 'drizzle-orm/subquery'
import { Table } from 'drizzle-orm/table'
import type { YdbTable } from '../table.js'
import { getTableColumns } from './utils.js'
import type { SelectFields } from './select.types.js'
 
export function normalizeSqlWrapperArray(
	values: SQLWrapper[] | SQLWrapper | undefined
): SQLWrapper[] | undefined {
	Iif (values === undefined) {
		return undefined
	}
 
	return Array.isArray(values) ? values : [values]
}
 
export function getTableLikeName(table: unknown): string | undefined {
	if (is(table, Subquery)) {
		return table._.alias
	}
 
	Iif (is(table, View)) {
		return table._.name
	}
 
	if (is(table, Table)) {
		return (table as any)[(Table as any).Symbol.Name]
	}
 
	return undefined
}
 
export function getSourceSelection(source: unknown): SelectFields {
	if (is(source, Subquery)) {
		return Object.fromEntries(
			Object.keys(source._.selectedFields).map((key) => [
				key,
				(source as unknown as Record<string, unknown>)[key],
			])
		)
	}
 
	Iif (is(source, View)) {
		return source._.selectedFields
	}
 
	Iif (is(source, SQL)) {
		return {}
	}
 
	return getTableColumns(source as YdbTable)
}
 
export function normalizeCountValue(value: number, methodName: string): number {
	if (!Number.isFinite(value) || value < 0) {
		throw new Error(`YDB ${methodName}() expects a non-negative finite number`)
	}
 
	return value
}
 
export function createSelectionProxy(fields: SelectFields, sqlAliasedBehavior: 'sql' | 'alias') {
	return new Proxy(
		fields,
		new SelectionProxyHandler({
			sqlAliasedBehavior,
			sqlBehavior: 'sql',
		})
	)
}