UNPKG

@adonisjs/lucid

Version:

SQL ORM built on top of Active Record pattern

52 lines (51 loc) 1.77 kB
/* * @adonisjs/lucid * * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { sourceFiles } from '../utils/index.js'; /** * Migration source exposes the API to read the migration files * from disk for a given connection. */ export class MigrationSource { config; app; constructor(config, app) { this.config = config; this.app = app; } /** * Returns an array of files inside a given directory. Relative * paths are resolved from the project root */ async getDirectoryFiles(directoryPath) { const { files } = await sourceFiles(this.app.appRoot, directoryPath, this.config.migrations?.naturalSort || false); return files; } /** * Returns an array of migrations paths for a given connection. If paths * are not defined, then `database/migrations` fallback is used */ getMigrationsPath() { const directories = (this.config.migrations || {}).paths; const defaultDirectory = this.app.relativePath(this.app.migrationsPath()) || 'database/migrations'; return directories && directories.length ? directories : [`./${defaultDirectory}`]; } /** * Returns an array of files for all defined directories */ async getMigrations() { const migrationPaths = this.getMigrationsPath(); const directories = await Promise.all(migrationPaths.map((directoryPath) => { return this.getDirectoryFiles(directoryPath); })); return directories.reduce((result, directory) => { result = result.concat(directory); return result; }, []); } }