1
1
import type { ColumnType , Generated , Insertable , Selectable , Updateable } from 'kysely'
2
- import { type Err , type Result , err , handleError , ok } from '@stacksjs/error-handling'
2
+ import type { Result } from '@stacksjs/error-handling'
3
+ import { err , handleError , ok } from '@stacksjs/error-handling'
3
4
import { db } from '@stacksjs/query-builder'
4
5
5
6
// import { Kysely, MysqlDialect, PostgresDialect } from 'kysely'
@@ -41,10 +42,89 @@ export class UserModel {
41
42
this . userData = userData
42
43
}
43
44
45
+ // Method to find a user by ID
46
+ static async find ( id : number ) : Promise < UserModel | null > {
47
+ const userData = await db . selectFrom ( 'users' )
48
+ . where ( 'id' , '=' , id )
49
+ . selectAll ( )
50
+ . executeTakeFirst ( )
51
+
52
+ if ( ! userData )
53
+ return null
54
+
55
+ return new UserModel ( userData )
56
+ }
57
+
58
+ // Method to get a user by criteria
59
+ static async get ( criteria : Partial < UserType > ) : Promise < UserModel [ ] > {
60
+ if ( criteria . id !== undefined ) {
61
+ const users = await db . selectFrom ( 'users' )
62
+ . where ( 'id' , '=' , criteria . id )
63
+ . selectAll ( )
64
+ . execute ( )
65
+ return users . map ( user => new UserModel ( user ) )
66
+ }
67
+
68
+ return [ ]
69
+ }
70
+
71
+ // Method to get all users
72
+ static async all ( ) : Promise < UserModel [ ] > {
73
+ const users = await db . selectFrom ( 'users' )
74
+ . selectAll ( )
75
+ . execute ( )
76
+
77
+ return users . map ( user => new UserModel ( user ) )
78
+ }
79
+
80
+ // Method to create a new user
81
+ static async create ( newUser : NewUser ) : Promise < UserModel > {
82
+ const user = await db . insertInto ( 'users' )
83
+ . values ( newUser )
84
+ . returningAll ( )
85
+ . executeTakeFirstOrThrow ( )
86
+
87
+ return new UserModel ( user )
88
+ }
89
+
90
+ // Method to update a user
91
+ static async update ( id : number , userUpdate : UserUpdate ) : Promise < UserModel > {
92
+ const user = await db . updateTable ( 'users' )
93
+ . set ( userUpdate )
94
+ . where ( 'id' , '=' , id )
95
+ . returningAll ( )
96
+ . executeTakeFirstOrThrow ( )
97
+
98
+ return new UserModel ( user )
99
+ }
100
+
101
+ // Method to remove a user
102
+ static async remove ( id : number ) : Promise < UserModel > {
103
+ const user = await db . deleteFrom ( 'users' )
104
+ . where ( 'id' , '=' , id )
105
+ . returningAll ( )
106
+ . executeTakeFirstOrThrow ( )
107
+
108
+ return new UserModel ( user )
109
+ }
110
+
111
+ // Method to find a user by email
112
+ static async findByEmail ( email : string ) : Promise < UserModel | null > {
113
+ const userData = await db . selectFrom ( 'users' )
114
+ . where ( 'email' , '=' , email )
115
+ . selectAll ( )
116
+ . executeTakeFirst ( )
117
+
118
+ if ( ! userData )
119
+ return null
120
+
121
+ return new UserModel ( userData )
122
+ }
123
+
44
124
async where ( criteria : Partial < UserType > ) {
45
125
let query = db . selectFrom ( 'users' )
46
126
if ( criteria . id )
47
- query = query . where ( 'id' , '=' , criteria . id ) // Kysely is immutable, you must re-assign!
127
+ query = query . where ( 'id' , '=' , criteria . id ) // Kysely is immutable, we must re-assign
48
128
if ( criteria . email )
49
129
query = query . where ( 'email' , '=' , criteria . email )
50
130
if ( criteria . name !== undefined ) {
@@ -65,6 +145,29 @@ export class UserModel {
65
145
return await query . selectAll ( ) . execute ( )
66
146
}
67
147
148
+ async first ( ) {
149
+ return await db . selectFrom ( 'users' )
150
+ . selectAll ( )
151
+ . executeTakeFirst ( )
152
+ }
153
+
154
+ async last ( ) {
155
+ return await db . selectFrom ( 'users' )
156
+ . selectAll ( )
157
+ . orderBy ( 'id' , 'desc' )
158
+ . executeTakeFirst ( )
159
+ }
160
+
161
+ // Method to get the user instance itself
162
+ self ( ) {
163
+ return this
164
+ }
165
+
166
+ // Method to get the user instance data
167
+ get ( ) {
168
+ return this . userData
169
+ }
170
+
68
171
// Method to update the user instance
69
172
async update ( userUpdate : UserUpdate ) : Promise < Result < UserType , Error > > {
70
173
if ( this . userData . id === undefined )
@@ -137,6 +240,8 @@ export class UserModel {
137
240
}
138
241
}
139
242
243
+ const Model = UserModel
244
+
140
245
// starting here, ORM functions
141
246
export async function find ( id : number ) : Promise < UserModel | null > {
142
247
const userData = await db . selectFrom ( 'users' )
@@ -154,7 +259,7 @@ export async function get(criteria: Partial<UserType>) {
154
259
let query = db . selectFrom ( 'users' )
155
260
156
261
if ( criteria . id )
157
- query = query . where ( 'id' , '=' , criteria . id ) // Kysely is immutable, you must re-assign!
262
+ query = query . where ( 'id' , '=' , criteria . id ) // Kysely is immutable, we must re-assign
158
263
159
264
if ( criteria . email )
160
265
query = query . where ( 'email' , '=' , criteria . email )
@@ -195,6 +300,26 @@ export async function create(newUser: NewUser) {
195
300
. executeTakeFirstOrThrow ( )
196
301
}
197
302
303
+ export async function getOne ( criteria : Partial < UserType > ) {
304
+ return await db . selectFrom ( 'users' )
305
+ . where ( 'id' , '=' , criteria . id )
306
+ . selectAll ( )
307
+ . executeTakeFirst ( )
308
+ }
309
+
310
+ export async function first ( ) {
311
+ return await db . selectFrom ( 'users' )
312
+ . selectAll ( )
313
+ . executeTakeFirst ( )
314
+ }
315
+
316
+ export async function last ( ) {
317
+ return await db . selectFrom ( 'users' )
318
+ . selectAll ( )
319
+ . orderBy ( 'id' , 'desc' )
320
+ . executeTakeFirst ( )
321
+ }
322
+
198
323
export async function update ( id : number , userUpdate : UserUpdate ) {
199
324
return await db . updateTable ( 'users' )
200
325
. set ( userUpdate )
@@ -216,6 +341,38 @@ export async function findByEmail(email: string) {
216
341
. executeTakeFirst ( )
217
342
}
218
343
344
+ export async function where ( criteria : Partial < UserType > ) {
345
+ let query = db . selectFrom ( 'users' )
346
+
347
+ if ( criteria . id )
348
+ query = query . where ( 'id' , '=' , criteria . id ) // Kysely is immutable, we must re-assign
349
+
350
+ if ( criteria . email )
351
+ query = query . where ( 'email' , '=' , criteria . email )
352
+
353
+ if ( criteria . name !== undefined ) {
354
+ query = query . where (
355
+ 'name' ,
356
+ criteria . name === null ? 'is' : '=' ,
357
+ criteria . name ,
358
+ )
359
+ }
360
+
361
+ if ( criteria . password )
362
+ query = query . where ( 'password' , '=' , criteria . password )
363
+
364
+ if ( criteria . created_at )
365
+ query = query . where ( 'created_at' , '=' , criteria . created_at )
366
+
367
+ if ( criteria . updated_at )
368
+ query = query . where ( 'updated_at' , '=' , criteria . updated_at )
369
+
370
+ if ( criteria . deleted_at )
371
+ query = query . where ( 'deleted_at' , '=' , criteria . deleted_at )
372
+
373
+ return await query . selectAll ( ) . execute ( )
374
+ }
375
+
219
376
export const User = {
220
377
find,
221
378
get,
@@ -224,4 +381,8 @@ export const User = {
224
381
update,
225
382
remove,
226
383
findByEmail,
384
+ Model,
385
+ first,
386
+ last,
387
+ where,
227
388
}
0 commit comments