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