@loopback/repository 3.2.0 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/connectors/connector.d.ts +35 -1
  3. package/dist/relations/belongs-to/{belongs-to-accessor.d.ts → belongs-to.accessor.d.ts} +0 -0
  4. package/dist/relations/belongs-to/{belongs-to-accessor.js → belongs-to.accessor.js} +2 -2
  5. package/dist/relations/belongs-to/{belongs-to-accessor.js.map → belongs-to.accessor.js.map} +1 -1
  6. package/dist/relations/belongs-to/belongs-to.decorator.js +2 -2
  7. package/dist/relations/belongs-to/belongs-to.decorator.js.map +1 -1
  8. package/dist/relations/belongs-to/belongs-to.helpers.js +1 -1
  9. package/dist/relations/belongs-to/belongs-to.helpers.js.map +1 -1
  10. package/dist/relations/belongs-to/belongs-to.inclusion-resolver.d.ts +1 -1
  11. package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js +2 -1
  12. package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js.map +1 -1
  13. package/dist/relations/belongs-to/belongs-to.repository.d.ts +1 -1
  14. package/dist/relations/belongs-to/belongs-to.repository.js +2 -2
  15. package/dist/relations/belongs-to/belongs-to.repository.js.map +1 -1
  16. package/dist/relations/belongs-to/index.d.ts +1 -1
  17. package/dist/relations/belongs-to/index.js +1 -1
  18. package/dist/relations/has-many/has-many-through.helpers.js +1 -1
  19. package/dist/relations/has-many/has-many-through.helpers.js.map +1 -1
  20. package/dist/relations/has-many/{has-many-through.inclusion.resolver.d.ts → has-many-through.inclusion-resolver.d.ts} +1 -1
  21. package/dist/relations/has-many/{has-many-through.inclusion.resolver.js → has-many-through.inclusion-resolver.js} +4 -3
  22. package/dist/relations/has-many/{has-many-through.inclusion.resolver.js.map → has-many-through.inclusion-resolver.js.map} +1 -1
  23. package/dist/relations/has-many/{has-many-through-repository.factory.d.ts → has-many-through.repository-factory.d.ts} +0 -0
  24. package/dist/relations/has-many/{has-many-through-repository.factory.js → has-many-through.repository-factory.js} +2 -2
  25. package/dist/relations/has-many/{has-many-through-repository.factory.js.map → has-many-through.repository-factory.js.map} +1 -1
  26. package/dist/relations/has-many/has-many.helpers.js +1 -1
  27. package/dist/relations/has-many/has-many.helpers.js.map +1 -1
  28. package/dist/relations/has-many/has-many.inclusion-resolver.d.ts +1 -1
  29. package/dist/relations/has-many/has-many.inclusion-resolver.js +3 -2
  30. package/dist/relations/has-many/has-many.inclusion-resolver.js.map +1 -1
  31. package/dist/relations/has-many/{has-many-repository.factory.d.ts → has-many.repository-factory.d.ts} +1 -1
  32. package/dist/relations/has-many/{has-many-repository.factory.js → has-many.repository-factory.js} +2 -2
  33. package/dist/relations/has-many/has-many.repository-factory.js.map +1 -0
  34. package/dist/relations/has-many/has-many.repository.d.ts +1 -1
  35. package/dist/relations/has-many/has-many.repository.js +5 -5
  36. package/dist/relations/has-many/has-many.repository.js.map +1 -1
  37. package/dist/relations/has-many/index.d.ts +2 -2
  38. package/dist/relations/has-many/index.js +2 -2
  39. package/dist/relations/has-one/has-one.helpers.js +1 -1
  40. package/dist/relations/has-one/has-one.helpers.js.map +1 -1
  41. package/dist/relations/has-one/has-one.inclusion-resolver.d.ts +1 -1
  42. package/dist/relations/has-one/has-one.inclusion-resolver.js +2 -1
  43. package/dist/relations/has-one/has-one.inclusion-resolver.js.map +1 -1
  44. package/dist/relations/has-one/{has-one-repository.factory.d.ts → has-one.repository-factory.d.ts} +1 -1
  45. package/dist/relations/has-one/{has-one-repository.factory.js → has-one.repository-factory.js} +2 -2
  46. package/dist/relations/has-one/has-one.repository-factory.js.map +1 -0
  47. package/dist/relations/has-one/index.d.ts +1 -1
  48. package/dist/relations/has-one/index.js +1 -1
  49. package/dist/relations/relation.helpers.d.ts +2 -2
  50. package/dist/relations/relation.helpers.js +10 -11
  51. package/dist/relations/relation.helpers.js.map +1 -1
  52. package/dist/relations/relation.types.d.ts +2 -2
  53. package/dist/repositories/kv.repository.bridge.js.map +1 -1
  54. package/dist/repositories/legacy-juggler-bridge.d.ts +2 -2
  55. package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
  56. package/package.json +18 -18
  57. package/src/connectors/connector.ts +41 -1
  58. package/src/define-model-class.ts +3 -4
  59. package/src/relations/belongs-to/{belongs-to-accessor.ts → belongs-to.accessor.ts} +1 -1
  60. package/src/relations/belongs-to/belongs-to.decorator.ts +2 -2
  61. package/src/relations/belongs-to/belongs-to.helpers.ts +1 -1
  62. package/src/relations/belongs-to/belongs-to.inclusion-resolver.ts +7 -4
  63. package/src/relations/belongs-to/belongs-to.repository.ts +1 -2
  64. package/src/relations/belongs-to/index.ts +1 -1
  65. package/src/relations/has-many/has-many-through.helpers.ts +3 -1
  66. package/src/relations/has-many/{has-many-through.inclusion.resolver.ts → has-many-through.inclusion-resolver.ts} +8 -11
  67. package/src/relations/has-many/{has-many-through-repository.factory.ts → has-many-through.repository-factory.ts} +1 -1
  68. package/src/relations/has-many/has-many.helpers.ts +1 -1
  69. package/src/relations/has-many/has-many.inclusion-resolver.ts +10 -5
  70. package/src/relations/has-many/{has-many-repository.factory.ts → has-many.repository-factory.ts} +4 -2
  71. package/src/relations/has-many/has-many.repository.ts +2 -2
  72. package/src/relations/has-many/index.ts +2 -2
  73. package/src/relations/has-one/has-one.helpers.ts +1 -1
  74. package/src/relations/has-one/has-one.inclusion-resolver.ts +7 -4
  75. package/src/relations/has-one/{has-one-repository.factory.ts → has-one.repository-factory.ts} +4 -2
  76. package/src/relations/has-one/index.ts +1 -1
  77. package/src/relations/relation.helpers.ts +14 -14
  78. package/src/relations/relation.types.ts +2 -2
  79. package/src/repositories/kv.repository.bridge.ts +4 -3
  80. package/src/repositories/legacy-juggler-bridge.ts +7 -2
  81. package/dist/relations/has-many/has-many-repository.factory.js.map +0 -1
  82. package/dist/relations/has-one/has-one-repository.factory.js.map +0 -1
@@ -11,8 +11,8 @@ import {
11
11
  constrainDataObject,
12
12
  constrainFilter,
13
13
  constrainWhere,
14
- } from '../../repositories/constraint-utils';
15
- import {EntityCrudRepository} from '../../repositories/repository';
14
+ EntityCrudRepository,
15
+ } from '../../repositories';
16
16
 
17
17
  /**
18
18
  * CRUD operations for a target repository of a HasMany relation
@@ -3,8 +3,8 @@
3
3
  // This file is licensed under the MIT License.
4
4
  // License text available at https://opensource.org/licenses/MIT
5
5
 
6
- export * from './has-many-repository.factory';
7
- export * from './has-many-through-repository.factory';
6
+ export * from './has-many.repository-factory';
7
+ export * from './has-many-through.repository-factory';
8
8
  export * from './has-many-through.repository';
9
9
  export * from './has-many.decorator';
10
10
  export * from './has-many.inclusion-resolver';
@@ -9,7 +9,7 @@ import {InvalidRelationError} from '../../errors';
9
9
  import {isTypeResolver} from '../../type-resolver';
10
10
  import {HasOneDefinition, RelationType} from '../relation.types';
11
11
 
12
- const debug = debugFactory('loopback:repository:has-one-helpers');
12
+ const debug = debugFactory('loopback:repository:relations:has-one:helpers');
13
13
 
14
14
  /**
15
15
  * Relation definition with optional metadata (e.g. `keyTo`) filled in.
@@ -3,10 +3,10 @@
3
3
  // This file is licensed under the MIT License.
4
4
  // License text available at https://opensource.org/licenses/MIT
5
5
 
6
- import {Filter, Inclusion} from '@loopback/filter';
6
+ import {Filter, InclusionFilter} from '@loopback/filter';
7
7
  import {AnyObject, Options} from '../../common-types';
8
8
  import {Entity} from '../../model';
9
- import {EntityCrudRepository} from '../../repositories/repository';
9
+ import {EntityCrudRepository} from '../../repositories';
10
10
  import {
11
11
  findByForeignKeys,
12
12
  flattenTargetsOfOneToOneRelation,
@@ -39,7 +39,7 @@ export function createHasOneInclusionResolver<
39
39
 
40
40
  return async function fetchHasOneModel(
41
41
  entities: Entity[],
42
- inclusion: Inclusion,
42
+ inclusion: InclusionFilter,
43
43
  options?: Options,
44
44
  ): Promise<((Target & TargetRelations) | undefined)[]> {
45
45
  if (!entities.length) return [];
@@ -48,12 +48,15 @@ export function createHasOneInclusionResolver<
48
48
  const sourceIds = entities.map(e => (e as AnyObject)[sourceKey]);
49
49
  const targetKey = relationMeta.keyTo as StringKeyOf<Target>;
50
50
 
51
+ const scope =
52
+ typeof inclusion === 'string' ? {} : (inclusion.scope as Filter<Target>);
53
+
51
54
  const targetRepo = await getTargetRepo();
52
55
  const targetsFound = await findByForeignKeys(
53
56
  targetRepo,
54
57
  targetKey,
55
58
  sourceIds,
56
- inclusion.scope as Filter<Target>,
59
+ scope,
57
60
  options,
58
61
  );
59
62
 
@@ -6,13 +6,15 @@
6
6
  import debugFactory from 'debug';
7
7
  import {DataObject} from '../../common-types';
8
8
  import {Entity} from '../../model';
9
- import {EntityCrudRepository} from '../../repositories/repository';
9
+ import {EntityCrudRepository} from '../../repositories';
10
10
  import {Getter, HasOneDefinition, InclusionResolver} from '../relation.types';
11
11
  import {resolveHasOneMetadata} from './has-one.helpers';
12
12
  import {createHasOneInclusionResolver} from './has-one.inclusion-resolver';
13
13
  import {DefaultHasOneRepository, HasOneRepository} from './has-one.repository';
14
14
 
15
- const debug = debugFactory('loopback:repository:has-one-repository-factory');
15
+ const debug = debugFactory(
16
+ 'loopback:repository:relations:has-one:repository-factory',
17
+ );
16
18
 
17
19
  export interface HasOneRepositoryFactory<
18
20
  Target extends Entity,
@@ -5,4 +5,4 @@
5
5
 
6
6
  export * from './has-one.decorator';
7
7
  export * from './has-one.repository';
8
- export * from './has-one-repository.factory';
8
+ export * from './has-one.repository-factory';
@@ -5,14 +5,14 @@
5
5
 
6
6
  import assert from 'assert';
7
7
  import debugFactory from 'debug';
8
- import _ from 'lodash';
8
+ import _, {cloneDeep} from 'lodash';
9
9
  import {
10
10
  AnyObject,
11
11
  Entity,
12
12
  EntityCrudRepository,
13
13
  Filter,
14
14
  FilterBuilder,
15
- Inclusion,
15
+ InclusionFilter,
16
16
  Options,
17
17
  Where,
18
18
  } from '..';
@@ -39,17 +39,11 @@ export async function findByForeignKeys<
39
39
  options?: Options,
40
40
  ): Promise<(Target & TargetRelations)[]> {
41
41
  let value;
42
+ scope = cloneDeep(scope);
42
43
 
43
44
  if (Array.isArray(fkValues)) {
44
45
  if (fkValues.length === 0) return [];
45
- value =
46
- fkValues.length === 1
47
- ? fkValues[0]
48
- : {
49
- // Create a copy to prevent query coercion algorithm
50
- // inside connectors from modifying the original values
51
- inq: [...fkValues],
52
- };
46
+ value = fkValues.length === 1 ? fkValues[0] : {inq: fkValues};
53
47
  } else {
54
48
  value = fkValues;
55
49
  }
@@ -84,9 +78,11 @@ export async function includeRelatedModels<
84
78
  >(
85
79
  targetRepository: EntityCrudRepository<T, unknown, Relations>,
86
80
  entities: T[],
87
- include?: Inclusion[],
81
+ include?: InclusionFilter[],
88
82
  options?: Options,
89
83
  ): Promise<(T & Relations)[]> {
84
+ entities = cloneDeep(entities);
85
+ include = cloneDeep(include);
90
86
  const result = entities as (T & Relations)[];
91
87
  if (!include) return result;
92
88
 
@@ -102,12 +98,16 @@ export async function includeRelatedModels<
102
98
  const err = new Error(msg);
103
99
  Object.assign(err, {
104
100
  code: 'INVALID_INCLUSION_FILTER',
101
+ statusCode: 400,
105
102
  });
106
103
  throw err;
107
104
  }
108
105
 
109
106
  const resolveTasks = include.map(async inclusionFilter => {
110
- const relationName = inclusionFilter.relation;
107
+ const relationName =
108
+ typeof inclusionFilter === 'string'
109
+ ? inclusionFilter
110
+ : inclusionFilter.relation;
111
111
  const resolver = targetRepository.inclusionResolvers.get(relationName)!;
112
112
  const targets = await resolver(entities, inclusionFilter, options);
113
113
 
@@ -130,9 +130,9 @@ export async function includeRelatedModels<
130
130
  */
131
131
  function isInclusionAllowed<T extends Entity, Relations extends object = {}>(
132
132
  targetRepository: EntityCrudRepository<T, unknown, Relations>,
133
- include: Inclusion,
133
+ include: InclusionFilter,
134
134
  ): boolean {
135
- const relationName = include.relation;
135
+ const relationName = typeof include === 'string' ? include : include.relation;
136
136
  if (!relationName) {
137
137
  debug('isInclusionAllowed for %j? No: missing relation name', include);
138
138
  return false;
@@ -3,7 +3,7 @@
3
3
  // This file is licensed under the MIT License.
4
4
  // License text available at https://opensource.org/licenses/MIT
5
5
 
6
- import {Inclusion} from '@loopback/filter';
6
+ import {InclusionFilter} from '@loopback/filter';
7
7
  import {Options} from '../common-types';
8
8
  import {Entity} from '../model';
9
9
  import {TypeResolver} from '../type-resolver';
@@ -172,7 +172,7 @@ export type InclusionResolver<S extends Entity, T extends Entity> = (
172
172
  /**
173
173
  * Inclusion requested by the user (e.g. scope constraints to apply).
174
174
  */
175
- inclusion: Inclusion,
175
+ inclusion: InclusionFilter,
176
176
  /**
177
177
  * Generic options object, e.g. carrying the Transaction object.
178
178
  */
@@ -53,9 +53,10 @@ export class DefaultKeyValueRepository<T extends Model>
53
53
  }
54
54
 
55
55
  async get(key: string, options?: Options): Promise<T> {
56
- const val = this.kvModelClass.get(key, options) as legacy.PromiseOrVoid<
57
- legacy.ModelData
58
- >;
56
+ const val = this.kvModelClass.get(
57
+ key,
58
+ options,
59
+ ) as legacy.PromiseOrVoid<legacy.ModelData>;
59
60
  const result = await ensurePromise(val);
60
61
  return this.toEntity(result);
61
62
  }
@@ -4,7 +4,12 @@
4
4
  // License text available at https://opensource.org/licenses/MIT
5
5
 
6
6
  import {Getter} from '@loopback/core';
7
- import {Filter, FilterExcludingWhere, Inclusion, Where} from '@loopback/filter';
7
+ import {
8
+ Filter,
9
+ FilterExcludingWhere,
10
+ InclusionFilter,
11
+ Where,
12
+ } from '@loopback/filter';
8
13
  import assert from 'assert';
9
14
  import legacy from 'loopback-datasource-juggler';
10
15
  import {
@@ -695,7 +700,7 @@ export class DefaultCrudRepository<
695
700
  */
696
701
  protected async includeRelatedModels(
697
702
  entities: T[],
698
- include?: Inclusion[],
703
+ include?: InclusionFilter[],
699
704
  options?: Options,
700
705
  ): Promise<(T & Relations)[]> {
701
706
  return includeRelatedModels<T, Relations>(this, entities, include, options);
@@ -1 +0,0 @@
1
- {"version":3,"file":"has-many-repository.factory.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-repository.factory.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AAKjC,yDAA0D;AAC1D,+EAA6E;AAC7E,+DAG+B;AAE/B,MAAM,KAAK,GAAG,eAAY,CAAC,iDAAiD,CAAC,CAAC;AAiB9E;;;;;;;;;;;;GAYG;AACH,SAAgB,8BAA8B,CAK5C,gBAAmC,EACnC,sBAAsE;IAEtE,MAAM,IAAI,GAAG,yCAAsB,CAAC,gBAAgB,CAAC,CAAC;IACtD,KAAK,CAAC,wCAAwC,EAAE,IAAI,CAAC,CAAC;IACtD,MAAM,MAAM,GAAqD,UAC/D,OAAuB;QAEvB,8DAA8D;QAC9D,MAAM,UAAU,GAAQ,EAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,OAAO,EAAC,CAAC;QAChD,OAAO,IAAI,8CAAwB,CAIjC,sBAAsB,EAAE,UAAgC,CAAC,CAAC;IAC9D,CAAC,CAAC;IACF,MAAM,CAAC,iBAAiB,GAAG,4DAA8B,CACvD,IAAI,EACJ,sBAAsB,CACvB,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AA1BD,wEA0BC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"has-one-repository.factory.js","sourceRoot":"","sources":["../../../src/relations/has-one/has-one-repository.factory.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AAKjC,uDAAwD;AACxD,6EAA2E;AAC3E,6DAA+E;AAE/E,MAAM,KAAK,GAAG,eAAY,CAAC,gDAAgD,CAAC,CAAC;AAgB7E;;;;;;;;;;;;GAYG;AACH,SAAgB,6BAA6B,CAK3C,gBAAkC,EAClC,sBAAsE;IAEtE,MAAM,IAAI,GAAG,uCAAqB,CAAC,gBAAgB,CAAC,CAAC;IACrD,KAAK,CAAC,uCAAuC,EAAE,IAAI,CAAC,CAAC;IACrD,MAAM,MAAM,GAAoD,UAC9D,OAAuB;QAEvB,8DAA8D;QAC9D,MAAM,UAAU,GAAQ,EAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,OAAO,EAAC,CAAC;QAChD,OAAO,IAAI,4CAAuB,CAIhC,sBAAsB,EAAE,UAAgC,CAAC,CAAC;IAC9D,CAAC,CAAC;IACF,MAAM,CAAC,iBAAiB,GAAG,0DAA6B,CACtD,IAAI,EACJ,sBAAsB,CACvB,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AA1BD,sEA0BC"}