Home / Function/ mapResultRow() — drizzle-orm Function Reference

mapResultRow() — drizzle-orm Function Reference

Architecture documentation for the mapResultRow() function in utils.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  7d57d70d_cd39_acc3_0c7e_58fea2a074cd["mapResultRow()"]
  99737bc3_a631_a054_9291_f966c791930f["utils.ts"]
  7d57d70d_cd39_acc3_0c7e_58fea2a074cd -->|defined in| 99737bc3_a631_a054_9291_f966c791930f
  c020d230_75a2_3639_d9a6_35f2ba7fd5bc["is()"]
  7d57d70d_cd39_acc3_0c7e_58fea2a074cd -->|calls| c020d230_75a2_3639_d9a6_35f2ba7fd5bc
  3ffe8b4f_1572_cbbf_08f8_d64f9131141c["getTableName()"]
  7d57d70d_cd39_acc3_0c7e_58fea2a074cd -->|calls| 3ffe8b4f_1572_cbbf_08f8_d64f9131141c
  style 7d57d70d_cd39_acc3_0c7e_58fea2a074cd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/utils.ts lines 15–73

export function mapResultRow<TResult>(
	columns: SelectedFieldsOrdered<AnyColumn>,
	row: unknown[],
	joinsNotNullableMap: Record<string, boolean> | undefined,
): TResult {
	// Key -> nested object key, value -> table name if all fields in the nested object are from the same table, false otherwise
	const nullifyMap: Record<string, string | false> = {};

	const result = columns.reduce<Record<string, any>>(
		(result, { path, field }, columnIndex) => {
			let decoder: DriverValueDecoder<unknown, unknown>;
			if (is(field, Column)) {
				decoder = field;
			} else if (is(field, SQL)) {
				decoder = field.decoder;
			} else if (is(field, Subquery)) {
				decoder = field._.sql.decoder;
			} else {
				decoder = field.sql.decoder;
			}
			let node = result;
			for (const [pathChunkIndex, pathChunk] of path.entries()) {
				if (pathChunkIndex < path.length - 1) {
					if (!(pathChunk in node)) {
						node[pathChunk] = {};
					}
					node = node[pathChunk];
				} else {
					const rawValue = row[columnIndex]!;
					const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);

					if (joinsNotNullableMap && is(field, Column) && path.length === 2) {
						const objectName = path[0]!;
						if (!(objectName in nullifyMap)) {
							nullifyMap[objectName] = value === null ? getTableName(field.table) : false;
						} else if (
							typeof nullifyMap[objectName] === 'string' && nullifyMap[objectName] !== getTableName(field.table)
						) {
							nullifyMap[objectName] = false;
						}
					}
				}
			}
			return result;
		},
		{},
	);

	// Nullify all nested objects from nullifyMap that are nullable
	if (joinsNotNullableMap && Object.keys(nullifyMap).length > 0) {
		for (const [objectName, tableName] of Object.entries(nullifyMap)) {
			if (typeof tableName === 'string' && !joinsNotNullableMap[tableName]) {
				result[objectName] = null;
			}
		}
	}

	return result as TResult;
}

Domain

Subdomains

Frequently Asked Questions

What does mapResultRow() do?
mapResultRow() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/utils.ts.
Where is mapResultRow() defined?
mapResultRow() is defined in drizzle-orm/src/utils.ts at line 15.
What does mapResultRow() call?
mapResultRow() calls 2 function(s): getTableName, is.

Analyze Your Own Codebase

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

Try Supermodel Free