1
+ import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions" ;
2
+ import { LokiJsConnection } from "@js-soft/docdb-access-loki" ;
3
+ import { MongoDbConnection } from "@js-soft/docdb-access-mongo" ;
4
+ import { NodeLoggerFactory } from "@js-soft/node-logger" ;
5
+ import { EventEmitter2EventBus } from "@js-soft/ts-utils" ;
6
+ import { Transport } from "@nmshd/transport" ;
1
7
import yargs from "yargs" ;
8
+ import { ConnectorRuntimeConfig , createConnectorConfig } from "../ConnectorRuntimeConfig" ;
9
+ import { DocumentationLink } from "../DocumentationLink" ;
2
10
3
11
export interface ConfigFileOptions {
4
12
config : string | undefined ;
@@ -13,3 +21,76 @@ Can also be set via the CUSTOM_CONFIG_LOCATION env variable`,
13
21
demandOption : false
14
22
} ) ;
15
23
} ;
24
+
25
+ export abstract class BaseCommand {
26
+ protected transport ?: Transport ;
27
+ protected connectorConfig ?: ConnectorRuntimeConfig ;
28
+ protected log = console ;
29
+
30
+ public async run ( configPath : string | undefined ) : Promise < void > {
31
+ let databaseConnection ;
32
+ let logger ;
33
+ try {
34
+ this . connectorConfig = createConnectorConfig ( undefined , configPath ) ;
35
+ this . connectorConfig . transportLibrary . allowIdentityCreation = true ;
36
+ this . connectorConfig . logging = {
37
+ appenders : {
38
+ console : { type : "console" }
39
+ } ,
40
+ categories : {
41
+ default : { appenders : [ "console" ] , level : "OFF" }
42
+ }
43
+ } ;
44
+
45
+ const eventBus = new EventEmitter2EventBus ( ( ) => {
46
+ // ignore errors
47
+ } ) ;
48
+ logger = new NodeLoggerFactory ( this . connectorConfig . logging ) ;
49
+ databaseConnection = await BaseCommand . createDBConnection ( this . connectorConfig ) ;
50
+
51
+ this . transport = new Transport ( databaseConnection , { ...this . connectorConfig . transportLibrary , supportedIdentityVersion : 1 } , eventBus , logger ) ;
52
+ await this . transport . init ( ) ;
53
+
54
+ return await this . runInternal ( ) ;
55
+ } catch ( error : any ) {
56
+ this . log . error ( "Error creating identity: " , error . message ) ;
57
+ } finally {
58
+ if ( databaseConnection ) {
59
+ await databaseConnection . close ( ) ;
60
+ }
61
+ if ( this . transport ) {
62
+ await this . transport . eventBus . close ( ) ;
63
+ }
64
+ if ( logger ) {
65
+ logger . close ( ) ;
66
+ }
67
+ }
68
+ }
69
+
70
+ public abstract runInternal ( ) : Promise < any > ;
71
+
72
+ public static async createDBConnection ( runtimeConfig : ConnectorRuntimeConfig ) : Promise < IDatabaseConnection > {
73
+ if ( runtimeConfig . database . driver === "lokijs" ) {
74
+ if ( ! runtimeConfig . debug ) throw new Error ( "LokiJS is only available in debug mode." ) ;
75
+
76
+ const folder = runtimeConfig . database . folder ;
77
+ if ( ! folder ) throw new Error ( "No folder provided for LokiJS database." ) ;
78
+
79
+ return new LokiJsConnection ( folder , undefined , { autoload : true , autosave : true , persistenceMethod : "fs" } ) ;
80
+ }
81
+
82
+ if ( ! runtimeConfig . database . connectionString ) {
83
+ throw new Error ( `No database connection string provided. See ${ DocumentationLink . operate__configuration ( "database" ) } on how to configure the database connection.` ) ;
84
+ }
85
+
86
+ const mongodbConnection = new MongoDbConnection ( runtimeConfig . database . connectionString ) ;
87
+
88
+ try {
89
+ await mongodbConnection . connect ( ) ;
90
+ } catch ( e ) {
91
+ throw new Error ( `Could not connect to the configured database. Try to check the connection string and the database status. Root error: ${ e } ` ) ;
92
+ }
93
+
94
+ return mongodbConnection ;
95
+ }
96
+ }
0 commit comments