@@ -3,23 +3,28 @@ import { db } from '@stacksjs/database'
3
3
import { italic , log } from '@stacksjs/cli'
4
4
import { fs } from '@stacksjs/storage'
5
5
import { snakeCase } from '@stacksjs/strings'
6
+ import type { Model } from '@stacksjs/types'
6
7
7
- async function seedModel ( model : any ) {
8
- const tableName = model . table ?? snakeCase ( model . name )
9
- const seedCount = model . traits . useSeeder ?. count ?? 10 // Default to 10
8
+ async function seedModel ( name : string , model ?: Model ) {
9
+ if ( ! model )
10
+ model = await import ( path . userModelsPath ( name ) )
11
+
12
+ const tableName = model ! . table ?? snakeCase ( model ! . name ?? name . replace ( '.ts' , '' ) )
13
+ const seedCount = typeof model ! . traits ?. useSeeder === 'object' && model ! . traits ?. useSeeder ?. count ? model ! . traits . useSeeder . count : 10
10
14
log . info ( `Seeding ${ seedCount } records into ${ italic ( tableName ) } ` )
11
15
const records = [ ]
12
16
13
17
for ( let i = 0 ; i < seedCount ; i ++ ) {
14
18
const record : any = { }
15
- for ( const fieldName in model . fields ) {
16
- const field = model . fields [ fieldName ]
19
+ for ( const fieldName in model ! . fields ) {
20
+ const field = model ! . fields [ fieldName ]
17
21
// Use the factory function if available, otherwise leave the field undefined
18
- record [ fieldName ] = field . factory ? field . factory ( ) : undefined
22
+ record [ fieldName ] = field ? .factory ? field . factory ( ) : undefined
19
23
}
20
24
records . push ( record )
21
25
}
22
26
27
+ // @ts -expect-error todo: we can improve this in the future
23
28
await db . insertInto ( tableName ) . values ( records ) . execute ( )
24
29
}
25
30
@@ -30,6 +35,6 @@ export async function seed() {
30
35
for ( const file of modelFiles ) {
31
36
const modelPath = path . join ( modelsDir , file )
32
37
const model = await import ( modelPath )
33
- await seedModel ( model . default )
38
+ await seedModel ( file , model . default )
34
39
}
35
40
}
0 commit comments