Skip to content

Commit 039e3d4

Browse files
committed
chore: wip
1 parent 90270ce commit 039e3d4

File tree

5 files changed

+23
-33
lines changed

5 files changed

+23
-33
lines changed

.stacks/core/actions/src/generate/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { GeneratorOptions } from '@stacksjs/types'
55
import { runNpmScript } from '@stacksjs/utils'
66
import { runCommand } from '@stacksjs/cli'
77
import { database } from '@stacksjs/config'
8-
import { runAction } from '../helpers'
98
import { frameworkPath } from '@stacksjs/path'
9+
import { runAction } from '../helpers'
1010

1111
export async function invoke(options?: GeneratorOptions) {
1212
if (options?.types)

.stacks/core/actions/src/make.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,10 @@ export default <Model> {
305305
},
306306
// more fields here
307307
},
308-
}`,})
308+
}`,
309+
})
309310

310-
log.success(`Successfully created your model at /config/models/${name}.ts`)
311+
log.success(`Successfully created your model at app/models/${name}.ts`)
311312
}
312313
catch (error) {
313314
log.error(error)

.stacks/core/database/src/migrations/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ ${model.name?.toLowerCase()}Id Int?
9494
}
9595
}
9696

97-
9897
if (!fs.existsSync(frameworkPath('database')))
9998
fs.mkdirSync(frameworkPath('database'))
10099

.stacks/core/database/src/seeder/index.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { filesystem } from '@stacksjs/storage'
22
import type { Model } from '@stacksjs/types'
3-
import { faker } from '@stacksjs/faker'
43
import { projectPath } from '@stacksjs/path'
54
import { PrismaClient } from '@prisma/client'
65

@@ -25,6 +24,7 @@ function readModels(folderPath: string): Promise<Model[]> {
2524
models.push({
2625
name: data.default.name,
2726
fields: data.default.fields,
27+
useSeed: data.default.useSeed,
2828
})
2929
})
3030
})
@@ -36,34 +36,24 @@ function readModels(folderPath: string): Promise<Model[]> {
3636
})
3737
}
3838

39-
function seedData(model: Model) {
40-
const fields = Object.entries(model.fields)
41-
const data: Record<string, any> = {}
42-
43-
fields.forEach(([name, type]) => {
44-
switch (type) {
45-
case 'number':
46-
data[name] = faker.random.numeric()
47-
break
48-
case 'boolean':
49-
data[name] = Math.round(Math.random())
50-
break
51-
default:
52-
data[name] = faker.lorem.words(3)
53-
}
54-
})
55-
56-
return data
57-
}
58-
5939
async function seed() {
6040
const models = await readModels(projectPath('app/models'))
61-
const promises = models.map((model) => {
62-
const data = seedData(model)
41+
const promises = models.flatMap((model) => {
42+
const { useSeed, fields } = model
43+
const data: Record<string, any>[] = []
44+
45+
if (useSeed && useSeed.count) {
46+
for (let i = 0; i < useSeed.count; i++) {
47+
const record: Record<string, any> = {}
48+
Object.entries(fields).forEach(([name, field]) => {
49+
if (field.factory)
50+
record[name] = field.factory()
51+
})
52+
data.push(record)
53+
}
54+
}
6355

64-
return prisma[model.name].create({
65-
data,
66-
})
56+
return data.map(record => prisma[model.name].create({ data: record }))
6757
})
6858

6959
return Promise.all(promises)

app/models/User.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ export default <Model> {
66
name: 'User',
77
searchable: true, // boolean | IndexSettings,
88
authenticatable: true, // boolean | AuthSettings,
9-
seeder: {
9+
useSeed: {
1010
count: 10,
1111
},
1212
fields: {
1313
name: {
1414
type: 'String',
1515
validation: validate.string().min(3).max(255),
16-
factory: () => faker.name,
16+
factory: () => faker.name.firstName(),
1717
},
1818
email: {
1919
type: 'String',
2020
validation: validate.string().min(1).max(255),
2121
unique: true,
22-
factory: () => faker.internet.email,
22+
factory: () => faker.internet.email(),
2323
},
2424
password: {
2525
type: 'String',

0 commit comments

Comments
 (0)