Skip to content

Commit 016e820

Browse files
committed
chore: wip
1 parent fbd7316 commit 016e820

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

app/Models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
useSoftDeletes: true, // defaults to false, `softDeletable` used as an alias
1919

2020
useSeeder: { // defaults to a count of 10, `seedable` used as an alias
21-
count: 10,
21+
count: 40,
2222
},
2323

2424
useApi: {

storage/framework/core/database/src/seeder.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ import { db } from '@stacksjs/database'
33
import { italic, log } from '@stacksjs/cli'
44
import { fs } from '@stacksjs/storage'
55
import { snakeCase } from '@stacksjs/strings'
6+
import type { Model } from '@stacksjs/types'
67

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
1014
log.info(`Seeding ${seedCount} records into ${italic(tableName)}`)
1115
const records = []
1216

1317
for (let i = 0; i < seedCount; i++) {
1418
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]
1721
// 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
1923
}
2024
records.push(record)
2125
}
2226

27+
// @ts-expect-error todo: we can improve this in the future
2328
await db.insertInto(tableName).values(records).execute()
2429
}
2530

@@ -30,6 +35,6 @@ export async function seed() {
3035
for (const file of modelFiles) {
3136
const modelPath = path.join(modelsDir, file)
3237
const model = await import(modelPath)
33-
await seedModel(model.default)
38+
await seedModel(file, model.default)
3439
}
3540
}

0 commit comments

Comments
 (0)