import { migrateV1ToV2 } from './migrations/v01_to_v02.ts'; import type { ToIumIndexV1 } from './v01.ts'; import type { ToIumIndexV2 } from './v02.ts'; export const CURRENT_IUM_VERSION = 2; /** * Index schema generated by the actual version of this package */ export type ToIumIndex = ToIumIndexV2; /** * Union of all index schema versions, used internally to check and migrate the index for proper unserialization. */ export type ToIumIndexVersions = ToIumIndexV1 | ToIumIndexV2; // eslint-disable-next-line @typescript-eslint/no-explicit-any const migrations: Array<(index: any) => any> = [migrateV1ToV2]; // eslint-disable-next-line jsdoc/require-jsdoc export function migrateIumIndex(index: ToIumIndexVersions): ToIumIndex { const version = index.version ?? 1; if (version === CURRENT_IUM_VERSION) return index as ToIumIndex; const indexMigration = version - 1; const migration = migrations[indexMigration]; if (!migration) throw new Error(`No migration found for version ${version}`); const indexMigrated = migration(index); return migrateIumIndex(indexMigrated); }