277e6420420135f98b87729f637b3f29bf127787
[e-mobility-charging-stations-simulator.git] / src / performance / storage / StorageFactory.ts
1 // Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
2
3 import type { Storage } from './Storage.js'
4
5 import { BaseError } from '../../exception/index.js'
6 import { StorageType } from '../../types/index.js'
7 import { JsonFileStorage } from './JsonFileStorage.js'
8 import { MikroOrmStorage } from './MikroOrmStorage.js'
9 import { MongoDBStorage } from './MongoDBStorage.js'
10 import { None } from './None.js'
11
12 // eslint-disable-next-line @typescript-eslint/no-extraneous-class
13 export class StorageFactory {
14 private constructor () {
15 // This is intentional
16 }
17
18 public static getStorage (
19 type: StorageType,
20 connectionUri: string,
21 logPrefix: string
22 ): Storage | undefined {
23 let storageInstance: Storage
24 switch (type) {
25 case StorageType.JSON_FILE:
26 storageInstance = new JsonFileStorage(connectionUri, logPrefix)
27 break
28 case StorageType.MARIA_DB:
29 case StorageType.MYSQL:
30 case StorageType.SQLITE:
31 storageInstance = new MikroOrmStorage(connectionUri, logPrefix, type)
32 break
33 case StorageType.MONGO_DB:
34 storageInstance = new MongoDBStorage(connectionUri, logPrefix)
35 break
36 case StorageType.NONE:
37 storageInstance = new None()
38 break
39 default:
40 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
41 throw new BaseError(`${logPrefix} Unknown storage type: ${type}`)
42 }
43 return storageInstance
44 }
45 }