All files / third-parties/drizzle-adapter/src/ydb-core result-mapping.ts

73.68% Statements 42/57
64.61% Branches 42/65
100% Functions 7/7
73.68% Lines 42/57

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                                        8x   8x 8x 8x                                                 107x 104x     3x 8x               199x 468x   468x 460x 8x     8x     468x                 59x 59x   59x     171x 163x 8x 8x             171x 171x 190x 19x 6x   19x   171x 171x 171x   171x 19x 19x 6x 13x                   171x     59x 3x 6x 2x         59x    
import { Column } from 'drizzle-orm/column'
import { is } from 'drizzle-orm/entity'
import { SQL } from 'drizzle-orm/sql/sql'
import { Subquery } from 'drizzle-orm/subquery'
import { Table, getTableName } from 'drizzle-orm/table'
import type { YdbColumn } from './columns/common.js'
 
export type YdbSelectedField = {
	path: string[]
	field: unknown
}
 
export type YdbSelectedFieldsOrdered = YdbSelectedField[]
 
function getRowObjectValue(
	row: Record<string, unknown>,
	path: string[],
	field: unknown,
	columnIndex: number
): unknown {
	let pathKey = path[path.length - 1]
 
	if (is(field, Column)) {
		Eif (field.name in row) {
			return row[field.name]
		}
 
		if (pathKey && pathKey in row) {
			return row[pathKey]
		}
	} else Eif (is(field, SQL.Aliased)) {
		if (field.fieldAlias in row) {
			return row[field.fieldAlias]
		}
 
		if (pathKey && pathKey in row) {
			return row[pathKey]
		}
	} else if (pathKey && pathKey in row) {
		return row[pathKey]
	}
 
	return Object.values(row)[columnIndex]
}
 
export function rowToArray(
	columns: YdbSelectedFieldsOrdered,
	row: unknown[] | Record<string, unknown>
): unknown[] {
	if (Array.isArray(row)) {
		return row
	}
 
	return columns.map(({ path, field }, columnIndex) =>
		getRowObjectValue(row, path, field, columnIndex)
	)
}
 
export function orderSelectedFields(
	fields: Record<string, unknown>,
	pathPrefix?: string[]
): YdbSelectedFieldsOrdered {
	return Object.entries(fields).reduce<YdbSelectedFieldsOrdered>((result, [name, field]) => {
		let path = pathPrefix ? [...pathPrefix, name] : [name]
 
		if (is(field, Column) || is(field, SQL) || is(field, SQL.Aliased) || is(field, Subquery)) {
			result.push({ path, field })
		} else Iif (is(field, Table)) {
			result.push(...orderSelectedFields((field as any)[(Table as any).Symbol.Columns], path))
		} else {
			result.push(...orderSelectedFields(field as Record<string, unknown>, path))
		}
 
		return result
	}, [])
}
 
export function mapResultRow<TResult>(
	columns: YdbSelectedFieldsOrdered,
	row: unknown[] | Record<string, unknown>,
	joinsNotNullableMap?: Record<string, boolean>
): TResult {
	let nullifyMap: Record<string, string | false> = {}
	let rowValues = rowToArray(columns, row)
 
	let result = columns.reduce<Record<string, any>>((current, { path, field }, columnIndex) => {
		let decoder: { mapFromDriverValue(value: unknown): unknown }
 
		if (is(field, Column)) {
			decoder = field as unknown as YdbColumn
		} else if (is(field, SQL)) {
			decoder = (field as any).decoder
		} else Eif (is(field, Subquery)) {
			decoder = (field as any)._.sql.decoder
		} else {
			decoder = (field as any).sql.decoder
		}
 
		let node = current
		for (let [pathChunkIndex, pathChunk] of path.entries()) {
			if (pathChunkIndex < path.length - 1) {
				if (!(pathChunk in node)) {
					node[pathChunk] = {}
				}
				node = node[pathChunk]
			} else {
				let rawValue = rowValues[columnIndex]
				let value = rawValue === null ? null : decoder.mapFromDriverValue(rawValue)
				node[pathChunk] = value
 
				if (joinsNotNullableMap && is(field, Column) && path.length === 2) {
					let objectName = path[0]!
					if (!(objectName in nullifyMap)) {
						nullifyMap[objectName] = value === null ? getTableName(field.table) : false
					} else Iif (
						typeof nullifyMap[objectName] === 'string' &&
						nullifyMap[objectName] !== getTableName(field.table)
					) {
						nullifyMap[objectName] = false
					}
				}
			}
		}
 
		return current
	}, {})
 
	if (joinsNotNullableMap && Object.keys(nullifyMap).length > 0) {
		for (let [objectName, tableName] of Object.entries(nullifyMap)) {
			if (typeof tableName === 'string' && !joinsNotNullableMap[tableName]) {
				result[objectName] = null
			}
		}
	}
 
	return result as TResult
}