Skip to content

Commit 167db8e

Browse files
chore: wip
1 parent f879da4 commit 167db8e

File tree

1 file changed

+85
-111
lines changed

1 file changed

+85
-111
lines changed

storage/framework/orm/src/builder.ts

Lines changed: 85 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
abilities: string[]
1818
team_id: number
1919

20-
created_at: string
21-
updated_at: string
22-
deleted_at: string
20+
created_at: ColumnType<Date, string | undefined, never>
21+
updated_at: ColumnType<Date, string | undefined, never>
22+
deleted_at: ColumnType<Date, string | undefined, never>
2323
}
2424

2525
interface AccessTokenResponse {
@@ -52,6 +52,7 @@
5252

5353
export class AccessTokenModel {
5454
private results: any
55+
private accesstoken: Partial<AccessTokenType> | null
5556
private hidden = ['password'] // TODO: this hidden functionality needs to be implemented still
5657
protected query: any
5758

@@ -61,10 +62,11 @@
6162
public plainTextToken: string | undefined
6263
public abilities: string[] | undefined
6364
public team_id: number | undefined
64-
public created_at: string | undefined
65-
public updated_at: string | undefined
65+
public created_at: Date | undefined
66+
public updated_at: Date | undefined
6667

6768
constructor(accesstoken: Partial<AccessTokenType> | null) {
69+
this.accesstoken = accesstoken
6870
this.id = accesstoken?.id
6971
this.name = accesstoken?.name
7072
this.token = accesstoken?.name
@@ -73,13 +75,10 @@
7375
this.team_id = accesstoken?.team_id
7476
this.created_at = accesstoken?.created_at
7577
this.updated_at = accesstoken?.updated_at
76-
this.query = null
78+
this.query = db.selectFrom('personal_access_tokens')
7779
this.results = null
7880
}
7981

80-
initialize() {
81-
82-
}
8382

8483
// Method to find a accesstoken by ID
8584
static async find(id: number, fields?: (keyof AccessTokenType)[]): Promise<AccessTokenModel | null> {
@@ -148,19 +147,17 @@
148147

149148
// Method to get a accesstoken by criteria
150149
static async get(): Promise<AccessTokenModel[]> {
151-
let query = db.selectFrom('personal_access_tokens')
150+
const query = db.selectFrom('personal_access_tokens')
152151

153152
const model = await query.selectAll().execute()
154153

155154
return model.map(modelItem => new AccessTokenModel(modelItem))
156155
}
157156

158-
// Method to get a accesstoken by criteria
159-
async get(): Promise<AccessTokenModel[]> {
160-
const model = await this.query.execute()
161-
162-
return model.map((modelItem: any) => new AccessTokenModel(modelItem))
163-
}
157+
// Method to get a accesstoken by criteria
158+
async get(): Promise<AccessTokenModel[]> {
159+
return await this.query.selectAll().execute()
160+
}
164161

165162
// Method to get all personal_access_tokens
166163
static async paginate(options: QueryOptions = { limit: 10, offset: 0, page: 1 }): Promise<AccessTokenResponse> {
@@ -209,7 +206,7 @@
209206
.execute()
210207
}
211208

212-
static where(...args: (string | number)[]): any {
209+
static where(...args: (string | number)[]): AccessTokenModel {
213210
let column: any
214211
let operator: any
215212
let value: any
@@ -225,11 +222,7 @@
225222
throw new Error("Invalid number of arguments")
226223
}
227224

228-
let query = db.selectFrom('personal_access_tokens')
229-
230-
query = query.where(column, operator, value).selectAll()
231-
232-
instance.query = query
225+
instance.query = instance.query.where(column, operator, value)
233226

234227
return instance
235228
}
@@ -278,89 +271,85 @@
278271
// return await query.selectAll().execute()
279272
// }
280273

281-
// async whereIn(column: keyof AccessTokenType, values: any[], options: QueryOptions = {}): Promise<AccessTokenType[]> {
282-
283-
// let query = db.selectFrom('personal_access_tokens')
284-
285-
// query = query.where(column, 'in', values)
286-
287-
// // Apply sorting from options
288-
// if (options.sort)
289-
// query = query.orderBy(options.sort.column, options.sort.order)
290-
291-
// // Apply pagination from options
292-
// if (options.limit !== undefined)
293-
// query = query.limit(options.limit)
274+
static whereIn(column: keyof AccessTokenType, values: any[]): AccessTokenModel {
275+
const instance = new this(null);
294276

295-
// if (options.offset !== undefined)
296-
// query = query.offset(options.offset)
277+
instance.query = instance.query.where(column, 'in', values)
297278

298-
// return await query.selectAll().execute()
299-
// }
279+
return instance
280+
}
300281

301282
async first(): Promise<AccessTokenType> {
302283
return this.query.executeTakeFirst()
303284
}
304285

305-
static async first(): Promise<AccessTokenType> {
286+
static async first(): Promise<AccessTokenType | undefined> {
306287
return await db.selectFrom('personal_access_tokens')
307288
.selectAll()
308289
.executeTakeFirst()
309290
}
310291

311-
// async last(): Promise<AccessTokenType> {
312-
// return await db.selectFrom('personal_access_tokens')
313-
// .selectAll()
314-
// .orderBy('id', 'desc')
315-
// .executeTakeFirst()
316-
// }
292+
async last(): Promise<AccessTokenType | undefined> {
293+
return await db.selectFrom('personal_access_tokens')
294+
.selectAll()
295+
.orderBy('id', 'desc')
296+
.executeTakeFirst()
297+
}
317298

318-
// async orderBy(column: keyof AccessTokenType, order: 'asc' | 'desc'): Promise<AccessTokenType[]> {
319-
// return await db.selectFrom('personal_access_tokens')
320-
// .selectAll()
321-
// .orderBy(column, order)
322-
// .execute()
323-
// }
299+
static orderBy(column: keyof AccessTokenType, order: 'asc' | 'desc'): AccessTokenModel {
300+
const instance = new this(null);
324301

325-
// async orderByDesc(column: keyof AccessTokenType): Promise<AccessTokenType[]> {
326-
// return await db.selectFrom('personal_access_tokens')
327-
// .selectAll()
328-
// .orderBy(column, 'desc')
329-
// .execute()
330-
// }
302+
instance.query.orderBy(column, order)
331303

332-
// async orderByAsc(column: keyof AccessTokenType): Promise<AccessTokenType[]> {
333-
// return await db.selectFrom('personal_access_tokens')
334-
// .selectAll()
335-
// .orderBy(column, 'asc')
336-
// .execute()
337-
// }
304+
return instance
305+
}
338306

339-
// // Method to get the accesstoken instance itself
340-
// self(): AccessTokenModel {
341-
// return this
342-
// }
307+
orderBy(column: keyof AccessTokenType, order: 'asc' | 'desc'): AccessTokenModel {
308+
this.query.orderBy(column, order)
343309

344-
// // Method to get the accesstoken instance data
345-
// get() {
346-
// return this.accesstoken
347-
// }
310+
return this
311+
}
348312

349-
// // Method to update the accesstoken instance
350-
// async update(accesstoken: AccessTokenUpdate): Promise<Result<AccessTokenType, Error>> {
351-
// if (this.accesstoken.id === undefined)
352-
// return err(handleError('AccessToken ID is undefined'))
313+
static orderByDesc(column: keyof AccessTokenType): AccessTokenModel {
314+
const instance = new this(null);
353315

354-
// const updatedModel = await db.updateTable('personal_access_tokens')
355-
// .set(accesstoken)
356-
// .where('id', '=', this.accesstoken.id)
357-
// .executeTakeFirst()
316+
instance.query.orderBy(column, 'desc')
358317

359-
// if (!updatedModel)
360-
// return err(handleError('AccessToken not found'))
318+
return instance
319+
}
361320

362-
// return ok(updatedModel)
363-
// }
321+
orderByDesc(column: keyof AccessTokenType): AccessTokenModel {
322+
this.query.orderBy(column, 'desc')
323+
324+
return this
325+
}
326+
327+
static orderByAsc(column: keyof AccessTokenType): AccessTokenModel {
328+
const instance = new this(null);
329+
330+
instance.query.orderBy(column, 'desc')
331+
332+
return instance
333+
}
334+
335+
orderByAsc(column: keyof AccessTokenType): AccessTokenModel {
336+
this.query.orderBy(column, 'desc')
337+
338+
return this
339+
}
340+
341+
// Method to update the accesstoken instance
342+
async update(accesstoken: AccessTokenUpdate): Promise<AccessTokenModel | null> {
343+
if (this.id === undefined)
344+
throw new Error('AccessToken ID is undefined')
345+
346+
await db.updateTable('personal_access_tokens')
347+
.set(accesstoken)
348+
.where('id', '=', this.id)
349+
.executeTakeFirst()
350+
351+
return await find(Number(this.id))
352+
}
364353

365354
// // Method to save (insert or update) the accesstoken instance
366355
// async save(): Promise<void> {
@@ -405,38 +394,23 @@
405394
// this.accesstoken = refreshedModel
406395
// }
407396

408-
409-
// async team() {
410-
// if (this.accesstoken.id === undefined)
411-
// throw new Error('Relation Error!')
412-
413-
// const model = await db.selectFrom('teams')
414-
// .where('accesstoken_id', '=', this.accesstoken.id)
415-
// .selectAll()
416-
// .executeTakeFirst()
417-
418-
// if (! model)
419-
// throw new Error('Model Relation Not Found!')
420-
421-
// return new Team.modelInstance(model)
422-
// }
423-
424-
425397

426-
// toJSON() {
427-
// const output: Partial<AccessTokenType> = { ...this.accesstoken }
398+
toJSON() {
399+
const output: Partial<AccessTokenType> = { ...this.accesstoken }
428400

429-
// this.hidden.forEach((attr) => {
430-
// if (attr in output)
431-
// delete output[attr as keyof Partial<AccessTokenType>]
432-
// })
401+
this.hidden.forEach((attr) => {
402+
if (attr in output)
403+
delete output[attr as keyof Partial<AccessTokenType>]
404+
})
433405

434-
// type AccessToken = Omit<AccessTokenType, 'password'>
406+
type AccessToken = Omit<AccessTokenType, 'password'>
435407

436-
// return output as AccessToken
437-
// }
408+
return output as AccessToken
409+
}
438410
}
439411

440-
const result = await AccessTokenModel.remove(2)
412+
const result = await AccessTokenModel
413+
.where('id', 10)
414+
.get()
441415

442416
console.log(result)

0 commit comments

Comments
 (0)