From d5603918a8ea990f7dcc11b612dea931aa084fdc Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 3 Sep 2021 10:28:01 +0200 Subject: [PATCH] Cleanup MikroORM configuration code for performance storage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/performance/storage/MikroORMStorage.ts | 21 +++++++++++++++++++-- src/utils/Configuration.ts | 19 ++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/performance/storage/MikroORMStorage.ts b/src/performance/storage/MikroORMStorage.ts index 4d39dc74..6afb8203 100644 --- a/src/performance/storage/MikroORMStorage.ts +++ b/src/performance/storage/MikroORMStorage.ts @@ -16,7 +16,7 @@ export class MikroORMStorage extends Storage { constructor(storageURI: string, logPrefix: string, storageType: StorageType) { super(storageURI, logPrefix); this.storageType = storageType; - this.dbName = this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME; + this.dbName = this.getDBName(); } public async storePerformanceStatistics(performanceStatistics: Statistics): Promise { @@ -36,13 +36,30 @@ export class MikroORMStorage extends Storage { await this.orm.close(); } + private getDBName(): string { + if (this.storageType === StorageType.SQLITE) { + return Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME; + } + return this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME; + } + private getOptions(): Configuration> | Options> { return { entities: [PerformanceRecord, PerformanceData], dbName: this.dbName, type: this.storageType as MikroORMDBType, - clientUrl: this.storageURI.toString() + clientUrl: this.getClientUrl() }; } + + private getClientUrl(): string { + switch (this.storageType) { + case StorageType.SQLITE: + return this.storageURI.pathname; + case StorageType.MARIA_DB: + case StorageType.MYSQL: + return this.storageURI.toString(); + } + } } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 1b95f341..f7fe64bf 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -25,9 +25,6 @@ export default class Configuration { } static getPerformanceStorage(): StorageConfiguration { - const defaultJSONFilePathURI = `file://${path.join(path.resolve(__dirname, '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME)}`; - const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`; - const defaultSQLiteFilePathURI = `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`; let storageConfiguration: StorageConfiguration; if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'performanceStorage')) { storageConfiguration = @@ -36,14 +33,14 @@ export default class Configuration { ...Configuration.objectHasOwnProperty(Configuration.getConfig().performanceStorage, 'type') ? { type: Configuration.getConfig().performanceStorage.type } : { type: StorageType.JSON_FILE }, ...Configuration.objectHasOwnProperty(Configuration.getConfig().performanceStorage, 'URI') ? { URI: Configuration.getConfig().performanceStorage.URI } - : { URI: (Configuration.getConfig().performanceStorage.type === StorageType.JSON_FILE) ? defaultJSONFilePathURI : defaultSQLiteFilePathURI } + : { URI: this.getDefaultPerformanceStorageURI(Configuration.getConfig()?.performanceStorage?.type ?? StorageType.JSON_FILE) } }; } else { storageConfiguration = { enabled: false, type: StorageType.JSON_FILE, - URI: defaultJSONFilePathURI + URI: this.getDefaultPerformanceStorageURI(StorageType.JSON_FILE) }; } return storageConfiguration; @@ -178,6 +175,18 @@ export default class Configuration { } } + private static getDefaultPerformanceStorageURI(storageType: StorageType) { + const SQLiteFileName = `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`; + switch (storageType) { + case StorageType.JSON_FILE: + return `file://${path.join(path.resolve(__dirname, '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME)}`; + case StorageType.SQLITE: + return `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`; + default: + throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`); + } + } + private static objectHasOwnProperty(object: any, property: string): boolean { return Object.prototype.hasOwnProperty.call(object, property) as boolean; } -- 2.34.1