Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed May 11, 2024
1 parent 1d72a0b commit 630e587
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -13,7 +13,6 @@ tests/unit/coverage
dist*
!.gitkeep
database/stacks.sqlite
storage/framework/orm
database/migrations
custom-cli
*bun-build
Expand Down
19 changes: 9 additions & 10 deletions storage/framework/core/actions/src/orm/generate-model.ts
@@ -1,3 +1,4 @@
import { log } from '@stacksjs/logging'
import { path } from '@stacksjs/path'
import { fs, glob } from '@stacksjs/storage'
import type { ModelDefault, RelationConfig } from '@stacksjs/types'
Expand All @@ -22,7 +23,7 @@ async function generateApiRoutes(model: ModelDefault) {
let routeString = `import { route } from '@stacksjs/router'\n\n\n`
if (model.default.traits?.useApi) {
const apiRoutes = model.default.traits?.useApi?.routes
if (apiRoutes.length) {
if (apiRoutes?.length) {
for (const apiRoute of apiRoutes) {
await writeOrmActions(apiRoute, model)
routeString += await writeApiRoutes(apiRoute, model)
Expand Down Expand Up @@ -80,7 +81,7 @@ async function writeOrmActions(apiRoute: string, model: ModelDefault): Promise<v
actionString += `export default new Action({
name: '${modelName} ${formattedApiRoute}',
description: '${modelName} ${formattedApiRoute} Orm Action',
${handleString}
})
`
Expand Down Expand Up @@ -120,23 +121,21 @@ async function initiateModelGeneration(): Promise<void> {
const modelFiles = glob.sync(path.userModelsPath('*.ts'))

for (const modelFile of modelFiles) {
const model = await import(modelFile)
log.debug(`Processing model file: ${modelFile}`)

const model = await import(modelFile)
const tableName = model.default.table
const modelName = model.default.name
const modelName = path.basename(modelFile, '.ts')

await generateApiRoutes(model)

Bun.write(path.projectStoragePath(`framework/orm/${modelName}.ts`), '')
const file = Bun.file(path.projectStoragePath(`framework/orm/${modelName}.ts`))

const fields = await extractFields(model, modelFile)

const classString = await generateModelString(tableName, model, fields)

const writer = file.writer()

writer.write(classString)

await writer.end()
}
}
Expand Down Expand Up @@ -425,7 +424,7 @@ async function generateModelString(
.selectAll()
.executeTakeFirst()
if (! firstModel)
if (! firstModel)
throw new Error('Model Relation Not Found!')
const finalModel = await db.selectFrom('${tableRelation}')
Expand Down Expand Up @@ -580,7 +579,7 @@ async function generateModelString(
if (!model)
return null
return new ${modelName}Model(model)
}
Expand Down
17 changes: 14 additions & 3 deletions storage/framework/core/router/src/router.ts
Expand Up @@ -98,9 +98,20 @@ export class Router implements RouterInterface {
public async action(path: Route['url']): Promise<this> {
path = pascalCase(path) // actions are PascalCase

// removes the potential `ActionAction` suffix in case the user does not choose to use the Job suffix in their file name
const actionModule = await import(p.userActionsPath(`${path}Action.ts`.replace(/ActionAction/, 'Action')))
const callback = actionModule.default.handle
let callback: Route['callback']
try {
// removes the potential `ActionAction` suffix in case the user does not choose to use the Job suffix in their file name
const actionModule = await import(p.userActionsPath(`${path}Action.ts`.replace(/ActionAction/, 'Action')))
callback = actionModule.default.handle
} catch (error) {
try {
const actionModule = await import(p.userActionsPath(`${path}.ts`.replace(/ActionAction/, 'Action')))
callback = actionModule.default.handle
} catch (error) {
log.error(`Could not find action module for path: ${path}`)
return this
}
}

path = this.prepareUri(path)
this.addRoute('GET', path, callback, 200)
Expand Down
1 change: 1 addition & 0 deletions storage/framework/orm/.gitignore
@@ -0,0 +1 @@
*.ts

0 comments on commit 630e587

Please sign in to comment.