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