@loopback/repository 4.1.2 → 5.0.2
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/LICENSE +1 -1
- package/dist/connectors/connector.js +1 -1
- package/dist/connectors/connector.js.map +1 -1
- package/dist/connectors/crud.connector.js +1 -1
- package/dist/connectors/crud.connector.js.map +1 -1
- package/dist/connectors/index.js +1 -1
- package/dist/connectors/index.js.map +1 -1
- package/dist/connectors/kv.connector.js +1 -1
- package/dist/connectors/kv.connector.js.map +1 -1
- package/dist/datasource.js +1 -1
- package/dist/datasource.js.map +1 -1
- package/dist/decorators/index.js +1 -1
- package/dist/decorators/index.js.map +1 -1
- package/dist/decorators/metadata.js +1 -1
- package/dist/decorators/metadata.js.map +1 -1
- package/dist/decorators/model.decorator.js +1 -1
- package/dist/decorators/model.decorator.js.map +1 -1
- package/dist/decorators/repository.decorator.js +1 -1
- package/dist/decorators/repository.decorator.js.map +1 -1
- package/dist/define-model-class.js +1 -1
- package/dist/define-model-class.js.map +1 -1
- package/dist/define-repository-class.js +1 -1
- package/dist/define-repository-class.js.map +1 -1
- package/dist/errors/entity-not-found.error.js +1 -1
- package/dist/errors/entity-not-found.error.js.map +1 -1
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +2 -1
- package/dist/errors/index.js.map +1 -1
- package/dist/errors/invalid-polymorphism.error.d.ts +5 -0
- package/dist/errors/invalid-polymorphism.error.js +22 -0
- package/dist/errors/invalid-polymorphism.error.js.map +1 -0
- package/dist/errors/invalid-relation.error.d.ts +1 -1
- package/dist/errors/invalid-relation.error.js +1 -1
- package/dist/errors/invalid-relation.error.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mixins/index.js +1 -1
- package/dist/mixins/index.js.map +1 -1
- package/dist/mixins/repository.mixin.d.ts +49 -50
- package/dist/mixins/repository.mixin.js +1 -1
- package/dist/mixins/repository.mixin.js.map +1 -1
- package/dist/model.d.ts +7 -1
- package/dist/model.js +15 -1
- package/dist/model.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.accessor.d.ts +6 -2
- package/dist/relations/belongs-to/belongs-to.accessor.js +19 -6
- package/dist/relations/belongs-to/belongs-to.accessor.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.decorator.js +1 -1
- package/dist/relations/belongs-to/belongs-to.decorator.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.helpers.d.ts +3 -0
- package/dist/relations/belongs-to/belongs-to.helpers.js +33 -9
- package/dist/relations/belongs-to/belongs-to.helpers.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.d.ts +5 -2
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js +82 -9
- package/dist/relations/belongs-to/belongs-to.inclusion-resolver.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.repository.d.ts +28 -5
- package/dist/relations/belongs-to/belongs-to.repository.js +50 -11
- package/dist/relations/belongs-to/belongs-to.repository.js.map +1 -1
- package/dist/relations/belongs-to/index.d.ts +2 -2
- package/dist/relations/belongs-to/index.js +3 -3
- package/dist/relations/belongs-to/index.js.map +1 -1
- package/dist/relations/has-many/has-many-through.helpers.d.ts +3 -0
- package/dist/relations/has-many/has-many-through.helpers.js +26 -2
- package/dist/relations/has-many/has-many-through.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many-through.inclusion-resolver.d.ts +3 -1
- package/dist/relations/has-many/has-many-through.inclusion-resolver.js +93 -22
- package/dist/relations/has-many/has-many-through.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-many/has-many-through.repository-factory.d.ts +3 -1
- package/dist/relations/has-many/has-many-through.repository-factory.js +8 -2
- package/dist/relations/has-many/has-many-through.repository-factory.js.map +1 -1
- package/dist/relations/has-many/has-many-through.repository.d.ts +66 -11
- package/dist/relations/has-many/has-many-through.repository.js +212 -36
- package/dist/relations/has-many/has-many-through.repository.js.map +1 -1
- package/dist/relations/has-many/has-many.decorator.js +1 -1
- package/dist/relations/has-many/has-many.decorator.js.map +1 -1
- package/dist/relations/has-many/has-many.helpers.js +1 -1
- package/dist/relations/has-many/has-many.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many.inclusion-resolver.js +1 -1
- package/dist/relations/has-many/has-many.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-many/has-many.repository-factory.js +1 -1
- package/dist/relations/has-many/has-many.repository-factory.js.map +1 -1
- package/dist/relations/has-many/has-many.repository.js +1 -1
- package/dist/relations/has-many/has-many.repository.js.map +1 -1
- package/dist/relations/has-many/index.d.ts +2 -2
- package/dist/relations/has-many/index.js +3 -3
- package/dist/relations/has-many/index.js.map +1 -1
- package/dist/relations/has-one/has-one.decorator.js +1 -1
- package/dist/relations/has-one/has-one.decorator.js.map +1 -1
- package/dist/relations/has-one/has-one.helpers.d.ts +3 -0
- package/dist/relations/has-one/has-one.helpers.js +35 -9
- package/dist/relations/has-one/has-one.helpers.js.map +1 -1
- package/dist/relations/has-one/has-one.inclusion-resolver.d.ts +6 -3
- package/dist/relations/has-one/has-one.inclusion-resolver.js +83 -8
- package/dist/relations/has-one/has-one.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-one/has-one.repository-factory.d.ts +9 -3
- package/dist/relations/has-one/has-one.repository-factory.js +14 -4
- package/dist/relations/has-one/has-one.repository-factory.js.map +1 -1
- package/dist/relations/has-one/has-one.repository.d.ts +58 -11
- package/dist/relations/has-one/has-one.repository.js +119 -16
- package/dist/relations/has-one/has-one.repository.js.map +1 -1
- package/dist/relations/has-one/index.js +1 -1
- package/dist/relations/has-one/index.js.map +1 -1
- package/dist/relations/index.d.ts +2 -0
- package/dist/relations/index.js +3 -1
- package/dist/relations/index.js.map +1 -1
- package/dist/relations/references-many/index.d.ts +4 -0
- package/dist/relations/references-many/index.js +12 -0
- package/dist/relations/references-many/index.js.map +1 -0
- package/dist/relations/references-many/references-many.accessor.d.ts +17 -0
- package/dist/relations/references-many/references-many.accessor.js +40 -0
- package/dist/relations/references-many/references-many.accessor.js.map +1 -0
- package/dist/relations/references-many/references-many.decorator.d.ts +11 -0
- package/dist/relations/references-many/references-many.decorator.js +73 -0
- package/dist/relations/references-many/references-many.decorator.js.map +1 -0
- package/dist/relations/references-many/references-many.helpers.d.ts +17 -0
- package/dist/relations/references-many/references-many.helpers.js +63 -0
- package/dist/relations/references-many/references-many.helpers.js.map +1 -0
- package/dist/relations/references-many/references-many.inclusion-resolver.d.ts +14 -0
- package/dist/relations/references-many/references-many.inclusion-resolver.js +42 -0
- package/dist/relations/references-many/references-many.inclusion-resolver.js.map +1 -0
- package/dist/relations/references-many/references-many.repository.d.ts +28 -0
- package/dist/relations/references-many/references-many.repository.js +33 -0
- package/dist/relations/references-many/references-many.repository.js.map +1 -0
- package/dist/relations/relation.decorator.d.ts +0 -6
- package/dist/relations/relation.decorator.js +2 -14
- package/dist/relations/relation.decorator.js.map +1 -1
- package/dist/relations/relation.filter.solver.d.ts +2 -0
- package/dist/relations/relation.filter.solver.js +57 -0
- package/dist/relations/relation.filter.solver.js.map +1 -0
- package/dist/relations/relation.helpers.js +18 -3
- package/dist/relations/relation.helpers.js.map +1 -1
- package/dist/relations/relation.types.d.ts +52 -1
- package/dist/relations/relation.types.js +1 -1
- package/dist/relations/relation.types.js.map +1 -1
- package/dist/repositories/constraint-utils.js +1 -1
- package/dist/repositories/constraint-utils.js.map +1 -1
- package/dist/repositories/index.d.ts +2 -2
- package/dist/repositories/index.js +3 -3
- package/dist/repositories/index.js.map +1 -1
- package/dist/repositories/kv.repository.bridge.js +1 -1
- package/dist/repositories/kv.repository.bridge.js.map +1 -1
- package/dist/repositories/kv.repository.d.ts +2 -2
- package/dist/repositories/kv.repository.js +1 -1
- package/dist/repositories/kv.repository.js.map +1 -1
- package/dist/repositories/legacy-juggler-bridge.d.ts +36 -9
- package/dist/repositories/legacy-juggler-bridge.js +38 -16
- package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
- package/dist/repositories/repository.js +1 -1
- package/dist/repositories/repository.js.map +1 -1
- package/dist/type-resolver.js +1 -1
- package/dist/type-resolver.js.map +1 -1
- package/dist/types/any.js +1 -1
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +1 -1
- package/dist/types/array.js.map +1 -1
- package/dist/types/boolean.js +1 -1
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/buffer.js +1 -1
- package/dist/types/buffer.js.map +1 -1
- package/dist/types/date.js +1 -1
- package/dist/types/date.js.map +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/model.js +1 -1
- package/dist/types/model.js.map +1 -1
- package/dist/types/null.js +1 -1
- package/dist/types/null.js.map +1 -1
- package/dist/types/number.js +1 -1
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +1 -1
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +1 -1
- package/dist/types/string.js.map +1 -1
- package/dist/types/type.js +1 -1
- package/dist/types/type.js.map +1 -1
- package/dist/types/union.js +1 -1
- package/dist/types/union.js.map +1 -1
- package/package.json +15 -15
- package/src/connectors/connector.ts +1 -1
- package/src/connectors/crud.connector.ts +1 -1
- package/src/connectors/index.ts +1 -1
- package/src/connectors/kv.connector.ts +1 -1
- package/src/datasource.ts +1 -1
- package/src/decorators/index.ts +1 -1
- package/src/decorators/metadata.ts +1 -1
- package/src/decorators/model.decorator.ts +1 -1
- package/src/decorators/repository.decorator.ts +1 -1
- package/src/define-model-class.ts +1 -1
- package/src/define-repository-class.ts +1 -1
- package/src/errors/entity-not-found.error.ts +1 -1
- package/src/errors/index.ts +2 -1
- package/src/errors/invalid-polymorphism.error.ts +28 -0
- package/src/errors/invalid-relation.error.ts +2 -2
- package/src/index.ts +1 -1
- package/src/mixins/index.ts +1 -1
- package/src/mixins/repository.mixin.ts +1 -6
- package/src/model.ts +20 -1
- package/src/relations/belongs-to/belongs-to.accessor.ts +36 -7
- package/src/relations/belongs-to/belongs-to.decorator.ts +2 -2
- package/src/relations/belongs-to/belongs-to.helpers.ts +37 -10
- package/src/relations/belongs-to/belongs-to.inclusion-resolver.ts +109 -18
- package/src/relations/belongs-to/belongs-to.repository.ts +78 -18
- package/src/relations/belongs-to/index.ts +3 -3
- package/src/relations/has-many/has-many-through.helpers.ts +28 -2
- package/src/relations/has-many/has-many-through.inclusion-resolver.ts +118 -27
- package/src/relations/has-many/has-many-through.repository-factory.ts +22 -4
- package/src/relations/has-many/has-many-through.repository.ts +344 -77
- package/src/relations/has-many/has-many.decorator.ts +1 -1
- package/src/relations/has-many/has-many.helpers.ts +1 -1
- package/src/relations/has-many/has-many.inclusion-resolver.ts +1 -1
- package/src/relations/has-many/has-many.repository-factory.ts +1 -1
- package/src/relations/has-many/has-many.repository.ts +1 -1
- package/src/relations/has-many/index.ts +3 -3
- package/src/relations/has-one/has-one.decorator.ts +1 -1
- package/src/relations/has-one/has-one.helpers.ts +41 -14
- package/src/relations/has-one/has-one.inclusion-resolver.ts +110 -16
- package/src/relations/has-one/has-one.repository-factory.ts +35 -7
- package/src/relations/has-one/has-one.repository.ts +189 -36
- package/src/relations/has-one/index.ts +1 -1
- package/src/relations/index.ts +3 -1
- package/src/relations/references-many/index.ts +9 -0
- package/src/relations/references-many/references-many.accessor.ts +76 -0
- package/src/relations/references-many/references-many.decorator.ts +100 -0
- package/src/relations/references-many/references-many.helpers.ts +82 -0
- package/src/relations/references-many/references-many.inclusion-resolver.ts +80 -0
- package/src/relations/references-many/references-many.repository.ts +55 -0
- package/src/relations/relation.decorator.ts +1 -13
- package/src/relations/relation.filter.solver.ts +56 -0
- package/src/relations/relation.helpers.ts +18 -3
- package/src/relations/relation.types.ts +52 -1
- package/src/repositories/constraint-utils.ts +1 -1
- package/src/repositories/index.ts +3 -3
- package/src/repositories/kv.repository.bridge.ts +1 -1
- package/src/repositories/kv.repository.ts +3 -3
- package/src/repositories/legacy-juggler-bridge.ts +80 -16
- package/src/repositories/repository.ts +1 -1
- package/src/type-resolver.ts +1 -1
- package/src/types/any.ts +1 -1
- package/src/types/array.ts +1 -1
- package/src/types/boolean.ts +1 -1
- package/src/types/buffer.ts +1 -1
- package/src/types/date.ts +1 -1
- package/src/types/index.ts +1 -1
- package/src/types/model.ts +1 -1
- package/src/types/null.ts +1 -1
- package/src/types/number.ts +1 -1
- package/src/types/object.ts +1 -1
- package/src/types/string.ts +1 -1
- package/src/types/type.ts +1 -1
- package/src/types/union.ts +1 -1
package/dist/types/type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/types/type.ts"],"names":[],"mappings":";AAAA,
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../src/types/type.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE"}
|
package/dist/types/union.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
|
|
2
|
+
// Copyright IBM Corp. and LoopBack contributors 2017,2019. All Rights Reserved.
|
|
3
3
|
// Node module: @loopback/repository
|
|
4
4
|
// This file is licensed under the MIT License.
|
|
5
5
|
// License text available at https://opensource.org/licenses/MIT
|
package/dist/types/union.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/types/union.ts"],"names":[],"mappings":";AAAA,
|
|
1
|
+
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/types/union.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,wDAAwB;AAGxB,uDAAuD;AAEvD;;GAEG;AACH,MAAa,SAAS;IACpB,YAAmB,SAAsB;QAAtB,cAAS,GAAT,SAAS,CAAa;QAEhC,SAAI,GAAG,OAAO,CAAC;IAFoB,CAAC;IAI7C,UAAU,CAAC,KAAU;QACnB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,WAAW,CAAC,KAAU;QACpB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,YAAY;QACV,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC;IAC1C,CAAC;IAED,MAAM,CAAC,KAAU;QACf,uBAAuB;QACvB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,SAAS,EAAE;YACjC,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;gBAC1B,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aAC3B;SACF;QACD,gBAAgB;QAChB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,SAAS,EAAE;YACjC,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE;gBAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aAC3B;SACF;QACD,MAAM,GAAG,GAAG,cAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC5D,MAAM,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,SAAS,CAAC,KAAU;QAClB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,SAAS,EAAE;YACjC,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;gBAC1B,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;aAC9B;SACF;IACH,CAAC;CACF;AAzCD,8BAyCC"}
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loopback/repository",
|
|
3
3
|
"description": "Define and implement a common set of interfaces for interacting with databases",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"author": "IBM Corp.",
|
|
9
|
-
"copyright.owner": "IBM Corp.",
|
|
8
|
+
"author": "IBM Corp. and LoopBack contributors",
|
|
9
|
+
"copyright.owner": "IBM Corp. and LoopBack contributors",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
12
|
"url": "https://github.com/loopbackio/loopback-next.git",
|
|
13
13
|
"directory": "packages/repository"
|
|
14
14
|
},
|
|
15
15
|
"engines": {
|
|
16
|
-
"node": "
|
|
16
|
+
"node": "14 || 16 || 17 || 18"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
|
|
@@ -33,26 +33,26 @@
|
|
|
33
33
|
"!*/__tests__"
|
|
34
34
|
],
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@loopback/core": "^
|
|
36
|
+
"@loopback/core": "^4.0.2"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@loopback/filter": "^
|
|
39
|
+
"@loopback/filter": "^3.0.2",
|
|
40
40
|
"@types/debug": "^4.1.7",
|
|
41
41
|
"debug": "^4.3.4",
|
|
42
42
|
"lodash": "^4.17.21",
|
|
43
43
|
"loopback-datasource-juggler": "^4.27.1",
|
|
44
|
-
"tslib": "^2.
|
|
44
|
+
"tslib": "^2.4.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@loopback/build": "^
|
|
48
|
-
"@loopback/core": "^
|
|
49
|
-
"@loopback/eslint-config": "^
|
|
50
|
-
"@loopback/testlab": "^
|
|
47
|
+
"@loopback/build": "^9.0.2",
|
|
48
|
+
"@loopback/core": "^4.0.2",
|
|
49
|
+
"@loopback/eslint-config": "^13.0.2",
|
|
50
|
+
"@loopback/testlab": "^5.0.2",
|
|
51
51
|
"@types/bson": "^4.0.5",
|
|
52
52
|
"@types/json-schema": "^7.0.11",
|
|
53
|
-
"@types/lodash": "^4.14.
|
|
54
|
-
"@types/node": "^
|
|
55
|
-
"bson": "4.6.
|
|
53
|
+
"@types/lodash": "^4.14.182",
|
|
54
|
+
"@types/node": "^14.18.21",
|
|
55
|
+
"bson": "4.6.4"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "d2eddfc1319810f4c0126e613c24499b5685f7e4"
|
|
58
58
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
package/src/connectors/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
package/src/datasource.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2017,2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
package/src/decorators/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2017,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
package/src/errors/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
export * from './entity-not-found.error';
|
|
7
|
+
export * from './invalid-polymorphism.error';
|
|
7
8
|
export * from './invalid-relation.error';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export class InvalidPolymorphismError<Props extends object = {}> extends Error {
|
|
2
|
+
code: string;
|
|
3
|
+
|
|
4
|
+
constructor(
|
|
5
|
+
typeName: string,
|
|
6
|
+
discriminator?: string,
|
|
7
|
+
extraProperties?: Props,
|
|
8
|
+
) {
|
|
9
|
+
const message = discriminator
|
|
10
|
+
? `Invalid class name ${typeName} by discriminator ${discriminator}. Please check polymorphic types and the discriminator.`
|
|
11
|
+
: `Invalid class name ${typeName}. Please check polymorphic types and the discriminator.`;
|
|
12
|
+
super(message);
|
|
13
|
+
|
|
14
|
+
Error.captureStackTrace(this, this.constructor);
|
|
15
|
+
|
|
16
|
+
this.code = 'INVALID_POLYMORPHISM';
|
|
17
|
+
|
|
18
|
+
Object.assign(this, extraProperties);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isInvalidPolymorphismError(
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
e: any,
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
): e is InvalidPolymorphismError<any> {
|
|
27
|
+
return e instanceof InvalidPolymorphismError;
|
|
28
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
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 {
|
|
6
|
+
import {RelationMetadata, RelationType} from '../relations';
|
|
7
7
|
|
|
8
8
|
export class InvalidRelationError<Props extends object = {}> extends Error {
|
|
9
9
|
code: string;
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2017,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
package/src/mixins/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -23,11 +23,6 @@ import {juggler, Repository} from '../repositories';
|
|
|
23
23
|
|
|
24
24
|
const debug = debugFactory('loopback:repository:mixin');
|
|
25
25
|
|
|
26
|
-
// FIXME(rfeng): Workaround for https://github.com/microsoft/rushstack/pull/1867
|
|
27
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
28
|
-
import * as loopbackContext from '@loopback/core';
|
|
29
|
-
import * as loopbackCore from '@loopback/core';
|
|
30
|
-
|
|
31
26
|
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
32
27
|
|
|
33
28
|
/**
|
package/src/model.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2017,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
HasManyDefinition,
|
|
10
10
|
HasOneDefinition,
|
|
11
11
|
JsonSchema,
|
|
12
|
+
ReferencesManyDefinition,
|
|
12
13
|
RelationMetadata,
|
|
13
14
|
RelationType,
|
|
14
15
|
} from './index';
|
|
@@ -240,6 +241,24 @@ export class ModelDefinition {
|
|
|
240
241
|
return this.addRelation(meta);
|
|
241
242
|
}
|
|
242
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Define a new referencesMany relation.
|
|
246
|
+
* @param name - The name of the referencesMany relation.
|
|
247
|
+
* @param definition - The definition of the referencesMany relation.
|
|
248
|
+
*/
|
|
249
|
+
referencesMany(
|
|
250
|
+
name: string,
|
|
251
|
+
definition: Omit<ReferencesManyDefinition, 'name' | 'type' | 'targetsMany'>,
|
|
252
|
+
): this {
|
|
253
|
+
const meta: ReferencesManyDefinition = {
|
|
254
|
+
...definition,
|
|
255
|
+
name,
|
|
256
|
+
type: RelationType.referencesMany,
|
|
257
|
+
targetsMany: true,
|
|
258
|
+
};
|
|
259
|
+
return this.addRelation(meta);
|
|
260
|
+
}
|
|
261
|
+
|
|
243
262
|
/**
|
|
244
263
|
* Get an array of names of ID properties, which are specified in
|
|
245
264
|
* the model settings or properties with `id` attribute.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -22,7 +22,7 @@ export interface BelongsToAccessor<Target extends Entity, SourceId> {
|
|
|
22
22
|
/**
|
|
23
23
|
* Invoke the function to obtain HasManyRepository.
|
|
24
24
|
*/
|
|
25
|
-
(sourceId: SourceId): Promise<Target>;
|
|
25
|
+
(sourceId: SourceId, polymorphicTypes?: string | string[]): Promise<Target>;
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Use `resolver` property to obtain an InclusionResolver for this relation.
|
|
@@ -32,6 +32,8 @@ export interface BelongsToAccessor<Target extends Entity, SourceId> {
|
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Enforces a BelongsTo constraint on a repository
|
|
35
|
+
* If the target model is polymorphic, i.e. stored within different repositories,
|
|
36
|
+
* supply the targetRepositoryGetter with a dictionary in the form of {[typeName: string]: repositoryGetter}
|
|
35
37
|
*/
|
|
36
38
|
export function createBelongsToAccessor<
|
|
37
39
|
Target extends Entity,
|
|
@@ -40,13 +42,35 @@ export function createBelongsToAccessor<
|
|
|
40
42
|
SourceId,
|
|
41
43
|
>(
|
|
42
44
|
belongsToMetadata: BelongsToDefinition,
|
|
43
|
-
|
|
45
|
+
targetRepositoryGetter:
|
|
46
|
+
| Getter<EntityCrudRepository<Target, TargetId>>
|
|
47
|
+
| {
|
|
48
|
+
[repoType: string]: Getter<EntityCrudRepository<Target, TargetId>>;
|
|
49
|
+
},
|
|
44
50
|
sourceRepository: EntityCrudRepository<Source, SourceId>,
|
|
45
51
|
): BelongsToAccessor<Target, SourceId> {
|
|
46
52
|
const meta = resolveBelongsToMetadata(belongsToMetadata);
|
|
53
|
+
// resolve the repositoryGetter into a dictionary
|
|
54
|
+
if (typeof targetRepositoryGetter === 'function') {
|
|
55
|
+
targetRepositoryGetter = {
|
|
56
|
+
[meta.target().name]: targetRepositoryGetter as Getter<
|
|
57
|
+
EntityCrudRepository<Target, TargetId>
|
|
58
|
+
>,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
47
61
|
debug('Resolved BelongsTo relation metadata: %o', meta);
|
|
48
62
|
const result: BelongsToAccessor<Target, SourceId> =
|
|
49
|
-
async function getTargetInstanceOfBelongsTo(
|
|
63
|
+
async function getTargetInstanceOfBelongsTo(
|
|
64
|
+
sourceId: SourceId,
|
|
65
|
+
polymorphicTypes?: string | string[],
|
|
66
|
+
) {
|
|
67
|
+
if (meta.polymorphic !== false) {
|
|
68
|
+
if (!polymorphicTypes || polymorphicTypes.length === 0) {
|
|
69
|
+
console.warn(
|
|
70
|
+
'It is highly recommended to specify the polymorphicTypes param when using polymorphic types.',
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
50
74
|
const foreignKey = meta.keyFrom;
|
|
51
75
|
const primaryKey = meta.keyTo;
|
|
52
76
|
const sourceModel = await sourceRepository.findById(sourceId);
|
|
@@ -60,15 +84,20 @@ export function createBelongsToAccessor<
|
|
|
60
84
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
85
|
const constraint: any = {[primaryKey]: foreignKeyValue};
|
|
62
86
|
const constrainedRepo = new DefaultBelongsToRepository(
|
|
63
|
-
|
|
87
|
+
targetRepositoryGetter as {
|
|
88
|
+
[repoType: string]: Getter<EntityCrudRepository<Target, TargetId>>;
|
|
89
|
+
},
|
|
64
90
|
constraint as DataObject<Target>,
|
|
91
|
+
belongsToMetadata.target,
|
|
65
92
|
);
|
|
66
|
-
return constrainedRepo.get();
|
|
93
|
+
return constrainedRepo.get({polymorphicType: polymorphicTypes});
|
|
67
94
|
};
|
|
68
95
|
|
|
69
96
|
result.inclusionResolver = createBelongsToInclusionResolver(
|
|
70
97
|
meta,
|
|
71
|
-
|
|
98
|
+
targetRepositoryGetter as {
|
|
99
|
+
[repoType: string]: Getter<EntityCrudRepository<Target, TargetId>>;
|
|
100
|
+
},
|
|
72
101
|
);
|
|
73
102
|
return result;
|
|
74
103
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import {DecoratorFactory, MetadataInspector} from '@loopback/core';
|
|
7
7
|
import {property} from '../../decorators';
|
|
8
|
-
import {relation} from '../relation.decorator';
|
|
9
8
|
import {Entity, EntityResolver, PropertyDefinition} from '../../model';
|
|
9
|
+
import {relation} from '../relation.decorator';
|
|
10
10
|
import {BelongsToDefinition, RelationType} from '../relation.types';
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2019. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
@@ -18,6 +18,7 @@ const debug = debugFactory('loopback:repository:relations:belongs-to:helpers');
|
|
|
18
18
|
export type BelongsToResolvedDefinition = BelongsToDefinition & {
|
|
19
19
|
keyFrom: string;
|
|
20
20
|
keyTo: string;
|
|
21
|
+
polymorphic: false | {discriminator: string};
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -27,7 +28,9 @@ export type BelongsToResolvedDefinition = BelongsToDefinition & {
|
|
|
27
28
|
* @param relationMeta - belongsTo metadata to resolve
|
|
28
29
|
* @internal
|
|
29
30
|
*/
|
|
30
|
-
export function resolveBelongsToMetadata(
|
|
31
|
+
export function resolveBelongsToMetadata(
|
|
32
|
+
relationMeta: BelongsToDefinition,
|
|
33
|
+
): BelongsToResolvedDefinition {
|
|
31
34
|
if ((relationMeta.type as RelationType) !== RelationType.belongsTo) {
|
|
32
35
|
const reason = 'relation type must be BelongsTo';
|
|
33
36
|
throw new InvalidRelationError(reason, relationMeta);
|
|
@@ -61,18 +64,42 @@ export function resolveBelongsToMetadata(relationMeta: BelongsToDefinition) {
|
|
|
61
64
|
const targetProperties = targetModel.definition.properties;
|
|
62
65
|
debug('relation metadata from %o: %o', targetName, targetProperties);
|
|
63
66
|
|
|
67
|
+
let keyTo;
|
|
64
68
|
if (relationMeta.keyTo && targetProperties[relationMeta.keyTo]) {
|
|
65
69
|
// The explicit cast is needed because of a limitation of type inference
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
keyTo = relationMeta.keyTo;
|
|
71
|
+
} else {
|
|
72
|
+
keyTo = targetModel.definition.idProperties()[0];
|
|
73
|
+
if (!keyTo) {
|
|
74
|
+
const reason = `${targetName} does not have any primary key (id property)`;
|
|
75
|
+
throw new InvalidRelationError(reason, relationMeta);
|
|
76
|
+
}
|
|
69
77
|
}
|
|
70
78
|
|
|
71
|
-
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
let polymorphic: false | {discriminator: string};
|
|
80
|
+
if (
|
|
81
|
+
relationMeta.polymorphic === undefined ||
|
|
82
|
+
relationMeta.polymorphic === false ||
|
|
83
|
+
!relationMeta.polymorphic
|
|
84
|
+
) {
|
|
85
|
+
const polymorphicFalse = false as const;
|
|
86
|
+
polymorphic = polymorphicFalse;
|
|
87
|
+
} else {
|
|
88
|
+
if (relationMeta.polymorphic === true) {
|
|
89
|
+
const polymorphicObject: {discriminator: string} = {
|
|
90
|
+
discriminator: camelCase(relationMeta.target().name + '_type'),
|
|
91
|
+
};
|
|
92
|
+
polymorphic = polymorphicObject;
|
|
93
|
+
} else {
|
|
94
|
+
const polymorphicObject: {discriminator: string} =
|
|
95
|
+
relationMeta.polymorphic as {discriminator: string};
|
|
96
|
+
polymorphic = polymorphicObject;
|
|
97
|
+
}
|
|
75
98
|
}
|
|
76
99
|
|
|
77
|
-
return Object.assign(relationMeta, {
|
|
100
|
+
return Object.assign(relationMeta, {
|
|
101
|
+
keyFrom,
|
|
102
|
+
keyTo: keyTo,
|
|
103
|
+
polymorphic: polymorphic,
|
|
104
|
+
});
|
|
78
105
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. and LoopBack contributors 2019,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/repository
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import {Filter, InclusionFilter} from '@loopback/filter';
|
|
7
|
+
import {includeFieldIfNot, InvalidPolymorphismError} from '../../';
|
|
7
8
|
import {AnyObject, Options} from '../../common-types';
|
|
8
9
|
import {Entity} from '../../model';
|
|
9
10
|
import {EntityCrudRepository} from '../../repositories';
|
|
@@ -28,7 +29,8 @@ import {resolveBelongsToMetadata} from './belongs-to.helpers';
|
|
|
28
29
|
* Notice: scope field for inclusion is not supported yet
|
|
29
30
|
*
|
|
30
31
|
* @param meta - resolved BelongsToMetadata
|
|
31
|
-
* @param
|
|
32
|
+
* @param getTargetRepoDict - dictionary of target model type - target repository
|
|
33
|
+
* i.e where related instances for different types are
|
|
32
34
|
*/
|
|
33
35
|
export function createBelongsToInclusionResolver<
|
|
34
36
|
Target extends Entity,
|
|
@@ -36,36 +38,125 @@ export function createBelongsToInclusionResolver<
|
|
|
36
38
|
TargetRelations extends object,
|
|
37
39
|
>(
|
|
38
40
|
meta: BelongsToDefinition,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
getTargetRepoDict: {
|
|
42
|
+
[repoType: string]: Getter<
|
|
43
|
+
EntityCrudRepository<Target, TargetID, TargetRelations>
|
|
44
|
+
>;
|
|
45
|
+
},
|
|
42
46
|
): InclusionResolver<Entity, Target> {
|
|
43
47
|
const relationMeta = resolveBelongsToMetadata(meta);
|
|
44
48
|
|
|
45
|
-
return async function
|
|
49
|
+
return async function fetchBelongsToModel(
|
|
46
50
|
entities: Entity[],
|
|
47
51
|
inclusion: InclusionFilter,
|
|
48
52
|
options?: Options,
|
|
49
|
-
): Promise<((Target &
|
|
53
|
+
): Promise<((Target & object) | undefined)[]> {
|
|
50
54
|
if (!entities.length) return [];
|
|
51
55
|
|
|
56
|
+
// Source ids are grouped by their target polymorphic types
|
|
57
|
+
// Each type search for target instances and then merge together in a merge-sort-like manner
|
|
58
|
+
|
|
52
59
|
const sourceKey = relationMeta.keyFrom;
|
|
53
|
-
const sourceIds = entities.map(e => (e as AnyObject)[sourceKey]);
|
|
54
60
|
const targetKey = relationMeta.keyTo as StringKeyOf<Target>;
|
|
55
|
-
const
|
|
61
|
+
const targetDiscriminator: keyof Entity | undefined =
|
|
62
|
+
relationMeta.polymorphic
|
|
63
|
+
? (relationMeta.polymorphic.discriminator as keyof Entity)
|
|
64
|
+
: undefined;
|
|
56
65
|
|
|
57
66
|
const scope =
|
|
58
67
|
typeof inclusion === 'string' ? {} : (inclusion.scope as Filter<Target>);
|
|
59
68
|
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
69
|
+
// sourceIds in {targetType -> sourceId}
|
|
70
|
+
const sourceIdsCategorized: {
|
|
71
|
+
[concreteItemType: string]: Target[StringKeyOf<Target>][];
|
|
72
|
+
} = {};
|
|
73
|
+
if (targetDiscriminator) {
|
|
74
|
+
entities.forEach((value, index, allEntites) => {
|
|
75
|
+
const concreteType = String(value[targetDiscriminator]);
|
|
76
|
+
if (!getTargetRepoDict[concreteType]) {
|
|
77
|
+
throw new InvalidPolymorphismError(concreteType, targetDiscriminator);
|
|
78
|
+
}
|
|
79
|
+
if (!sourceIdsCategorized[concreteType]) {
|
|
80
|
+
sourceIdsCategorized[concreteType] = [];
|
|
81
|
+
}
|
|
82
|
+
sourceIdsCategorized[concreteType].push(
|
|
83
|
+
(value as AnyObject)[sourceKey],
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
} else {
|
|
87
|
+
const concreteType = relationMeta.target().name;
|
|
88
|
+
if (!getTargetRepoDict[concreteType]) {
|
|
89
|
+
throw new InvalidPolymorphismError(concreteType);
|
|
90
|
+
}
|
|
91
|
+
entities.forEach((value, index, allEntites) => {
|
|
92
|
+
if (!sourceIdsCategorized[concreteType]) {
|
|
93
|
+
sourceIdsCategorized[concreteType] = [];
|
|
94
|
+
}
|
|
95
|
+
sourceIdsCategorized[concreteType].push(
|
|
96
|
+
(value as AnyObject)[sourceKey],
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Ensure targetKey is included otherwise flatten function cannot work
|
|
102
|
+
const changedTargetKeyField = includeFieldIfNot(scope?.fields, targetKey);
|
|
103
|
+
let needToRemoveTargetKeyFieldLater = false;
|
|
104
|
+
if (changedTargetKeyField !== false) {
|
|
105
|
+
scope.fields = changedTargetKeyField;
|
|
106
|
+
needToRemoveTargetKeyFieldLater = true;
|
|
107
|
+
}
|
|
108
|
+
// Each sourceIds array with same target type extract target instances
|
|
109
|
+
const targetCategorized: {
|
|
110
|
+
[concreteItemType: string]: ((Target & TargetRelations) | undefined)[];
|
|
111
|
+
} = {};
|
|
112
|
+
for (const k of Object.keys(sourceIdsCategorized)) {
|
|
113
|
+
const targetRepo = await getTargetRepoDict[k]();
|
|
114
|
+
const targetsFound = await findByForeignKeys(
|
|
115
|
+
targetRepo,
|
|
116
|
+
targetKey,
|
|
117
|
+
deduplicate(sourceIdsCategorized[k]).filter(e => e),
|
|
118
|
+
scope,
|
|
119
|
+
options,
|
|
120
|
+
);
|
|
121
|
+
targetCategorized[k] = flattenTargetsOfOneToOneRelation(
|
|
122
|
+
sourceIdsCategorized[k],
|
|
123
|
+
targetsFound,
|
|
124
|
+
targetKey,
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// Remove targetKey if should be excluded but included above
|
|
128
|
+
if (needToRemoveTargetKeyFieldLater) {
|
|
129
|
+
targetCategorized[k] = targetCategorized[k].map(e => {
|
|
130
|
+
if (e) {
|
|
131
|
+
delete e[targetKey];
|
|
132
|
+
}
|
|
133
|
+
return e;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
68
137
|
|
|
69
|
-
|
|
138
|
+
// Merge
|
|
139
|
+
// Why the order is correct:
|
|
140
|
+
// e.g. target model 1 = a, target model 2 = b
|
|
141
|
+
// all entities: [S(a-1), S(a-2), S(b-3), S(a-4), S(b-5)]
|
|
142
|
+
// a-result: [a-1, a-2, a-4]
|
|
143
|
+
// b-result: [b-3, b-4]
|
|
144
|
+
// merged:
|
|
145
|
+
// entities[1]->a => targets: [a-1 from a-result.shift()]
|
|
146
|
+
// entities[2]->a => targets: [a-1, a-2 from a-result.shift()]
|
|
147
|
+
// entities[3]->b => targets: [a-1, a-2, b-3 from b-result.shift()]
|
|
148
|
+
// entities[4]->a => targets: [a-1, a-2, b-3, a-4 from a-result.shift()]
|
|
149
|
+
// entities[5]->b => targets: [a-1, a-2, b-3, a-4, b-5 from b-result.shift()]
|
|
150
|
+
if (targetDiscriminator) {
|
|
151
|
+
const allTargets: ((Target & TargetRelations) | undefined)[] = [];
|
|
152
|
+
entities.forEach((value, index, allEntites) => {
|
|
153
|
+
allTargets.push(
|
|
154
|
+
targetCategorized[String(value[targetDiscriminator])].shift(),
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
return allTargets;
|
|
158
|
+
} else {
|
|
159
|
+
return targetCategorized[relationMeta.target().name];
|
|
160
|
+
}
|
|
70
161
|
};
|
|
71
162
|
}
|