@biorate/connector 0.28.0 → 0.30.0
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/.nyc_output/9094869e-bb8f-4fce-b5de-b16be1e9c26f.json +1 -0
- package/.nyc_output/processinfo/9094869e-bb8f-4fce-b5de-b16be1e9c26f.json +1 -0
- package/.nyc_output/processinfo/index.json +1 -0
- package/CHANGELOG.md +41 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/connector/index.html +116 -0
- package/coverage/lcov-report/connector/index.ts.html +88 -0
- package/coverage/lcov-report/connector/src/errors.ts.html +106 -0
- package/coverage/lcov-report/connector/src/index.html +131 -0
- package/coverage/lcov-report/connector/src/index.ts.html +454 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +77 -0
- package/dist/src/index.js +29 -10
- package/dist/src/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/index.ts +0 -1
- package/src/errors.ts +0 -7
- package/src/index.ts +0 -111
- package/src/interfaces.ts +0 -9
package/src/index.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { init, inject, injectable, Types } from '@biorate/inversion';
|
|
2
|
-
import { IConfig } from '@biorate/config';
|
|
3
|
-
import { IConnector, IConnectorConfig } from './interfaces';
|
|
4
|
-
import { ConnectorConnectionNotExistsError } from './errors';
|
|
5
|
-
export * from './errors';
|
|
6
|
-
export * from './interfaces';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @description Connector interface
|
|
10
|
-
*
|
|
11
|
-
* ### Features:
|
|
12
|
-
* - Common interface for all connectors
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```
|
|
16
|
-
* import { Connector, IConnector } from '../..';
|
|
17
|
-
* import { inject, container, Types, Core } from '@biorate/inversion';
|
|
18
|
-
* import { IConfig, Config } from '@biorate/config';
|
|
19
|
-
*
|
|
20
|
-
* export class Connection {
|
|
21
|
-
* public name: string;
|
|
22
|
-
*
|
|
23
|
-
* public constructor(name: string) {
|
|
24
|
-
* this.name = name;
|
|
25
|
-
* }
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* export class TestConnector extends Connector<{ name: string }, Connection> {
|
|
29
|
-
* protected namespace = 'TestConnector';
|
|
30
|
-
*
|
|
31
|
-
* protected async connect(config) {
|
|
32
|
-
* return new Connection(config.name);
|
|
33
|
-
* }
|
|
34
|
-
* }
|
|
35
|
-
*
|
|
36
|
-
* export class Root extends Core() {
|
|
37
|
-
* @inject(TestConnector) public connector: IConnector<{ name: string }, Connection>;
|
|
38
|
-
* }
|
|
39
|
-
*
|
|
40
|
-
* container.bind(Types.Config).to(Config).inSingletonScope();
|
|
41
|
-
* container.bind(TestConnector).toSelf().inSingletonScope();
|
|
42
|
-
* container.bind(Root).toSelf().inSingletonScope();
|
|
43
|
-
*
|
|
44
|
-
* container.get<IConfig>(Types.Config).merge({
|
|
45
|
-
* TestConnector: [{ name: 'test-connection' }],
|
|
46
|
-
* });
|
|
47
|
-
*
|
|
48
|
-
* (async () => {
|
|
49
|
-
* const root = container.get<Root>(Root);
|
|
50
|
-
* await root.$run();
|
|
51
|
-
* console.log(root.connector.connection('test-connection')); // Connection { name: 'test-connection' }
|
|
52
|
-
* })();
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
@injectable()
|
|
56
|
-
export abstract class Connector<C extends IConnectorConfig, T = any>
|
|
57
|
-
implements IConnector<C, T>
|
|
58
|
-
{
|
|
59
|
-
/**
|
|
60
|
-
* @description Config dependency
|
|
61
|
-
*/
|
|
62
|
-
@inject(Types.Config) protected config: IConfig;
|
|
63
|
-
/**
|
|
64
|
-
* @description Namespace path for fetching configuration
|
|
65
|
-
*/
|
|
66
|
-
protected abstract readonly namespace: string;
|
|
67
|
-
/**
|
|
68
|
-
* @description Connections storage
|
|
69
|
-
*/
|
|
70
|
-
public readonly connections = new Map<string, T>();
|
|
71
|
-
/**
|
|
72
|
-
* @description Link to selected (used) connection
|
|
73
|
-
*/
|
|
74
|
-
public current: T = null;
|
|
75
|
-
/**
|
|
76
|
-
* @description Abstract method describing connection
|
|
77
|
-
*/
|
|
78
|
-
protected abstract connect(config: C): Promise<T>;
|
|
79
|
-
/**
|
|
80
|
-
* @description Method for change current connection
|
|
81
|
-
*/
|
|
82
|
-
public use(name: string) {
|
|
83
|
-
if (!this.connections.has(name))
|
|
84
|
-
throw new ConnectorConnectionNotExistsError(this.constructor.name, name);
|
|
85
|
-
this.current = this.connections.get(name);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* @description Method for get existed the connection
|
|
89
|
-
*/
|
|
90
|
-
public connection(name?: string) {
|
|
91
|
-
if (!name) return this.current;
|
|
92
|
-
if (!this.connections.has(name))
|
|
93
|
-
throw new ConnectorConnectionNotExistsError(this.constructor.name, name);
|
|
94
|
-
return this.connections.get(name);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* @description Alias for connection method
|
|
98
|
-
*/
|
|
99
|
-
public get(name?: string) {
|
|
100
|
-
return this.connection(name);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* @description Initialize method
|
|
104
|
-
*/
|
|
105
|
-
@init() protected async initialize() {
|
|
106
|
-
for (const config of this.config.get<C[]>(this.namespace, [])) {
|
|
107
|
-
this.connections.set(config.name, await this.connect(config));
|
|
108
|
-
if (!this.current) this.current = this.connections.get(config.name);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|