@@ -23,11 +23,13 @@ await initiateModelGeneration()
23
23
await setKyselyTypes ( )
24
24
25
25
async function generateApiRoutes ( model : ModelDefault ) {
26
+
26
27
let routeString = `import { route } from '@stacksjs/router'\n\n\n`
27
28
if ( model . default . traits ?. useApi ) {
28
29
const apiRoutes = model . default . traits ?. useApi ?. routes
29
30
if ( apiRoutes . length ) {
30
31
for ( const apiRoute of apiRoutes ) {
32
+ await writeOrmActions ( apiRoute , model )
31
33
routeString += await writeApiRoutes ( apiRoute , model )
32
34
}
33
35
}
@@ -41,36 +43,61 @@ async function generateApiRoutes(model: ModelDefault) {
41
43
}
42
44
}
43
45
44
- async function writeOrmActions ( ) : Promise < void > { }
46
+ async function writeOrmActions ( apiRoute : string , model : ModelDefault ) : Promise < void > {
47
+ const modelName = model . default . name
48
+ const formattedApiRoute = apiRoute . charAt ( 0 ) . toUpperCase ( ) + apiRoute . slice ( 1 )
49
+
50
+ let actionString = `import { Action } from '@stacksjs/actions'\n`
51
+ actionString += `import ${ modelName } from '../${ modelName } '\n\n`
52
+
53
+ actionString += `export default new Action({
54
+ name: '${ modelName } ${ formattedApiRoute } ',
55
+ description: '${ modelName } ${ formattedApiRoute } Orm Action',
56
+
57
+ handle() {
58
+ return ${ modelName } .find(1)
59
+ },
60
+ })
61
+ `
62
+
63
+ const file = Bun . file ( path . projectStoragePath ( `framework/orm/Actions/${ modelName } ${ formattedApiRoute } OrmAction.ts` ) )
64
+
65
+ const writer = file . writer ( )
66
+
67
+ writer . write ( actionString )
68
+ }
45
69
46
70
async function writeApiRoutes (
47
71
apiRoute : string ,
48
72
model : ModelDefault ,
49
73
) : Promise < string > {
50
74
let routeString = ``
51
- const modelNameFormatted = model . default . name . toLowerCase ( )
75
+ const tableName = model . default . table
52
76
const modelName = model . default . name
53
77
54
78
if ( apiRoute === 'index' )
55
- routeString += `await route.get('${ modelNameFormatted } ', () => 'Actions/${ modelName } IndexOrmAction')\n\n`
79
+ routeString += `await route.get('${ tableName } ', () => 'Actions/${ modelName } IndexOrmAction')\n\n`
56
80
57
81
if ( apiRoute === 'store' )
58
- routeString += `await route.post('${ modelNameFormatted } ', () => 'Actions/${ modelName } StoreOrmAction')\n\n`
82
+ routeString += `await route.post('${ tableName } ', () => 'Actions/${ modelName } StoreOrmAction')\n\n`
59
83
60
84
if ( apiRoute === 'update' )
61
- routeString += `await route.patch('${ modelNameFormatted } /{id}', () => 'Actions/${ modelName } UpdateOrmAction')\n\n`
85
+ routeString += `await route.patch('${ tableName } /{id}', () => 'Actions/${ modelName } UpdateOrmAction')\n\n`
62
86
63
87
if ( apiRoute === 'show' )
64
- routeString += `await route.get('${ modelNameFormatted } /{id}', () => 'Actions/${ modelName } ShowOrmAction')\n\n`
88
+ routeString += `await route.get('${ tableName } /{id}', () => 'Actions/${ modelName } ShowOrmAction')\n\n`
65
89
66
90
if ( apiRoute === 'destroy' )
67
- routeString += `await route.delete('${ modelNameFormatted } /{id}', () => 'Actions/${ modelName } DestroyOrmAction')\n\n`
91
+ routeString += `await route.delete('${ tableName } /{id}', () => 'Actions/${ modelName } DestroyOrmAction')\n\n`
68
92
69
93
return routeString
70
94
}
71
95
72
96
async function initiateModelGeneration ( ) : Promise < void > {
97
+
73
98
await deleteExistingModels ( )
99
+ await deleteExistingOrmActions ( )
100
+
74
101
const modelFiles = glob . sync ( path . userModelsPath ( '*.ts' ) )
75
102
76
103
for ( const modelFile of modelFiles ) {
@@ -79,7 +106,8 @@ async function initiateModelGeneration(): Promise<void> {
79
106
const tableName = model . default . table
80
107
const modelName = model . default . name
81
108
82
- generateApiRoutes ( model )
109
+ await generateApiRoutes ( model )
110
+
83
111
84
112
const file = Bun . file (
85
113
path . projectStoragePath ( `framework/orm/${ modelName } .ts` ) ,
@@ -156,6 +184,14 @@ async function deleteExistingModels() {
156
184
if ( fs . existsSync ( typePath ) ) await Bun . $ `rm ${ typePath } `
157
185
}
158
186
187
+ async function deleteExistingOrmActions ( ) {
188
+ const ormPaths = glob . sync ( path . projectStoragePath ( `framework/orm/Actions/*.ts` ) )
189
+
190
+ for ( const ormPath of ormPaths ) {
191
+ if ( fs . existsSync ( ormPath ) ) await Bun . $ `rm ${ ormPath } `
192
+ }
193
+ }
194
+
159
195
async function setKyselyTypes ( ) {
160
196
let text = ``
161
197
const modelFiles = glob . sync ( path . userModelsPath ( '*.ts' ) )
0 commit comments