-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: improving seeds and migrations flow
- Loading branch information
1 parent
d818980
commit 4a6acad
Showing
14 changed files
with
295 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/// <reference path="../../adonis-typings/index.ts" /> | ||
|
||
import { ApplicationContract } from '@ioc:Adonis/Core/Application' | ||
import { ConnectionConfig, FileNode } from '@ioc:Adonis/Lucid/Database' | ||
import { sourceFiles } from '../utils' | ||
|
||
/** | ||
* Seeders source exposes the API to read the seeders from disk for a given connection. | ||
*/ | ||
export class SeedersSource { | ||
constructor(private config: ConnectionConfig, private app: ApplicationContract) {} | ||
|
||
/** | ||
* Returns an array of files inside a given directory. Relative | ||
* paths are resolved from the project root | ||
*/ | ||
private async getDirectoryFiles(directoryPath: string): Promise<FileNode<unknown>[]> { | ||
const { files } = await sourceFiles(this.app.appRoot, directoryPath) | ||
return files | ||
} | ||
|
||
/** | ||
* Returns an array of seeders paths for a given connection. If paths | ||
* are not defined, then `database/seeders` fallback is used | ||
*/ | ||
private getSeedersPaths(): string[] { | ||
const directories = (this.config.seeders || {}).paths | ||
return directories && directories.length ? directories : ['./database/seeders'] | ||
} | ||
|
||
/** | ||
* Returns an array of files for the defined seed directories | ||
*/ | ||
public async getSeeders() { | ||
const seedersPaths = this.getSeedersPaths() | ||
const directories = await Promise.all( | ||
seedersPaths.map((directoryPath) => { | ||
return this.getDirectoryFiles(directoryPath) | ||
}) | ||
) | ||
|
||
return directories.reduce((result, directory) => { | ||
result = result.concat(directory) | ||
return result | ||
}, []) | ||
} | ||
} |
Oops, something went wrong.