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
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 {
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)
}
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, '')
}
// 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'
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
import { Entity, PrimaryKey, Property } from '@mikro-orm/core'
-interface PerformanceData {
+interface StatisticsData {
name: string
requestCount: number
responseCount: number
updatedAt?: Date
@Property()
- performanceData!: PerformanceData[]
+ statisticsData!: Array<Partial<StatisticsData>>
}
}
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
"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
+ }
}