@biorate/connector 0.28.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
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
- }
package/src/interfaces.ts DELETED
@@ -1,9 +0,0 @@
1
- export interface IConnectorConfig {
2
- name: string;
3
- }
4
-
5
- export interface IConnector<C extends IConnectorConfig, T = any> {
6
- readonly connections: Map<string, T>;
7
- use(name: string): void;
8
- connection(name?: string): T;
9
- }