@loopback/repository 1.10.1 → 1.13.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 +55 -0
- package/dist/common-types.d.ts +1 -1
- package/dist/common-types.js.map +1 -1
- package/dist/decorators/model.decorator.js +2 -2
- package/dist/decorators/model.decorator.js.map +1 -1
- package/dist/decorators/repository.decorator.d.ts +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/mixins/repository.mixin.d.ts +1 -1
- package/dist/model.js +3 -3
- package/dist/model.js.map +1 -1
- package/dist/relations/index.d.ts +3 -2
- package/dist/relations/index.js +3 -2
- package/dist/relations/index.js.map +1 -1
- package/dist/relations/relation.decorator.js +15 -5
- package/dist/relations/relation.decorator.js.map +1 -1
- package/dist/relations/relation.helpers.d.ts +23 -0
- package/dist/relations/relation.helpers.js +94 -0
- package/dist/relations/relation.helpers.js.map +1 -0
- package/dist/relations/relation.types.d.ts +22 -0
- package/dist/relations/relation.types.js.map +1 -1
- package/dist/repositories/legacy-juggler-bridge.d.ts +39 -4
- package/dist/repositories/legacy-juggler-bridge.js +63 -6
- package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
- package/dist/repositories/repository.d.ts +23 -0
- package/dist/repositories/repository.js +1 -0
- package/dist/repositories/repository.js.map +1 -1
- package/dist/transaction.d.ts +26 -0
- package/dist/transaction.js +17 -0
- package/dist/transaction.js.map +1 -0
- package/package.json +11 -11
- package/src/common-types.ts +5 -1
- package/src/decorators/model.decorator.ts +2 -0
- package/src/index.ts +6 -5
- package/src/relations/index.ts +3 -2
- package/src/relations/relation.decorator.ts +15 -5
- package/src/relations/relation.helpers.ts +134 -0
- package/src/relations/relation.types.ts +24 -0
- package/src/repositories/legacy-juggler-bridge.ts +106 -9
- package/src/repositories/repository.ts +32 -0
- package/src/transaction.ts +34 -0
|
@@ -16,6 +16,8 @@ var juggler;
|
|
|
16
16
|
juggler.ModelBase = legacy.ModelBase;
|
|
17
17
|
juggler.PersistedModel = legacy.PersistedModel;
|
|
18
18
|
juggler.KeyValueModel = legacy.KeyValueModel;
|
|
19
|
+
// eslint-disable-next-line no-shadow
|
|
20
|
+
juggler.IsolationLevel = legacy.IsolationLevel;
|
|
19
21
|
})(juggler = exports.juggler || (exports.juggler = {}));
|
|
20
22
|
function isModelClass(propertyType) {
|
|
21
23
|
return (!type_resolver_1.isTypeResolver(propertyType) &&
|
|
@@ -65,6 +67,7 @@ class DefaultCrudRepository {
|
|
|
65
67
|
entityClass, dataSource) {
|
|
66
68
|
this.entityClass = entityClass;
|
|
67
69
|
this.dataSource = dataSource;
|
|
70
|
+
this.inclusionResolvers = new Map();
|
|
68
71
|
const definition = entityClass.definition;
|
|
69
72
|
assert(!!definition, `Entity ${entityClass.name} must have valid model definition.`);
|
|
70
73
|
assert(definition.idProperties().length > 0, `Entity ${entityClass.name} must have at least one id/pk property.`);
|
|
@@ -222,21 +225,29 @@ class DefaultCrudRepository {
|
|
|
222
225
|
}
|
|
223
226
|
}
|
|
224
227
|
async find(filter, options) {
|
|
225
|
-
const
|
|
226
|
-
|
|
228
|
+
const include = filter && filter.include;
|
|
229
|
+
const models = await ensurePromise(this.modelClass.find(this.normalizeFilter(filter), options));
|
|
230
|
+
const entities = this.toEntities(models);
|
|
231
|
+
return this.includeRelatedModels(entities, include, options);
|
|
227
232
|
}
|
|
228
233
|
async findOne(filter, options) {
|
|
229
|
-
const model = await ensurePromise(this.modelClass.findOne(filter, options));
|
|
234
|
+
const model = await ensurePromise(this.modelClass.findOne(this.normalizeFilter(filter), options));
|
|
230
235
|
if (!model)
|
|
231
236
|
return null;
|
|
232
|
-
|
|
237
|
+
const entity = this.toEntity(model);
|
|
238
|
+
const include = filter && filter.include;
|
|
239
|
+
const resolved = await this.includeRelatedModels([entity], include, options);
|
|
240
|
+
return resolved[0];
|
|
233
241
|
}
|
|
234
242
|
async findById(id, filter, options) {
|
|
235
|
-
const
|
|
243
|
+
const include = filter && filter.include;
|
|
244
|
+
const model = await ensurePromise(this.modelClass.findById(id, this.normalizeFilter(filter), options));
|
|
236
245
|
if (!model) {
|
|
237
246
|
throw new errors_1.EntityNotFoundError(this.entityClass, id);
|
|
238
247
|
}
|
|
239
|
-
|
|
248
|
+
const entity = this.toEntity(model);
|
|
249
|
+
const resolved = await this.includeRelatedModels([entity], include, options);
|
|
250
|
+
return resolved[0];
|
|
240
251
|
}
|
|
241
252
|
update(entity, options) {
|
|
242
253
|
return this.updateById(entity.getId(), entity, options);
|
|
@@ -295,6 +306,52 @@ class DefaultCrudRepository {
|
|
|
295
306
|
toEntities(models) {
|
|
296
307
|
return models.map(m => this.toEntity(m));
|
|
297
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* Register an inclusion resolver for the related model name.
|
|
311
|
+
*
|
|
312
|
+
* @param relationName - Name of the relation defined on the source model
|
|
313
|
+
* @param resolver - Resolver function for getting related model entities
|
|
314
|
+
*/
|
|
315
|
+
registerInclusionResolver(relationName, resolver) {
|
|
316
|
+
this.inclusionResolvers.set(relationName, resolver);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Returns model instances that include related models of this repository
|
|
320
|
+
* that have a registered resolver.
|
|
321
|
+
*
|
|
322
|
+
* @param entities - An array of entity instances or data
|
|
323
|
+
* @param include -Inclusion filter
|
|
324
|
+
* @param options - Options for the operations
|
|
325
|
+
*/
|
|
326
|
+
async includeRelatedModels(entities, include, options) {
|
|
327
|
+
return relations_1.includeRelatedModels(this, entities, include, options);
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Removes juggler's "include" filter as it does not apply to LoopBack 4
|
|
331
|
+
* relations.
|
|
332
|
+
*
|
|
333
|
+
* @param filter - Query filter
|
|
334
|
+
*/
|
|
335
|
+
normalizeFilter(filter) {
|
|
336
|
+
if (!filter)
|
|
337
|
+
return undefined;
|
|
338
|
+
return Object.assign(Object.assign({}, filter), { include: undefined });
|
|
339
|
+
}
|
|
298
340
|
}
|
|
299
341
|
exports.DefaultCrudRepository = DefaultCrudRepository;
|
|
342
|
+
/**
|
|
343
|
+
* Default implementation of CRUD repository using legacy juggler model
|
|
344
|
+
* and data source with beginTransaction() method for connectors which
|
|
345
|
+
* support Transactions
|
|
346
|
+
*/
|
|
347
|
+
class DefaultTransactionalRepository extends DefaultCrudRepository {
|
|
348
|
+
async beginTransaction(options) {
|
|
349
|
+
const dsOptions = options || {};
|
|
350
|
+
// juggler.Transaction still has the Promise/Callback variants of the
|
|
351
|
+
// Transaction methods
|
|
352
|
+
// so we need it cast it back
|
|
353
|
+
return (await this.dataSource.beginTransaction(dsOptions));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
exports.DefaultTransactionalRepository = DefaultTransactionalRepository;
|
|
300
357
|
//# sourceMappingURL=legacy-juggler-bridge.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"legacy-juggler-bridge.js","sourceRoot":"","sources":["../../src/repositories/legacy-juggler-bridge.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;AAGhE,iCAAiC;AACjC,sDAAsD;AAUtD,sCAA8C;AAG9C,
|
|
1
|
+
{"version":3,"file":"legacy-juggler-bridge.js","sourceRoot":"","sources":["../../src/repositories/legacy-juggler-bridge.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;AAGhE,iCAAiC;AACjC,sDAAsD;AAUtD,sCAA8C;AAG9C,4CAYsB;AAEtB,oDAA6D;AAM7D,IAAiB,OAAO,CAYvB;AAZD,WAAiB,OAAO;IACtB,sDAAsD;IACxC,kBAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAC/B,iBAAS,GAAG,MAAM,CAAC,SAAS,CAAC;IAE7B,sBAAc,GAAG,MAAM,CAAC,cAAc,CAAC;IACvC,qBAAa,GAAG,MAAM,CAAC,a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C,CAAC;IAC9E,CAAC;IAED;;;;;OAKG;IACO,eAAe,CAAC,MAAkB;QAC1C,IAAI,CAAC,MAAM;YAAE,OAAO,SAAS,CAAC;QAC9B,OAAO,gCAAI,MAAM,KAAE,OAAO,EAAE,SAAS,GAAkB,CAAC;IAC1D,CAAC;CACF;AAvbD,sDAubC;AAED;;;;GAIG;AAEH,MAAa,8BAIX,SAAQ,qBAAuC;IAE/C,KAAK,CAAC,gBAAgB,CACpB,OAAkC;QAElC,MAAM,SAAS,GAAqC,OAAO,IAAI,EAAE,CAAC;QAClE,qEAAqE;QACrE,sBAAsB;QACtB,6BAA6B;QAC7B,OAAO,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAgB,CAAC;IAC5E,CAAC;CACF;AAfD,wEAeC"}
|
|
@@ -2,6 +2,8 @@ import { AnyObject, Command, Count, DataObject, NamedParameters, Options, Positi
|
|
|
2
2
|
import { DataSource } from '../datasource';
|
|
3
3
|
import { Entity, Model, ValueObject } from '../model';
|
|
4
4
|
import { Filter, Where } from '../query';
|
|
5
|
+
import { InclusionResolver } from '../relations/relation.types';
|
|
6
|
+
import { IsolationLevel, Transaction } from '../transaction';
|
|
5
7
|
export interface Repository<T extends Model> {
|
|
6
8
|
}
|
|
7
9
|
export interface ExecutableRepository<T extends Model> extends Repository<T> {
|
|
@@ -14,6 +16,25 @@ export interface ExecutableRepository<T extends Model> extends Repository<T> {
|
|
|
14
16
|
*/
|
|
15
17
|
execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise<AnyObject>;
|
|
16
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* A type for CRUD repositories that are backed by IDs and support
|
|
21
|
+
* Transactions
|
|
22
|
+
*/
|
|
23
|
+
export declare type TransactionalEntityRepository<T extends Entity, ID, Relations extends object = {}> = TransactionalRepository<T> & EntityCrudRepository<T, ID>;
|
|
24
|
+
/**
|
|
25
|
+
* Repository Interface for Repositories that support Transactions
|
|
26
|
+
*
|
|
27
|
+
* @typeParam T Generic type for the Entity
|
|
28
|
+
*/
|
|
29
|
+
export interface TransactionalRepository<T extends Entity> extends Repository<T> {
|
|
30
|
+
/**
|
|
31
|
+
* Begin a new Transaction
|
|
32
|
+
* @param options - Options for the operations
|
|
33
|
+
* @returns Promise<Transaction> Promise that resolves to a new Transaction
|
|
34
|
+
* object
|
|
35
|
+
*/
|
|
36
|
+
beginTransaction(options?: IsolationLevel | Options): Promise<Transaction>;
|
|
37
|
+
}
|
|
17
38
|
/**
|
|
18
39
|
* Basic CRUD operations for ValueObject and Entity. No ID is required.
|
|
19
40
|
*/
|
|
@@ -74,6 +95,7 @@ export interface EntityCrudRepository<T extends Entity, ID, Relations extends ob
|
|
|
74
95
|
entityClass: typeof Entity & {
|
|
75
96
|
prototype: T;
|
|
76
97
|
};
|
|
98
|
+
inclusionResolvers: Map<string, InclusionResolver<T, Entity>>;
|
|
77
99
|
/**
|
|
78
100
|
* Save an entity. If no id is present, create a new entity
|
|
79
101
|
* @param entity - Entity to be saved
|
|
@@ -170,6 +192,7 @@ export declare class CrudRepositoryImpl<T extends Entity, ID> implements EntityC
|
|
|
170
192
|
prototype: T;
|
|
171
193
|
};
|
|
172
194
|
private connector;
|
|
195
|
+
readonly inclusionResolvers: Map<string, InclusionResolver<T, Entity>>;
|
|
173
196
|
constructor(dataSource: DataSource, entityClass: typeof Entity & {
|
|
174
197
|
prototype: T;
|
|
175
198
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../src/repositories/repository.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;AAahE,sCAA8C;
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../../src/repositories/repository.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;AAahE,sCAA8C;A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
|
5
|
+
/**
|
|
6
|
+
* Commit the transaction
|
|
7
|
+
*/
|
|
8
|
+
commit(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Rollback the transaction
|
|
11
|
+
*/
|
|
12
|
+
rollback(): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* The transaction Identifier
|
|
15
|
+
*/
|
|
16
|
+
id: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Isolation level
|
|
20
|
+
*/
|
|
21
|
+
export declare enum IsolationLevel {
|
|
22
|
+
READ_COMMITTED = "READ COMMITTED",
|
|
23
|
+
READ_UNCOMMITTED = "READ UNCOMMITTED",
|
|
24
|
+
SERIALIZABLE = "SERIALIZABLE",
|
|
25
|
+
REPEATABLE_READ = "REPEATABLE READ"
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright IBM Corp. 2019. All Rights Reserved.
|
|
3
|
+
// Node module: @loopback/repository
|
|
4
|
+
// This file is licensed under the MIT License.
|
|
5
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
/**
|
|
8
|
+
* Isolation level
|
|
9
|
+
*/
|
|
10
|
+
var IsolationLevel;
|
|
11
|
+
(function (IsolationLevel) {
|
|
12
|
+
IsolationLevel["READ_COMMITTED"] = "READ COMMITTED";
|
|
13
|
+
IsolationLevel["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
|
|
14
|
+
IsolationLevel["SERIALIZABLE"] = "SERIALIZABLE";
|
|
15
|
+
IsolationLevel["REPEATABLE_READ"] = "REPEATABLE READ";
|
|
16
|
+
})(IsolationLevel = exports.IsolationLevel || (exports.IsolationLevel = {}));
|
|
17
|
+
//# sourceMappingURL=transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.js","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,oCAAoC;AACpC,+CAA+C;AAC/C,gEAAgE;;AAsBhE;;GAEG;AACH,IAAY,cAKX;AALD,WAAY,cAAc;IACxB,mDAAiC,CAAA;IACjC,uDAAqC,CAAA;IACrC,+CAA6B,CAAA;IAC7B,qDAAmC,CAAA;AACrC,CAAC,EALW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAKzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loopback/repository",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.1",
|
|
4
4
|
"description": "Repository based persistence for LoopBack 4",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=8.9"
|
|
@@ -18,19 +18,19 @@
|
|
|
18
18
|
"copyright.owner": "IBM Corp.",
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@loopback/build": "^2.0.
|
|
22
|
-
"@loopback/eslint-config": "^
|
|
23
|
-
"@loopback/testlab": "^1.
|
|
24
|
-
"@types/lodash": "^4.14.
|
|
25
|
-
"@types/node": "^10.14.
|
|
21
|
+
"@loopback/build": "^2.0.10",
|
|
22
|
+
"@loopback/eslint-config": "^4.0.2",
|
|
23
|
+
"@loopback/testlab": "^1.8.0",
|
|
24
|
+
"@types/lodash": "^4.14.138",
|
|
25
|
+
"@types/node": "^10.14.17"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@loopback/context": "^1.
|
|
29
|
-
"@loopback/core": "^1.
|
|
30
|
-
"@types/debug": "^4.1.
|
|
28
|
+
"@loopback/context": "^1.22.1",
|
|
29
|
+
"@loopback/core": "^1.10.1",
|
|
30
|
+
"@types/debug": "^4.1.5",
|
|
31
31
|
"debug": "^4.1.1",
|
|
32
32
|
"lodash": "^4.17.15",
|
|
33
|
-
"loopback-datasource-juggler": "^4.
|
|
33
|
+
"loopback-datasource-juggler": "^4.12.1"
|
|
34
34
|
},
|
|
35
35
|
"files": [
|
|
36
36
|
"README.md",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"url": "https://github.com/strongloop/loopback-next.git",
|
|
46
46
|
"directory": "packages/repository"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "4e653bbff797b26437bd1b0817ed9b8c91f3477b"
|
|
49
49
|
}
|
package/src/common-types.ts
CHANGED
|
@@ -49,7 +49,11 @@ export interface AnyObject {
|
|
|
49
49
|
* An extension of the built-in Partial<T> type which allows partial values
|
|
50
50
|
* in deeply nested properties too.
|
|
51
51
|
*/
|
|
52
|
-
|
|
52
|
+
// FIXME(rfeng): https://github.com/microsoft/TypeScript/issues/21592#issuecomment-496723647
|
|
53
|
+
// export type DeepPartial<T> = {[P in keyof T]?: DeepPartial<T[P]>};
|
|
54
|
+
export type DeepPartial<T> = {
|
|
55
|
+
[P in keyof T]?: T[P] extends never ? DeepPartial<T[P]> : DeepPartial<T[P]>;
|
|
56
|
+
};
|
|
53
57
|
|
|
54
58
|
/**
|
|
55
59
|
* Type alias for strongly or weakly typed objects of T
|
|
@@ -48,6 +48,7 @@ export function model(definition?: Partial<ModelDefinitionSyntax>) {
|
|
|
48
48
|
const decorator = ClassDecoratorFactory.createDecorator(
|
|
49
49
|
MODEL_KEY,
|
|
50
50
|
definition,
|
|
51
|
+
{decoratorName: '@model'},
|
|
51
52
|
);
|
|
52
53
|
|
|
53
54
|
decorator(target);
|
|
@@ -111,6 +112,7 @@ export function property(definition?: Partial<PropertyDefinition>) {
|
|
|
111
112
|
return PropertyDecoratorFactory.createDecorator(
|
|
112
113
|
MODEL_PROPERTIES_KEY,
|
|
113
114
|
Object.assign({}, definition),
|
|
115
|
+
{decoratorName: '@property'},
|
|
114
116
|
);
|
|
115
117
|
}
|
|
116
118
|
|
package/src/index.ts
CHANGED
|
@@ -3,15 +3,16 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
+
export * from './common-types';
|
|
6
7
|
export * from './connectors';
|
|
8
|
+
export * from './datasource';
|
|
7
9
|
export * from './decorators';
|
|
8
10
|
export * from './errors';
|
|
9
11
|
export * from './mixins';
|
|
10
|
-
export * from './repositories';
|
|
11
|
-
export * from './types';
|
|
12
|
-
export * from './common-types';
|
|
13
|
-
export * from './datasource';
|
|
14
12
|
export * from './model';
|
|
15
13
|
export * from './query';
|
|
16
|
-
export * from './type-resolver';
|
|
17
14
|
export * from './relations';
|
|
15
|
+
export * from './repositories';
|
|
16
|
+
export * from './transaction';
|
|
17
|
+
export * from './type-resolver';
|
|
18
|
+
export * from './types';
|
package/src/relations/index.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
-
export * from './relation.types';
|
|
7
|
-
export * from './relation.decorator';
|
|
8
6
|
export * from './belongs-to';
|
|
9
7
|
export * from './has-many';
|
|
10
8
|
export * from './has-one';
|
|
9
|
+
export * from './relation.decorator';
|
|
10
|
+
export * from './relation.helpers';
|
|
11
|
+
export * from './relation.types';
|
|
@@ -17,7 +17,9 @@ export const RELATIONS_KEY = 'loopback:relations';
|
|
|
17
17
|
*/
|
|
18
18
|
export function relation(definition?: Object) {
|
|
19
19
|
// Apply relation definition to the model class
|
|
20
|
-
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, definition
|
|
20
|
+
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, definition, {
|
|
21
|
+
decoratorName: '@relation',
|
|
22
|
+
});
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
/**
|
|
@@ -46,7 +48,9 @@ export function getModelRelations(
|
|
|
46
48
|
*/
|
|
47
49
|
export function embedsOne(definition?: Object) {
|
|
48
50
|
const rel = Object.assign({type: RelationType.embedsOne}, definition);
|
|
49
|
-
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel
|
|
51
|
+
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel, {
|
|
52
|
+
decoratorName: '@embedsOne',
|
|
53
|
+
});
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
/**
|
|
@@ -56,7 +60,9 @@ export function embedsOne(definition?: Object) {
|
|
|
56
60
|
*/
|
|
57
61
|
export function embedsMany(definition?: Object) {
|
|
58
62
|
const rel = Object.assign({type: RelationType.embedsMany}, definition);
|
|
59
|
-
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel
|
|
63
|
+
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel, {
|
|
64
|
+
decoratorName: '@embedsMany',
|
|
65
|
+
});
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
/**
|
|
@@ -66,7 +72,9 @@ export function embedsMany(definition?: Object) {
|
|
|
66
72
|
*/
|
|
67
73
|
export function referencesOne(definition?: Object) {
|
|
68
74
|
const rel = Object.assign({type: RelationType.referencesOne}, definition);
|
|
69
|
-
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel
|
|
75
|
+
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel, {
|
|
76
|
+
decoratorName: '@referencesOne',
|
|
77
|
+
});
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
/**
|
|
@@ -76,5 +84,7 @@ export function referencesOne(definition?: Object) {
|
|
|
76
84
|
*/
|
|
77
85
|
export function referencesMany(definition?: Object) {
|
|
78
86
|
const rel = Object.assign({type: RelationType.referencesMany}, definition);
|
|
79
|
-
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel
|
|
87
|
+
return PropertyDecoratorFactory.createDecorator(RELATIONS_KEY, rel, {
|
|
88
|
+
decoratorName: '@referencesMany',
|
|
89
|
+
});
|
|
80
90
|
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Copyright IBM Corp. 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 * as debugFactory from 'debug';
|
|
7
|
+
import * as _ from 'lodash';
|
|
8
|
+
import {
|
|
9
|
+
AnyObject,
|
|
10
|
+
Entity,
|
|
11
|
+
EntityCrudRepository,
|
|
12
|
+
Filter,
|
|
13
|
+
Inclusion,
|
|
14
|
+
Options,
|
|
15
|
+
Where,
|
|
16
|
+
} from '..';
|
|
17
|
+
const debug = debugFactory('loopback:repository:relation-helpers');
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Finds model instances that contain any of the provided foreign key values.
|
|
21
|
+
*
|
|
22
|
+
* @param targetRepository - The target repository where the model instances are found
|
|
23
|
+
* @param fkName - Name of the foreign key
|
|
24
|
+
* @param fkValues - One value or array of values of the foreign key to be included
|
|
25
|
+
* @param scope - Additional scope constraints (not currently supported)
|
|
26
|
+
* @param options - Options for the operations
|
|
27
|
+
*/
|
|
28
|
+
export async function findByForeignKeys<
|
|
29
|
+
Target extends Entity,
|
|
30
|
+
TargetRelations extends object,
|
|
31
|
+
ForeignKey extends StringKeyOf<Target>
|
|
32
|
+
>(
|
|
33
|
+
targetRepository: EntityCrudRepository<Target, unknown, TargetRelations>,
|
|
34
|
+
fkName: ForeignKey,
|
|
35
|
+
fkValues: Target[ForeignKey][] | Target[ForeignKey],
|
|
36
|
+
scope?: Filter<Target>,
|
|
37
|
+
options?: Options,
|
|
38
|
+
): Promise<(Target & TargetRelations)[]> {
|
|
39
|
+
// throw error if scope is defined and non-empty
|
|
40
|
+
// see https://github.com/strongloop/loopback-next/issues/3453
|
|
41
|
+
if (scope && !_.isEmpty(scope)) {
|
|
42
|
+
throw new Error('scope is not supported');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let value;
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(fkValues)) {
|
|
48
|
+
if (fkValues.length === 0) return [];
|
|
49
|
+
value = fkValues.length === 1 ? fkValues[0] : {inq: fkValues};
|
|
50
|
+
} else {
|
|
51
|
+
value = fkValues;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const where = ({[fkName]: value} as unknown) as Where<Target>;
|
|
55
|
+
const targetFilter = {where};
|
|
56
|
+
|
|
57
|
+
return targetRepository.find(targetFilter, options);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type StringKeyOf<T> = Extract<keyof T, string>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns model instances that include related models that have a registered
|
|
64
|
+
* resolver.
|
|
65
|
+
*
|
|
66
|
+
* @param targetRepository - The target repository where the model instances are found
|
|
67
|
+
* @param entities - An array of entity instances or data
|
|
68
|
+
* @param include -Inclusion filter
|
|
69
|
+
* @param options - Options for the operations
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
export async function includeRelatedModels<
|
|
73
|
+
T extends Entity,
|
|
74
|
+
Relations extends object = {}
|
|
75
|
+
>(
|
|
76
|
+
targetRepository: EntityCrudRepository<T, unknown, Relations>,
|
|
77
|
+
entities: T[],
|
|
78
|
+
include?: Inclusion<T>[],
|
|
79
|
+
options?: Options,
|
|
80
|
+
): Promise<(T & Relations)[]> {
|
|
81
|
+
const result = entities as (T & Relations)[];
|
|
82
|
+
if (!include) return result;
|
|
83
|
+
|
|
84
|
+
const invalidInclusions = include.filter(
|
|
85
|
+
inclusionFilter => !isInclusionAllowed(targetRepository, inclusionFilter),
|
|
86
|
+
);
|
|
87
|
+
if (invalidInclusions.length) {
|
|
88
|
+
const msg =
|
|
89
|
+
'Invalid "filter.include" entries: ' +
|
|
90
|
+
invalidInclusions
|
|
91
|
+
.map(inclusionFilter => JSON.stringify(inclusionFilter))
|
|
92
|
+
.join('; ');
|
|
93
|
+
const err = new Error(msg);
|
|
94
|
+
Object.assign(err, {
|
|
95
|
+
code: 'INVALID_INCLUSION_FILTER',
|
|
96
|
+
});
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const resolveTasks = include.map(async inclusionFilter => {
|
|
101
|
+
const relationName = inclusionFilter.relation;
|
|
102
|
+
const resolver = targetRepository.inclusionResolvers.get(relationName)!;
|
|
103
|
+
const targets = await resolver(entities, inclusionFilter, options);
|
|
104
|
+
|
|
105
|
+
result.forEach((entity, ix) => {
|
|
106
|
+
const src = entity as AnyObject;
|
|
107
|
+
src[relationName] = targets[ix];
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
await Promise.all(resolveTasks);
|
|
112
|
+
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Checks if the resolver of the inclusion relation is registered
|
|
117
|
+
* in the inclusionResolver of the target repository
|
|
118
|
+
*
|
|
119
|
+
* @param targetRepository - The target repository where the relations are registered
|
|
120
|
+
* @param include - Inclusion filter
|
|
121
|
+
*/
|
|
122
|
+
function isInclusionAllowed<T extends Entity, Relations extends object = {}>(
|
|
123
|
+
targetRepository: EntityCrudRepository<T, unknown, Relations>,
|
|
124
|
+
include: Inclusion,
|
|
125
|
+
): boolean {
|
|
126
|
+
const relationName = include.relation;
|
|
127
|
+
if (!relationName) {
|
|
128
|
+
debug('isInclusionAllowed for %j? No: missing relation name', include);
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
const allowed = targetRepository.inclusionResolvers.has(relationName);
|
|
132
|
+
debug('isInclusionAllowed for %j (relation %s)? %s', include, allowed);
|
|
133
|
+
return allowed;
|
|
134
|
+
}
|
|
@@ -3,7 +3,9 @@
|
|
|
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 {Options} from '../common-types';
|
|
6
7
|
import {Entity} from '../model';
|
|
8
|
+
import {Inclusion} from '../query';
|
|
7
9
|
import {TypeResolver} from '../type-resolver';
|
|
8
10
|
|
|
9
11
|
export enum RelationType {
|
|
@@ -108,3 +110,25 @@ export type RelationMetadata =
|
|
|
108
110
|
|
|
109
111
|
// Re-export Getter so that users don't have to import from @loopback/context
|
|
110
112
|
export {Getter} from '@loopback/context';
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @returns An array of resolved values, the items must be ordered in the same
|
|
116
|
+
* way as `sourceEntities`. The resolved value can be one of:
|
|
117
|
+
* - `undefined` when no target model(s) were found
|
|
118
|
+
* - `Entity` for relations targeting a single model
|
|
119
|
+
* - `Entity[]` for relations targeting multiple models
|
|
120
|
+
*/
|
|
121
|
+
export type InclusionResolver<S extends Entity, T extends Entity> = (
|
|
122
|
+
/**
|
|
123
|
+
* List of source models as returned by the first database query.
|
|
124
|
+
*/
|
|
125
|
+
sourceEntities: S[],
|
|
126
|
+
/**
|
|
127
|
+
* Inclusion requested by the user (e.g. scope constraints to apply).
|
|
128
|
+
*/
|
|
129
|
+
inclusion: Inclusion,
|
|
130
|
+
/**
|
|
131
|
+
* Generic options object, e.g. carrying the Transaction object.
|
|
132
|
+
*/
|
|
133
|
+
options?: Options,
|
|
134
|
+
) => Promise<(T | undefined)[] | (T[] | undefined)[]>;
|