Skip to content

Commit 34672d6

Browse files
committed
chore: add base command
1 parent 96cd213 commit 34672d6

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/cli/BaseCommand.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
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";
17
import yargs from "yargs";
8+
import { ConnectorRuntimeConfig, createConnectorConfig } from "../ConnectorRuntimeConfig";
9+
import { DocumentationLink } from "../DocumentationLink";
210

311
export interface ConfigFileOptions {
412
config: string | undefined;
@@ -13,3 +21,76 @@ Can also be set via the CUSTOM_CONFIG_LOCATION env variable`,
1321
demandOption: false
1422
});
1523
};
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

Comments
 (0)