@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.
- package/CHANGELOG.md +57 -0
- package/dist/common-types.d.ts +1 -0
- package/dist/common-types.js +2 -0
- package/dist/common-types.js.map +1 -1
- package/dist/connectors/index.js +4 -0
- package/dist/connectors/index.js.map +1 -1
- package/dist/decorators/metadata.js +1 -0
- package/dist/decorators/metadata.js.map +1 -1
- package/dist/decorators/model.decorator.d.ts +1 -1
- package/dist/decorators/model.decorator.js +1 -0
- package/dist/decorators/model.decorator.js.map +1 -1
- package/dist/decorators/repository.decorator.js +1 -0
- package/dist/decorators/repository.decorator.js.map +1 -1
- package/dist/define-model-class.js +1 -0
- package/dist/define-model-class.js.map +1 -1
- package/dist/define-repository-class.d.ts +119 -0
- package/dist/define-repository-class.js +98 -0
- package/dist/define-repository-class.js.map +1 -0
- package/dist/errors/entity-not-found.error.js +1 -0
- package/dist/errors/entity-not-found.error.js.map +1 -1
- package/dist/errors/invalid-relation.error.js +1 -0
- package/dist/errors/invalid-relation.error.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/keys.d.ts +34 -0
- package/dist/keys.js +44 -0
- package/dist/keys.js.map +1 -0
- package/dist/mixins/repository.mixin.d.ts +91 -9
- package/dist/mixins/repository.mixin.js +62 -20
- package/dist/mixins/repository.mixin.js.map +1 -1
- package/dist/model.d.ts +16 -1
- package/dist/model.js +61 -9
- package/dist/model.js.map +1 -1
- package/dist/query.js +1 -0
- package/dist/query.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to-accessor.js +1 -0
- package/dist/relations/belongs-to/belongs-to-accessor.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.decorator.js +1 -0
- package/dist/relations/belongs-to/belongs-to.decorator.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.helpers.js +1 -0
- package/dist/relations/belongs-to/belongs-to.helpers.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js +1 -0
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.repository.js +1 -0
- package/dist/relations/belongs-to/belongs-to.repository.js.map +1 -1
- package/dist/relations/has-many/has-many-repository.factory.js +1 -0
- package/dist/relations/has-many/has-many-repository.factory.js.map +1 -1
- package/dist/relations/has-many/has-many-through.helpers.d.ts +74 -0
- package/dist/relations/has-many/has-many-through.helpers.js +145 -0
- package/dist/relations/has-many/has-many-through.helpers.js.map +1 -0
- package/dist/relations/has-many/has-many.decorator.js +1 -0
- package/dist/relations/has-many/has-many.decorator.js.map +1 -1
- package/dist/relations/has-many/has-many.helpers.d.ts +9 -0
- package/dist/relations/has-many/has-many.helpers.js +33 -21
- package/dist/relations/has-many/has-many.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many.inclusion-resolver.js +1 -0
- package/dist/relations/has-many/has-many.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-many/has-many.repository.js +1 -0
- package/dist/relations/has-many/has-many.repository.js.map +1 -1
- package/dist/relations/has-one/has-one-repository.factory.js +1 -0
- package/dist/relations/has-one/has-one-repository.factory.js.map +1 -1
- package/dist/relations/has-one/has-one.decorator.js +1 -0
- package/dist/relations/has-one/has-one.decorator.js.map +1 -1
- package/dist/relations/has-one/has-one.helpers.js +1 -0
- package/dist/relations/has-one/has-one.helpers.js.map +1 -1
- package/dist/relations/has-one/has-one.inclusion-resolver.js +1 -0
- package/dist/relations/has-one/has-one.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-one/has-one.repository.js +1 -0
- package/dist/relations/has-one/has-one.repository.js.map +1 -1
- package/dist/relations/relation.decorator.js +1 -0
- package/dist/relations/relation.decorator.js.map +1 -1
- package/dist/relations/relation.helpers.js +1 -0
- package/dist/relations/relation.helpers.js.map +1 -1
- package/dist/relations/relation.types.d.ts +25 -27
- package/dist/relations/relation.types.js +2 -1
- package/dist/relations/relation.types.js.map +1 -1
- package/dist/repositories/constraint-utils.js +1 -0
- package/dist/repositories/constraint-utils.js.map +1 -1
- package/dist/repositories/index.js +1 -0
- package/dist/repositories/index.js.map +1 -1
- package/dist/repositories/kv.repository.bridge.js +1 -0
- package/dist/repositories/kv.repository.bridge.js.map +1 -1
- package/dist/repositories/legacy-juggler-bridge.d.ts +3 -3
- package/dist/repositories/legacy-juggler-bridge.js +8 -18
- package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
- package/dist/repositories/repository.js +1 -0
- package/dist/repositories/repository.js.map +1 -1
- package/dist/transaction.js +1 -0
- package/dist/transaction.js.map +1 -1
- package/dist/type-resolver.d.ts +4 -0
- package/dist/type-resolver.js +9 -0
- package/dist/type-resolver.js.map +1 -1
- package/dist/types/any.js +1 -0
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +1 -0
- package/dist/types/array.js.map +1 -1
- package/dist/types/boolean.js +1 -0
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/buffer.js +1 -0
- package/dist/types/buffer.js.map +1 -1
- package/dist/types/date.js +1 -0
- package/dist/types/date.js.map +1 -1
- package/dist/types/index.d.ts +11 -9
- package/dist/types/index.js +33 -17
- package/dist/types/index.js.map +1 -1
- package/dist/types/model.js +1 -0
- package/dist/types/model.js.map +1 -1
- package/dist/types/null.d.ts +12 -0
- package/dist/types/null.js +33 -0
- package/dist/types/null.js.map +1 -0
- package/dist/types/number.js +1 -0
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +1 -0
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +1 -0
- package/dist/types/string.js.map +1 -1
- package/dist/types/union.js +1 -0
- package/dist/types/union.js.map +1 -1
- package/package.json +13 -14
- package/src/common-types.ts +1 -0
- package/src/define-repository-class.ts +170 -0
- package/src/index.ts +2 -0
- package/src/keys.ts +40 -0
- package/src/mixins/repository.mixin.ts +120 -25
- package/src/model.ts +74 -11
- package/src/relations/has-many/has-many-through.helpers.ts +193 -0
- package/src/relations/has-many/has-many.helpers.ts +41 -27
- package/src/relations/relation.types.ts +24 -30
- package/src/repositories/legacy-juggler-bridge.ts +16 -24
- package/src/type-resolver.ts +8 -0
- package/src/types/index.ts +11 -8
- package/src/types/null.ts +35 -0
- package/index.d.ts +0 -6
- 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
|
-
*
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
*
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
through: {
|
|
92
|
+
through?: {
|
|
98
93
|
/**
|
|
99
94
|
* The through model of this relation.
|
|
100
95
|
*
|
|
101
|
-
* E.g. when a
|
|
102
|
-
* then
|
|
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.
|
|
102
|
+
* The foreign key of the source model defined in the through model, e.g. CategoryProductLink#categoryId
|
|
108
103
|
*/
|
|
109
|
-
keyFrom
|
|
104
|
+
keyFrom?: string;
|
|
110
105
|
|
|
111
106
|
/**
|
|
112
|
-
* The foreign key of the target model defined in the through model, e.g.
|
|
107
|
+
* The foreign key of the target model defined in the through model, e.g. CategoryProductLink#productId
|
|
113
108
|
*/
|
|
114
|
-
keyTo
|
|
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 {
|
|
20
|
-
|
|
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 -
|
|
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
|
|
package/src/type-resolver.ts
CHANGED
|
@@ -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
|
);
|
package/src/types/index.ts
CHANGED
|
@@ -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 {
|
|
27
|
-
import {
|
|
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
package/index.js
DELETED