@loopback/repository 2.9.0 → 2.11.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/CHANGELOG.md +57 -0
- package/README.md +1 -6
- package/dist/decorators/metadata.js +3 -3
- package/dist/decorators/metadata.js.map +1 -1
- package/dist/define-model-class.js +3 -1
- package/dist/define-model-class.js.map +1 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.js.map +1 -1
- package/dist/mixins/repository.mixin.js +0 -2
- package/dist/mixins/repository.mixin.js.map +1 -1
- package/dist/model.d.ts +19 -2
- package/dist/model.js +54 -0
- package/dist/model.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.helpers.d.ts +1 -0
- package/dist/relations/belongs-to/belongs-to.helpers.js +13 -6
- package/dist/relations/belongs-to/belongs-to.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many-through-repository.factory.js +4 -1
- package/dist/relations/has-many/has-many-through-repository.factory.js.map +1 -1
- package/dist/relations/has-many/has-many-through.helpers.d.ts +35 -0
- package/dist/relations/has-many/has-many-through.helpers.js +44 -1
- package/dist/relations/has-many/has-many-through.helpers.js.map +1 -1
- package/dist/relations/has-many/has-many-through.repository.d.ts +2 -1
- package/dist/relations/has-many/has-many-through.repository.js +21 -5
- package/dist/relations/has-many/has-many-through.repository.js.map +1 -1
- package/dist/relations/relation.types.d.ts +1 -1
- package/dist/relations/relation.types.js +1 -1
- package/dist/repositories/legacy-juggler-bridge.d.ts +75 -0
- package/dist/repositories/legacy-juggler-bridge.js +2 -2
- package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
- package/dist/repositories/repository.js.map +1 -1
- package/package.json +12 -12
- package/src/decorators/metadata.ts +3 -3
- package/src/define-model-class.ts +4 -1
- package/src/index.ts +0 -6
- package/src/mixins/repository.mixin.ts +0 -2
- package/src/model.ts +74 -2
- package/src/relations/belongs-to/belongs-to.helpers.ts +19 -8
- package/src/relations/has-many/has-many-through-repository.factory.ts +5 -1
- package/src/relations/has-many/has-many-through.helpers.ts +49 -0
- package/src/relations/has-many/has-many-through.repository.ts +26 -9
- package/src/relations/relation.types.ts +1 -1
- package/src/repositories/legacy-juggler-bridge.ts +91 -7
- package/src/repositories/repository.ts +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,63 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [2.11.2](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.11.1...@loopback/repository@2.11.2) (2020-08-27)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **repository:** fix test case for repository.execute ([ff0c969](https://github.com/strongloop/loopback-next/commit/ff0c969b441030cc21c0d2e6413ab6b1943f2281))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [2.11.1](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.11.0...@loopback/repository@2.11.1) (2020-08-19)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **docs:** rename legacy juggler to juggler ([d1febb1](https://github.com/strongloop/loopback-next/commit/d1febb17341ef647e203cb24cba3b716c0163f4c))
|
|
23
|
+
* **repository:** apply `[@model](https://github.com/model)` to generated model class ([9a1b1e4](https://github.com/strongloop/loopback-next/commit/9a1b1e4324516bc1351e08943b8b0c5a00e75992))
|
|
24
|
+
* **repository:** remove experimental note ([921e6ae](https://github.com/strongloop/loopback-next/commit/921e6aee81130e92948c24e844424183e4147ab3))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# [2.11.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.10.0...@loopback/repository@2.11.0) (2020-08-05)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Bug Fixes
|
|
34
|
+
|
|
35
|
+
* **repository:** convert relation property in toobject ([85a6164](https://github.com/strongloop/loopback-next/commit/85a616415253bdca0475b7db9072da1ee611ac28))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### Features
|
|
39
|
+
|
|
40
|
+
* **repository:** add support for non-SQL variants of `Repository.execute()` ([49b86c4](https://github.com/strongloop/loopback-next/commit/49b86c463648ef5e0cfac4e9db14334291e992ab))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# [2.10.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.9.0...@loopback/repository@2.10.0) (2020-07-20)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### Bug Fixes
|
|
50
|
+
|
|
51
|
+
* **repository:** hasManyThrough can delete correct target n through based on filter ([c1ba91f](https://github.com/strongloop/loopback-next/commit/c1ba91f2dcbc33dd1cee905d25f9719a71bc7919))
|
|
52
|
+
* ensure delete only applies to optional properties ([89cd43f](https://github.com/strongloop/loopback-next/commit/89cd43f1a455983f120d9bb9c869eac36adc7ad7))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### Features
|
|
56
|
+
|
|
57
|
+
* **repository:** add helpers to create relations ([42d17aa](https://github.com/strongloop/loopback-next/commit/42d17aa37fa88bb4c4152b26450ddb8b72da1f49))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
6
63
|
# [2.9.0](https://github.com/strongloop/loopback-next/compare/@loopback/repository@2.8.0...@loopback/repository@2.9.0) (2020-06-30)
|
|
7
64
|
|
|
8
65
|
|
package/README.md
CHANGED
|
@@ -4,11 +4,6 @@ This module provides a common set of interfaces for interacting with databases.
|
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
**NOTE**: This module is experimental and evolving. It is likely going to be
|
|
8
|
-
refactored and decomposed into multiple modules as we refine the story based on
|
|
9
|
-
the legacy `loopback-datasource-juggler` and connector modules from LoopBack
|
|
10
|
-
3.x.
|
|
11
|
-
|
|
12
7
|
This module provides data access facilities to various databases and services as
|
|
13
8
|
well as the constructs for modeling and accessing those data.
|
|
14
9
|
|
|
@@ -97,7 +92,7 @@ or properties.
|
|
|
97
92
|
import {repository} from '@loopback/repository';
|
|
98
93
|
import {NoteRepository} from '../repositories';
|
|
99
94
|
import {Note} from '../models';
|
|
100
|
-
import {post, requestBody, get, param} from '@loopback/
|
|
95
|
+
import {post, requestBody, get, param} from '@loopback/rest';
|
|
101
96
|
|
|
102
97
|
export class NoteController {
|
|
103
98
|
constructor(
|
|
@@ -36,11 +36,11 @@ class ModelMetadataHelper {
|
|
|
36
36
|
else {
|
|
37
37
|
// sets the metadata to a dedicated key if cached value does not exist
|
|
38
38
|
// set ModelDefinition properties if they don't already exist
|
|
39
|
-
const meta = new model_1.ModelDefinition(
|
|
39
|
+
const meta = new model_1.ModelDefinition({ ...modelMeta });
|
|
40
40
|
// set properties lost from creating instance of ModelDefinition
|
|
41
41
|
Object.assign(meta, modelMeta);
|
|
42
|
-
meta.properties = Object.assign(
|
|
43
|
-
meta.relations = Object.assign(
|
|
42
|
+
meta.properties = Object.assign(meta.properties, core_1.MetadataInspector.getAllPropertyMetadata(model_decorator_1.MODEL_PROPERTIES_KEY, target.prototype, options));
|
|
43
|
+
meta.relations = Object.assign(meta.relations, core_1.MetadataInspector.getAllPropertyMetadata(relations_1.RELATIONS_KEY, target.prototype, options));
|
|
44
44
|
core_1.MetadataInspector.defineMetadata(model_decorator_1.MODEL_WITH_PROPERTIES_KEY.key, meta, target);
|
|
45
45
|
return meta;
|
|
46
46
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata.js","sourceRoot":"","sources":["../../src/decorators/metadata.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,yCAAoE;AACpE,oCAA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AiB,CAAC,gBAAgB,CAClD,2BAAS,EACT,MAAM,EACN,OAAO,CACR,CAAC;YACF,IAAI,CAAC,SAAS,EAAE;gBACd,OAAO,EAAE,CAAC;aACX;iBAAM;gBACL,sEAAsE;gBAEtE,6DAA6D;gBAC7D,MAAM,IAAI,GAAG,IAAI,uBAAe,CAAC,EAAC,GAAG,SAAS,EAAC,CAAC,CAAC;gBAEjD,gEAAgE;gBAChE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;gBAE/B,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,MAAM,CAChB,IAAI,CAAC,UAAU,EAC5B,wBAAiB,CAAC,sBAAsB,CACtC,sCAAoB,EACpB,MAAM,CAAC,SAAS,EAChB,OAAO,CACR,CACF,CAAC;gBAEF,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,CACL,IAAI,CAAC,SAAS,EACrC,wBAAiB,CAAC,sBAAsB,CACtC,yBAAa,EACb,MAAM,CAAC,SAAS,EAChB,OAAO,CACR,CACF,CAAC;gBAEF,wBAAiB,CAAC,cAAc,CAC9B,2CAAyB,CAAC,GAAG,EAC7B,IAAI,EACJ,MAAM,CACP,CAAC;gBACF,OAAO,IAAI,CAAC;aACb;SACF;IACH,CAAC;CACF;AApED,kDAoEC"}
|
|
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
exports.defineModelClass = void 0;
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
const assert = tslib_1.__importStar(require("assert"));
|
|
10
|
+
const decorators_1 = require("./decorators");
|
|
10
11
|
/**
|
|
11
12
|
* Create (define) a new model class with the given name and definition.
|
|
12
13
|
*
|
|
@@ -48,7 +49,8 @@ function defineModelClass(base /* Model or Entity */, definition) {
|
|
|
48
49
|
const defineNamedModelClass = new Function(base.name, `return class ${modelName} extends ${base.name} {}`);
|
|
49
50
|
const modelClass = defineNamedModelClass(base);
|
|
50
51
|
assert.equal(modelClass.name, modelName);
|
|
51
|
-
|
|
52
|
+
// Apply `@model(definition)` to the generated class
|
|
53
|
+
decorators_1.model(definition)(modelClass);
|
|
52
54
|
return modelClass;
|
|
53
55
|
}
|
|
54
56
|
exports.defineModelClass = defineModelClass;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"define-model-class.js","sourceRoot":"","sources":["../src/define-model-class.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,uDAAiC;
|
|
1
|
+
{"version":3,"file":"define-model-class.js","sourceRoot":"","sources":["../src/define-model-class.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,uDAAiC;AAEjC,6CAAmC;AAGnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,SAAgB,gBAAgB,CAI9B,IAAc,CAAC,qBAAqB,EACpC,UAA2B;IAE3B,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC;IAClC,MAAM,qBAAqB,GAAG,IAAI,QAAQ,CACxC,IAAI,CAAC,IAAI,EACT,gBAAgB,SAAS,YAAY,IAAI,CAAC,IAAI,KAAK,CACpD,CAAC;IACF,MAAM,UAAU,GAAG,qBAAqB,CAAC,IAAI,CAG5C,CAAC;IACF,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IAEzC,oDAAoD;IACpD,kBAAK,CAAC,UAAU,CAAC,CAAC,UAAU,CAAC,CAAC;IAC9B,OAAO,UAAU,CAAC;AACpB,CAAC;AArBD,4CAqBC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A common set of interfaces for interacting with databases.
|
|
3
3
|
*
|
|
4
|
-
* @remarks
|
|
5
|
-
* *NOTE:* This module is experimental and evolving. It is likely going to be
|
|
6
|
-
* refactored and decomposed into multiple modules as we refine the story based
|
|
7
|
-
* on the legacy loopback-datasource-juggler and connector modules from LoopBack
|
|
8
|
-
* 3.x.
|
|
9
|
-
*
|
|
10
4
|
* This module provides data access facilities to various databases and services
|
|
11
5
|
* as well as the constructs for modeling and accessing those data.
|
|
12
6
|
*
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAYhE,yDAA+B;AAC/B,uDAA6B;AAC7B,uDAA6B;AAC7B,uDAA6B;AAC7B,+DAAqC;AACrC,oEAA0C;AAC1C,mDAAyB;AACzB,iDAAuB;AACvB,mDAAyB;AACzB,kDAAwB;AACxB,kDAAwB;AACxB,sDAA4B;AAC5B,yDAA+B;AAC/B,wDAA8B;AAC9B,0DAAgC;AAChC,kDAAwB"}
|
|
@@ -112,14 +112,12 @@ function RepositoryMixin(superClass) {
|
|
|
112
112
|
const options = toOptions(nameOrOptions);
|
|
113
113
|
// We have an instance of
|
|
114
114
|
if (dataSource instanceof repositories_1.juggler.DataSource) {
|
|
115
|
-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
116
115
|
const name = options.name || dataSource.name;
|
|
117
116
|
const namespace = (_a = options.namespace) !== null && _a !== void 0 ? _a : keys_1.RepositoryBindings.DATASOURCES;
|
|
118
117
|
const key = `${namespace}.${name}`;
|
|
119
118
|
return this.bind(key).to(dataSource).tag(keys_1.RepositoryTags.DATASOURCE);
|
|
120
119
|
}
|
|
121
120
|
else if (typeof dataSource === 'function') {
|
|
122
|
-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
123
121
|
options.name = options.name || dataSource.dataSourceName;
|
|
124
122
|
const binding = core_1.createBindingFromClass(dataSource, {
|
|
125
123
|
namespace: keys_1.RepositoryBindings.DATASOURCES,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.mixin.js","sourceRoot":"","sources":["../../src/mixins/repository.mixin.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,yCAKwB;AACxB,yCAMwB;AACxB,0DAAiC;AAGjC,kCAA2D;AAE3D,kDAAoD;AAEpD,MAAM,KAAK,GAAG,eAAY,CAAC,2BAA2B,CAAC,CAAC;AAMxD,qDAAqD;AAErD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,eAAe,CAC7B,UAAa;IAEb,OAAO,KAAM,SAAQ,UAAU;QAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA4BG;QACH,8DAA8D;QAC9D,UAAU,CACR,SAAmB,EACnB,aAAgD;YAEhD,MAAM,OAAO,GAAG,6BAAsB,CAAC,SAAS,EAAE;gBAChD,SAAS,EAAE,yBAAkB,CAAC,YAAY;gBAC1C,IAAI,EAAE,qBAAc,CAAC,UAAU;gBAC/B,YAAY,EAAE,mBAAY,CAAC,SAAS;gBACpC,GAAG,SAAS,CAAC,aAAa,CAAC;aAC5B,CAAC,CAAC,GAAG,CAAC,qBAAc,CAAC,UAAU,CAAC,CAAC;YAClC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YAClB,OAAO,OAAO,CAAC;QACjB,CAAC;QAED;;;;WAIG;QACH,8DAA8D;QAC9D,KAAK,CAAC,aAAa,CAA4B,IAAc;YAC3D,OAAO,IAAI,CAAC,GAAG,CAAC,gBAAgB,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC/C,CAAC;QAED;;;;;;;;;;;;;;;;;;;;;;WAsBG;QACH,UAAU,CACR,UAAwB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,GAAG,IAAW;QACxB,MAAM,IAAI,KAAK,CACb,2CAA2C,GAAG,uBAAuB,CACtE,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,8DAA8D;IAC9D,UAAU,CAAC,IAA4B;QACrC,MAAM,IAAI,KAAK,EAAE,CAAC;IACpB,CAAC;IAED;;;;OAIG;IACH,8DAA8D;IAC9D,KAAK,CAAC,aAAa,CAA4B,IAAc;QAC3D,OAAO,IAAI,IAAI,EAAO,CAAC;IACzB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,UAAU,CACR,UAA0D,EAC1D,IAAa;QAEb,MAAM,IAAI,KAAK,EAAE,CAAC;IACpB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACI,SAAS,CAAC,SAAoB;QACnC,MAAM,IAAI,KAAK,EAAE,CAAC;IACpB,CAAC;IAED;;;;;;OAMG;IACH,wBAAwB,CAAC,SAAoB,IAAG,CAAC;IAEjD;;;;;;;;;;;;OAYG;IACH,KAAK,CAAC,aAAa,CAAC,OAAgC,IAAkB,CAAC;CACxE;AAhID,gDAgIC;AAED;;;GAGG;AACH,SAAgB,uBAAuB,CACrC,UAAa;IAEb,OAAO,cAAO,CAAC,IAAI,CAAI,GAAG,yBAAkB,CAAC,MAAM,IAAI,UAAU,CAAC,IAAI,EAAE,CAAC;SACtE,EAAE,CAAC,UAAU,CAAC;SACd,GAAG,CAAC,qBAAc,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC;AAND,0DAMC"}
|
package/dist/model.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { AnyObject, DataObject, Options, PrototypeOf } from './common-types';
|
|
2
|
-
import { JsonSchema } from './index';
|
|
3
|
-
import { RelationMetadata } from './relations';
|
|
2
|
+
import { BelongsToDefinition, HasManyDefinition, HasOneDefinition, JsonSchema, RelationMetadata } from './index';
|
|
4
3
|
import { TypeResolver } from './type-resolver';
|
|
5
4
|
import { Type } from './types';
|
|
6
5
|
/**
|
|
@@ -85,6 +84,24 @@ export declare class ModelDefinition {
|
|
|
85
84
|
* @param definition - The definition of the new relation.
|
|
86
85
|
*/
|
|
87
86
|
addRelation(definition: RelationMetadata): this;
|
|
87
|
+
/**
|
|
88
|
+
* Define a new belongsTo relation.
|
|
89
|
+
* @param name - The name of the belongsTo relation.
|
|
90
|
+
* @param definition - The definition of the belongsTo relation.
|
|
91
|
+
*/
|
|
92
|
+
belongsTo(name: string, definition: Omit<BelongsToDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
|
93
|
+
/**
|
|
94
|
+
* Define a new hasOne relation.
|
|
95
|
+
* @param name - The name of the hasOne relation.
|
|
96
|
+
* @param definition - The definition of the hasOne relation.
|
|
97
|
+
*/
|
|
98
|
+
hasOne(name: string, definition: Omit<HasOneDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
|
99
|
+
/**
|
|
100
|
+
* Define a new hasMany relation.
|
|
101
|
+
* @param name - The name of the hasMany relation.
|
|
102
|
+
* @param definition - The definition of the hasMany relation.
|
|
103
|
+
*/
|
|
104
|
+
hasMany(name: string, definition: Omit<HasManyDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
|
88
105
|
/**
|
|
89
106
|
* Get an array of names of ID properties, which are specified in
|
|
90
107
|
* the model settings or properties with `id` attribute.
|
package/dist/model.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// License text available at https://opensource.org/licenses/MIT
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.rejectNavigationalPropertiesInData = exports.Event = exports.Entity = exports.ValueObject = exports.Model = exports.ModelDefinition = void 0;
|
|
8
|
+
const index_1 = require("./index");
|
|
8
9
|
/**
|
|
9
10
|
* Definition for a model
|
|
10
11
|
*/
|
|
@@ -59,6 +60,48 @@ class ModelDefinition {
|
|
|
59
60
|
this.relations[definition.name] = definition;
|
|
60
61
|
return this;
|
|
61
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Define a new belongsTo relation.
|
|
65
|
+
* @param name - The name of the belongsTo relation.
|
|
66
|
+
* @param definition - The definition of the belongsTo relation.
|
|
67
|
+
*/
|
|
68
|
+
belongsTo(name, definition) {
|
|
69
|
+
const meta = {
|
|
70
|
+
...definition,
|
|
71
|
+
name,
|
|
72
|
+
type: index_1.RelationType.belongsTo,
|
|
73
|
+
targetsMany: false,
|
|
74
|
+
};
|
|
75
|
+
return this.addRelation(meta);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Define a new hasOne relation.
|
|
79
|
+
* @param name - The name of the hasOne relation.
|
|
80
|
+
* @param definition - The definition of the hasOne relation.
|
|
81
|
+
*/
|
|
82
|
+
hasOne(name, definition) {
|
|
83
|
+
const meta = {
|
|
84
|
+
...definition,
|
|
85
|
+
name,
|
|
86
|
+
type: index_1.RelationType.hasOne,
|
|
87
|
+
targetsMany: false,
|
|
88
|
+
};
|
|
89
|
+
return this.addRelation(meta);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Define a new hasMany relation.
|
|
93
|
+
* @param name - The name of the hasMany relation.
|
|
94
|
+
* @param definition - The definition of the hasMany relation.
|
|
95
|
+
*/
|
|
96
|
+
hasMany(name, definition) {
|
|
97
|
+
const meta = {
|
|
98
|
+
...definition,
|
|
99
|
+
name,
|
|
100
|
+
type: index_1.RelationType.hasMany,
|
|
101
|
+
targetsMany: true,
|
|
102
|
+
};
|
|
103
|
+
return this.addRelation(meta);
|
|
104
|
+
}
|
|
62
105
|
/**
|
|
63
106
|
* Get an array of names of ID properties, which are specified in
|
|
64
107
|
* the model settings or properties with `id` attribute.
|
|
@@ -186,6 +229,17 @@ class Model {
|
|
|
186
229
|
}
|
|
187
230
|
return obj;
|
|
188
231
|
}
|
|
232
|
+
if (def === null || def === void 0 ? void 0 : def.relations) {
|
|
233
|
+
for (const r in def.relations) {
|
|
234
|
+
const relName = def.relations[r].name;
|
|
235
|
+
if (relName in this) {
|
|
236
|
+
obj[relName] = asObject(this[relName], {
|
|
237
|
+
...options,
|
|
238
|
+
ignoreUnknownProperties: false,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
189
243
|
const props = def.properties;
|
|
190
244
|
const keys = Object.keys(props);
|
|
191
245
|
for (const i in keys) {
|
package/dist/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;AAGhE,mCAOiB;AAiEjB;;GAEG;AACH,MAAa,eAAe;IAQ1B,YAAY,SAAyC;QACnD,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE;YACjC,SAAS,GAAG,EAAC,IAAI,EAAE,SAAS,EAAC,CAAC;SAC/B;QACD,MAAM,EAAC,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAC,GAAG,SAAS,CAAC;QAE1D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,UAAU,EAAE;YACd,KAAK,MAAM,CAAC,IAAI,UAAU,EAAE;gBAC1B,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;aACpC;SACF;QAED,IAAI,CAAC,QAAQ,GAAG,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,IAAI,GAAG,EAAE,CAAC;QACtC,IAAI,CAAC,SAAS,GAAG,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,EAAE,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,WAAW,CAC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AGjB;AAHD,sBAGC;AAMD;;;;;;;GAOG;AACH,SAAgB,kCAAkC,CAChD,UAAa,EACb,IAAgC;IAEhC,MAAM,GAAG,GAAG,UAAU,CAAC,UAAU,CAAC;IAClC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC;IAE7B,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,SAAS,EAAE;QAC7B,MAAM,OAAO,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACtC,IAAI,CAAC,CAAC,OAAO,IAAI,IAAI,CAAC;YAAE,SAAS;QAEjC,IAAI,GAAG,GACL,wDAAwD;YACxD,WAAW,UAAU,CAAC,SAAS,eAAe,OAAO,MAAM;YAC3D,mBAAmB,CAAC;QAEtB,IAAI,OAAO,IAAI,KAAK,EAAE;YACpB,GAAG;gBACD,iEAAiE;oBACjE,+DAA+D,CAAC;SACnE;QAED,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;KACtB;AACH,CAAC;AAxBD,gFAwBC"}
|
|
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
exports.resolveBelongsToMetadata = void 0;
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
10
|
+
const lodash_1 = require("lodash");
|
|
10
11
|
const errors_1 = require("../../errors");
|
|
11
12
|
const type_resolver_1 = require("../../type-resolver");
|
|
12
13
|
const relation_types_1 = require("../relation.types");
|
|
@@ -27,10 +28,6 @@ function resolveBelongsToMetadata(relationMeta) {
|
|
|
27
28
|
const reason = 'target must be a type resolver';
|
|
28
29
|
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
29
30
|
}
|
|
30
|
-
if (!relationMeta.keyFrom) {
|
|
31
|
-
const reason = 'keyFrom is required';
|
|
32
|
-
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
33
|
-
}
|
|
34
31
|
const sourceModel = relationMeta.source;
|
|
35
32
|
if (!sourceModel || !sourceModel.modelName) {
|
|
36
33
|
const reason = 'source model must be defined';
|
|
@@ -39,18 +36,28 @@ function resolveBelongsToMetadata(relationMeta) {
|
|
|
39
36
|
const targetModel = relationMeta.target();
|
|
40
37
|
const targetName = targetModel.modelName;
|
|
41
38
|
debug('Resolved model %s from given metadata: %o', targetName, targetModel);
|
|
39
|
+
let keyFrom;
|
|
40
|
+
if (relationMeta.keyFrom &&
|
|
41
|
+
relationMeta.source.definition.properties[relationMeta.keyFrom]) {
|
|
42
|
+
keyFrom = relationMeta.keyFrom;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
keyFrom = lodash_1.camelCase(targetName + '_id');
|
|
46
|
+
}
|
|
42
47
|
const targetProperties = targetModel.definition.properties;
|
|
43
48
|
debug('relation metadata from %o: %o', targetName, targetProperties);
|
|
44
49
|
if (relationMeta.keyTo && targetProperties[relationMeta.keyTo]) {
|
|
45
50
|
// The explicit cast is needed because of a limitation of type inference
|
|
46
|
-
return relationMeta
|
|
51
|
+
return Object.assign(relationMeta, {
|
|
52
|
+
keyFrom,
|
|
53
|
+
});
|
|
47
54
|
}
|
|
48
55
|
const targetPrimaryKey = targetModel.definition.idProperties()[0];
|
|
49
56
|
if (!targetPrimaryKey) {
|
|
50
57
|
const reason = `${targetName} does not have any primary key (id property)`;
|
|
51
58
|
throw new errors_1.InvalidRelationError(reason, relationMeta);
|
|
52
59
|
}
|
|
53
|
-
return Object.assign(relationMeta, { keyTo: targetPrimaryKey });
|
|
60
|
+
return Object.assign(relationMeta, { keyFrom, keyTo: targetPrimaryKey });
|
|
54
61
|
}
|
|
55
62
|
exports.resolveBelongsToMetadata = resolveBelongsToMetadata;
|
|
56
63
|
//# sourceMappingURL=belongs-to.helpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"belongs-to.helpers.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,yCAAkD;AAClD,uDAAmD;AACnD,sDAAoE;AAEpE,MAAM,KAAK,GAAG,eAAY,CAAC,wCAAwC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"belongs-to.helpers.js","sourceRoot":"","sources":["../../../src/relations/belongs-to/belongs-to.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,mCAAiC;AACjC,yCAAkD;AAClD,uDAAmD;AACnD,sDAAoE;AAEpE,MAAM,KAAK,GAAG,eAAY,CAAC,wCAAwC,CAAC,CAAC;AAWrE;;;;;;GAMG;AACH,SAAgB,wBAAwB,CAAC,YAAiC;IACxE,IAAK,YAAY,CAAC,IAAqB,KAAK,6BAAY,CAAC,SAAS,EAAE;QAClE,MAAM,MAAM,GAAG,iCAAiC,CAAC;QACjD,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,IAAI,CAAC,8BAAc,CAAC,YAAY,CAAC,MAAM,CAAC,EAAE;QACxC,MAAM,MAAM,GAAG,gCAAgC,CAAC;QAChD,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,WAAW,GAAG,YAAY,CAAC,MAAM,CAAC;IACxC,IAAI,CAAC,WAAW,IAAI,CAAC,WAAW,CAAC,SAAS,EAAE;QAC1C,MAAM,MAAM,GAAG,8BAA8B,CAAC;QAC9C,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,WAAW,GAAG,YAAY,CAAC,MAAM,EAAE,CAAC;IAC1C,MAAM,UAAU,GAAG,WAAW,CAAC,SAAS,CAAC;IACzC,KAAK,CAAC,2CAA2C,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC;IAE5E,IAAI,OAAO,CAAC;IACZ,IACE,YAAY,CAAC,OAAO;QACpB,YAAY,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,EAC/D;QACA,OAAO,GAAG,YAAY,CAAC,OAAO,CAAC;KAChC;SAAM;QACL,OAAO,GAAG,kBAAS,CAAC,UAAU,GAAG,KAAK,CAAC,CAAC;KACzC;IAED,MAAM,gBAAgB,GAAG,WAAW,CAAC,UAAU,CAAC,UAAU,CAAC;IAC3D,KAAK,CAAC,+BAA+B,EAAE,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAErE,IAAI,YAAY,CAAC,KAAK,IAAI,gBAAgB,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAC9D,wEAAwE;QACxE,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE;YACjC,OAAO;SACR,CAAgC,CAAC;KACnC;IAED,MAAM,gBAAgB,GAAG,WAAW,CAAC,UAAU,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IAClE,IAAI,CAAC,gBAAgB,EAAE;QACrB,MAAM,MAAM,GAAG,GAAG,UAAU,8CAA8C,CAAC;QAC3E,MAAM,IAAI,6BAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,EAAC,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAC,CAAC,CAAC;AACzE,CAAC;AAhDD,4DAgDC"}
|
|
@@ -16,11 +16,14 @@ function createHasManyThroughRepositoryFactory(relationMetadata, targetRepositor
|
|
|
16
16
|
const constraint = has_many_through_helpers_1.createThroughConstraintFromSource(meta, fkValue);
|
|
17
17
|
return constraint;
|
|
18
18
|
}
|
|
19
|
+
function getTargetIds(targetInstances) {
|
|
20
|
+
return has_many_through_helpers_1.getTargetIdsFromTargetModels(meta, targetInstances);
|
|
21
|
+
}
|
|
19
22
|
function getThroughConstraintFromTarget(fkValues) {
|
|
20
23
|
const constraint = has_many_through_helpers_1.createThroughConstraintFromTarget(meta, fkValues);
|
|
21
24
|
return constraint;
|
|
22
25
|
}
|
|
23
|
-
return new has_many_through_repository_1.DefaultHasManyThroughRepository(targetRepositoryGetter, throughRepositoryGetter, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getThroughConstraintFromTarget);
|
|
26
|
+
return new has_many_through_repository_1.DefaultHasManyThroughRepository(targetRepositoryGetter, throughRepositoryGetter, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getTargetIds, getThroughConstraintFromTarget);
|
|
24
27
|
};
|
|
25
28
|
return result;
|
|
26
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"has-many-through-repository.factory.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through-repository.factory.ts"],"names":[],"mappings":";;;AAWA,
|
|
1
|
+
{"version":3,"file":"has-many-through-repository.factory.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through-repository.factory.ts"],"names":[],"mappings":";;;AAWA,yEAOoC;AACpC,+EAGuC;AAmBvC,SAAgB,qCAAqC,CAOnD,gBAAmC,EACnC,sBAAsE,EACtE,uBAAyE;IAEzE,MAAM,IAAI,GAAG,wDAA6B,CAAC,gBAAgB,CAAC,CAAC;IAC7D,MAAM,MAAM,GAAG,UAAU,OAAiB;QACxC,SAAS,oCAAoC,CAC3C,gBAA2B;YAE3B,OAAO,4DAAiC,CACtC,IAAI,EACJ,gBAAgB,CACjB,CAAC;QACJ,CAAC;QACD,SAAS,aAAa,CAAC,gBAA2B;YAChD,OAAO,yDAA8B,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;QAChE,CAAC;QACD,SAAS,8BAA8B;YACrC,MAAM,UAAU,GAAwB,4DAAiC,CAGvE,IAAI,EAAE,OAAO,CAAC,CAAC;YACjB,OAAO,UAAU,CAAC;QACpB,CAAC;QACD,SAAS,YAAY,CAAC,eAAyB;YAC7C,OAAO,uDAA4B,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;QAC7D,CAAC;QACD,SAAS,8BAA8B,CACrC,QAAoB;YAEpB,MAAM,UAAU,GAAwB,4DAAiC,CAGvE,IAAI,EAAE,QAAQ,CAAC,CAAC;YAClB,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,OAAO,IAAI,6DAA+B,CAQxC,sBAAsB,EACtB,uBAAuB,EACvB,oCAAoC,EACpC,aAAa,EACb,8BAA8B,EAC9B,YAAY,EACZ,8BAA8B,CAC/B,CAAC;IACJ,CAAC,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC;AA9DD,sFA8DC"}
|
|
@@ -109,6 +109,41 @@ export declare function getTargetKeysFromThroughModels<Through extends Entity, T
|
|
|
109
109
|
* ```
|
|
110
110
|
*/
|
|
111
111
|
export declare function createThroughConstraintFromSource<Through extends Entity, SourceID>(relationMeta: HasManyThroughResolvedDefinition, fkValue: SourceID): DataObject<Through>;
|
|
112
|
+
/**
|
|
113
|
+
* Returns an array of target ids of the given target instances.
|
|
114
|
+
*
|
|
115
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
116
|
+
* @param targetInstances - an array of target instances
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* const resolvedMetadata = {
|
|
121
|
+
* // .. other props
|
|
122
|
+
* keyFrom: 'id',
|
|
123
|
+
* keyTo: 'id',
|
|
124
|
+
* through: {
|
|
125
|
+
* model: () => CategoryProductLink,
|
|
126
|
+
* keyFrom: 'categoryId',
|
|
127
|
+
* keyTo: 'productId',
|
|
128
|
+
* },
|
|
129
|
+
* };
|
|
130
|
+
* getTargetKeysFromTargetModels(resolvedMetadata,[{
|
|
131
|
+
id: 2,
|
|
132
|
+
des: 'a target',
|
|
133
|
+
}]);
|
|
134
|
+
* >>> [2]
|
|
135
|
+
* getTargetKeysFromTargetModels(resolvedMetadata, [
|
|
136
|
+
{
|
|
137
|
+
id: 2,
|
|
138
|
+
des: 'a target',
|
|
139
|
+
}, {
|
|
140
|
+
id: 1,
|
|
141
|
+
des: 'a target',
|
|
142
|
+
}
|
|
143
|
+
]);
|
|
144
|
+
>>> [2, 1]
|
|
145
|
+
*/
|
|
146
|
+
export declare function getTargetIdsFromTargetModels<Target extends Entity, TargetID>(relationMeta: HasManyThroughResolvedDefinition, targetInstances: Target[]): TargetID[];
|
|
112
147
|
/**
|
|
113
148
|
* Creates through constraint based on the target foreign key
|
|
114
149
|
*
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// This file is licensed under the MIT License.
|
|
5
5
|
// License text available at https://opensource.org/licenses/MIT
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.resolveHasManyThroughMetadata = exports.createThroughConstraintFromTarget = exports.createThroughConstraintFromSource = exports.getTargetKeysFromThroughModels = exports.createTargetConstraintFromThrough = void 0;
|
|
7
|
+
exports.resolveHasManyThroughMetadata = exports.createThroughConstraintFromTarget = exports.getTargetIdsFromTargetModels = exports.createThroughConstraintFromSource = exports.getTargetKeysFromThroughModels = exports.createTargetConstraintFromThrough = void 0;
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
10
10
|
const lodash_1 = require("lodash");
|
|
@@ -135,6 +135,49 @@ function createThroughConstraintFromSource(relationMeta, fkValue) {
|
|
|
135
135
|
return constraint;
|
|
136
136
|
}
|
|
137
137
|
exports.createThroughConstraintFromSource = createThroughConstraintFromSource;
|
|
138
|
+
/**
|
|
139
|
+
* Returns an array of target ids of the given target instances.
|
|
140
|
+
*
|
|
141
|
+
* @param relationMeta - resolved hasManyThrough metadata
|
|
142
|
+
* @param targetInstances - an array of target instances
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const resolvedMetadata = {
|
|
147
|
+
* // .. other props
|
|
148
|
+
* keyFrom: 'id',
|
|
149
|
+
* keyTo: 'id',
|
|
150
|
+
* through: {
|
|
151
|
+
* model: () => CategoryProductLink,
|
|
152
|
+
* keyFrom: 'categoryId',
|
|
153
|
+
* keyTo: 'productId',
|
|
154
|
+
* },
|
|
155
|
+
* };
|
|
156
|
+
* getTargetKeysFromTargetModels(resolvedMetadata,[{
|
|
157
|
+
id: 2,
|
|
158
|
+
des: 'a target',
|
|
159
|
+
}]);
|
|
160
|
+
* >>> [2]
|
|
161
|
+
* getTargetKeysFromTargetModels(resolvedMetadata, [
|
|
162
|
+
{
|
|
163
|
+
id: 2,
|
|
164
|
+
des: 'a target',
|
|
165
|
+
}, {
|
|
166
|
+
id: 1,
|
|
167
|
+
des: 'a target',
|
|
168
|
+
}
|
|
169
|
+
]);
|
|
170
|
+
>>> [2, 1]
|
|
171
|
+
*/
|
|
172
|
+
function getTargetIdsFromTargetModels(relationMeta, targetInstances) {
|
|
173
|
+
const targetId = relationMeta.keyTo;
|
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
175
|
+
let ids = [];
|
|
176
|
+
ids = targetInstances.map((targetInstance) => targetInstance[targetId]);
|
|
177
|
+
ids = __1.deduplicate(ids);
|
|
178
|
+
return ids;
|
|
179
|
+
}
|
|
180
|
+
exports.getTargetIdsFromTargetModels = getTargetIdsFromTargetModels;
|
|
138
181
|
/**
|
|
139
182
|
* Creates through constraint based on the target foreign key
|
|
140
183
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"has-many-through.helpers.js","sourceRoot":"","sources":["../../../src/relations/has-many/has-many-through.helpers.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,0DAAiC;AACjC,mCAAiC;AACjC,6BAOe;AACf,yDAA4D;AAE5D,MAAM,KAAK,GAAG,eAAY,CAAC,8CAA8C,CAAC,CAAC;AAW3E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AC,CAAC;IAC3E,IAAI,CAAC,sBAAsB,CAAC,YAAY,CAAC,EAAE;QACzC,MAAM,MAAM,GAAG,iBAAiB,YAAY,CAAC,IAAI,8CAA8C,CAAC;QAChG,MAAM,IAAI,wBAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,YAAY,SAChB,YAAY,CAAC,OAAO,CAAC,KAAK,mCAAI,kBAAS,CAAC,WAAW,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;IACzE,IAAI,CAAC,sBAAsB,CAAC,YAAY,CAAC,EAAE;QACzC,MAAM,MAAM,GAAG,iBAAiB,YAAY,CAAC,IAAI,8CAA8C,CAAC;QAChG,MAAM,IAAI,wBAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,MAAM,gBAAgB,SACpB,YAAY,CAAC,KAAK,mCAAI,WAAW,CAAC,UAAU,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;IACjE,IAAI,CAAC,gBAAgB,IAAI,CAAC,qBAAqB,CAAC,gBAAgB,CAAC,EAAE;QACjE,MAAM,MAAM,GAAG,gBAAgB,WAAW,CAAC,SAAS,8CAA8C,CAAC;QACnG,MAAM,IAAI,wBAAoB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;KACtD;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE;QACjC,KAAK,EAAE,gBAAgB;QACvB,OAAO,EAAE,YAAY,CAAC,OAAQ;QAC9B,OAAO,EAAE;YACP,GAAG,YAAY,CAAC,OAAO;YACvB,KAAK,EAAE,YAAY;YACnB,OAAO,EAAE,YAAY;SACtB;KACF,CAAC,CAAC;AACL,CAAC;AA9ED,sEA8EC"}
|
|
@@ -77,8 +77,9 @@ export declare class DefaultHasManyThroughRepository<TargetEntity extends Entity
|
|
|
77
77
|
getTargetConstraintFromThroughModels: (throughInstances: ThroughEntity[]) => DataObject<TargetEntity>;
|
|
78
78
|
getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[];
|
|
79
79
|
getThroughConstraintFromSource: () => DataObject<ThroughEntity>;
|
|
80
|
+
getTargetIds: (targetInstances: TargetEntity[]) => TargetID[];
|
|
80
81
|
getThroughConstraintFromTarget: (targetID: TargetID[]) => DataObject<ThroughEntity>;
|
|
81
|
-
constructor(getTargetRepository: Getter<TargetRepository>, getThroughRepository: Getter<ThroughRepository>, getTargetConstraintFromThroughModels: (throughInstances: ThroughEntity[]) => DataObject<TargetEntity>, getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[], getThroughConstraintFromSource: () => DataObject<ThroughEntity>, getThroughConstraintFromTarget: (targetID: TargetID[]) => DataObject<ThroughEntity>);
|
|
82
|
+
constructor(getTargetRepository: Getter<TargetRepository>, getThroughRepository: Getter<ThroughRepository>, getTargetConstraintFromThroughModels: (throughInstances: ThroughEntity[]) => DataObject<TargetEntity>, getTargetKeys: (throughInstances: ThroughEntity[]) => TargetID[], getThroughConstraintFromSource: () => DataObject<ThroughEntity>, getTargetIds: (targetInstances: TargetEntity[]) => TargetID[], getThroughConstraintFromTarget: (targetID: TargetID[]) => DataObject<ThroughEntity>);
|
|
82
83
|
create(targetModelData: DataObject<TargetEntity>, options?: Options & {
|
|
83
84
|
throughData?: DataObject<ThroughEntity>;
|
|
84
85
|
throughOptions?: Options;
|
|
@@ -14,12 +14,13 @@ const __1 = require("../..");
|
|
|
14
14
|
* released.
|
|
15
15
|
*/
|
|
16
16
|
class DefaultHasManyThroughRepository {
|
|
17
|
-
constructor(getTargetRepository, getThroughRepository, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getThroughConstraintFromTarget) {
|
|
17
|
+
constructor(getTargetRepository, getThroughRepository, getTargetConstraintFromThroughModels, getTargetKeys, getThroughConstraintFromSource, getTargetIds, getThroughConstraintFromTarget) {
|
|
18
18
|
this.getTargetRepository = getTargetRepository;
|
|
19
19
|
this.getThroughRepository = getThroughRepository;
|
|
20
20
|
this.getTargetConstraintFromThroughModels = getTargetConstraintFromThroughModels;
|
|
21
21
|
this.getTargetKeys = getTargetKeys;
|
|
22
22
|
this.getThroughConstraintFromSource = getThroughConstraintFromSource;
|
|
23
|
+
this.getTargetIds = getTargetIds;
|
|
23
24
|
this.getThroughConstraintFromTarget = getThroughConstraintFromTarget;
|
|
24
25
|
}
|
|
25
26
|
async create(targetModelData, options) {
|
|
@@ -41,10 +42,25 @@ class DefaultHasManyThroughRepository {
|
|
|
41
42
|
const throughRepository = await this.getThroughRepository();
|
|
42
43
|
const sourceConstraint = this.getThroughConstraintFromSource();
|
|
43
44
|
const throughInstances = await throughRepository.find(__1.constrainFilter(undefined, sourceConstraint), options === null || options === void 0 ? void 0 : options.throughOptions);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if (where) {
|
|
46
|
+
// only delete related through models
|
|
47
|
+
// TODO(Agnes): this performance can be improved by only fetching related data
|
|
48
|
+
// TODO: add target ids to the `where` constraint
|
|
49
|
+
const targets = await targetRepository.find({ where });
|
|
50
|
+
const targetIds = this.getTargetIds(targets);
|
|
51
|
+
if (targetIds.length > 0) {
|
|
52
|
+
const targetConstraint = this.getThroughConstraintFromTarget(targetIds);
|
|
53
|
+
const constraints = { ...targetConstraint, ...sourceConstraint };
|
|
54
|
+
await throughRepository.deleteAll(__1.constrainDataObject({}, constraints), options === null || options === void 0 ? void 0 : options.throughOptions);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// otherwise, delete through models that relate to the sourceId
|
|
59
|
+
const targetFkValues = this.getTargetKeys(throughInstances);
|
|
60
|
+
// delete through instances that have the targets that are going to be deleted
|
|
61
|
+
const throughFkConstraint = this.getThroughConstraintFromTarget(targetFkValues);
|
|
62
|
+
await throughRepository.deleteAll(__1.constrainWhereOr({}, [sourceConstraint, throughFkConstraint]));
|
|
63
|
+
}
|
|
48
64
|
// delete target(s)
|
|
49
65
|
const targetConstraint = this.getTargetConstraintFromThroughModels(throughInstances);
|
|
50
66
|
return targetRepository.deleteAll(__1.constrainWhere(where, targetConstraint), options);
|