UNPKG

4.9 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. 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
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.repository = exports.RepositoryMetadata = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const assert_1 = tslib_1.__importDefault(require("assert"));
11const repositories_1 = require("../repositories");
12const legacy_juggler_bridge_1 = require("../repositories/legacy-juggler-bridge");
13/**
14 * Metadata for a repository
15 */
16class RepositoryMetadata {
17 /**
18 * Constructor for RepositoryMetadata
19 *
20 * @param modelOrRepo - Name or class of the model. If the value is a string and
21 * `dataSource` is not present, it will treated as the name of a predefined
22 * repository
23 * @param dataSource - Name or instance of the data source
24 *
25 * For example:
26 *
27 * - new RepositoryMetadata(repoName);
28 * - new RepositoryMetadata(modelName, dataSourceName);
29 * - new RepositoryMetadata(modelClass, dataSourceInstance);
30 * - new RepositoryMetadata(modelName, dataSourceInstance);
31 * - new RepositoryMetadata(modelClass, dataSourceName);
32 */
33 constructor(modelOrRepo, dataSource) {
34 this.name =
35 typeof modelOrRepo === 'string' && dataSource === undefined
36 ? modelOrRepo
37 : undefined;
38 this.modelName =
39 typeof modelOrRepo === 'string' && dataSource != null
40 ? modelOrRepo
41 : undefined;
42 this.modelClass =
43 typeof modelOrRepo === 'function' ? modelOrRepo : undefined;
44 this.dataSourceName =
45 typeof dataSource === 'string' ? dataSource : undefined;
46 this.dataSource = typeof dataSource === 'object' ? dataSource : undefined;
47 }
48}
49exports.RepositoryMetadata = RepositoryMetadata;
50function repository(modelOrRepo, dataSource) {
51 // if string, repository or not a model ctor,
52 // keep it a string / assign to ctor's name (string) for DI
53 const stringOrModel = typeof modelOrRepo !== 'string' && !modelOrRepo.prototype.getId
54 ? modelOrRepo.name
55 : modelOrRepo;
56 const meta = new RepositoryMetadata(stringOrModel, dataSource);
57 return function (target, key,
58 // eslint-disable-next-line @typescript-eslint/no-explicit-any
59 descriptorOrIndex) {
60 if (key || typeof descriptorOrIndex === 'number') {
61 if (meta.name) {
62 // Make it shortcut to `@inject('repositories.MyRepo')`
63 // Please note key is undefined for constructor. If strictNullChecks
64 // is true, the compiler will complain as reflect-metadata won't
65 // accept undefined or null for key. Use ! to fool the compiler.
66 (0, core_1.inject)('repositories.' + meta.name, meta)(target, key, descriptorOrIndex);
67 }
68 else {
69 // Use repository-factory to create a repository from model + dataSource
70 (0, core_1.inject)('', meta, resolve)(target, key, descriptorOrIndex);
71 }
72 return;
73 }
74 // Mixin repository into the class
75 throw new Error('Class level @repository is not implemented');
76 };
77}
78exports.repository = repository;
79(function (repository) {
80 /**
81 * Decorator used to inject a Getter for a repository
82 * Mainly intended for usage with repository injections on relation repository
83 * factory
84 * @param nameOrClass - The repository class (ProductRepository) or a string name ('ProductRepository').
85 */
86 function getter(nameOrClass) {
87 const name = typeof nameOrClass === 'string' ? nameOrClass : nameOrClass.name;
88 return core_1.inject.getter(`repositories.${name}`);
89 }
90 repository.getter = getter;
91})(repository = exports.repository || (exports.repository = {}));
92/**
93 * Resolve the @repository injection
94 * @param ctx - Context
95 * @param injection - Injection metadata
96 */
97async function resolve(ctx, injection) {
98 const meta = injection.metadata;
99 let modelClass = meta.modelClass;
100 if (meta.modelName) {
101 modelClass = (await ctx.get('models.' + meta.modelName));
102 }
103 if (!modelClass) {
104 throw new Error('Invalid repository config: ' +
105 ' neither modelClass nor modelName was specified.');
106 }
107 let dataSource = meta.dataSource;
108 if (meta.dataSourceName) {
109 dataSource = await ctx.get('datasources.' + meta.dataSourceName);
110 }
111 (0, assert_1.default)(dataSource instanceof legacy_juggler_bridge_1.juggler.DataSource, 'DataSource must be provided');
112 return new repositories_1.DefaultCrudRepository(modelClass, dataSource);
113}
114//# sourceMappingURL=repository.decorator.js.map
\No newline at end of file