Skip to content

Commit 69c0f83

Browse files
committed
chore: wip
1 parent 3fa6ff7 commit 69c0f83

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ _Develop serverless (or server) functions with countless helpers to build scalab
267267

268268
- 🪄 **AI** _deep AI integrations & foundational model access_
269269
- 🤖 **APIs** _scalability & maintainability built-in_
270-
- 🏎️ **Cache** _Redis, DynamoDB, and more—serverless_
270+
- 🏎️ **Cache** _unified caching for DynamoDB, Redis and more_
271271
- ⚙️ **CLIs** _create beautiful CLIs for Linux, Windows, and Mac (dependency-free binaries)_
272272
- 📀 **Database** _DynamoDB, SQLite, MySQL, Postgres, and more_
273273
- 👾 **Errors** _native type-safe error handling_

app/Models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default {
6565
},
6666
},
6767

68-
// accessors for fullname & mutoators for password
68+
// accessors for fullname & mutators for password
6969
accessors: {
7070
fullname: (user: Model) => user.name,
7171
},

storage/framework/core/orm/src/generated/User.ts

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,28 @@ export interface UsersTable {
2828
deleted_at: ColumnType<Date, string | undefined, never>
2929
}
3030

31+
interface UserResponse {
32+
data: Users
33+
paging: {
34+
total_records: number
35+
page: number
36+
total_pages: number
37+
}
38+
next_cursor: number | null
39+
}
40+
3141
export type UserType = Selectable<UsersTable>
3242
export type NewUser = Insertable<UsersTable>
3343
export type UserUpdate = Updateable<UsersTable>
44+
export type Users = UserType[]
45+
46+
// Define a type for the options parameter
47+
interface QueryOptions {
48+
sort?: { column: keyof UserType, order: 'asc' | 'desc' }
49+
limit?: number
50+
offset?: number
51+
page?: number // New
52+
}
3453

3554
export class UserModel {
3655
private user: Partial<UserType>
@@ -73,15 +92,34 @@ export class UserModel {
7392
}
7493

7594
// Method to get all users
76-
static async all(limit: number = 10, offset: number = 0): Promise<UserModel[]> {
77-
const users = await db.selectFrom('users')
95+
static async all(options: QueryOptions = { limit: 10, offset: 0, page: 1 }): Promise<UserResponse> {
96+
const totalRecordsResult = await db.selectFrom('users')
97+
.select(db.fn.count('id').as('total')) // Use 'id' or another actual column name
98+
.executeTakeFirst()
99+
100+
const totalRecords = Number(totalRecordsResult?.total) || 0
101+
const totalPages = Math.ceil(totalRecords / (options.limit ?? 10))
102+
103+
const usersWithExtra = await db.selectFrom('users')
78104
.selectAll()
79-
.orderBy('created_at', 'desc')
80-
.limit(limit)
81-
.offset(offset)
105+
.orderBy('id', 'asc') // Assuming 'id' is used for cursor-based pagination
106+
.limit((options.limit ?? 10) + 1) // Fetch one extra record
107+
.offset((options.page! - 1) * (options.limit ?? 10))
82108
.execute()
83109

84-
return users.map(user => new UserModel(user))
110+
let nextCursor = null
111+
if (usersWithExtra.length > (options.limit ?? 10))
112+
nextCursor = usersWithExtra.pop()!.id // Use the ID of the extra record as the next cursor
113+
114+
return {
115+
data: usersWithExtra,
116+
paging: {
117+
total_records: totalRecords,
118+
page: options.page!,
119+
total_pages: totalPages,
120+
},
121+
next_cursor: nextCursor,
122+
}
85123
}
86124

87125
// Method to create a new user
@@ -128,27 +166,47 @@ export class UserModel {
128166
return new UserModel(user)
129167
}
130168

131-
async where(criteria: Partial<UserType>) {
169+
async where(criteria: Partial<UserType>, options: QueryOptions = {}) {
132170
let query = db.selectFrom('users')
171+
172+
// Existing criteria checks
133173
if (criteria.id)
134174
query = query.where('id', '=', criteria.id) // Kysely is immutable, we must re-assign
175+
135176
if (criteria.email)
136177
query = query.where('email', '=', criteria.email)
178+
137179
if (criteria.name !== undefined) {
138180
query = query.where(
139181
'name',
140182
criteria.name === null ? 'is' : '=',
141183
criteria.name,
142184
)
143185
}
186+
144187
if (criteria.password)
145188
query = query.where('password', '=', criteria.password)
189+
146190
if (criteria.created_at)
147191
query = query.where('created_at', '=', criteria.created_at)
192+
148193
if (criteria.updated_at)
149194
query = query.where('updated_at', '=', criteria.updated_at)
195+
150196
if (criteria.deleted_at)
151197
query = query.where('deleted_at', '=', criteria.deleted_at)
198+
199+
// Apply sorting from options
200+
if (options.sort)
201+
query = query.orderBy(options.sort.column, options.sort.order)
202+
203+
// Apply pagination from options
204+
if (options.limit !== undefined)
205+
query = query.limit(options.limit)
206+
207+
if (options.offset !== undefined)
208+
query = query.offset(options.offset)
209+
152210
return await query.selectAll().execute()
153211
}
154212

@@ -359,11 +417,15 @@ export async function findByEmail(email: string) {
359417
.executeTakeFirst()
360418
}
361419

362-
export async function where(criteria: Partial<UserType>, sort: { column: keyof UserType, order: 'asc' | 'desc' } = { column: 'created_at', order: 'desc' }) {
420+
export async function where(
421+
criteria: Partial<UserType>,
422+
options: QueryOptions = {},
423+
) {
363424
let query = db.selectFrom('users')
364425

426+
// Apply criteria
365427
if (criteria.id)
366-
query = query.where('id', '=', criteria.id) // Kysely is immutable, we must re-assign
428+
query = query.where('id', '=', criteria.id)
367429

368430
if (criteria.email)
369431
query = query.where('email', '=', criteria.email)
@@ -388,8 +450,16 @@ export async function where(criteria: Partial<UserType>, sort: { column: keyof U
388450
if (criteria.deleted_at)
389451
query = query.where('deleted_at', '=', criteria.deleted_at)
390452

391-
// Apply sorting
392-
query = query.orderBy(sort.column, sort.order)
453+
// Apply sorting from options
454+
if (options.sort)
455+
query = query.orderBy(options.sort.column, options.sort.order)
456+
457+
// Apply pagination from options
458+
if (options.limit !== undefined)
459+
query = query.limit(options.limit)
460+
461+
if (options.offset !== undefined)
462+
query = query.offset(options.offset)
393463

394464
return await query.selectAll().execute()
395465
}
@@ -407,3 +477,5 @@ export const User = {
407477
last,
408478
where,
409479
}
480+
481+
export default User

0 commit comments

Comments
 (0)