Skip to content

Commit 5b63a48

Browse files
authored
fix: natural order on seeder (#1120)
1 parent e2ef286 commit 5b63a48

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/seeders/source.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ export class SeedersSource {
2626
* paths are resolved from the project root
2727
*/
2828
private async getDirectoryFiles(directoryPath: string): Promise<FileNode<unknown>[]> {
29-
const { files } = await sourceFiles(this.app.appRoot, directoryPath, false)
29+
const { files } = await sourceFiles(
30+
this.app.appRoot,
31+
directoryPath,
32+
this.config.seeders?.naturalSort || false
33+
)
3034
return files
3135
}
3236

src/types/database.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ export type MigratorConfig = {
325325
* Seeders config
326326
*/
327327
export type SeedersConfig = {
328-
paths: string[]
328+
paths?: string[]
329+
naturalSort?: boolean
329330
}
330331

331332
/**

test/seeders/seeders_source.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,59 @@ test.group('Seeds Source', (group) => {
144144
]
145145
)
146146
})
147+
148+
test('use a natural sort to order files when configured', async ({ fs, assert }) => {
149+
const app = new AppFactory().create(fs.baseUrl, () => {})
150+
await app.init()
151+
const db = getDb()
152+
153+
const config = Object.assign({}, db.getRawConnection('primary')!.config, {
154+
seeders: {
155+
naturalSort: true,
156+
paths: ['database/seeders'],
157+
},
158+
})
159+
160+
const seedersSource = new SeedersSource(config, app)
161+
await fs.create('database/seeders/1.ts', '')
162+
await fs.create('database/seeders/2.ts', '')
163+
await fs.create('database/seeders/10.ts', '')
164+
await fs.create('database/seeders/3.ts', '')
165+
await fs.create('database/seeders/100.ts', '')
166+
await fs.create('database/seeders/4.ts', '')
167+
await db.manager.closeAll()
168+
169+
const files = await seedersSource.getSeeders()
170+
assert.deepEqual(
171+
files.map((file) => {
172+
return { absPath: file.absPath, name: file.name }
173+
}),
174+
[
175+
{
176+
absPath: join(fs.basePath, 'database/seeders/1.ts'),
177+
name: 'database/seeders/1',
178+
},
179+
{
180+
absPath: join(fs.basePath, 'database/seeders/2.ts'),
181+
name: 'database/seeders/2',
182+
},
183+
{
184+
absPath: join(fs.basePath, 'database/seeders/3.ts'),
185+
name: 'database/seeders/3',
186+
},
187+
{
188+
absPath: join(fs.basePath, 'database/seeders/4.ts'),
189+
name: 'database/seeders/4',
190+
},
191+
{
192+
absPath: join(fs.basePath, 'database/seeders/10.ts'),
193+
name: 'database/seeders/10',
194+
},
195+
{
196+
absPath: join(fs.basePath, 'database/seeders/100.ts'),
197+
name: 'database/seeders/100',
198+
},
199+
]
200+
)
201+
})
147202
})

0 commit comments

Comments
 (0)