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