Skip to content

Commit

Permalink
feat(repository): add has-many-inclusion-resolver function and tests …
Browse files Browse the repository at this point in the history
…for flatten method
  • Loading branch information
Agnes Lin committed Aug 28, 2019
1 parent f98e0ab commit 6bf98c6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface HasManyRepositoryFactory<
/**
* Use `resolver` property to obtain an InclusionResolver for this relation.
*/
inclusionResolver: InclusionResolver;
inclusionResolver: InclusionResolver<Entity, Target>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function createHasManyInclusionResolver<
getTargetRepo: Getter<
EntityCrudRepository<Target, TargetID, TargetRelations>
>,
): InclusionResolver {
): InclusionResolver<Entity, Target> {
const relationMeta = resolveHasManyMetadata(meta);

return async function fetchHasManyModels(
Expand Down
35 changes: 17 additions & 18 deletions packages/repository/src/relations/relation.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ function isInclusionAllowed<T extends Entity, Relations extends object = {}>(
return allowed;
}

/** Returns an array of arrays. Each nested array has one or more instance
/**
* Returns an array of arrays. Each nested array has one or more instance
* as a result of one to many relations.
*
* @param sourceIds - One value or array of values of the target key
Expand All @@ -156,7 +157,8 @@ export function flattenTargetsOfOneToManyRelation<Target extends Entity>(
return flattenMapByKeys(sourceIds, lookup);
}

/** Returns an array of instaces, which's indexes map the sourceIds' indexes
/**
* Returns an array of instaces, which's indexes map the sourceIds' indexes
*
* @param sourceIds - One value or array of values (of the target key)
* @param targetMap - a map that matches sourceIds with instances
Expand All @@ -166,24 +168,17 @@ export function flattenMapByKeys<T>(
targetMap: Map<unknown, T>,
): (T | undefined)[] {
const result: (T | undefined)[] = new Array(sourceIds.length);
// should we change the for loop to:

for (const ix in sourceIds) {
const key = sourceIds[ix];
const target = targetMap.get(key);
result[ix] = target;
}
// for(const i of sourceIds.keys()){
// const key = sourceIds[i];
// const target = targetMap.get(key);
// result[i] = target;
// }
sourceIds.forEach((id, index) => {
const target = targetMap.get(id);
result[index] = target;
});

return result;
}

// TODO(bajtos) add test coverage
/** Returns a map which maps key values(ids) to instances.
/**
* Returns a map which maps key values(ids) to instances.
*
* @param list - an array of instances
* @param keyName - key name of the source
Expand All @@ -205,7 +200,9 @@ export function buildLookupMap<Key, InType, OutType = InType>(
}
return lookup;
}
/** returns value of a key. Aim to resolve ObjectId problem of Mongo

/**
* Returns value of a key. Aim to resolve ObjectId problem of Mongo
*
* @param model - target model
* @param keyName - target key that gets the value from
Expand All @@ -219,7 +216,8 @@ export function getKeyValue<T>(model: T, keyName: string) {
return rawKey;
}

/** Returns an array of instance. For HasMany relation usage.
/**
* Returns an array of instance. For HasMany relation usage.
*
* @param acc
* @param it
Expand All @@ -229,7 +227,8 @@ export function reduceAsArray<T>(acc: T[] | undefined, it: T) {
else acc = [it];
return acc;
}
/** Returns a single of instance. For HasOne and BelongsTo relation usage.
/**
* Returns a single of instance. For HasOne and BelongsTo relation usage.
*
* @param _acc
* @param it
Expand Down

0 comments on commit 6bf98c6

Please sign in to comment.