import { Logger } from "./logger.js"; import { MigrationBuilderActions } from "./sqlMigration.js"; import { Jiti } from "jiti"; //#region src/migrationLoader.d.ts /*** * Migration loader module. * * This module is responsible for loading migrations from the file system, allowing for custom loading behaviours. * If no configuration is provided, loading will be performed in a manner that is identical to the classic loading behaviour. * * A new SQL migration loading behaviour is available that supports pairing up/down migrations in separate files using up/down * suffixes before the .sql extension. * * This has been kept intentionally simple for maintainability and readability reasons. * */ /** * The jiti instance to use to load migration files. */ declare const jiti: Jiti; /************************* * Types and interfaces *************************/ /** * A migration unit is a collection of file paths and related migration actions that are related to a single migration. */ interface MigrationUnit { /** * The unique identifier for the migration unit. Represents the significant part of the file name. * Used for tracking which migrations have been performed. */ id: string; /** * The file paths that are part of the migration unit. */ filePaths: string[]; /** * The migration builder actions that are contained within the migration files. */ actions: MigrationBuilderActions; } /** * Loader function type. * * @param filePaths - The file paths to load migrations from. * @returns A list of migration units. */ type MigrationLoader = (filePaths: string[]) => Promise; /** * Predefined loader references. */ type PredefinedLoader = 'default' | 'legacySql' | 'sql'; /** * Configuration extension to support multiple loader strategies. */ interface MigrationLoaderConfig { /** * Configuration for the migration loader strategies. * Strategies are used to load migrations from different sources based on file patterns. * * If no strategy matches, the default strategy is used. */ migrationLoaderStrategies?: MigrationLoaderStrategy[]; /** * Enable [`jiti`](https://github.com/unjs/jiti) tsconfig paths resolution when * loading TypeScript/JavaScript migration files. This makes `compilerOptions.paths` * aliases defined in your `tsconfig.json` resolvable from within migration files. * * - `true`: auto-discover the nearest `tsconfig.json` (walking up from `cwd()`) * - `string`: explicit path to a `tsconfig.json` file * - `false` / `undefined` (default): disabled * * Has no effect on the built-in SQL loaders. * * @default false */ tsconfigPaths?: boolean | string; /** * Redirect messages to this logger object, rather than `console`. */ logger?: Logger; } /** * A migration loader strategy is a configuration object that defines a specific loader for given file extensions. */ interface MigrationLoaderStrategy { /** * File extensions handled by this strategy. */ extensions: string[]; /** * Loader that handles conversion of file paths to migration units. * Can be a loader function, when used as an API, or a predefined loader when using a json configuration file. * * @param filePaths - The file paths to load migrations from. * @returns The migration units. */ loader: MigrationLoader | PredefinedLoader; } /************************* * Loader implementations *************************/ /** * Creates a default migration loader that uses jiti to load migrations from the file paths. * @param jitiInstance - The jiti instance to use to load migration files. Defaults to the shared {@link jiti} instance. * @returns The default migration loader. */ declare function createDefaultMigrationLoader(jitiInstance?: Jiti): MigrationLoader; /** * Creates a legacy SQL migration loader that loads migrations from the file paths using the legacy SQL migration loading behaviour. * @returns The legacy SQL migration loader. */ declare function createLegacySqlMigrationLoader(): MigrationLoader; /** * Creates a SQL migration loader that loads migrations from the file paths using the new SQL migration loading behaviour. * While it handles the legacy format, it does add new behaviour that may be unwanted in existing usage so it has been * separated from the legacy loader and can be used enabled as needed. * * @returns The SQL migration loader. */ declare function createSqlMigrationLoader(): MigrationLoader; /************************* * Loader instances *************************/ /** * Built-in predefined loaders. */ declare const builtInLoaders: Record; /*********************************** * Migration loader main function ***********************************/ /** * Loads the migration units from the file paths. * @param config - The migration loader configuration. * @param filePaths - List of files containing migrations. * @returns List of migration units, sorted according to the given file paths. */ declare function loadMigrationUnits(config: MigrationLoaderConfig, filePaths: string[]): Promise; //#endregion export { MigrationLoader, MigrationLoaderConfig, MigrationLoaderStrategy, MigrationUnit, builtInLoaders, createDefaultMigrationLoader, createLegacySqlMigrationLoader, createSqlMigrationLoader, jiti, loadMigrationUnits };