Skip to content

Commit 8751ddd

Browse files
chore: wip
1 parent 10f41e8 commit 8751ddd

File tree

16 files changed

+104
-101
lines changed

16 files changed

+104
-101
lines changed

routes/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ await route.health() // adds an `/api/health` route
2525
// await route.group('/some-path', async () => {...})
2626
// await route.action('/example') // equivalent to `route.get('/example', 'ExampleAction')`
2727

28-
await route.action('Dashboard/GetProjects')
29-
await route.action('Dashboard/Settings/UpdateAiConfig')
28+
// await route.action('Dashboard/GetProjects')
29+
// await route.action('Dashboard/Settings/UpdateAiConfig')
3030

3131
// await route.job('/example-two') // equivalent to `route.get('/example-two', 'ExampleTwoJob')`

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async function writeModelNames() {
6868
async function writeOrmActions(apiRoute: string, model: Model): Promise<void> {
6969
const modelName = model.name
7070
const formattedApiRoute = apiRoute.charAt(0).toUpperCase() + apiRoute.slice(1)
71-
71+
let method = 'GET'
7272
let actionString = `import { Action } from '@stacksjs/actions'\n`
7373
actionString += `import ${modelName} from '../src/models/${modelName}'\n\n`
7474
actionString += `import { request } from '@stacksjs/router'\n\n`
@@ -79,6 +79,8 @@ async function writeOrmActions(apiRoute: string, model: Model): Promise<void> {
7979
handleString += `handle() {
8080
return ${modelName}.all()
8181
},`
82+
83+
method = 'GET'
8284
}
8385

8486
if (apiRoute === 'show') {
@@ -87,6 +89,8 @@ async function writeOrmActions(apiRoute: string, model: Model): Promise<void> {
8789
8890
return ${modelName}.find(id)
8991
},`
92+
93+
method = 'GET'
9094
}
9195

9296
if (apiRoute === 'destroy') {
@@ -99,6 +103,8 @@ async function writeOrmActions(apiRoute: string, model: Model): Promise<void> {
99103
100104
return 'Model deleted!'
101105
},`
106+
107+
method = 'DELETE'
102108
}
103109

104110
if (apiRoute === 'store') {
@@ -107,6 +113,8 @@ async function writeOrmActions(apiRoute: string, model: Model): Promise<void> {
107113
108114
return model
109115
},`
116+
117+
method = 'POST'
110118
}
111119

112120
if (apiRoute === 'update') {
@@ -117,12 +125,14 @@ async function writeOrmActions(apiRoute: string, model: Model): Promise<void> {
117125
118126
return model.update(req.all())
119127
},`
128+
129+
method = 'PATCH'
120130
}
121131

122132
actionString += `export default new Action({
123133
name: '${modelName} ${formattedApiRoute}',
124134
description: '${modelName} ${formattedApiRoute} ORM Action',
125-
135+
method: '${method}',
126136
${handleString}
127137
})
128138
`
@@ -224,7 +234,7 @@ function hasRelations(obj: any, key: string): boolean {
224234
}
225235

226236
async function deleteExistingModels() {
227-
const modelPaths = glob.sync(path.projectStoragePath(`framework/orm/src/models*.ts`))
237+
const modelPaths = glob.sync(path.projectStoragePath(`framework/orm/src/models/*.ts`))
228238

229239
for (const modelPath of modelPaths) {
230240
if (fs.existsSync(modelPath)) await Bun.$`rm ${modelPath}`
@@ -730,18 +740,20 @@ async function generateModelString(tableName: string, model: Model, attributes:
730740
static async create(new${modelName}: New${modelName}): Promise<${modelName}Model> {
731741
const model = await db.insertInto('${tableName}')
732742
.values(new${modelName})
733-
.returningAll()
734743
.executeTakeFirstOrThrow()
735744
736-
return new ${modelName}Model(model)
745+
const result = await db.insertInto('users')
746+
.values(newUser)
747+
.executeTakeFirstOrThrow()
748+
749+
return await find(Number(result.insertId))
737750
}
738751
739752
// Method to update a ${formattedModelName}
740753
static async update(id: number, ${formattedModelName}Update: ${modelName}Update): Promise<${modelName}Model> {
741754
const model = await db.updateTable('${tableName}')
742755
.set(${formattedModelName}Update)
743756
.where('id', '=', id)
744-
.returningAll()
745757
.executeTakeFirstOrThrow()
746758
747759
return new ${modelName}Model(model)
@@ -751,7 +763,6 @@ async function generateModelString(tableName: string, model: Model, attributes:
751763
static async remove(id: number): Promise<${modelName}Model> {
752764
const model = await db.deleteFrom('${tableName}')
753765
.where('id', '=', id)
754-
.returningAll()
755766
.executeTakeFirstOrThrow()
756767
757768
return new ${modelName}Model(model)
@@ -880,7 +891,6 @@ async function generateModelString(tableName: string, model: Model, attributes:
880891
const updatedModel = await db.updateTable('${tableName}')
881892
.set(${formattedModelName})
882893
.where('id', '=', this.${formattedModelName}.id)
883-
.returningAll()
884894
.executeTakeFirst()
885895
886896
if (!updatedModel)
@@ -900,7 +910,6 @@ async function generateModelString(tableName: string, model: Model, attributes:
900910
// Insert new ${formattedModelName}
901911
const newModel = await db.insertInto('${tableName}')
902912
.values(this.${formattedModelName} as New${modelName})
903-
.returningAll()
904913
.executeTakeFirstOrThrow()
905914
this.${formattedModelName} = newModel
906915
}
@@ -1039,10 +1048,11 @@ async function generateModelString(tableName: string, model: Model, attributes:
10391048
}
10401049
10411050
export async function create(new${modelName}: New${modelName}) {
1042-
return await db.insertInto('${tableName}')
1043-
.values(new${modelName})
1044-
.returningAll()
1045-
.executeTakeFirstOrThrow()
1051+
const result = await db.insertInto('users')
1052+
.values(newUser)
1053+
.executeTakeFirstOrThrow()
1054+
1055+
return await find(Number(result.insertId))
10461056
}
10471057
10481058
export async function first() {
@@ -1076,7 +1086,6 @@ async function generateModelString(tableName: string, model: Model, attributes:
10761086
export async function remove(id: number) {
10771087
return await db.deleteFrom('${tableName}')
10781088
.where('id', '=', id)
1079-
.returningAll()
10801089
.executeTakeFirst()
10811090
}
10821091

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export class Router implements RouterInterface {
212212
public async getRoutes(): Promise<Route[]> {
213213
await import(routesPath('api.ts'))
214214
await import(projectStoragePath('framework/orm/routes.ts'))
215-
215+
216216
return this.routes
217217
}
218218

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ export async function serverResponse(req: Request) {
5757

5858
log.info(`URL: ${JSON.stringify(url)}`)
5959

60-
const foundRoute: Route | undefined = routesList.find((route: Route) => {
60+
const foundRoute: Route | undefined = routesList.filter((route: Route) => {
6161
const pattern = new RegExp(`^${route.uri.replace(/\{(\w+)\}/g, '(\\w+)')}$`);
6262

6363
return pattern.test(url.pathname)
64-
})
64+
}).find((route: Route) => route.method === req.method)
6565

6666
log.info(`Found Route: ${JSON.stringify(foundRoute)}`)
6767
// if (url.pathname === '/favicon.ico')
@@ -128,6 +128,7 @@ async function execute(foundRoute: Route, req: Request, { statusCode }: Options)
128128

129129
if (isFunction(foundCallback)) {
130130
const result = foundCallback()
131+
131132
return await new Response(JSON.stringify(result))
132133
}
133134

storage/framework/orm/Actions/UserDestroyOrmAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { request } from '@stacksjs/router'
66
export default new Action({
77
name: 'User Destroy',
88
description: 'User Destroy ORM Action',
9-
9+
method: 'DELETE',
1010
handle() {
1111
const id = request.getParam('id')
1212

storage/framework/orm/Actions/UserIndexOrmAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { request } from '@stacksjs/router'
66
export default new Action({
77
name: 'User Index',
88
description: 'User Index ORM Action',
9-
9+
method: 'GET',
1010
handle() {
1111
return User.all()
1212
},

storage/framework/orm/Actions/UserShowOrmAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { request } from '@stacksjs/router'
66
export default new Action({
77
name: 'User Show',
88
description: 'User Show ORM Action',
9-
9+
method: 'GET',
1010
handle() {
1111
const id = request.getParam('id')
1212

storage/framework/orm/Actions/UserStoreOrmAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { request } from '@stacksjs/router'
66
export default new Action({
77
name: 'User Store',
88
description: 'User Store ORM Action',
9-
9+
method: 'POST',
1010
handle() {
1111
const model = User.create(request.all())
1212

storage/framework/orm/Actions/UserUpdateOrmAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { request } from '@stacksjs/router'
66
export default new Action({
77
name: 'User Update',
88
description: 'User Update ORM Action',
9-
9+
method: 'PATCH',
1010
handle() {
1111
const id = request.getParam('id')
1212

storage/framework/orm/src/models/AccessToken.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,20 @@ import type { ColumnType, Generated, Insertable, Selectable, Updateable } from '
143143
static async create(newAccessToken: NewAccessToken): Promise<AccessTokenModel> {
144144
const model = await db.insertInto('access_tokens')
145145
.values(newAccessToken)
146-
.returningAll()
147146
.executeTakeFirstOrThrow()
148147

149-
return new AccessTokenModel(model)
148+
const result = await db.insertInto('users')
149+
.values(newUser)
150+
.executeTakeFirstOrThrow()
151+
152+
return await find(Number(result.insertId))
150153
}
151154

152155
// Method to update a accesstoken
153156
static async update(id: number, accesstokenUpdate: AccessTokenUpdate): Promise<AccessTokenModel> {
154157
const model = await db.updateTable('access_tokens')
155158
.set(accesstokenUpdate)
156159
.where('id', '=', id)
157-
.returningAll()
158160
.executeTakeFirstOrThrow()
159161

160162
return new AccessTokenModel(model)
@@ -164,7 +166,6 @@ import type { ColumnType, Generated, Insertable, Selectable, Updateable } from '
164166
static async remove(id: number): Promise<AccessTokenModel> {
165167
const model = await db.deleteFrom('access_tokens')
166168
.where('id', '=', id)
167-
.returningAll()
168169
.executeTakeFirstOrThrow()
169170

170171
return new AccessTokenModel(model)
@@ -293,7 +294,6 @@ import type { ColumnType, Generated, Insertable, Selectable, Updateable } from '
293294
const updatedModel = await db.updateTable('access_tokens')
294295
.set(accesstoken)
295296
.where('id', '=', this.accesstoken.id)
296-
.returningAll()
297297
.executeTakeFirst()
298298

299299
if (!updatedModel)
@@ -313,7 +313,6 @@ import type { ColumnType, Generated, Insertable, Selectable, Updateable } from '
313313
// Insert new accesstoken
314314
const newModel = await db.insertInto('access_tokens')
315315
.values(this.accesstoken as NewAccessToken)
316-
.returningAll()
317316
.executeTakeFirstOrThrow()
318317
this.accesstoken = newModel
319318
}
@@ -468,10 +467,11 @@ import type { ColumnType, Generated, Insertable, Selectable, Updateable } from '
468467
}
469468

470469
export async function create(newAccessToken: NewAccessToken) {
471-
return await db.insertInto('access_tokens')
472-
.values(newAccessToken)
473-
.returningAll()
474-
.executeTakeFirstOrThrow()
470+
const result = await db.insertInto('users')
471+
.values(newUser)
472+
.executeTakeFirstOrThrow()
473+
474+
return await find(Number(result.insertId))
475475
}
476476

477477
export async function first() {
@@ -505,7 +505,6 @@ import type { ColumnType, Generated, Insertable, Selectable, Updateable } from '
505505
export async function remove(id: number) {
506506
return await db.deleteFrom('access_tokens')
507507
.where('id', '=', id)
508-
.returningAll()
509508
.executeTakeFirst()
510509
}
511510

0 commit comments

Comments
 (0)