diff --git a/src/seeders/source.ts b/src/seeders/source.ts index 9d1d33d2b..6288e1ece 100644 --- a/src/seeders/source.ts +++ b/src/seeders/source.ts @@ -26,7 +26,11 @@ export class SeedersSource { * paths are resolved from the project root */ private async getDirectoryFiles(directoryPath: string): Promise[]> { - const { files } = await sourceFiles(this.app.appRoot, directoryPath, false) + const { files } = await sourceFiles( + this.app.appRoot, + directoryPath, + this.config.seeders?.naturalSort || false + ) return files } diff --git a/src/types/database.ts b/src/types/database.ts index 0d11f978f..232f11aac 100644 --- a/src/types/database.ts +++ b/src/types/database.ts @@ -325,7 +325,8 @@ export type MigratorConfig = { * Seeders config */ export type SeedersConfig = { - paths: string[] + paths?: string[] + naturalSort?: boolean } /** diff --git a/test/seeders/seeders_source.spec.ts b/test/seeders/seeders_source.spec.ts index fa59c3436..d122850ce 100644 --- a/test/seeders/seeders_source.spec.ts +++ b/test/seeders/seeders_source.spec.ts @@ -144,4 +144,59 @@ test.group('Seeds Source', (group) => { ] ) }) + + test('use a natural sort to order files when configured', async ({ fs, assert }) => { + const app = new AppFactory().create(fs.baseUrl, () => {}) + await app.init() + const db = getDb() + + const config = Object.assign({}, db.getRawConnection('primary')!.config, { + seeders: { + naturalSort: true, + paths: ['database/seeders'], + }, + }) + + const seedersSource = new SeedersSource(config, app) + await fs.create('database/seeders/1.ts', '') + await fs.create('database/seeders/2.ts', '') + await fs.create('database/seeders/10.ts', '') + await fs.create('database/seeders/3.ts', '') + await fs.create('database/seeders/100.ts', '') + await fs.create('database/seeders/4.ts', '') + await db.manager.closeAll() + + const files = await seedersSource.getSeeders() + assert.deepEqual( + files.map((file) => { + return { absPath: file.absPath, name: file.name } + }), + [ + { + absPath: join(fs.basePath, 'database/seeders/1.ts'), + name: 'database/seeders/1', + }, + { + absPath: join(fs.basePath, 'database/seeders/2.ts'), + name: 'database/seeders/2', + }, + { + absPath: join(fs.basePath, 'database/seeders/3.ts'), + name: 'database/seeders/3', + }, + { + absPath: join(fs.basePath, 'database/seeders/4.ts'), + name: 'database/seeders/4', + }, + { + absPath: join(fs.basePath, 'database/seeders/10.ts'), + name: 'database/seeders/10', + }, + { + absPath: join(fs.basePath, 'database/seeders/100.ts'), + name: 'database/seeders/100', + }, + ] + ) + }) })