import { type Class, hasFunction, Runtime } from '@travetto/runtime'; import { SchemaRegistryIndex } from '@travetto/schema'; import type { ModelStorageSupport } from '../types/storage.ts'; import { ModelRegistryIndex } from '../registry/registry-index.ts'; /** * Model storage util */ export class ModelStorageUtil { /** * Type guard for determining if service supports storage operation */ static isSupported = hasFunction('createStorage'); /** * Storage Initialization */ static async storageInitialization(storage: ModelStorageSupport): Promise { if (storage.config?.modifyStorage === false) { return; } const checkType = (cls: Class, enforceBase = true): boolean => { if (enforceBase && SchemaRegistryIndex.getBaseClass(cls) !== cls) { return false; } const { autoCreate } = ModelRegistryIndex.getConfig(cls) ?? {}; if (autoCreate === 'off') { return false; } return (autoCreate === 'production' || !Runtime.production); }; // Initialize on startup (test manages) await storage.createStorage(); if (storage.upsertModel) { for (const cls of ModelRegistryIndex.getClasses()) { if (checkType(cls)) { await storage.upsertModel(cls); } } } } }