Skip to content

Commit 630e587

Browse files
committed
chore: wip
1 parent 1d72a0b commit 630e587

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ tests/unit/coverage
1313
dist*
1414
!.gitkeep
1515
database/stacks.sqlite
16-
storage/framework/orm
1716
database/migrations
1817
custom-cli
1918
*bun-build

storage/framework/core/actions/src/orm/generate-model.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { log } from '@stacksjs/logging'
12
import { path } from '@stacksjs/path'
23
import { fs, glob } from '@stacksjs/storage'
34
import type { ModelDefault, RelationConfig } from '@stacksjs/types'
@@ -22,7 +23,7 @@ async function generateApiRoutes(model: ModelDefault) {
2223
let routeString = `import { route } from '@stacksjs/router'\n\n\n`
2324
if (model.default.traits?.useApi) {
2425
const apiRoutes = model.default.traits?.useApi?.routes
25-
if (apiRoutes.length) {
26+
if (apiRoutes?.length) {
2627
for (const apiRoute of apiRoutes) {
2728
await writeOrmActions(apiRoute, model)
2829
routeString += await writeApiRoutes(apiRoute, model)
@@ -80,7 +81,7 @@ async function writeOrmActions(apiRoute: string, model: ModelDefault): Promise<v
8081
actionString += `export default new Action({
8182
name: '${modelName} ${formattedApiRoute}',
8283
description: '${modelName} ${formattedApiRoute} Orm Action',
83-
84+
8485
${handleString}
8586
})
8687
`
@@ -120,23 +121,21 @@ async function initiateModelGeneration(): Promise<void> {
120121
const modelFiles = glob.sync(path.userModelsPath('*.ts'))
121122

122123
for (const modelFile of modelFiles) {
123-
const model = await import(modelFile)
124+
log.debug(`Processing model file: ${modelFile}`)
124125

126+
const model = await import(modelFile)
125127
const tableName = model.default.table
126-
const modelName = model.default.name
128+
const modelName = path.basename(modelFile, '.ts')
127129

128130
await generateApiRoutes(model)
129131

132+
Bun.write(path.projectStoragePath(`framework/orm/${modelName}.ts`), '')
130133
const file = Bun.file(path.projectStoragePath(`framework/orm/${modelName}.ts`))
131-
132134
const fields = await extractFields(model, modelFile)
133-
134135
const classString = await generateModelString(tableName, model, fields)
135136

136137
const writer = file.writer()
137-
138138
writer.write(classString)
139-
140139
await writer.end()
141140
}
142141
}
@@ -425,7 +424,7 @@ async function generateModelString(
425424
.selectAll()
426425
.executeTakeFirst()
427426
428-
if (! firstModel)
427+
if (! firstModel)
429428
throw new Error('Model Relation Not Found!')
430429
431430
const finalModel = await db.selectFrom('${tableRelation}')
@@ -580,7 +579,7 @@ async function generateModelString(
580579
581580
if (!model)
582581
return null
583-
582+
584583
return new ${modelName}Model(model)
585584
}
586585

storage/framework/core/router/src/router.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,20 @@ export class Router implements RouterInterface {
9898
public async action(path: Route['url']): Promise<this> {
9999
path = pascalCase(path) // actions are PascalCase
100100

101-
// removes the potential `ActionAction` suffix in case the user does not choose to use the Job suffix in their file name
102-
const actionModule = await import(p.userActionsPath(`${path}Action.ts`.replace(/ActionAction/, 'Action')))
103-
const callback = actionModule.default.handle
101+
let callback: Route['callback']
102+
try {
103+
// removes the potential `ActionAction` suffix in case the user does not choose to use the Job suffix in their file name
104+
const actionModule = await import(p.userActionsPath(`${path}Action.ts`.replace(/ActionAction/, 'Action')))
105+
callback = actionModule.default.handle
106+
} catch (error) {
107+
try {
108+
const actionModule = await import(p.userActionsPath(`${path}.ts`.replace(/ActionAction/, 'Action')))
109+
callback = actionModule.default.handle
110+
} catch (error) {
111+
log.error(`Could not find action module for path: ${path}`)
112+
return this
113+
}
114+
}
104115

105116
path = this.prepareUri(path)
106117
this.addRoute('GET', path, callback, 200)

storage/framework/orm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.ts

0 commit comments

Comments
 (0)