| 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
| 2 | |
| 3 | import { JsonFileStorage } from './JsonFileStorage'; |
| 4 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 5 | import { MikroOrmStorage } from './MikroOrmStorage'; |
| 6 | import { MongoDBStorage } from './MongoDBStorage'; |
| 7 | import type { Storage } from './Storage'; |
| 8 | import { BaseError } from '../../exception'; |
| 9 | import { StorageType } from '../../types'; |
| 10 | |
| 11 | export class StorageFactory { |
| 12 | private constructor() { |
| 13 | // This is intentional |
| 14 | } |
| 15 | |
| 16 | public static getStorage( |
| 17 | type: StorageType, |
| 18 | connectionUri: string, |
| 19 | logPrefix: string, |
| 20 | ): Storage | undefined { |
| 21 | let storageInstance: Storage; |
| 22 | switch (type) { |
| 23 | case StorageType.JSON_FILE: |
| 24 | storageInstance = new JsonFileStorage(connectionUri, logPrefix); |
| 25 | break; |
| 26 | case StorageType.MONGO_DB: |
| 27 | storageInstance = new MongoDBStorage(connectionUri, logPrefix); |
| 28 | break; |
| 29 | // case StorageType.MYSQL: |
| 30 | // case StorageType.MARIA_DB: |
| 31 | // case StorageType.SQLITE: |
| 32 | // storageInstance = new MikroOrmStorage(connectionUri, logPrefix, type); |
| 33 | // break; |
| 34 | default: |
| 35 | throw new BaseError(`${logPrefix} Unknown storage type: ${type}`); |
| 36 | } |
| 37 | return storageInstance; |
| 38 | } |
| 39 | } |