Skip to content

Commit 40c8766

Browse files
committed
chore: wip
1 parent 206ee17 commit 40c8766

File tree

3 files changed

+30
-76
lines changed

3 files changed

+30
-76
lines changed

bun.lockb

-992 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './schema'
22
export * from './migrations'
3+
export * from './seeder'
34
export * from './types'
45
export * from './utils'
Lines changed: 29 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,32 @@
1-
// import { MysqlDialect, QueryBuilder, createPool } from '@stacksjs/query-builder'
2-
// import { filesystem } from '@stacksjs/storage'
3-
// import type { Model } from '@stacksjs/types'
4-
// import { projectPath } from '@stacksjs/path'
5-
// import { database as config } from '@stacksjs/config'
6-
7-
// const { fs } = filesystem
8-
9-
// function readModels(folderPath: string): Promise<Model[]> {
10-
// return new Promise((resolve, reject) => {
11-
// const models: Model[] = []
12-
13-
// fs.readdir(folderPath, (err, files) => {
14-
// if (err)
15-
// reject(err)
16-
17-
// const promises = files
18-
// .filter(file => file.endsWith('.ts'))
19-
// .map((file) => {
20-
// const filePath = `${folderPath}/${file}`
21-
22-
// return import(filePath).then((data) => {
23-
// models.push({
24-
// name: data.default.name,
25-
// fields: data.default.fields,
26-
// useSeed: data.default.useSeed,
27-
// })
28-
// })
29-
// })
30-
31-
// Promise.all(promises)
32-
// .then(() => resolve(models))
33-
// .catch(err => reject(err))
34-
// })
35-
// })
36-
// }
37-
38-
async function seed() {
39-
// const db = new QueryBuilder({
40-
// dialect: new MysqlDialect({
41-
// pool: createPool({
42-
// database: config.database,
43-
// host: config.host,
44-
// password: config.password,
45-
// user: config.username,
46-
// }),
47-
// }),
48-
// })
49-
50-
// const models = await readModels(projectPath('app/Models'))
51-
52-
// const queries = models.flatMap((model) => {
53-
// const { seedable, fields } = model
54-
55-
// if (!seedable)
56-
// return []
57-
58-
// const count = typeof seedable === 'boolean' ? 10 : seedable.count
59-
60-
// const records: Record<string, any>[] = []
61-
// for (let i = 0; i < count; i++) {
62-
// const record: Record<string, any> = {}
63-
// Object.entries(fields).forEach(([name, field]) => {
64-
// if (field.factory)
65-
// record[name] = field.factory()
66-
// })
67-
// records.push(record)
68-
// }
69-
70-
// return model
71-
// // return db.insertInto('users').values(records).build(sql`RETURNING *`)
72-
// })
1+
import { path } from '@stacksjs/path'
2+
import { db } from '@stacksjs/database'
3+
import { fs } from '@stacksjs/storage'
4+
5+
async function seedModel(model: any) {
6+
const tableName = model.table
7+
const seedCount = model.traits.useSeeder?.count || 10 // Default to 10 if not specified
8+
const records = []
9+
10+
for (let i = 0; i < seedCount; i++) {
11+
const record: any = {}
12+
for (const fieldName in model.fields) {
13+
const field = model.fields[fieldName]
14+
// Use the factory function if available, otherwise leave the field undefined
15+
record[fieldName] = field.factory ? field.factory() : undefined
16+
}
17+
records.push(record)
18+
}
19+
20+
await db.insertInto(tableName).values(records).execute()
21+
}
7322

74-
// const { rows } = await db.transaction().execute()
23+
export async function seed() {
24+
const modelsDir = path.userModelsPath()
25+
const modelFiles = fs.readdirSync(modelsDir).filter(file => file.endsWith('.ts'))
7526

76-
// return rows
27+
for (const file of modelFiles) {
28+
const modelPath = path.join(modelsDir, file)
29+
const model = await import(modelPath)
30+
await seedModel(model.default)
31+
}
7732
}
78-
79-
export { seed }

0 commit comments

Comments
 (0)