@@ -3,7 +3,7 @@ import { db } from '@stacksjs/database'
3
3
import { ok } from '@stacksjs/error-handling'
4
4
import { path } from '@stacksjs/path'
5
5
import { fs , glob } from '@stacksjs/storage'
6
- import type { Attributes , ModelOptions , RelationConfig } from '@stacksjs/types'
6
+ import type { Attributes , Model , RelationConfig } from '@stacksjs/types'
7
7
import { checkPivotMigration , getLastMigrationFields , hasTableBeenMigrated , mapFieldTypeToColumnType } from '.'
8
8
9
9
export async function resetMysqlDatabase ( ) {
@@ -23,9 +23,8 @@ export async function resetMysqlDatabase() {
23
23
const userModelFiles = glob . sync ( path . userModelsPath ( '*.ts' ) )
24
24
25
25
for ( const userModel of userModelFiles ) {
26
- const userModelPath = await import ( userModel )
27
-
28
- const pivotTables = await getPivotTables ( userModelPath . default )
26
+ const model = ( await import ( userModel ) ) . default as Model
27
+ const pivotTables = await getPivotTables ( model )
29
28
30
29
for ( const pivotTable of pivotTables ) await db . schema . dropTable ( pivotTable . table ) . ifExists ( ) . execute ( )
31
30
}
@@ -80,12 +79,11 @@ export async function generateMysqlMigration(modelPath: string) {
80
79
}
81
80
}
82
81
83
- const model = await import ( modelPath )
84
-
82
+ const model = ( await import ( modelPath ) ) . default as Model
85
83
const fileName = path . basename ( modelPath )
86
- const tableName = model . default . table
84
+ const tableName = model . table
87
85
88
- const fieldsString = JSON . stringify ( model . default . attributes , null , 2 ) // Pretty print the JSON
86
+ const fieldsString = JSON . stringify ( model . attributes , null , 2 ) // Pretty print the JSON
89
87
const copiedModelPath = path . frameworkPath ( `database/models/${ fileName } ` )
90
88
91
89
let haveFieldsChanged = false
@@ -119,35 +117,32 @@ export async function generateMysqlMigration(modelPath: string) {
119
117
120
118
log . debug ( `Has ${ tableName } been migrated? ${ hasBeenMigrated } ` )
121
119
122
- await createForeignKeysMigration ( model . default )
120
+ await createForeignKeysMigration ( model )
123
121
124
122
if ( haveFieldsChanged ) await createAlterTableMigration ( modelPath )
125
123
else await createTableMigration ( modelPath )
126
124
}
127
125
128
- async function getRelations ( model : ModelOptions ) : Promise < RelationConfig [ ] > {
126
+ async function getRelations ( model : Model ) : Promise < RelationConfig [ ] > {
129
127
const relationsArray = [ 'hasOne' , 'belongsTo' , 'hasMany' , 'belongsToMany' , 'hasOneThrough' ]
130
-
131
128
const relationships = [ ]
132
129
133
130
for ( const relation of relationsArray ) {
134
131
if ( hasRelations ( model , relation ) ) {
135
132
for ( const relationInstance of model [ relation ] ) {
136
133
const modelRelationPath = path . userModelsPath ( `${ relationInstance . model . name } .ts` )
137
-
138
- const modelRelation = await import ( modelRelationPath )
139
-
134
+ const modelRelation = ( await import ( modelRelationPath ) ) . default
140
135
const formattedModelName = model . name . toLowerCase ( )
141
136
142
137
relationships . push ( {
143
138
relationship : relation ,
144
139
model : relationInstance . model . name ,
145
- table : modelRelation . default . table ,
140
+ table : modelRelation . table ,
146
141
foreignKey : relationInstance . foreignKey || `${ formattedModelName } _id` ,
147
142
relationName : relationInstance . relationName || '' ,
148
143
throughModel : relationInstance . through || '' ,
149
144
throughForeignKey : relationInstance . throughForeignKey || '' ,
150
- pivotTable : relationInstance ?. pivotTable || `${ formattedModelName } _${ modelRelation . default . table } ` ,
145
+ pivotTable : relationInstance ?. pivotTable || `${ formattedModelName } _${ modelRelation . table } ` ,
151
146
} )
152
147
}
153
148
}
@@ -161,19 +156,18 @@ function hasRelations(obj: any, key: string): boolean {
161
156
}
162
157
163
158
async function getPivotTables (
164
- model : ModelOptions ,
159
+ model : Model ,
165
160
) : Promise < { table : string ; firstForeignKey : string ; secondForeignKey : string } [ ] > {
166
161
const pivotTable = [ ]
167
162
168
163
if ( 'belongsToMany' in model ) {
169
164
for ( const belongsToManyRelation of model . belongsToMany ) {
170
165
const modelRelationPath = path . userModelsPath ( `${ belongsToManyRelation . model } .ts` )
171
- const modelRelation = await import ( modelRelationPath )
172
-
166
+ const modelRelation = ( await import ( modelRelationPath ) ) . default
173
167
const formattedModelName = model . name . toLowerCase ( )
174
168
175
169
pivotTable . push ( {
176
- table : belongsToManyRelation ?. pivotTable || `${ formattedModelName } _${ modelRelation . default . table } ` ,
170
+ table : belongsToManyRelation ?. pivotTable || `${ formattedModelName } _${ modelRelation . table } ` ,
177
171
firstForeignKey : belongsToManyRelation . firstForeignKey ,
178
172
secondForeignKey : belongsToManyRelation . secondForeignKey ,
179
173
} )
@@ -188,14 +182,14 @@ async function getPivotTables(
188
182
async function createTableMigration ( modelPath : string ) {
189
183
log . debug ( 'createTableMigration modelPath:' , modelPath )
190
184
191
- const model = await import ( modelPath )
192
- const tableName = model . default . table
185
+ const model = ( await import ( modelPath ) ) . default as Model
186
+ const tableName = model . table
193
187
194
- await createPivotTableMigration ( model . default )
188
+ await createPivotTableMigration ( model )
195
189
196
- const fields = model . default . attributes
197
- const useTimestamps = model . default ?. traits ?. useTimestamps ?? model . default ? .traits ?. timestampable
198
- const useSoftDeletes = model . default ?. traits ?. useSoftDeletes ?? model . default ? .traits ?. softDeletable
190
+ const fields = model . attributes
191
+ const useTimestamps = model . traits ?. useTimestamps ?? model . traits ?. timestampable
192
+ const useSoftDeletes = model . traits ?. useSoftDeletes ?? model . traits ?. softDeletable
199
193
200
194
let migrationContent = `import type { Database } from '@stacksjs/database'\n`
201
195
migrationContent += `import { sql } from '@stacksjs/database'\n\n`
@@ -242,7 +236,7 @@ async function createTableMigration(modelPath: string) {
242
236
log . success ( `Created migration: ${ migrationFileName } ` )
243
237
}
244
238
245
- async function createPivotTableMigration ( model : ModelOptions ) {
239
+ async function createPivotTableMigration ( model : Model ) {
246
240
const pivotTables = await getPivotTables ( model )
247
241
248
242
if ( ! pivotTables . length ) return
@@ -273,7 +267,7 @@ async function createPivotTableMigration(model: ModelOptions) {
273
267
}
274
268
}
275
269
276
- async function createForeignKeysMigration ( model : ModelOptions ) {
270
+ async function createForeignKeysMigration ( model : Model ) {
277
271
const relations = await getRelations ( model )
278
272
279
273
for ( const relation of relations ) {
@@ -297,15 +291,15 @@ async function createForeignKeysMigration(model: ModelOptions) {
297
291
export async function createAlterTableMigration ( modelPath : string ) {
298
292
console . log ( 'createAlterTableMigration' )
299
293
300
- const model = await import ( modelPath )
294
+ const model = ( await import ( modelPath ) ) . default as Model
301
295
const modelName = path . basename ( modelPath )
302
- const tableName = model . default . table
296
+ const tableName = model . table
303
297
304
298
// Assuming you have a function to get the fields from the last migration
305
299
// For simplicity, this is not implemented here
306
300
const lastMigrationFields = await getLastMigrationFields ( modelName )
307
301
const lastFields = lastMigrationFields ?? { }
308
- const currentFields = model . default . attributes as Attributes
302
+ const currentFields = model . attributes as Attributes
309
303
310
304
// Determine fields to add and remove
311
305
const fieldsToAdd = Object . keys ( currentFields )
@@ -344,9 +338,8 @@ export async function fetchMysqlTables(): Promise<string[]> {
344
338
const tables : string [ ] = [ ]
345
339
346
340
for ( const modelPath of modelFiles ) {
347
- const model = await import ( modelPath )
348
-
349
- const tableName = model . default . table
341
+ const model = ( await import ( modelPath ) ) . default as Model
342
+ const tableName = model . table
350
343
351
344
tables . push ( tableName )
352
345
}
0 commit comments