1
1
import { db } from '@stacksjs/database'
2
+
2
3
import type { Collection } from '@stacksjs/collections'
3
4
import { collect } from '@stacksjs/collections'
4
5
@@ -8,50 +9,54 @@ export interface User {
8
9
email : string
9
10
password : string
10
11
created_at : Date
12
+ deleted_at : Date
11
13
}
12
14
15
+ export type UserColumn = keyof User
16
+ export type UserColumns = Array < keyof User >
17
+
13
18
export class UserModel {
14
19
private _data : User | undefined = undefined
15
20
16
21
private _isSelectInvoked = false
17
22
public _id : number | undefined = undefined
18
- private cols : string [ ] = [ ]
23
+ private cols : UserColumns = [ ]
19
24
private useSoftDeletes = true
25
+ private keyName : keyof User = 'id'
20
26
21
27
queryBuilder = db . selectFrom ( 'users' ) // Initialize queryBuilder
22
28
queryBuilderStore = db . insertInto ( 'users' ) // Initialize queryBuilder
23
29
queryBuilderUpdate = db . updateTable ( 'users' ) // Initialize queryBuilder
24
30
queryBuilderDelete = db . deleteFrom ( 'users' ) // Initialize queryBuilder
25
31
26
- private getKeyName ( ) : string {
27
- return 'id'
28
- }
29
-
30
32
public async find ( id : number | number [ ] ) : Promise < User | Collection < User > > {
31
33
if ( Array . isArray ( id ) )
32
34
return await this . findMany ( id )
33
35
34
36
let query = this . queryBuilder . selectAll ( )
35
- . where ( this . getKeyName ( ) , '=' , id )
37
+ . where ( this . keyName , '=' , id )
36
38
37
39
if ( this . useSoftDeletes )
38
40
query = query . where ( 'deleted_at' , 'is' , null )
39
41
40
42
this . _data = await query . executeTakeFirst ( )
41
43
44
+ if ( ! this . _data )
45
+ throw new Error ( 'User not found!' )
46
+
42
47
return this . _createProxy ( )
43
48
}
44
49
45
50
public async findMany ( id : number [ ] | string [ ] ) : Promise < User | Collection < User > > {
46
- return await this . whereIn ( this . getKeyName ( ) , id ) . get ( )
51
+ return await this . whereIn ( this . keyName , id ) . get ( )
47
52
}
48
53
49
- private _createProxy ( ) : User & { [ key : string ] : Function } {
54
+ private _createProxy ( ) {
50
55
return new Proxy ( this . _data || { } , {
51
- get : ( target , prop : keyof User | string ) : any => {
56
+ get : ( target : User , prop : UserColumn ) => {
52
57
// Property lookup in the User data
53
58
if ( prop in target )
54
- return target [ prop as keyof User ]
59
+ return target [ prop ]
55
60
56
61
// If it's a method on the UserModel, bind it
57
62
const method = this [ prop as keyof this]
@@ -63,12 +68,16 @@ export class UserModel {
63
68
} )
64
69
}
65
70
66
- public update ( obj : Partial < User > ) : any {
71
+ public async update ( obj : Partial < User > ) : Promise < User | undefined | Collection < User > > {
67
72
if ( this . _data && this . _data ?. id ) {
68
- return this . queryBuilderUpdate . set ( obj )
69
- . where ( this . getKeyName ( ) , '=' , this . _data ?. id )
73
+ await this . queryBuilderUpdate . set ( obj )
74
+ . where ( this . keyName , '=' , this . _data ?. id )
70
75
. executeTakeFirst ( )
76
+
77
+ return await this . find ( this . _data ?. id )
71
78
}
79
+
80
+ return undefined
72
81
}
73
82
74
83
public async all ( ) {
@@ -103,7 +112,7 @@ export class UserModel {
103
112
public where ( ...args : ( string | number | boolean ) [ ] ) : this {
104
113
if ( args . length === 2 ) {
105
114
const [ column , value ] = args
106
- this . queryBuilder = this . queryBuilder . where ( column , '=' , value )
115
+ this . queryBuilder = this . queryBuilder . where ( column as UserColumn , '=' , value )
107
116
}
108
117
else { this . queryBuilder = this . queryBuilder . where ( ...args ) }
109
118
@@ -128,7 +137,7 @@ export class UserModel {
128
137
return this
129
138
}
130
139
131
- public whereNull ( col : string ) : this {
140
+ public whereNull ( col : UserColumn ) : this {
132
141
this . queryBuilder = this . queryBuilder . where ( col , 'is' , null )
133
142
134
143
return this
@@ -141,7 +150,7 @@ export class UserModel {
141
150
return this
142
151
}
143
152
144
- public whereNotNull ( col : string ) : this {
153
+ public whereNotNull ( col : UserColumn ) : this {
145
154
this . queryBuilder = this . queryBuilder . where ( col , 'is not' , null )
146
155
147
156
return this
@@ -153,7 +162,7 @@ export class UserModel {
153
162
return this
154
163
}
155
164
156
- public distinctOn ( col : string ) : this {
165
+ public distinctOn ( col : UserColumn ) : this {
157
166
this . queryBuilder = this . queryBuilder . distinctOn ( col )
158
167
159
168
return this
@@ -165,13 +174,13 @@ export class UserModel {
165
174
return this
166
175
}
167
176
168
- public orderBy ( col : string ) {
177
+ public orderBy ( col : UserColumn ) {
169
178
this . queryBuilder = this . queryBuilder . orderBy ( col )
170
179
171
180
return this
172
181
}
173
182
174
- public groupBy ( col : string ) {
183
+ public groupBy ( col : UserColumn ) {
175
184
this . queryBuilder = this . queryBuilder . groupBy ( col )
176
185
177
186
return this
@@ -231,13 +240,13 @@ export class UserModel {
231
240
return this
232
241
}
233
242
234
- public orderByDesc ( col : string ) : this {
243
+ public orderByDesc ( col : UserColumn ) : this {
235
244
this . queryBuilder = this . queryBuilder . orderBy ( col , 'desc' )
236
245
237
246
return this
238
247
}
239
248
240
- public select ( ...args : string [ ] ) : this {
249
+ public select ( ...args : UserColumns ) : this {
241
250
this . cols = args
242
251
this . _isSelectInvoked = true
243
252
@@ -281,19 +290,17 @@ export class UserModel {
281
290
282
291
public forceDelete ( ) : any {
283
292
if ( this . _data && this . _data ?. id ) {
284
- return this . queryBuilderDelete . where ( this . getKeyName ( ) , '=' , this . _data ?. id )
293
+ return this . queryBuilderDelete . where ( this . keyName , '=' , this . _data ?. id )
285
294
. executeTakeFirst ( )
286
295
}
287
296
}
288
297
}
289
298
290
299
const UserInstance = new UserModel ( )
291
300
292
- const user = await UserInstance . when ( true , ( query ) => {
293
- // Modify the query as needed
294
- return query . where ( 'id' , 1 )
295
- } ) . get ( )
301
+ const user = await UserInstance . find ( 1 )
296
302
297
- console . log ( user )
303
+ const res = await user . update ( { name : 'glenn 2' } )
298
304
305
+ console . log ( res )
299
306
process . exit ( 0 )
0 commit comments