From 6bf98c66af0724cf520655217b36f46917a7242e Mon Sep 17 00:00:00 2001 From: Agnes Lin Date: Wed, 28 Aug 2019 17:13:18 -0400 Subject: [PATCH] feat(repository): add has-many-inclusion-resolver function and tests for flatten method --- .../has-many/has-many-repository.factory.ts | 2 +- .../has-many/has-many.inclusion-resolver.ts | 2 +- .../src/relations/relation.helpers.ts | 35 +++++++++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/repository/src/relations/has-many/has-many-repository.factory.ts b/packages/repository/src/relations/has-many/has-many-repository.factory.ts index 65e505826c1a..e95972b8cec1 100644 --- a/packages/repository/src/relations/has-many/has-many-repository.factory.ts +++ b/packages/repository/src/relations/has-many/has-many-repository.factory.ts @@ -29,7 +29,7 @@ export interface HasManyRepositoryFactory< /** * Use `resolver` property to obtain an InclusionResolver for this relation. */ - inclusionResolver: InclusionResolver; + inclusionResolver: InclusionResolver; } /** diff --git a/packages/repository/src/relations/has-many/has-many.inclusion-resolver.ts b/packages/repository/src/relations/has-many/has-many.inclusion-resolver.ts index cf75daeade27..4339064daa84 100644 --- a/packages/repository/src/relations/has-many/has-many.inclusion-resolver.ts +++ b/packages/repository/src/relations/has-many/has-many.inclusion-resolver.ts @@ -24,7 +24,7 @@ export function createHasManyInclusionResolver< getTargetRepo: Getter< EntityCrudRepository >, -): InclusionResolver { +): InclusionResolver { const relationMeta = resolveHasManyMetadata(meta); return async function fetchHasManyModels( diff --git a/packages/repository/src/relations/relation.helpers.ts b/packages/repository/src/relations/relation.helpers.ts index 77c6169d7a6f..dc62260d1d92 100644 --- a/packages/repository/src/relations/relation.helpers.ts +++ b/packages/repository/src/relations/relation.helpers.ts @@ -133,7 +133,8 @@ function isInclusionAllowed( 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 @@ -156,7 +157,8 @@ export function flattenTargetsOfOneToManyRelation( 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 @@ -166,24 +168,17 @@ export function flattenMapByKeys( targetMap: Map, ): (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 @@ -205,7 +200,9 @@ export function buildLookupMap( } 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 @@ -219,7 +216,8 @@ export function getKeyValue(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 @@ -229,7 +227,8 @@ export function reduceAsArray(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