52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
import {
|
|
getTableName,
|
|
is,
|
|
Column,
|
|
SQL
|
|
} from "drizzle-orm";
|
|
export function mapResultRow(columns, row, joinsNotNullableMap) {
|
|
const nullifyMap = {};
|
|
const result = columns.reduce(
|
|
(result2, { path, field }, columnIndex) => {
|
|
let decoder;
|
|
if (is(field, Column)) {
|
|
decoder = field;
|
|
} else if (is(field, SQL)) {
|
|
decoder = "decoder" in field && field.decoder;
|
|
} else {
|
|
decoder = "decoder" in field.sql && field.sql.decoder;
|
|
}
|
|
let node = result2;
|
|
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 result2;
|
|
},
|
|
{}
|
|
);
|
|
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;
|
|
}
|