17
17
abilities : string [ ]
18
18
team_id : number
19
19
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 >
23
23
}
24
24
25
25
interface AccessTokenResponse {
52
52
53
53
export class AccessTokenModel {
54
54
private results : any
55
+ private accesstoken : Partial < AccessTokenType > | null
55
56
private hidden = [ 'password' ] // TODO: this hidden functionality needs to be implemented still
56
57
protected query : any
57
58
61
62
public plainTextToken : string | undefined
62
63
public abilities : string [ ] | undefined
63
64
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
66
67
67
68
constructor ( accesstoken : Partial < AccessTokenType > | null ) {
69
+ this . accesstoken = accesstoken
68
70
this . id = accesstoken ?. id
69
71
this . name = accesstoken ?. name
70
72
this . token = accesstoken ?. name
73
75
this . team_id = accesstoken ?. team_id
74
76
this . created_at = accesstoken ?. created_at
75
77
this . updated_at = accesstoken ?. updated_at
76
- this . query = null
78
+ this . query = db . selectFrom ( 'personal_access_tokens' )
77
79
this . results = null
78
80
}
79
81
80
- initialize ( ) {
81
-
82
- }
83
82
84
83
// Method to find a accesstoken by ID
85
84
static async find ( id : number , fields ?: ( keyof AccessTokenType ) [ ] ) : Promise < AccessTokenModel | null > {
148
147
149
148
// Method to get a accesstoken by criteria
150
149
static async get ( ) : Promise < AccessTokenModel [ ] > {
151
- let query = db . selectFrom ( 'personal_access_tokens' )
150
+ const query = db . selectFrom ( 'personal_access_tokens' )
152
151
153
152
const model = await query . selectAll ( ) . execute ( )
154
153
155
154
return model . map ( modelItem => new AccessTokenModel ( modelItem ) )
156
155
}
157
156
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
+ }
164
161
165
162
// Method to get all personal_access_tokens
166
163
static async paginate ( options : QueryOptions = { limit : 10 , offset : 0 , page : 1 } ) : Promise < AccessTokenResponse > {
209
206
. execute ( )
210
207
}
211
208
212
- static where ( ...args : ( string | number ) [ ] ) : any {
209
+ static where ( ...args : ( string | number ) [ ] ) : AccessTokenModel {
213
210
let column : any
214
211
let operator : any
215
212
let value : any
225
222
throw new Error ( "Invalid number of arguments" )
226
223
}
227
224
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 )
233
226
234
227
return instance
235
228
}
278
271
// return await query.selectAll().execute()
279
272
// }
280
273
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 ) ;
294
276
295
- // if (options.offset !== undefined)
296
- // query = query.offset(options.offset)
277
+ instance . query = instance . query . where ( column , 'in' , values )
297
278
298
- // return await query.selectAll().execute()
299
- // }
279
+ return instance
280
+ }
300
281
301
282
async first ( ) : Promise < AccessTokenType > {
302
283
return this . query . executeTakeFirst ( )
303
284
}
304
285
305
- static async first ( ) : Promise < AccessTokenType > {
286
+ static async first ( ) : Promise < AccessTokenType | undefined > {
306
287
return await db . selectFrom ( 'personal_access_tokens' )
307
288
. selectAll ( )
308
289
. executeTakeFirst ( )
309
290
}
310
291
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
+ }
317
298
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 ) ;
324
301
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 )
331
303
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
+ }
338
306
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 )
343
309
344
- // // Method to get the accesstoken instance data
345
- // get() {
346
- // return this.accesstoken
347
- // }
310
+ return this
311
+ }
348
312
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 ) ;
353
315
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' )
358
317
359
- // if (!updatedModel)
360
- // return err(handleError('AccessToken not found'))
318
+ return instance
319
+ }
361
320
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
+ }
364
353
365
354
// // Method to save (insert or update) the accesstoken instance
366
355
// async save(): Promise<void> {
405
394
// this.accesstoken = refreshedModel
406
395
// }
407
396
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
-
425
397
426
- // toJSON() {
427
- // const output: Partial<AccessTokenType> = { ...this.accesstoken }
398
+ toJSON ( ) {
399
+ const output : Partial < AccessTokenType > = { ...this . accesstoken }
428
400
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
+ } )
433
405
434
- // type AccessToken = Omit<AccessTokenType, 'password'>
406
+ type AccessToken = Omit < AccessTokenType , 'password' >
435
407
436
- // return output as AccessToken
437
- // }
408
+ return output as AccessToken
409
+ }
438
410
}
439
411
440
- const result = await AccessTokenModel . remove ( 2 )
412
+ const result = await AccessTokenModel
413
+ . where ( 'id' , 10 )
414
+ . get ( )
441
415
442
416
console . log ( result )
0 commit comments