1
+ import { log } from '@stacksjs/logging'
1
2
import { path } from '@stacksjs/path'
2
3
import { fs , glob } from '@stacksjs/storage'
3
4
import type { ModelDefault , RelationConfig } from '@stacksjs/types'
@@ -22,7 +23,7 @@ async function generateApiRoutes(model: ModelDefault) {
22
23
let routeString = `import { route } from '@stacksjs/router'\n\n\n`
23
24
if ( model . default . traits ?. useApi ) {
24
25
const apiRoutes = model . default . traits ?. useApi ?. routes
25
- if ( apiRoutes . length ) {
26
+ if ( apiRoutes ? .length ) {
26
27
for ( const apiRoute of apiRoutes ) {
27
28
await writeOrmActions ( apiRoute , model )
28
29
routeString += await writeApiRoutes ( apiRoute , model )
@@ -80,7 +81,7 @@ async function writeOrmActions(apiRoute: string, model: ModelDefault): Promise<v
80
81
actionString += `export default new Action({
81
82
name: '${ modelName } ${ formattedApiRoute } ',
82
83
description: '${ modelName } ${ formattedApiRoute } Orm Action',
83
-
84
+
84
85
${ handleString }
85
86
})
86
87
`
@@ -120,23 +121,21 @@ async function initiateModelGeneration(): Promise<void> {
120
121
const modelFiles = glob . sync ( path . userModelsPath ( '*.ts' ) )
121
122
122
123
for ( const modelFile of modelFiles ) {
123
- const model = await import ( modelFile )
124
+ log . debug ( `Processing model file: ${ modelFile } ` )
124
125
126
+ const model = await import ( modelFile )
125
127
const tableName = model . default . table
126
- const modelName = model . default . name
128
+ const modelName = path . basename ( modelFile , '.ts' )
127
129
128
130
await generateApiRoutes ( model )
129
131
132
+ Bun . write ( path . projectStoragePath ( `framework/orm/${ modelName } .ts` ) , '' )
130
133
const file = Bun . file ( path . projectStoragePath ( `framework/orm/${ modelName } .ts` ) )
131
-
132
134
const fields = await extractFields ( model , modelFile )
133
-
134
135
const classString = await generateModelString ( tableName , model , fields )
135
136
136
137
const writer = file . writer ( )
137
-
138
138
writer . write ( classString )
139
-
140
139
await writer . end ( )
141
140
}
142
141
}
@@ -425,7 +424,7 @@ async function generateModelString(
425
424
.selectAll()
426
425
.executeTakeFirst()
427
426
428
- if (! firstModel)
427
+ if (! firstModel)
429
428
throw new Error('Model Relation Not Found!')
430
429
431
430
const finalModel = await db.selectFrom('${ tableRelation } ')
@@ -580,7 +579,7 @@ async function generateModelString(
580
579
581
580
if (!model)
582
581
return null
583
-
582
+
584
583
return new ${ modelName } Model(model)
585
584
}
586
585
0 commit comments