@loopback/repository 3.6.0 → 3.7.0

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 (39) hide show
  1. package/dist/mixins/repository.mixin.d.ts +2 -3
  2. package/dist/mixins/repository.mixin.js +6 -3
  3. package/dist/mixins/repository.mixin.js.map +1 -1
  4. package/dist/relations/belongs-to/belongs-to.accessor.js.map +1 -1
  5. package/dist/relations/belongs-to/belongs-to.repository.js.map +1 -1
  6. package/dist/relations/has-many/has-many-through.inclusion-resolver.js.map +1 -1
  7. package/dist/relations/has-many/has-many-through.repository.d.ts +11 -0
  8. package/dist/relations/has-many/has-many-through.repository.js +9 -0
  9. package/dist/relations/has-many/has-many-through.repository.js.map +1 -1
  10. package/dist/relations/has-many/has-many.repository.js.map +1 -1
  11. package/dist/relations/has-one/has-one.repository.js.map +1 -1
  12. package/dist/relations/relation.helpers.js.map +1 -1
  13. package/dist/repositories/kv.repository.bridge.js +2 -1
  14. package/dist/repositories/kv.repository.bridge.js.map +1 -1
  15. package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
  16. package/dist/repositories/repository.js.map +1 -1
  17. package/package.json +11 -11
  18. package/src/define-model-class.ts +2 -2
  19. package/src/define-repository-class.ts +4 -4
  20. package/src/mixins/repository.mixin.ts +11 -8
  21. package/src/relations/belongs-to/belongs-to.accessor.ts +21 -23
  22. package/src/relations/belongs-to/belongs-to.inclusion-resolver.ts +1 -1
  23. package/src/relations/belongs-to/belongs-to.repository.ts +3 -2
  24. package/src/relations/has-many/has-many-through.helpers.ts +4 -4
  25. package/src/relations/has-many/has-many-through.inclusion-resolver.ts +2 -2
  26. package/src/relations/has-many/has-many-through.repository-factory.ts +2 -2
  27. package/src/relations/has-many/has-many-through.repository.ts +44 -15
  28. package/src/relations/has-many/has-many.inclusion-resolver.ts +1 -1
  29. package/src/relations/has-many/has-many.repository-factory.ts +2 -2
  30. package/src/relations/has-many/has-many.repository.ts +3 -2
  31. package/src/relations/has-one/has-one.inclusion-resolver.ts +1 -1
  32. package/src/relations/has-one/has-one.repository-factory.ts +2 -2
  33. package/src/relations/has-one/has-one.repository.ts +3 -2
  34. package/src/relations/relation.helpers.ts +3 -3
  35. package/src/repositories/kv.repository.bridge.ts +4 -4
  36. package/src/repositories/legacy-juggler-bridge.ts +11 -9
  37. package/src/repositories/repository.ts +5 -4
  38. package/src/type-resolver.ts +1 -1
  39. package/CHANGELOG.md +0 -1925
@@ -38,7 +38,7 @@ export function createHasManyThroughInclusionResolver<
38
38
  ThroughRelations extends object,
39
39
  Target extends Entity,
40
40
  TargetID,
41
- TargetRelations extends object
41
+ TargetRelations extends object,
42
42
  >(
43
43
  meta: HasManyDefinition,
44
44
  getThroughRepo: Getter<
@@ -118,7 +118,7 @@ export function createHasManyThroughInclusionResolver<
118
118
  Target,
119
119
  TargetRelations,
120
120
  StringKeyOf<Target>
121
- >(targetRepo, targetKey, (targetIds as unknown) as [], scope, options);
121
+ >(targetRepo, targetKey, targetIds as unknown as [], scope, options);
122
122
  result.push(targetEntityList);
123
123
  } else {
124
124
  // no entities found, add undefined to results
@@ -37,7 +37,7 @@ export type HasManyThroughRepositoryFactory<
37
37
  TargetEntity extends Entity,
38
38
  TargetID,
39
39
  ThroughEntity extends Entity,
40
- SourceID
40
+ SourceID,
41
41
  > = {
42
42
  (fkValue: SourceID): HasManyThroughRepository<
43
43
  TargetEntity,
@@ -56,7 +56,7 @@ export function createHasManyThroughRepositoryFactory<
56
56
  TargetID,
57
57
  Through extends Entity,
58
58
  ThroughID,
59
- SourceID
59
+ SourceID,
60
60
  >(
61
61
  relationMetadata: HasManyDefinition,
62
62
  targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -27,7 +27,7 @@ import {
27
27
  export interface HasManyThroughRepository<
28
28
  Target extends Entity,
29
29
  TargetID,
30
- Through extends Entity
30
+ Through extends Entity,
31
31
  > {
32
32
  /**
33
33
  * Create a target model instance
@@ -110,6 +110,17 @@ export interface HasManyThroughRepository<
110
110
  throughOptions?: Options;
111
111
  },
112
112
  ): Promise<void>;
113
+
114
+ /**
115
+ * Remove all association to an existing target model instance
116
+ * @param options
117
+ * @return A promise which resolves to void
118
+ */
119
+ unlinkAll(
120
+ options?: Options & {
121
+ throughOptions?: Options;
122
+ },
123
+ ): Promise<void>;
113
124
  }
114
125
 
115
126
  /**
@@ -125,8 +136,9 @@ export class DefaultHasManyThroughRepository<
125
136
  TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>,
126
137
  ThroughEntity extends Entity,
127
138
  ThroughID,
128
- ThroughRepository extends EntityCrudRepository<ThroughEntity, ThroughID>
129
- > implements HasManyThroughRepository<TargetEntity, TargetID, ThroughEntity> {
139
+ ThroughRepository extends EntityCrudRepository<ThroughEntity, ThroughID>,
140
+ > implements HasManyThroughRepository<TargetEntity, TargetID, ThroughEntity>
141
+ {
130
142
  constructor(
131
143
  public getTargetRepository: Getter<TargetRepository>,
132
144
  public getThroughRepository: Getter<ThroughRepository>,
@@ -170,9 +182,8 @@ export class DefaultHasManyThroughRepository<
170
182
  constrainFilter(undefined, sourceConstraint),
171
183
  options?.throughOptions,
172
184
  );
173
- const targetConstraint = this.getTargetConstraintFromThroughModels(
174
- throughInstances,
175
- );
185
+ const targetConstraint =
186
+ this.getTargetConstraintFromThroughModels(throughInstances);
176
187
  return targetRepository.find(
177
188
  constrainFilter(filter, targetConstraint),
178
189
  options,
@@ -210,17 +221,15 @@ export class DefaultHasManyThroughRepository<
210
221
  // otherwise, delete through models that relate to the sourceId
211
222
  const targetFkValues = this.getTargetKeys(throughInstances);
212
223
  // delete through instances that have the targets that are going to be deleted
213
- const throughFkConstraint = this.getThroughConstraintFromTarget(
214
- targetFkValues,
215
- );
224
+ const throughFkConstraint =
225
+ this.getThroughConstraintFromTarget(targetFkValues);
216
226
  await throughRepository.deleteAll(
217
227
  constrainWhereOr({}, [sourceConstraint, throughFkConstraint]),
218
228
  );
219
229
  }
220
230
  // delete target(s)
221
- const targetConstraint = this.getTargetConstraintFromThroughModels(
222
- throughInstances,
223
- );
231
+ const targetConstraint =
232
+ this.getTargetConstraintFromThroughModels(throughInstances);
224
233
  return targetRepository.deleteAll(
225
234
  constrainWhere(where, targetConstraint as Where<TargetEntity>),
226
235
  options,
@@ -241,9 +250,8 @@ export class DefaultHasManyThroughRepository<
241
250
  constrainFilter(undefined, sourceConstraint),
242
251
  options?.throughOptions,
243
252
  );
244
- const targetConstraint = this.getTargetConstraintFromThroughModels(
245
- throughInstances,
246
- );
253
+ const targetConstraint =
254
+ this.getTargetConstraintFromThroughModels(throughInstances);
247
255
  return targetRepository.updateAll(
248
256
  constrainDataObject(dataObject, targetConstraint),
249
257
  constrainWhere(where, targetConstraint as Where<TargetEntity>),
@@ -286,4 +294,25 @@ export class DefaultHasManyThroughRepository<
286
294
  options?.throughOptions,
287
295
  );
288
296
  }
297
+
298
+ async unlinkAll(
299
+ options?: Options & {
300
+ throughOptions?: Options;
301
+ },
302
+ ): Promise<void> {
303
+ const throughRepository = await this.getThroughRepository();
304
+ const sourceConstraint = this.getThroughConstraintFromSource();
305
+ const throughInstances = await throughRepository.find(
306
+ constrainFilter(undefined, sourceConstraint),
307
+ options?.throughOptions,
308
+ );
309
+ const targetFkValues = this.getTargetKeys(throughInstances);
310
+ const targetConstraint =
311
+ this.getThroughConstraintFromTarget(targetFkValues);
312
+ const constraints = {...targetConstraint, ...sourceConstraint};
313
+ await throughRepository.deleteAll(
314
+ constrainDataObject({}, constraints as DataObject<ThroughEntity>),
315
+ options?.throughOptions,
316
+ );
317
+ }
289
318
  }
@@ -33,7 +33,7 @@ const debug = debugFactory(
33
33
  export function createHasManyInclusionResolver<
34
34
  Target extends Entity,
35
35
  TargetID,
36
- TargetRelations extends object
36
+ TargetRelations extends object,
37
37
  >(
38
38
  meta: HasManyDefinition,
39
39
  getTargetRepo: Getter<
@@ -21,7 +21,7 @@ const debug = debugFactory(
21
21
 
22
22
  export interface HasManyRepositoryFactory<
23
23
  Target extends Entity,
24
- ForeignKeyType
24
+ ForeignKeyType,
25
25
  > {
26
26
  /**
27
27
  * Invoke the function to obtain HasManyRepository.
@@ -50,7 +50,7 @@ export interface HasManyRepositoryFactory<
50
50
  export function createHasManyRepositoryFactory<
51
51
  Target extends Entity,
52
52
  TargetID,
53
- ForeignKeyType
53
+ ForeignKeyType,
54
54
  >(
55
55
  relationMetadata: HasManyDefinition,
56
56
  targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -59,8 +59,9 @@ export interface HasManyRepository<Target extends Entity> {
59
59
  export class DefaultHasManyRepository<
60
60
  TargetEntity extends Entity,
61
61
  TargetID,
62
- TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>
63
- > implements HasManyRepository<TargetEntity> {
62
+ TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>,
63
+ > implements HasManyRepository<TargetEntity>
64
+ {
64
65
  /**
65
66
  * Constructor of DefaultHasManyEntityCrudRepository
66
67
  * @param getTargetRepository - the getter of the related target model repository instance
@@ -28,7 +28,7 @@ import {resolveHasOneMetadata} from './has-one.helpers';
28
28
  export function createHasOneInclusionResolver<
29
29
  Target extends Entity,
30
30
  TargetID,
31
- TargetRelations extends object
31
+ TargetRelations extends object,
32
32
  >(
33
33
  meta: HasOneDefinition,
34
34
  getTargetRepo: Getter<
@@ -18,7 +18,7 @@ const debug = debugFactory(
18
18
 
19
19
  export interface HasOneRepositoryFactory<
20
20
  Target extends Entity,
21
- ForeignKeyType
21
+ ForeignKeyType,
22
22
  > {
23
23
  /**
24
24
  * Invoke the function to obtain HasOneRepository.
@@ -46,7 +46,7 @@ export interface HasOneRepositoryFactory<
46
46
  export function createHasOneRepositoryFactory<
47
47
  Target extends Entity,
48
48
  TargetID,
49
- ForeignKeyType
49
+ ForeignKeyType,
50
50
  >(
51
51
  relationMetadata: HasOneDefinition,
52
52
  targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -61,8 +61,9 @@ export interface HasOneRepository<Target extends Entity> {
61
61
  export class DefaultHasOneRepository<
62
62
  TargetEntity extends Entity,
63
63
  TargetID,
64
- TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>
65
- > implements HasOneRepository<TargetEntity> {
64
+ TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>,
65
+ > implements HasOneRepository<TargetEntity>
66
+ {
66
67
  /**
67
68
  * Constructor of DefaultHasOneEntityCrudRepository
68
69
  * @param getTargetRepository - the getter of the related target model repository instance
@@ -30,7 +30,7 @@ const debug = debugFactory('loopback:repository:relation-helpers');
30
30
  export async function findByForeignKeys<
31
31
  Target extends Entity,
32
32
  TargetRelations extends object,
33
- ForeignKey extends StringKeyOf<Target>
33
+ ForeignKey extends StringKeyOf<Target>,
34
34
  >(
35
35
  targetRepository: EntityCrudRepository<Target, unknown, TargetRelations>,
36
36
  fkName: ForeignKey,
@@ -48,7 +48,7 @@ export async function findByForeignKeys<
48
48
  value = fkValues;
49
49
  }
50
50
 
51
- const where = ({[fkName]: value} as unknown) as Where<Target>;
51
+ const where = {[fkName]: value} as unknown as Where<Target>;
52
52
 
53
53
  if (scope && !_.isEmpty(scope)) {
54
54
  // combine where clause to scope filter
@@ -74,7 +74,7 @@ export type StringKeyOf<T> = Extract<keyof T, string>;
74
74
 
75
75
  export async function includeRelatedModels<
76
76
  T extends Entity,
77
- Relations extends object = {}
77
+ Relations extends object = {},
78
78
  >(
79
79
  targetRepository: EntityCrudRepository<T, unknown, Relations>,
80
80
  entities: T[],
@@ -13,7 +13,8 @@ import {ensurePromise, juggler} from './legacy-juggler-bridge';
13
13
  * An implementation of KeyValueRepository based on loopback-datasource-juggler
14
14
  */
15
15
  export class DefaultKeyValueRepository<T extends Model>
16
- implements KeyValueRepository<T> {
16
+ implements KeyValueRepository<T>
17
+ {
17
18
  /**
18
19
  * A legacy KeyValueModel class
19
20
  */
@@ -29,9 +30,8 @@ export class DefaultKeyValueRepository<T extends Model>
29
30
  ) {
30
31
  // KVModel class is placeholder to receive methods from KeyValueAccessObject
31
32
  // through mixin
32
- this.kvModelClass = ds.createModel<typeof juggler.KeyValueModel>(
33
- '_kvModel',
34
- );
33
+ this.kvModelClass =
34
+ ds.createModel<typeof juggler.KeyValueModel>('_kvModel');
35
35
  }
36
36
 
37
37
  delete(key: string, options?: Options): Promise<void> {
@@ -111,8 +111,9 @@ export function ensurePromise<T>(p: legacy.PromiseOrVoid<T>): Promise<T> {
111
111
  export class DefaultCrudRepository<
112
112
  T extends Entity,
113
113
  ID,
114
- Relations extends object = {}
115
- > implements EntityCrudRepository<T, ID, Relations> {
114
+ Relations extends object = {},
115
+ > implements EntityCrudRepository<T, ID, Relations>
116
+ {
116
117
  modelClass: juggler.PersistedModelClass;
117
118
 
118
119
  public readonly inclusionResolvers: Map<
@@ -236,7 +237,7 @@ export class DefaultCrudRepository<
236
237
  protected _createHasManyRepositoryFactoryFor<
237
238
  Target extends Entity,
238
239
  TargetID,
239
- ForeignKeyType
240
+ ForeignKeyType,
240
241
  >(
241
242
  relationName: string,
242
243
  targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -278,7 +279,7 @@ export class DefaultCrudRepository<
278
279
  protected createHasManyRepositoryFactoryFor<
279
280
  Target extends Entity,
280
281
  TargetID,
281
- ForeignKeyType
282
+ ForeignKeyType,
282
283
  >(
283
284
  relationName: string,
284
285
  targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -325,7 +326,7 @@ export class DefaultCrudRepository<
325
326
  TargetID,
326
327
  Through extends Entity,
327
328
  ThroughID,
328
- ForeignKeyType
329
+ ForeignKeyType,
329
330
  >(
330
331
  relationName: string,
331
332
  targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -390,7 +391,7 @@ export class DefaultCrudRepository<
390
391
  protected _createHasOneRepositoryFactoryFor<
391
392
  Target extends Entity,
392
393
  TargetID,
393
- ForeignKeyType
394
+ ForeignKeyType,
394
395
  >(
395
396
  relationName: string,
396
397
  targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -410,7 +411,7 @@ export class DefaultCrudRepository<
410
411
  protected createHasOneRepositoryFactoryFor<
411
412
  Target extends Entity,
412
413
  TargetID,
413
- ForeignKeyType
414
+ ForeignKeyType,
414
415
  >(
415
416
  relationName: string,
416
417
  targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>,
@@ -770,10 +771,11 @@ export class DefaultCrudRepository<
770
771
  export class DefaultTransactionalRepository<
771
772
  T extends Entity,
772
773
  ID,
773
- Relations extends object = {}
774
+ Relations extends object = {},
774
775
  >
775
776
  extends DefaultCrudRepository<T, ID, Relations>
776
- implements TransactionalEntityRepository<T, ID, Relations> {
777
+ implements TransactionalEntityRepository<T, ID, Relations>
778
+ {
777
779
  async beginTransaction(
778
780
  options?: IsolationLevel | Options,
779
781
  ): Promise<Transaction> {
@@ -46,7 +46,7 @@ export interface ExecutableRepository<T extends Model> extends Repository<T> {
46
46
  export type TransactionalEntityRepository<
47
47
  T extends Entity,
48
48
  ID,
49
- Relations extends object = {}
49
+ Relations extends object = {},
50
50
  > = TransactionalRepository<T> & EntityCrudRepository<T, ID>;
51
51
  /**
52
52
  * Repository Interface for Repositories that support Transactions
@@ -69,7 +69,7 @@ export interface TransactionalRepository<T extends Entity>
69
69
  */
70
70
  export interface CrudRepository<
71
71
  T extends ValueObject | Entity,
72
- Relations extends object = {}
72
+ Relations extends object = {},
73
73
  > extends Repository<T> {
74
74
  /**
75
75
  * Create a new record
@@ -137,7 +137,7 @@ export interface EntityRepository<T extends Entity, ID>
137
137
  export interface EntityCrudRepository<
138
138
  T extends Entity,
139
139
  ID,
140
- Relations extends object = {}
140
+ Relations extends object = {},
141
141
  > extends EntityRepository<T, ID>,
142
142
  CrudRepository<T, Relations> {
143
143
  // entityClass should have type "typeof T", but that's not supported by TSC
@@ -254,7 +254,8 @@ export interface EntityCrudRepository<
254
254
  * ```
255
255
  */
256
256
  export class CrudRepositoryImpl<T extends Entity, ID>
257
- implements EntityCrudRepository<T, ID> {
257
+ implements EntityCrudRepository<T, ID>
258
+ {
258
259
  private connector: CrudConnector;
259
260
  public readonly inclusionResolvers: Map<
260
261
  string,
@@ -26,7 +26,7 @@ import {Class} from './common-types';
26
26
  */
27
27
  export type TypeResolver<
28
28
  Type extends Object,
29
- StaticMembers = Function
29
+ StaticMembers = Function,
30
30
  > = () => Class<Type> & StaticMembers;
31
31
 
32
32
  /**