Skip to content

Commit 75f4d12

Browse files
chore: wip
1 parent e988775 commit 75f4d12

File tree

11 files changed

+486
-107
lines changed

11 files changed

+486
-107
lines changed

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,6 @@ async function generateModelString(
781781
}
782782

783783
return `import type { ColumnType, Generated, Insertable, Selectable, Updateable } from 'kysely'
784-
import type { Result } from '@stacksjs/error-handling'
785-
import { err, handleError, ok } from '@stacksjs/error-handling'
786784
import { db } from '@stacksjs/database'
787785
${relationImports}
788786
// import { Kysely, MysqlDialect, PostgresDialect } from 'kysely'
@@ -830,12 +828,14 @@ async function generateModelString(
830828
private results: Partial<${modelName}Type>[]
831829
private hidden = ['password'] // TODO: this hidden functionality needs to be implemented still
832830
protected query: any
831+
protected hasSelect: boolean
833832
${declareFields}
834833
constructor(${formattedModelName}: Partial<${modelName}Type> | null) {
835834
this.${formattedModelName} = ${formattedModelName}
836835
${constructorFields}
837836
838837
this.query = db.selectFrom('${tableName}')
838+
this.hasSelect = false
839839
}
840840
841841
// Method to find a ${modelName} by ID
@@ -931,7 +931,15 @@ async function generateModelString(
931931
932932
// Method to get a ${modelName} by criteria
933933
async get(): Promise<${modelName}Model[]> {
934-
return await this.query.selectAll().execute()
934+
if (this.hasSelect) {
935+
const model = await this.query.execute()
936+
937+
return model.map((modelItem: ${modelName}Model) => new ${modelName}Model(modelItem))
938+
}
939+
940+
const model = await this.query.selectAll().execute()
941+
942+
return model.map((modelItem: ${modelName}Model) => new ${modelName}Model(modelItem))
935943
}
936944
937945
// Method to get all ${tableName}
@@ -1051,41 +1059,41 @@ async function generateModelString(
10511059
static orderBy(column: keyof ${modelName}Type, order: 'asc' | 'desc'): ${modelName}Model {
10521060
const instance = new this(null);
10531061
1054-
instance.query.orderBy(column, order)
1062+
instance.query = instance.orderBy(column, order)
10551063
10561064
return instance
10571065
}
10581066
10591067
orderBy(column: keyof ${modelName}Type, order: 'asc' | 'desc'): ${modelName}Model {
1060-
this.query.orderBy(column, order)
1068+
this.query = this.query.orderBy(column, order)
10611069
10621070
return this
10631071
}
10641072
10651073
static orderByDesc(column: keyof ${modelName}Type): ${modelName}Model {
10661074
const instance = new this(null);
10671075
1068-
instance.query.orderBy(column, 'desc')
1076+
instance.query = instance.query.orderBy(column, 'desc')
10691077
10701078
return instance
10711079
}
10721080
10731081
orderByDesc(column: keyof ${modelName}Type): ${modelName}Model {
1074-
this.query.orderBy(column, 'desc')
1082+
this.query = this.orderBy(column, 'desc')
10751083
10761084
return this
10771085
}
10781086
10791087
static orderByAsc(column: keyof ${modelName}Type): ${modelName}Model {
10801088
const instance = new this(null);
10811089
1082-
instance.query.orderBy(column, 'desc')
1090+
instance.query = instance.query.orderBy(column, 'desc')
10831091
10841092
return instance
10851093
}
10861094
10871095
orderByAsc(column: keyof ${modelName}Type): ${modelName}Model {
1088-
this.query.orderBy(column, 'desc')
1096+
this.query = this.query.orderBy(column, 'desc')
10891097
10901098
return this
10911099
}
@@ -1132,6 +1140,34 @@ async function generateModelString(
11321140
11331141
${relationMethods}
11341142
1143+
distinct(column: keyof ${modelName}Type): ${modelName}Model {
1144+
this.query = this.query.distinctOn(column)
1145+
1146+
return this
1147+
}
1148+
1149+
static distinct(column: keyof ${modelName}Type): ${modelName}Model {
1150+
const instance = new this(null)
1151+
1152+
instance.query = instance.query.distinctOn(column)
1153+
1154+
return instance
1155+
}
1156+
1157+
join(table: string, firstCol: string, secondCol: string): ${modelName}Model {
1158+
this.query = this.query.innerJoin(table, firstCol, secondCol)
1159+
1160+
return this
1161+
}
1162+
1163+
static join(table: string, firstCol: string, secondCol: string): ${modelName}Model {
1164+
const instance = new this(null)
1165+
1166+
instance.query = instance.query.innerJoin(table, firstCol, secondCol)
1167+
1168+
return instance
1169+
}
1170+
11351171
toJSON() {
11361172
const output: Partial<${modelName}Type> = { ...this.${formattedModelName} }
11371173

storage/framework/orm/src/builder.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export class AccessTokenModel {
8181
this.results = null
8282
this.hasSelect = false
8383
}
84-
8584

8685
// Method to find a accesstoken by ID
8786
static async find(id: number, fields?: (keyof AccessTokenType)[]): Promise<AccessTokenModel | null> {
@@ -100,11 +99,11 @@ export class AccessTokenModel {
10099
// Method to find a accesstoken by ID
101100
static select(columns: string[]): AccessTokenModel {
102101
const instance = new this(null)
103-
102+
104103
instance.query = instance.query.select(columns)
105104

106105
instance.hasSelect = true
107-
106+
108107
return instance
109108
}
110109

@@ -182,9 +181,6 @@ export class AccessTokenModel {
182181

183182
// Method to get a accesstoken by criteria
184183
async get(): Promise<AccessTokenModel[]> {
185-
186-
console.log(this.query)
187-
188184
if (this.hasSelect) {
189185
const model = await this.query.execute()
190186

@@ -251,10 +247,10 @@ export class AccessTokenModel {
251247
const instance = new this(null)
252248

253249
if (args.length === 2) {
254-
[column, value] = args
250+
;[column, value] = args
255251
operator = '='
256252
} else if (args.length === 3) {
257-
[column, operator, value] = args
253+
;[column, operator, value] = args
258254
} else {
259255
throw new Error('Invalid number of arguments')
260256
}
@@ -303,27 +299,27 @@ export class AccessTokenModel {
303299
static orderByDesc(column: keyof AccessTokenType): AccessTokenModel {
304300
const instance = new this(null)
305301

306-
instance.query.orderBy(column, 'desc')
302+
instance.query = instance.query.orderBy(column, 'desc')
307303

308304
return instance
309305
}
310306

311307
orderByDesc(column: keyof AccessTokenType): AccessTokenModel {
312-
this.query.orderBy(column, 'desc')
308+
this.query = this.orderBy(column, 'desc')
313309

314310
return this
315311
}
316312

317313
static orderByAsc(column: keyof AccessTokenType): AccessTokenModel {
318314
const instance = new this(null)
319315

320-
instance.query.orderBy(column, 'desc')
316+
instance.query = instance.orderBy(column, 'desc')
321317

322318
return instance
323319
}
324320

325321
orderByAsc(column: keyof AccessTokenType): AccessTokenModel {
326-
this.query.orderBy(column, 'desc')
322+
this.query = this.orderBy(column, 'desc')
327323

328324
return this
329325
}
@@ -360,9 +356,32 @@ export class AccessTokenModel {
360356
await db.deleteFrom('personal_access_tokens').where('id', '=', this.id).execute()
361357
}
362358

363-
// Method to delete the accesstoken instance
364-
async distinct(column: keyof AccessTokenType): Promise<void> {
365-
return this.query.distinctOn(column)
359+
distinct(column: keyof AccessTokenType): AccessTokenModel {
360+
this.query = this.query.distinctOn(column)
361+
362+
return this
363+
}
364+
365+
static distinct(column: keyof AccessTokenType): AccessTokenModel {
366+
const instance = new this(null)
367+
368+
instance.query = instance.query.distinctOn(column)
369+
370+
return instance
371+
}
372+
373+
join(table: string, firstCol: string, secondCol: string): AccessTokenModel {
374+
this.query = this.query.innerJoin(table, firstCol, secondCol)
375+
376+
return this
377+
}
378+
379+
static join(table: string, firstCol: string, secondCol: string): AccessTokenModel {
380+
const instance = new this(null)
381+
382+
instance.query = instance.query.innerJoin(table, firstCol, secondCol)
383+
384+
return instance
366385
}
367386

368387
// // Method to refresh the accesstoken instance data from the database
@@ -394,6 +413,6 @@ export class AccessTokenModel {
394413
}
395414
}
396415

397-
const result = await AccessTokenModel.select(['name', 'token']).get()
416+
const result = await AccessTokenModel.orderByDesc('id').get()
398417

399-
console.log(result)
418+
console.log(result)

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { db } from '@stacksjs/database'
2-
import type { Result } from '@stacksjs/error-handling'
3-
import { err, handleError, ok } from '@stacksjs/error-handling'
42
import type { ColumnType, Generated, Insertable, Selectable, Updateable } from 'kysely'
53
import Team from './Team'
64

@@ -57,6 +55,7 @@ export class AccessTokenModel {
5755
private results: Partial<AccessTokenType>[]
5856
private hidden = ['password'] // TODO: this hidden functionality needs to be implemented still
5957
protected query: any
58+
protected hasSelect: boolean
6059
public id: number | undefined
6160
public name: string | undefined
6261
public token: string | undefined
@@ -74,6 +73,7 @@ export class AccessTokenModel {
7473
this.team_id = accesstoken?.team_id
7574

7675
this.query = db.selectFrom('personal_access_tokens')
76+
this.hasSelect = false
7777
}
7878

7979
// Method to find a AccessToken by ID
@@ -155,7 +155,15 @@ export class AccessTokenModel {
155155

156156
// Method to get a AccessToken by criteria
157157
async get(): Promise<AccessTokenModel[]> {
158-
return await this.query.selectAll().execute()
158+
if (this.hasSelect) {
159+
const model = await this.query.execute()
160+
161+
return model.map((modelItem: AccessTokenModel) => new AccessTokenModel(modelItem))
162+
}
163+
164+
const model = await this.query.selectAll().execute()
165+
166+
return model.map((modelItem: AccessTokenModel) => new AccessTokenModel(modelItem))
159167
}
160168

161169
// Method to get all personal_access_tokens
@@ -268,41 +276,41 @@ export class AccessTokenModel {
268276
static orderBy(column: keyof AccessTokenType, order: 'asc' | 'desc'): AccessTokenModel {
269277
const instance = new this(null)
270278

271-
instance.query.orderBy(column, order)
279+
instance.query = instance.orderBy(column, order)
272280

273281
return instance
274282
}
275283

276284
orderBy(column: keyof AccessTokenType, order: 'asc' | 'desc'): AccessTokenModel {
277-
this.query.orderBy(column, order)
285+
this.query = this.query.orderBy(column, order)
278286

279287
return this
280288
}
281289

282290
static orderByDesc(column: keyof AccessTokenType): AccessTokenModel {
283291
const instance = new this(null)
284292

285-
instance.query.orderBy(column, 'desc')
293+
instance.query = instance.query.orderBy(column, 'desc')
286294

287295
return instance
288296
}
289297

290298
orderByDesc(column: keyof AccessTokenType): AccessTokenModel {
291-
this.query.orderBy(column, 'desc')
299+
this.query = this.orderBy(column, 'desc')
292300

293301
return this
294302
}
295303

296304
static orderByAsc(column: keyof AccessTokenType): AccessTokenModel {
297305
const instance = new this(null)
298306

299-
instance.query.orderBy(column, 'desc')
307+
instance.query = instance.query.orderBy(column, 'desc')
300308

301309
return instance
302310
}
303311

304312
orderByAsc(column: keyof AccessTokenType): AccessTokenModel {
305-
this.query.orderBy(column, 'desc')
313+
this.query = this.query.orderBy(column, 'desc')
306314

307315
return this
308316
}
@@ -353,6 +361,34 @@ export class AccessTokenModel {
353361
return model
354362
}
355363

364+
distinct(column: keyof AccessTokenType): AccessTokenModel {
365+
this.query = this.query.distinctOn(column)
366+
367+
return this
368+
}
369+
370+
static distinct(column: keyof AccessTokenType): AccessTokenModel {
371+
const instance = new this(null)
372+
373+
instance.query = instance.query.distinctOn(column)
374+
375+
return instance
376+
}
377+
378+
join(table: string, firstCol: string, secondCol: string): AccessTokenModel {
379+
this.query = this.query.innerJoin(table, firstCol, secondCol)
380+
381+
return this
382+
}
383+
384+
static join(table: string, firstCol: string, secondCol: string): AccessTokenModel {
385+
const instance = new this(null)
386+
387+
instance.query = instance.query.innerJoin(table, firstCol, secondCol)
388+
389+
return instance
390+
}
391+
356392
toJSON() {
357393
const output: Partial<AccessTokenType> = { ...this.accesstoken }
358394

0 commit comments

Comments
 (0)