@@ -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 , Model } from '@stacksjs/types'
6
+ import type { Attribute , Attributes , Model } from '@stacksjs/types'
7
7
import { checkPivotMigration , fetchOtherModelRelations , getLastMigrationFields , hasTableBeenMigrated , mapFieldTypeToColumnType } from '.'
8
8
9
9
export async function resetMysqlDatabase ( ) {
@@ -102,7 +102,7 @@ export async function generateMysqlMigration(modelPath: string) {
102
102
// if the fields have not changed, we need to migrate the table
103
103
104
104
// we need to check if this tableName has already been migrated
105
- const hasBeenMigrated = await hasTableBeenMigrated ( tableName )
105
+ const hasBeenMigrated = await hasTableBeenMigrated ( tableName as string )
106
106
107
107
log . debug ( `Has ${ tableName } been migrated? ${ hasBeenMigrated } ` )
108
108
@@ -112,23 +112,25 @@ export async function generateMysqlMigration(modelPath: string) {
112
112
113
113
async function getPivotTables (
114
114
model : Model ,
115
- ) : Promise < { table : string ; firstForeignKey : string ; secondForeignKey : string } [ ] > {
115
+ ) : Promise < { table : string ; firstForeignKey : string | undefined ; secondForeignKey : string | undefined } [ ] > {
116
116
const pivotTable = [ ]
117
117
118
- if ( 'belongsToMany' in model ) {
119
- for ( const belongsToManyRelation of model . belongsToMany ) {
120
- const modelRelationPath = path . userModelsPath ( `${ belongsToManyRelation . model } .ts` )
121
- const modelRelation = ( await import ( modelRelationPath ) ) . default
122
- const formattedModelName = model . name . toLowerCase ( )
123
-
124
- pivotTable . push ( {
125
- table : belongsToManyRelation ?. pivotTable || `${ formattedModelName } _${ modelRelation . table } ` ,
126
- firstForeignKey : belongsToManyRelation . firstForeignKey ,
127
- secondForeignKey : belongsToManyRelation . secondForeignKey ,
128
- } )
118
+ if ( model . belongsToMany && model . name ) {
119
+ if ( 'belongsToMany' in model ) {
120
+ for ( const belongsToManyRelation of model . belongsToMany ) {
121
+ const modelRelationPath = path . userModelsPath ( `${ belongsToManyRelation . model } .ts` )
122
+ const modelRelation = ( await import ( modelRelationPath ) ) . default
123
+ const formattedModelName = model . name . toLowerCase ( )
124
+
125
+ pivotTable . push ( {
126
+ table : belongsToManyRelation ?. pivotTable || `${ formattedModelName } _${ modelRelation . table } ` ,
127
+ firstForeignKey : belongsToManyRelation . firstForeignKey ,
128
+ secondForeignKey : belongsToManyRelation . secondForeignKey ,
129
+ } )
130
+ }
131
+
132
+ return pivotTable
129
133
}
130
-
131
- return pivotTable
132
134
}
133
135
134
136
return [ ]
@@ -146,7 +148,7 @@ async function createTableMigration(modelPath: string) {
146
148
147
149
const otherModelRelations = await fetchOtherModelRelations ( model , modelFiles )
148
150
149
- const fields = model . attributes
151
+ const fields = model . attributes as Attributes
150
152
const useTimestamps = model ?. traits ?. useTimestamps ?? model ?. traits ?. timestampable
151
153
const useSoftDeletes = model ?. traits ?. useSoftDeletes ?? model ?. traits ?. softDeletable
152
154
@@ -158,7 +160,7 @@ async function createTableMigration(modelPath: string) {
158
160
migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())\n`
159
161
160
162
for ( const [ fieldName , options ] of Object . entries ( fields ) ) {
161
- const fieldOptions = options as Attributes
163
+ const fieldOptions = options as Attribute
162
164
const columnType = mapFieldTypeToColumnType ( fieldOptions . validator ?. rule )
163
165
migrationContent += ` .addColumn('${ fieldName } ', '${ columnType } '`
164
166
@@ -176,7 +178,7 @@ async function createTableMigration(modelPath: string) {
176
178
if ( otherModelRelations ?. length ) {
177
179
for ( const modelRelation of otherModelRelations ) {
178
180
migrationContent += ` .addColumn('${ modelRelation . foreignKey } ', 'integer', (col) =>
179
- col.references('${ modelRelation . relationTable } .id').onDelete('cascade').notNull()
181
+ col.references('${ modelRelation . relationTable } .id').onDelete('cascade')
180
182
) \n`
181
183
}
182
184
}
@@ -258,7 +260,7 @@ export async function createAlterTableMigration(modelPath: string) {
258
260
259
261
// Add new fields
260
262
for ( const fieldName of fieldsToAdd ) {
261
- const options = currentFields [ fieldName ] as Attributes
263
+ const options = currentFields [ fieldName ] as Attribute
262
264
const columnType = mapFieldTypeToColumnType ( options . validator ?. rule )
263
265
migrationContent += ` .addColumn('${ fieldName } ', '${ columnType } ')\n`
264
266
}
@@ -286,7 +288,7 @@ export async function fetchMysqlTables(): Promise<string[]> {
286
288
287
289
for ( const modelPath of modelFiles ) {
288
290
const model = ( await import ( modelPath ) ) . default as Model
289
- const tableName = model . table
291
+ const tableName = model . table as string
290
292
291
293
tables . push ( tableName )
292
294
}
0 commit comments