MCPcopy
hub / github.com/drizzle-team/drizzle-orm / mapResultRow

Function mapResultRow

drizzle-orm/src/utils.ts:15–73  ·  view source on GitHub ↗
(
	columns: SelectedFieldsOrdered<AnyColumn>,
	row: unknown[],
	joinsNotNullableMap: Record<string, boolean> | undefined,
)

Source from the content-addressed store, hash-verified

13
14/** @internal */
15export function mapResultRow<TResult>(
16 columns: SelectedFieldsOrdered<AnyColumn>,
17 row: unknown[],
18 joinsNotNullableMap: Record<string, boolean> | undefined,
19): TResult {
20 // Key -> nested object key, value -> table name if all fields in the nested object are from the same table, false otherwise
21 const nullifyMap: Record<string, string | false> = {};
22
23 const result = columns.reduce<Record<string, any>>(
24 (result, { path, field }, columnIndex) => {
25 let decoder: DriverValueDecoder<unknown, unknown>;
26 if (is(field, Column)) {
27 decoder = field;
28 } else if (is(field, SQL)) {
29 decoder = field.decoder;
30 } else if (is(field, Subquery)) {
31 decoder = field._.sql.decoder;
32 } else {
33 decoder = field.sql.decoder;
34 }
35 let node = result;
36 for (const [pathChunkIndex, pathChunk] of path.entries()) {
37 if (pathChunkIndex < path.length - 1) {
38 if (!(pathChunk in node)) {
39 node[pathChunk] = {};
40 }
41 node = node[pathChunk];
42 } else {
43 const rawValue = row[columnIndex]!;
44 const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
45
46 if (joinsNotNullableMap && is(field, Column) && path.length === 2) {
47 const objectName = path[0]!;
48 if (!(objectName in nullifyMap)) {
49 nullifyMap[objectName] = value === null ? getTableName(field.table) : false;
50 } else if (
51 typeof nullifyMap[objectName] === 'string' && nullifyMap[objectName] !== getTableName(field.table)
52 ) {
53 nullifyMap[objectName] = false;
54 }
55 }
56 }
57 }
58 return result;
59 },
60 {},
61 );
62
63 // Nullify all nested objects from nullifyMap that are nullable
64 if (joinsNotNullableMap && Object.keys(nullifyMap).length > 0) {
65 for (const [objectName, tableName] of Object.entries(nullifyMap)) {
66 if (typeof tableName === 'string' && !joinsNotNullableMap[tableName]) {
67 result[objectName] = null;
68 }
69 }
70 }
71
72 return result as TResult;

Callers 15

allMethod · 0.90
getMethod · 0.90
mapAllResultMethod · 0.90
getMethod · 0.90
mapGetResultMethod · 0.90
mapAllResultMethod · 0.90
mapGetResultMethod · 0.90
executeMethod · 0.90
executeMethod · 0.90
mapAllResultMethod · 0.90
mapGetResultMethod · 0.90
executeMethod · 0.90

Calls 3

isFunction · 0.90
getTableNameFunction · 0.90
mapFromDriverValueMethod · 0.65

Tested by

no test coverage detected