1
- import type { Model , RelationConfig } from '@stacksjs/types'
1
+ import type { Model } from '@stacksjs/types'
2
2
import { italic , log } from '@stacksjs/cli'
3
3
import { db } from '@stacksjs/database'
4
4
import { faker } from '@stacksjs/faker'
5
- import { fetchOtherModelRelations , findCoreModel , getModelName , getTableName } from '@stacksjs/orm'
5
+ import { findCoreModel , getModelName , getTableName } from '@stacksjs/orm'
6
6
import { path } from '@stacksjs/path'
7
7
import { makeHash } from '@stacksjs/security'
8
8
import { fs } from '@stacksjs/storage'
@@ -80,66 +80,66 @@ async function seedModel(name: string, modelPath: string, model: Model) {
80
80
log . info ( `Successfully seeded ${ italic ( tableName ) } ` )
81
81
}
82
82
83
- async function seedPivotRelation ( relation : RelationConfig ) : Promise < any > {
84
- const record : Record < string , any > = { }
85
- const record2 : Record < string , any > = { }
86
- const pivotRecord : Record < string , any > = { }
87
- const modelInstance = await getModelInstance ( relation ?. model )
83
+ // async function seedPivotRelation(relation: RelationConfig): Promise<any> {
84
+ // const record: Record<string, any> = {}
85
+ // const record2: Record<string, any> = {}
86
+ // const pivotRecord: Record<string, any> = {}
87
+ // const modelInstance = await getModelInstance(relation?.model)
88
88
89
- const relationModelInstance = ( await import ( path . userModelsPath ( `${ relation ?. relationModel } .ts` ) ) ) . default
89
+ // const relationModelInstance = (await import(path.userModelsPath(`${relation?.relationModel}.ts`))).default
90
90
91
- const useUuid = modelInstance ?. traits ?. useUuid || false
91
+ // const useUuid = modelInstance?.traits?.useUuid || false
92
92
93
- if ( ! relationModelInstance )
94
- return 1
95
-
96
- const relationModelTable = relationModelInstance . table
97
- const relationalModelUuid = relationModelInstance . traits ?. useUuid || false
98
- const relationTable = relation . table
99
- const pivotTable = relation . pivotTable
100
- const modelKey = relation . pivotForeign || ''
101
- const foreignKey = relation . pivotKey || ''
102
-
103
- for ( const fieldName in relationModelInstance . attributes ) {
104
- const formattedFieldName = snakeCase ( fieldName )
105
- const field = relationModelInstance . attributes [ fieldName ]
106
- // Pass faker to the factory function
107
- if ( formattedFieldName === 'password' )
108
- record [ formattedFieldName ] = field ?. factory ? await makeHash ( field . factory ( faker ) , { algorithm : 'bcrypt' } ) : undefined
109
- else
110
- record [ formattedFieldName ] = field ?. factory ? field . factory ( faker ) : undefined
111
- }
93
+ // if (!relationModelInstance)
94
+ // return 1
112
95
113
- for ( const fieldName in modelInstance . attributes ) {
114
- const formattedFieldName = snakeCase ( fieldName )
115
- const field = modelInstance . attributes [ fieldName ]
116
- // Pass faker to the factory function
117
- if ( formattedFieldName === 'password' )
118
- record2 [ formattedFieldName ] = field ?. factory ? await makeHash ( field . factory ( faker ) , { algorithm : 'bcrypt' } ) : undefined
119
- else record2 [ formattedFieldName ] = field ?. factory ? field . factory ( faker ) : undefined
120
- }
96
+ // const relationModelTable = relationModelInstance.table
97
+ // const relationalModelUuid = relationModelInstance.traits?.useUuid || false
98
+ // const relationTable = relation.table
99
+ // const pivotTable = relation.pivotTable
100
+ // const modelKey = relation.pivotForeign || ''
101
+ // const foreignKey = relation.pivotKey || ''
121
102
122
- if ( relationalModelUuid )
123
- record . uuid = Bun . randomUUIDv7 ( )
103
+ // for (const fieldName in relationModelInstance.attributes) {
104
+ // const formattedFieldName = snakeCase(fieldName)
105
+ // const field = relationModelInstance.attributes[fieldName]
106
+ // // Pass faker to the factory function
107
+ // if (formattedFieldName === 'password')
108
+ // record[formattedFieldName] = field?.factory ? await makeHash(field.factory(faker), { algorithm: 'bcrypt' }) : undefined
109
+ // else
110
+ // record[formattedFieldName] = field?.factory ? field.factory(faker) : undefined
111
+ // }
124
112
125
- const data = await db . insertInto ( relationModelTable ) . values ( record ) . executeTakeFirstOrThrow ( )
113
+ // for (const fieldName in modelInstance.attributes) {
114
+ // const formattedFieldName = snakeCase(fieldName)
115
+ // const field = modelInstance.attributes[fieldName]
116
+ // // Pass faker to the factory function
117
+ // if (formattedFieldName === 'password')
118
+ // record2[formattedFieldName] = field?.factory ? await makeHash(field.factory(faker), { algorithm: 'bcrypt' }) : undefined
119
+ // else record2[formattedFieldName] = field?.factory ? field.factory(faker) : undefined
120
+ // }
126
121
127
- if ( useUuid )
128
- record2 . uuid = Bun . randomUUIDv7 ( )
122
+ // if (relationalModelUuid )
123
+ // record .uuid = Bun.randomUUIDv7()
129
124
130
- const data2 = await db . insertInto ( relationTable ) . values ( record2 ) . executeTakeFirstOrThrow ( )
131
- const relationData = data . insertId || 1
132
- const modelData = data2 . insertId || 1
125
+ // const data = await db.insertInto(relationModelTable).values(record).executeTakeFirstOrThrow()
133
126
134
- pivotRecord [ foreignKey ] = relationData
135
- pivotRecord [ modelKey ] = modelData
127
+ // if (useUuid)
128
+ // record2.uuid = Bun.randomUUIDv7()
136
129
137
- if ( useUuid )
138
- pivotRecord . uuid = Bun . randomUUIDv7 ( )
130
+ // const data2 = await db.insertInto(relationTable).values(record2).executeTakeFirstOrThrow()
131
+ // const relationData = data.insertId || 1
132
+ // const modelData = data2.insertId || 1
139
133
140
- if ( pivotTable )
141
- await db . insertInto ( pivotTable ) . values ( pivotRecord ) . executeTakeFirstOrThrow ( )
142
- }
134
+ // pivotRecord[foreignKey] = relationData
135
+ // pivotRecord[modelKey] = modelData
136
+
137
+ // if (useUuid)
138
+ // pivotRecord.uuid = Bun.randomUUIDv7()
139
+
140
+ // if (pivotTable)
141
+ // await db.insertInto(pivotTable).values(pivotRecord).executeTakeFirstOrThrow()
142
+ // }
143
143
144
144
async function getModelInstance ( modelName : string ) : Promise < Model > {
145
145
let modelInstance : Model
@@ -157,42 +157,42 @@ async function getModelInstance(modelName: string): Promise<Model> {
157
157
return modelInstance
158
158
}
159
159
160
- async function seedModelRelation ( modelName : string ) : Promise < bigint | number > {
161
- let modelInstance : Model
162
- let currentPath = path . userModelsPath ( `${ modelName } .ts` )
160
+ // async function seedModelRelation(modelName: string): Promise<bigint | number> {
161
+ // let modelInstance: Model
162
+ // let currentPath = path.userModelsPath(`${modelName}.ts`)
163
163
164
- if ( fs . existsSync ( currentPath ) ) {
165
- modelInstance = ( await import ( path . userModelsPath ( `${ modelName } .ts` ) ) ) . default as Model
166
- }
167
- else {
168
- currentPath = findCoreModel ( `${ modelName } .ts` )
169
- modelInstance = ( await import ( currentPath ) ) . default as Model
170
- }
164
+ // if (fs.existsSync(currentPath)) {
165
+ // modelInstance = (await import(path.userModelsPath(`${modelName}.ts`))).default as Model
166
+ // }
167
+ // else {
168
+ // currentPath = findCoreModel(`${modelName}.ts`)
169
+ // modelInstance = (await import(currentPath)).default as Model
170
+ // }
171
171
172
- if ( modelInstance === null || modelInstance === undefined )
173
- return 1
172
+ // if (modelInstance === null || modelInstance === undefined)
173
+ // return 1
174
174
175
- const record : any = { }
176
- const tableName = getTableName ( modelInstance , currentPath )
175
+ // const record: any = {}
176
+ // const tableName = getTableName(modelInstance, currentPath)
177
177
178
- const useUuid = modelInstance . traits ?. useUuid || false
178
+ // const useUuid = modelInstance.traits?.useUuid || false
179
179
180
- for ( const fieldName in modelInstance . attributes ) {
181
- const formattedFieldName = snakeCase ( fieldName )
182
- const field = modelInstance . attributes [ fieldName ]
183
- // Pass faker to the factory function
184
- if ( formattedFieldName === 'password' )
185
- record [ formattedFieldName ] = field ?. factory ? await makeHash ( field . factory ( faker ) , { algorithm : 'bcrypt' } ) : undefined
186
- else record [ formattedFieldName ] = field ?. factory ? field . factory ( faker ) : undefined
187
- }
180
+ // for (const fieldName in modelInstance.attributes) {
181
+ // const formattedFieldName = snakeCase(fieldName)
182
+ // const field = modelInstance.attributes[fieldName]
183
+ // // Pass faker to the factory function
184
+ // if (formattedFieldName === 'password')
185
+ // record[formattedFieldName] = field?.factory ? await makeHash(field.factory(faker), { algorithm: 'bcrypt' }) : undefined
186
+ // else record[formattedFieldName] = field?.factory ? field.factory(faker) : undefined
187
+ // }
188
188
189
- if ( useUuid )
190
- record . uuid = Bun . randomUUIDv7 ( )
189
+ // if (useUuid)
190
+ // record.uuid = Bun.randomUUIDv7()
191
191
192
- const data = await db . insertInto ( tableName ) . values ( record ) . executeTakeFirstOrThrow ( )
192
+ // const data = await db.insertInto(tableName).values(record).executeTakeFirstOrThrow()
193
193
194
- return ( Number ( data . insertId ) || Number ( data . numInsertedOrUpdatedRows ) ) || 1
195
- }
194
+ // return (Number(data.insertId) || Number(data.numInsertedOrUpdatedRows)) || 1
195
+ // }
196
196
197
197
export async function seed ( ) : Promise < void > {
198
198
// if a custom seeder exists, use it instead
0 commit comments