Skip to content

Commit

Permalink
refactor: optimize mapping of joined results
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Aug 9, 2020
1 parent 3bd0e98 commit 47f899e
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,17 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
return null;
}

// TODO we might want to optimize this bit, as we are creating a lot of new arrays via destructing (so might be memory heavy)
return rawResults.reduce((result, value) => {
joinedProps.forEach(prop => {
if ([ReferenceType.MANY_TO_MANY, ReferenceType.ONE_TO_MANY].includes(prop.reference)) {
const relation = value[prop.name];
const existing = result[prop.name] || [];
result[prop.name] = [...existing, ...relation];
result[prop.name] = result[prop.name] || [];
result[prop.name].push(...value[prop.name]);
} else {
result[prop.name] = value[prop.name];
}
});

return { ...value, ...result };
return Object.assign(value, result);
}, {}) as unknown as T;
}

Expand Down Expand Up @@ -463,7 +461,6 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra
}

if (fields && !hasExplicitFields) {
// TODO joined loads will need different aliasing here, this works only for the root entity
Object.values<EntityProperty<T>>(meta.properties)
.filter(prop => prop.formula)
.forEach(prop => {
Expand Down

0 comments on commit 47f899e

Please sign in to comment.