@loopback/repository 2.2.1 → 2.5.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 (135) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/dist/common-types.d.ts +1 -0
  3. package/dist/common-types.js +2 -0
  4. package/dist/common-types.js.map +1 -1
  5. package/dist/connectors/index.js +4 -0
  6. package/dist/connectors/index.js.map +1 -1
  7. package/dist/decorators/metadata.js +1 -0
  8. package/dist/decorators/metadata.js.map +1 -1
  9. package/dist/decorators/model.decorator.d.ts +1 -1
  10. package/dist/decorators/model.decorator.js +1 -0
  11. package/dist/decorators/model.decorator.js.map +1 -1
  12. package/dist/decorators/repository.decorator.js +1 -0
  13. package/dist/decorators/repository.decorator.js.map +1 -1
  14. package/dist/define-model-class.js +1 -0
  15. package/dist/define-model-class.js.map +1 -1
  16. package/dist/define-repository-class.d.ts +119 -0
  17. package/dist/define-repository-class.js +98 -0
  18. package/dist/define-repository-class.js.map +1 -0
  19. package/dist/errors/entity-not-found.error.js +1 -0
  20. package/dist/errors/entity-not-found.error.js.map +1 -1
  21. package/dist/errors/invalid-relation.error.js +1 -0
  22. package/dist/errors/invalid-relation.error.js.map +1 -1
  23. package/dist/index.d.ts +2 -0
  24. package/dist/index.js +4 -0
  25. package/dist/index.js.map +1 -1
  26. package/dist/keys.d.ts +34 -0
  27. package/dist/keys.js +44 -0
  28. package/dist/keys.js.map +1 -0
  29. package/dist/mixins/repository.mixin.d.ts +91 -9
  30. package/dist/mixins/repository.mixin.js +62 -20
  31. package/dist/mixins/repository.mixin.js.map +1 -1
  32. package/dist/model.d.ts +16 -1
  33. package/dist/model.js +61 -9
  34. package/dist/model.js.map +1 -1
  35. package/dist/query.js +1 -0
  36. package/dist/query.js.map +1 -1
  37. package/dist/relations/belongs-to/belongs-to-accessor.js +1 -0
  38. package/dist/relations/belongs-to/belongs-to-accessor.js.map +1 -1
  39. package/dist/relations/belongs-to/belongs-to.decorator.js +1 -0
  40. package/dist/relations/belongs-to/belongs-to.decorator.js.map +1 -1
  41. package/dist/relations/belongs-to/belongs-to.helpers.js +1 -0
  42. package/dist/relations/belongs-to/belongs-to.helpers.js.map +1 -1
  43. package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js +1 -0
  44. package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js.map +1 -1
  45. package/dist/relations/belongs-to/belongs-to.repository.js +1 -0
  46. package/dist/relations/belongs-to/belongs-to.repository.js.map +1 -1
  47. package/dist/relations/has-many/has-many-repository.factory.js +1 -0
  48. package/dist/relations/has-many/has-many-repository.factory.js.map +1 -1
  49. package/dist/relations/has-many/has-many-through.helpers.d.ts +74 -0
  50. package/dist/relations/has-many/has-many-through.helpers.js +145 -0
  51. package/dist/relations/has-many/has-many-through.helpers.js.map +1 -0
  52. package/dist/relations/has-many/has-many.decorator.js +1 -0
  53. package/dist/relations/has-many/has-many.decorator.js.map +1 -1
  54. package/dist/relations/has-many/has-many.helpers.d.ts +9 -0
  55. package/dist/relations/has-many/has-many.helpers.js +33 -21
  56. package/dist/relations/has-many/has-many.helpers.js.map +1 -1
  57. package/dist/relations/has-many/has-many.inclusion-resolver.js +1 -0
  58. package/dist/relations/has-many/has-many.inclusion-resolver.js.map +1 -1
  59. package/dist/relations/has-many/has-many.repository.js +1 -0
  60. package/dist/relations/has-many/has-many.repository.js.map +1 -1
  61. package/dist/relations/has-one/has-one-repository.factory.js +1 -0
  62. package/dist/relations/has-one/has-one-repository.factory.js.map +1 -1
  63. package/dist/relations/has-one/has-one.decorator.js +1 -0
  64. package/dist/relations/has-one/has-one.decorator.js.map +1 -1
  65. package/dist/relations/has-one/has-one.helpers.js +1 -0
  66. package/dist/relations/has-one/has-one.helpers.js.map +1 -1
  67. package/dist/relations/has-one/has-one.inclusion-resolver.js +1 -0
  68. package/dist/relations/has-one/has-one.inclusion-resolver.js.map +1 -1
  69. package/dist/relations/has-one/has-one.repository.js +1 -0
  70. package/dist/relations/has-one/has-one.repository.js.map +1 -1
  71. package/dist/relations/relation.decorator.js +1 -0
  72. package/dist/relations/relation.decorator.js.map +1 -1
  73. package/dist/relations/relation.helpers.js +1 -0
  74. package/dist/relations/relation.helpers.js.map +1 -1
  75. package/dist/relations/relation.types.d.ts +25 -27
  76. package/dist/relations/relation.types.js +2 -1
  77. package/dist/relations/relation.types.js.map +1 -1
  78. package/dist/repositories/constraint-utils.js +1 -0
  79. package/dist/repositories/constraint-utils.js.map +1 -1
  80. package/dist/repositories/index.js +1 -0
  81. package/dist/repositories/index.js.map +1 -1
  82. package/dist/repositories/kv.repository.bridge.js +1 -0
  83. package/dist/repositories/kv.repository.bridge.js.map +1 -1
  84. package/dist/repositories/legacy-juggler-bridge.d.ts +3 -3
  85. package/dist/repositories/legacy-juggler-bridge.js +8 -18
  86. package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
  87. package/dist/repositories/repository.js +1 -0
  88. package/dist/repositories/repository.js.map +1 -1
  89. package/dist/transaction.js +1 -0
  90. package/dist/transaction.js.map +1 -1
  91. package/dist/type-resolver.d.ts +4 -0
  92. package/dist/type-resolver.js +9 -0
  93. package/dist/type-resolver.js.map +1 -1
  94. package/dist/types/any.js +1 -0
  95. package/dist/types/any.js.map +1 -1
  96. package/dist/types/array.js +1 -0
  97. package/dist/types/array.js.map +1 -1
  98. package/dist/types/boolean.js +1 -0
  99. package/dist/types/boolean.js.map +1 -1
  100. package/dist/types/buffer.js +1 -0
  101. package/dist/types/buffer.js.map +1 -1
  102. package/dist/types/date.js +1 -0
  103. package/dist/types/date.js.map +1 -1
  104. package/dist/types/index.d.ts +11 -9
  105. package/dist/types/index.js +33 -17
  106. package/dist/types/index.js.map +1 -1
  107. package/dist/types/model.js +1 -0
  108. package/dist/types/model.js.map +1 -1
  109. package/dist/types/null.d.ts +12 -0
  110. package/dist/types/null.js +33 -0
  111. package/dist/types/null.js.map +1 -0
  112. package/dist/types/number.js +1 -0
  113. package/dist/types/number.js.map +1 -1
  114. package/dist/types/object.js +1 -0
  115. package/dist/types/object.js.map +1 -1
  116. package/dist/types/string.js +1 -0
  117. package/dist/types/string.js.map +1 -1
  118. package/dist/types/union.js +1 -0
  119. package/dist/types/union.js.map +1 -1
  120. package/package.json +13 -14
  121. package/src/common-types.ts +1 -0
  122. package/src/define-repository-class.ts +170 -0
  123. package/src/index.ts +2 -0
  124. package/src/keys.ts +40 -0
  125. package/src/mixins/repository.mixin.ts +120 -25
  126. package/src/model.ts +74 -11
  127. package/src/relations/has-many/has-many-through.helpers.ts +193 -0
  128. package/src/relations/has-many/has-many.helpers.ts +41 -27
  129. package/src/relations/relation.types.ts +24 -30
  130. package/src/repositories/legacy-juggler-bridge.ts +16 -24
  131. package/src/type-resolver.ts +8 -0
  132. package/src/types/index.ts +11 -8
  133. package/src/types/null.ts +35 -0
  134. package/index.d.ts +0 -6
  135. package/index.js +0 -6
@@ -52,6 +52,10 @@ export interface RelationDefinitionBase {
52
52
  target: TypeResolver<Entity, typeof Entity>;
53
53
  }
54
54
 
55
+ /**
56
+ * HasManyDefinition defines one-to-many relations and also possible defines
57
+ * many-to-many relations with through models.
58
+ */
55
59
  export interface HasManyDefinition extends RelationDefinitionBase {
56
60
  type: RelationType.hasMany;
57
61
  targetsMany: true;
@@ -69,49 +73,40 @@ export interface HasManyDefinition extends RelationDefinitionBase {
69
73
  */
70
74
  keyTo?: string;
71
75
  keyFrom?: string;
72
- }
73
-
74
- /**
75
- * A `hasManyThrough` relation defines a many-to-many connection with another model.
76
- * This relation indicates that the declaring model can be matched with zero or more
77
- * instances of another model by proceeding through a third model.
78
- *
79
- * Warning: The hasManyThrough interface is experimental and is subject to change.
80
- * If backwards-incompatible changes are made, a new major version may not be
81
- * released.
82
- */
83
- export interface HasManyThroughDefinition extends RelationDefinitionBase {
84
- type: RelationType.hasMany;
85
- targetsMany: true;
86
76
 
87
77
  /**
88
- * The foreign key in the source model, e.g. Customer#id.
89
- */
90
- keyFrom: string;
91
-
92
- /**
93
- * The primary key of the target model, e.g Seller#id.
78
+ * Description of the through model of the hasManyThrough relation.
79
+ *
80
+ * A `hasManyThrough` relation defines a many-to-many connection with another model.
81
+ * This relation indicates that the declaring model can be matched with zero or more
82
+ * instances of another model by proceeding through a third model.
83
+ *
84
+ * E.g a Category has many Products, and a Product can have many Categories.
85
+ * CategoryProductLink can be the through model.
86
+ * Such a through model has information of foreign keys of the source model(Category) and the target model(Product).
87
+ *
88
+ * Warning: The hasManyThrough interface is experimental and is subject to change.
89
+ * If backwards-incompatible changes are made, a new major version may not be
90
+ * released.
94
91
  */
95
- keyTo: string;
96
-
97
- through: {
92
+ through?: {
98
93
  /**
99
94
  * The through model of this relation.
100
95
  *
101
- * E.g. when a Customer has many Order instances and a Seller has many Order instances,
102
- * then Order is through.
96
+ * E.g. when a Category has many CategoryProductLink instances and a Product has many CategoryProductLink instances,
97
+ * then CategoryProductLink is through.
103
98
  */
104
99
  model: TypeResolver<Entity, typeof Entity>;
105
100
 
106
101
  /**
107
- * The foreign key of the source model defined in the through model, e.g. Order#customerId
102
+ * The foreign key of the source model defined in the through model, e.g. CategoryProductLink#categoryId
108
103
  */
109
- keyFrom: string;
104
+ keyFrom?: string;
110
105
 
111
106
  /**
112
- * The foreign key of the target model defined in the through model, e.g. Order#sellerId
107
+ * The foreign key of the target model defined in the through model, e.g. CategoryProductLink#productId
113
108
  */
114
- keyTo: string;
109
+ keyTo?: string;
115
110
  };
116
111
  }
117
112
 
@@ -153,7 +148,6 @@ export interface HasOneDefinition extends RelationDefinitionBase {
153
148
  */
154
149
  export type RelationMetadata =
155
150
  | HasManyDefinition
156
- | HasManyThroughDefinition
157
151
  | BelongsToDefinition
158
152
  | HasOneDefinition
159
153
  // TODO(bajtos) add other relation types and remove RelationDefinitionBase once
@@ -11,13 +11,19 @@ import {
11
11
  Command,
12
12
  Count,
13
13
  DataObject,
14
+ DeepPartial,
14
15
  NamedParameters,
15
16
  Options,
16
17
  PositionalParameters,
17
18
  } from '../common-types';
18
19
  import {EntityNotFoundError} from '../errors';
19
- import {Entity, Model, PropertyType} from '../model';
20
- import {Filter, Inclusion, Where, FilterExcludingWhere} from '../query';
20
+ import {
21
+ Entity,
22
+ Model,
23
+ PropertyType,
24
+ rejectNavigationalPropertiesInData,
25
+ } from '../model';
26
+ import {Filter, FilterExcludingWhere, Inclusion, Where} from '../query';
21
27
  import {
22
28
  BelongsToAccessor,
23
29
  BelongsToDefinition,
@@ -109,8 +115,8 @@ export class DefaultCrudRepository<
109
115
 
110
116
  /**
111
117
  * Constructor of DefaultCrudRepository
112
- * @param entityClass - Legacy entity class
113
- * @param dataSource - Legacy data source
118
+ * @param entityClass - LoopBack 4 entity class
119
+ * @param dataSource - Legacy juggler data source
114
120
  */
115
121
  constructor(
116
122
  // entityClass should have type "typeof T", but that's not supported by TSC
@@ -458,6 +464,9 @@ export class DefaultCrudRepository<
458
464
  data: DataObject<T>,
459
465
  options?: Options,
460
466
  ): Promise<void> {
467
+ if (id === undefined) {
468
+ throw new Error('Invalid Argument: id cannot be undefined');
469
+ }
461
470
  const idProp = this.modelClass.definition.idName();
462
471
  const where = {} as Where<T>;
463
472
  (where as AnyObject)[idProp] = id;
@@ -587,27 +596,10 @@ export class DefaultCrudRepository<
587
596
  const data: AnyObject =
588
597
  typeof entity.toJSON === 'function' ? entity.toJSON() : {...entity};
589
598
  */
599
+ const data: DeepPartial<R> = new this.entityClass(entity);
600
+
601
+ rejectNavigationalPropertiesInData(this.entityClass, data);
590
602
 
591
- const data: AnyObject = new this.entityClass(entity);
592
-
593
- const def = this.entityClass.definition;
594
- const props = def.properties;
595
- for (const r in def.relations) {
596
- const relName = def.relations[r].name;
597
- if (relName in data) {
598
- let invalidNameMsg = '';
599
- if (relName in props) {
600
- invalidNameMsg =
601
- ` The error might be invoked by belongsTo relations, please make sure the relation name is not the same as` +
602
- ` the property name.`;
603
- }
604
- throw new Error(
605
- `Navigational properties are not allowed in model data (model "${this.entityClass.modelName}"` +
606
- ` property "${relName}"), please remove it.` +
607
- invalidNameMsg,
608
- );
609
- }
610
- }
611
603
  return data;
612
604
  }
613
605
 
@@ -51,6 +51,13 @@ export function isTypeResolver<T extends object>(
51
51
  return true;
52
52
  }
53
53
 
54
+ /**
55
+ * A boxed type for `null`
56
+ */
57
+ export function Null() {
58
+ return null;
59
+ }
60
+
54
61
  /**
55
62
  * Check if the provided function is a built-in type provided by JavaScript
56
63
  * and/or Node.js. E.g. `Number`, `Array`, `Buffer`, etc.
@@ -67,6 +74,7 @@ export function isBuiltinType(fn: Function): boolean {
67
74
  fn === Date ||
68
75
  fn === RegExp ||
69
76
  fn === Buffer ||
77
+ fn === Null ||
70
78
  // function as a type
71
79
  fn === Function
72
80
  );
@@ -15,17 +15,18 @@
15
15
  * - ArrayType: Array<T>
16
16
  * - UnionType: Union of types
17
17
  */
18
- import {Type} from './type';
19
- import {StringType} from './string';
20
- import {BooleanType} from './boolean';
21
- import {NumberType} from './number';
22
- import {DateType} from './date';
23
- import {BufferType} from './buffer';
24
18
  import {AnyType} from './any';
25
19
  import {ArrayType} from './array';
26
- import {UnionType} from './union';
27
- import {ObjectType} from './object';
20
+ import {BooleanType} from './boolean';
21
+ import {BufferType} from './buffer';
22
+ import {DateType} from './date';
28
23
  import {ModelType} from './model';
24
+ import {NullType} from './null';
25
+ import {NumberType} from './number';
26
+ import {ObjectType} from './object';
27
+ import {StringType} from './string';
28
+ import {Type} from './type';
29
+ import {UnionType} from './union';
29
30
 
30
31
  export {
31
32
  Type,
@@ -38,6 +39,7 @@ export {
38
39
  ArrayType,
39
40
  UnionType,
40
41
  ModelType,
42
+ NullType,
41
43
  ObjectType,
42
44
  };
43
45
 
@@ -47,3 +49,4 @@ export const NUMBER = new NumberType();
47
49
  export const DATE = new DateType();
48
50
  export const BUFFER = new BufferType();
49
51
  export const ANY = new AnyType();
52
+ export const NULL = new NullType();
@@ -0,0 +1,35 @@
1
+ // Copyright IBM Corp. 2017,2019. All Rights Reserved.
2
+ // Node module: @loopback/repository
3
+ // This file is licensed under the MIT License.
4
+ // License text available at https://opensource.org/licenses/MIT
5
+
6
+ import {Type} from './type';
7
+
8
+ /* eslint-disable @typescript-eslint/no-explicit-any */
9
+
10
+ /**
11
+ * Null type
12
+ */
13
+ export class NullType implements Type<null> {
14
+ readonly name = 'boolean';
15
+
16
+ isInstance(value: any) {
17
+ return value === null;
18
+ }
19
+
20
+ defaultValue() {
21
+ return null;
22
+ }
23
+
24
+ isCoercible(value: any): boolean {
25
+ return value == null;
26
+ }
27
+
28
+ coerce(value: any) {
29
+ return null;
30
+ }
31
+
32
+ serialize(value: boolean | null | undefined) {
33
+ return null;
34
+ }
35
+ }
package/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- // Copyright IBM Corp. 2017,2020. All Rights Reserved.
2
- // Node module: @loopback/repository
3
- // This file is licensed under the MIT License.
4
- // License text available at https://opensource.org/licenses/MIT
5
-
6
- export * from './dist';
package/index.js DELETED
@@ -1,6 +0,0 @@
1
- // Copyright IBM Corp. 2017,2020. All Rights Reserved.
2
- // Node module: @loopback/repository
3
- // This file is licensed under the MIT License.
4
- // License text available at https://opensource.org/licenses/MIT
5
-
6
- module.exports = require('./dist');