Skip to content

Commit

Permalink
Crashing on many relations if foreign key column is omitted in strategy
Browse files Browse the repository at this point in the history
closes #83
  • Loading branch information
Lars-Erik Roald committed May 10, 2024
1 parent f0d3f82 commit f8ee069
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/getManyDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const executeQueries = require('./table/executeQueries');
const newPrimaryKeyFilter = require('./table/newPrimaryKeyFilter');
const newForeignKeyFilter = require('./table/relation/newForeignKeyFilter');

async function getManyDto(table, filter, strategy) {
async function getManyDto(table, filter, strategy, spanFromParent) {
filter = negotiateRawSqlFilter(filter, table);
if (strategy && strategy.where) {
let arg = typeof strategy.where === 'function' ? strategy.where(table) : strategy.where;
filter = filter.and(arg);
}

let span = strategyToSpan(table, strategy);
let span = spanFromParent || strategyToSpan(table, strategy);
let alias = table._dbName;

const query = newQuery(table, filter, span, alias);
Expand Down Expand Up @@ -188,7 +188,7 @@ async function decodeRelations(strategy, span, rawRows, resultRows, keys) {
const relation = table._relations[name];
const filter = createOneFilter(relation, span._ids, resultRows, table);
const rowsMap = span._rowsMap;
const p = getManyDto(relation.childTable, filter, strategy[name]).then(subRows => {
const p = getManyDto(relation.childTable, filter, strategy[name], leg.span).then(subRows => {
for (let i = 0; i < subRows.length; i++) {
const key = leg.columns.map(column => subRows[i][column.alias]);
const parentRow = getFromMap(rowsMap, table._primaryColumns, key);
Expand Down
6 changes: 3 additions & 3 deletions src/map.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ type ExpandedMappedTable<T, FL = ExpandedFetchingStrategy<T>> = {
): Promise<StrategyToRowArray<FetchedProperties<T, FL>, T>>;
getAll(): Promise<StrategyToRowArray<FetchedProperties<T, FL>, T>>;
getAll<FS extends FetchingStrategy<T>>(
fetchingStrategy?: FS
): Promise<StrategyToRowArray<FetchedProperties<T, FL>, T>>;
fetchingStrategy: FS
): Promise<StrategyTRowArray<FetchedProperties<T, FL>, T>>;
count(filter?: Filter | PrimaryRowFilter<T>[]): Promise<number>;
delete(filter?: Filter | PrimaryRowFilter<T>[]): Promise<void>;
deleteCascade(filter?: Filter | PrimaryRowFilter<T>[]): Promise<void>;
Expand Down Expand Up @@ -359,7 +359,7 @@ type MappedTable<T> = {
): Promise<StrategyToRowArray<FetchedProperties<T, FS>, T>>;
getAll(): Promise<StrategyToRowArray<FetchedProperties<T, {}>, T>>;
getAll<FS extends FetchingStrategy<T>>(
fetchingStrategy?: FS
fetchingStrategy: FS
): Promise<StrategyToRowArray<FetchedProperties<T, FS>, T>>;
count(filter?: Filter | PrimaryRowFilter<T>[]): Promise<number>;
delete(filter?: Filter | PrimaryRowFilter<T>[]): Promise<void>;
Expand Down
5 changes: 3 additions & 2 deletions src/table/purifyStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ function purifyStrategy(table, strategy, columns = new Map()) {
continue;
else if (table._relations[name])
strategy[name] = addLeg(table._relations[name], strategy[name], columns);
else if (table[name] && table[name].eq) {
columns.set(table[name], strategy[name]);
else if (table[name] && table[name].eq ) {
if (!columns.has(table[name]))
columns.set(table[name], strategy[name]);
hasIncludedColumns = hasIncludedColumns || strategy[name];
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/getMany.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ describe('getMany with aggregates', () => {
customerName: x => x.customer.name,
id2: x => x.id,
lines: {
product: true,
id2: x => x.id,
numberOfPackages: x => x.count(x => x.packages.id)
},
Expand All @@ -530,6 +531,7 @@ describe('getMany with aggregates', () => {
balance: x => x.min(x => x.customer.balance),
customerId2: x => x.sum(x => x.customer.id),
});
// rows[0].lines[0].

//mssql workaround because datetime has no time offset
for (let i = 0; i < rows.length; i++) {
Expand Down Expand Up @@ -576,7 +578,7 @@ describe('getMany with aggregates', () => {
balance: 200,
customerId2: 2,
lines: [
{ id2: 3, product: 'Magic wand', id: 3, orderId: 2, numberOfPackages: 1 }
{ id2: 3, product: 'Magic wand', id: 3, orderId: 2, numberOfPackages: 1 }
],
customer: {
id: 2,
Expand Down

0 comments on commit f8ee069

Please sign in to comment.