1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.repository = exports.RepositoryMetadata = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const core_1 = require("@loopback/core");
|
10 | const assert_1 = tslib_1.__importDefault(require("assert"));
|
11 | const repositories_1 = require("../repositories");
|
12 | const legacy_juggler_bridge_1 = require("../repositories/legacy-juggler-bridge");
|
13 |
|
14 |
|
15 |
|
16 | class RepositoryMetadata {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
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 | }
|
49 | exports.RepositoryMetadata = RepositoryMetadata;
|
50 | function repository(modelOrRepo, dataSource) {
|
51 |
|
52 |
|
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 |
|
63 |
|
64 |
|
65 |
|
66 | (0, core_1.inject)('repositories.' + meta.name, meta)(target, key, descriptorOrIndex);
|
67 | }
|
68 | else {
|
69 |
|
70 | (0, core_1.inject)('', meta, resolve)(target, key, descriptorOrIndex);
|
71 | }
|
72 | return;
|
73 | }
|
74 |
|
75 | throw new Error('Class level @repository is not implemented');
|
76 | };
|
77 | }
|
78 | exports.repository = repository;
|
79 | (function (repository) {
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
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 = repository = {}));
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | async 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 |
|
\ | No newline at end of file |