All files / src/helpers getProjectModelsAndDaos.ts

66.31% Statements 63/95
66.66% Branches 6/9
42.85% Functions 3/7
66.31% Lines 63/95

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 4x                         1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x         1x 1x           1x 1x 1x 1x 1x 1x       1x 1x           1x 1x 1x     1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
 
 
import { getDbConfigs } from './getGreenDotConfigs'
import { Definition } from 'good-cop'
import { MongoDao, MongoDaoParsed } from '../databases/mongo/types/mongoDbTypes'
import { C, objEntries } from 'topkat-utils'
import { safeImport } from './safeImports'
import { parseDaos } from '../databases/parseDaos'
import { error } from '../error'
import defaultDaoConfigMongo from '../databases/mongo/defaultDaoConfigMongo'
 
//  ═╦═ ╦╗ ╔ ═╦═ ══╦══
//   ║  ║╚╗║  ║    ║
//  ═╩═ ╩ ╚╩ ═╩═   ╩
 
let modelsCache: { [dbName: string]: { [modelName: string]: Definition } }
let daosCache: { [dbName: string]: { [modelName: string]: MongoDaoParsed<any> } }
 
let cacheInitialized = false
 
export async function initProjectAndDaosCache(resetCache = false) {
  if (cacheInitialized && resetCache === false) return
  modelsCache = {}
  daosCache = {}
  cacheInitialized = true
  const dbConfigs = getDbConfigs()
  for (const { generatedIndexPath, name: dbName } of dbConfigs) {
    const fileContent = await safeImport(generatedIndexPath) as DatabaseIndexFileContent
    modelsCache[dbName] = objEntries(fileContent.models).reduce((obj, [modelName, content]) => ({ ...obj, [modelName.replace(/Model$/, '')]: content }), {})

    const { _defaultDao, ...regularDaos } = fileContent.daos

    daosCache[dbName] = await parseDaos(
      Object.keys(modelsCache[dbName]),
      regularDaos,
      _defaultDao
    )
  }
}
 
 
//  ╦╗╔╦ ╔══╗ ╔═╗  ╔══╗ ╦    ╔═══
//  ║╚╝║ ║  ║ ║  ║ ╠═   ║    ╚══╗
//  ╩  ╩ ╚══╝ ╚══╝ ╚══╝ ╚══╝ ═══╝
 
export async function getProjectDatabaseModels(resetCache = false) {
  await initProjectAndDaosCache(resetCache)
  return modelsCache
}
 
export function getProjectDatabaseModelsSync() {
  if (!modelsCache) throw error.serverError(`Cache for database has not been initialized, please make sure you run initProjectAndDaosCache() first`)
  return modelsCache
}
 
export async function getProjectDatabaseModelsForDbName(dbName: string, resetCache = false) {
  await initProjectAndDaosCache(resetCache)
  const models = modelsCache[dbName]
  if (!models) throw error.serverError(`No database model with name ${dbName} found in configs. Available names: ${Object.keys(modelsCache)}`)
  return models
}
 
//  ╔═╗  ╔══╗ ╔══╗ ╔═══
//  ║  ║ ╠══╣ ║  ║ ╚══╗
//  ╚══╝ ╩  ╩ ╚══╝ ═══╝
 
export async function getProjectDatabaseDaos(resetCache = false) {
  await initProjectAndDaosCache(resetCache)
  return daosCache
}
 
export async function getProjectDatabaseDaosForDbName(dbName: string, resetCache = false) {
  await initProjectAndDaosCache(resetCache)
  const daos = daosCache[dbName]
  if (!daos) throw error.serverError(`No Dao model with name ${dbName} found in configs. Available names: ${Object.keys(daosCache)}`)
  return daos
}
 
export async function getProjectDatabaseDaosForModel(dbName: string, modelName: string, resetCache = false) {
  await initProjectAndDaosCache(resetCache)
  const daos = daosCache[dbName]
  if (!daos) throw error.serverError(`No Dao model with name ${dbName} found in configs. Available names: ${Object.keys(daosCache)}`)
  if (!daos[modelName] && !modelName.startsWith('GD_')) C.warning(false, `No Dao file set for model ${modelName}. Using default Dao.`)
  return daos[modelName] || defaultDaoConfigMongo
}
 
//  ╦  ╦ ╔══╗ ╦    ╔══╗ ╔══╗ ╔══╗ ╔═══
//  ╠══╣ ╠═   ║    ╠══╝ ╠═   ╠═╦╝ ╚══╗
//  ╩  ╩ ╚══╝ ╚══╝ ╩    ╚══╝ ╩ ╚  ═══╝
 
type DatabaseIndexFileContent = {
  models: { [modelName: string]: Definition }
  daos: { [modelName: string]: MongoDaoParsed<any> | MongoDao<any> }
  defaultDao?: MongoDaoParsed<any> | MongoDao<any>
}