@ -1,24 +1,19 @@
|
||||
/**
|
||||
* TODO:
|
||||
* Add to `DaoItem` your model name
|
||||
* Add to the function `initDao` the Dao
|
||||
*/
|
||||
|
||||
/**
|
||||
* the different Daos that can be initialized
|
||||
*
|
||||
* Touch this interface to define which key is linked to which Dao
|
||||
*/
|
||||
interface DaoItem {}
|
||||
import type { default as Dao, default as DaoAdapter } from './Adapters/DaoAdapter'
|
||||
import config from './config'
|
||||
|
||||
/**
|
||||
* Class to get any DAO
|
||||
*/
|
||||
|
||||
// biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
|
||||
export default class DaoFactory {
|
||||
/**
|
||||
* reference of the different Daos for a correct singleton implementation
|
||||
* get the total list of daos available
|
||||
* @returns return the list of daos available
|
||||
*/
|
||||
private static daos: Partial<DaoItem> = {}
|
||||
public static getAll(): Record<string, DaoAdapter> {
|
||||
return config.models
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a a dao by its key
|
||||
@ -28,27 +23,15 @@ export default class DaoFactory {
|
||||
* @param key the dao key to get
|
||||
* @returns the Dao you want as a singleton
|
||||
*/
|
||||
public static get<Key extends keyof DaoItem>(key: Key): DaoItem[Key] {
|
||||
if (!(key in this.daos)) {
|
||||
const dao = this.initDao(key)
|
||||
if (!dao) {
|
||||
throw new Error(`${key} has no valid Dao`)
|
||||
}
|
||||
this.daos[key] = dao as DaoItem[Key]
|
||||
}
|
||||
return this.daos[key] as DaoItem[Key]
|
||||
public static get<Key extends keyof typeof config['models']>(key: Key): typeof config['models'][Key] {
|
||||
return config.models[key]
|
||||
}
|
||||
|
||||
/**
|
||||
* init a dao by its key, it does not care if it exists or not
|
||||
*
|
||||
* @param item the element to init
|
||||
* @returns a new initialized dao or undefined if no dao is linked
|
||||
* get the main client linked to migrations
|
||||
* @returns the main client
|
||||
*/
|
||||
private static initDao(item: keyof DaoItem): any | undefined {
|
||||
switch (item) {
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
public static async client(): ReturnType<(typeof config.mainClient)['get']> {
|
||||
return config.mainClient.get()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user