refactor: prepare for MikroORM storage support
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 14 Jan 2024 11:23:34 +0000 (12:23 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 14 Jan 2024 11:23:34 +0000 (12:23 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
mikro-orm.config-template.ts
src/performance/storage/MikroOrmStorage.ts
src/performance/storage/StorageFactory.ts
src/types/orm/entities/PerformanceRecord.ts
src/utils/Configuration.ts
tsconfig-mikro-orm.json

index a418c75e7f7c2ca9960d8c7eafb78dd4f2fd8dc6..1291cbd2cc3dad2f96864021cd194af178944c58 100644 (file)
@@ -3,7 +3,7 @@ import { defineConfig } from '@mikro-orm/sqlite'
 import { Constants } from './src/utils/index.js'
 
 export default defineConfig({
-  dbName: `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`,
+  dbName: `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`,
   entities: ['./dist/types/orm/entities/*.js'],
   entitiesTs: ['./src/types/orm/entities/*.ts'],
   debug: true
index d5e25bb2c6e7e205a98963bd3bd97195d7dc73cb..9746c6bd5f5a09b439129338fc1dde2a1a82a013 100644 (file)
@@ -4,7 +4,7 @@ import { MikroORM as MariaDbORM, type Options as MariaDbOptions } from '@mikro-o
 import { MikroORM as SqliteORM, type Options as SqliteOptions } from '@mikro-orm/sqlite'
 
 import { Storage } from './Storage.js'
-import { type Statistics, StorageType } from '../../types/index.js'
+import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js'
 import { Constants } from '../../utils/index.js'
 
 export class MikroOrmStorage extends Storage {
@@ -19,8 +19,13 @@ export class MikroOrmStorage extends Storage {
 
   public async storePerformanceStatistics (performanceStatistics: Statistics): Promise<void> {
     try {
-      // TODO: build PerformanceRecord entity
-      await this.orm?.em.persistAndFlush({})
+      await this.orm?.em.persistAndFlush({
+        ...performanceStatistics,
+        statisticsData: Array.from(performanceStatistics.statisticsData, ([name, value]) => ({
+          name,
+          ...value
+        }))
+      } satisfies PerformanceRecord)
     } catch (error) {
       this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE)
     }
@@ -57,7 +62,7 @@ export class MikroOrmStorage extends Storage {
 
   private getDBName (): string {
     if (this.storageType === StorageType.SQLITE) {
-      return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
+      return `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
     }
     return this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '')
   }
index e1e42751533c60afc930533484e975c169dda18e..8753bea1159d863f83b6a08cf2f0b102039d3219 100644 (file)
@@ -1,7 +1,6 @@
 // Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
 
 import { JsonFileStorage } from './JsonFileStorage.js'
-// eslint-disable-next-line @typescript-eslint/no-unused-vars
 import { MikroOrmStorage } from './MikroOrmStorage.js'
 import { MongoDBStorage } from './MongoDBStorage.js'
 import type { Storage } from './Storage.js'
@@ -27,12 +26,13 @@ export class StorageFactory {
       case StorageType.MONGO_DB:
         storageInstance = new MongoDBStorage(connectionUri, logPrefix)
         break
-      // case StorageType.SQLITE:
-      // case StorageType.MYSQL:
-      // case StorageType.MARIA_DB:
-      //   storageInstance = new MikroOrmStorage(connectionUri, logPrefix, type)
-      //   break
+      case StorageType.SQLITE:
+      case StorageType.MARIA_DB:
+      case StorageType.MYSQL:
+        storageInstance = new MikroOrmStorage(connectionUri, logPrefix, type)
+        break
       default:
+        // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
         throw new BaseError(`${logPrefix} Unknown storage type: ${type}`)
     }
     return storageInstance
index dbe9ceeca87e4160e66f73c2c5ccced5edf0ab4e..bdd88abaeb6cd04e4ab8fc79752343ade355d295 100644 (file)
@@ -1,6 +1,6 @@
 import { Entity, PrimaryKey, Property } from '@mikro-orm/core'
 
-interface PerformanceData {
+interface StatisticsData {
   name: string
   requestCount: number
   responseCount: number
@@ -38,5 +38,5 @@ export class PerformanceRecord {
     updatedAt?: Date
 
   @Property()
-    performanceData!: PerformanceData[]
+    statisticsData!: Array<Partial<StatisticsData>>
 }
index dc3ca595273d523a4dff6a12d2da26270e2cb3f7..8f6e6c844069b6accf5b08a381a9baaff55bf5d6 100644 (file)
@@ -177,17 +177,31 @@ export class Configuration {
   }
 
   private static buildPerformanceStorageSection (): StorageConfiguration {
-    let storageConfiguration: StorageConfiguration = {
-      enabled: false,
-      type: StorageType.JSON_FILE,
-      uri: getDefaultPerformanceStorageUri(StorageType.JSON_FILE)
+    let storageConfiguration: StorageConfiguration
+    switch (Configuration.getConfigurationData()?.performanceStorage?.type) {
+      case StorageType.SQLITE:
+        storageConfiguration = {
+          enabled: false,
+          type: StorageType.SQLITE,
+          uri: getDefaultPerformanceStorageUri(StorageType.SQLITE)
+        }
+        break
+      case StorageType.JSON_FILE:
+      default:
+        storageConfiguration = {
+          enabled: false,
+          type: StorageType.JSON_FILE,
+          uri: getDefaultPerformanceStorageUri(StorageType.JSON_FILE)
+        }
+        break
     }
     if (hasOwnProp(Configuration.getConfigurationData(), ConfigurationSection.performanceStorage)) {
       storageConfiguration = {
         ...storageConfiguration,
         ...Configuration.getConfigurationData()?.performanceStorage,
-        ...(Configuration.getConfigurationData()?.performanceStorage?.type ===
-          StorageType.JSON_FILE &&
+        ...((Configuration.getConfigurationData()?.performanceStorage?.type ===
+          StorageType.JSON_FILE ||
+          Configuration.getConfigurationData()?.performanceStorage?.type === StorageType.SQLITE) &&
           Configuration.getConfigurationData()?.performanceStorage?.uri != null && {
           uri: buildPerformanceUriFilePath(
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
index 1d40e6e2c8c0aad83c53c8e8f6d5203d01a56944..5d7590cd829d96caf19998d58770f576651dd95a 100644 (file)
@@ -3,8 +3,12 @@
   "compilerOptions": {
     "rootDir": "./src/types/orm/entities",
     "outDir": "./dist/types/orm/entities",
-    "sourceMap": true,
-    "esModuleInterop": true
+    "declaration": true,
+    "sourceMap": true
   },
-  "include": ["src/types/orm/entities/*.ts"]
+  "include": ["src/types/orm/entities/*.ts"],
+  "ts-node": {
+    "esm": true,
+    "transpileOnly": true
+  }
 }